Search in sources :

Example 46 with IgniteInstanceResource

use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.

the class GridFailoverTopologySelfTest method getConfiguration.

/**
 * {@inheritDoc}
 */
@Override
protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
    IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
    cfg.setNodeId(null);
    cfg.setFailoverSpi(new AlwaysFailoverSpi() {

        /**
         * Ignite instance.
         */
        @IgniteInstanceResource
        private Ignite ignite;

        /**
         * {@inheritDoc}
         */
        @Override
        public ClusterNode failover(FailoverContext ctx, List<ClusterNode> grid) {
            if (grid.size() != 1) {
                failed.set(true);
                error("Unexpected grid size [expected=1, grid=" + grid + ']');
            }
            UUID locNodeId = ignite.configuration().getNodeId();
            for (ClusterNode node : grid) {
                if (node.id().equals(locNodeId)) {
                    failed.set(true);
                    error("Grid shouldn't contain local node [localNodeId=" + locNodeId + ", grid=" + grid + ']');
                }
            }
            return super.failover(ctx, grid);
        }
    });
    return cfg;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) AlwaysFailoverSpi(org.apache.ignite.spi.failover.always.AlwaysFailoverSpi) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) Ignite(org.apache.ignite.Ignite) FailoverContext(org.apache.ignite.spi.failover.FailoverContext) UUID(java.util.UUID)

Example 47 with IgniteInstanceResource

use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.

the class IgniteCountDownLatchAbstractSelfTest method checkLatch.

/**
 * @throws Exception If failed.
 */
private void checkLatch() throws Exception {
    // Test API.
    checkAutoDelete();
    checkAwait();
    checkCountDown();
    // Test main functionality.
    IgniteCountDownLatch latch1 = grid(0).countDownLatch("latch", 2, false, true);
    assertEquals(2, latch1.count());
    IgniteFuture<Object> fut = grid(0).compute().callAsync(new IgniteCallable<Object>() {

        @IgniteInstanceResource
        private Ignite ignite;

        @LoggerResource
        private IgniteLogger log;

        @Nullable
        @Override
        public Object call() throws Exception {
            // Test latch in multiple threads on each node.
            IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

                @Nullable
                @Override
                public Object call() throws Exception {
                    IgniteCountDownLatch latch = ignite.countDownLatch("latch", 2, false, true);
                    assert latch != null && latch.count() == 2;
                    log.info("Thread is going to wait on latch: " + Thread.currentThread().getName());
                    assert latch.await(1, MINUTES);
                    log.info("Thread is again runnable: " + Thread.currentThread().getName());
                    return null;
                }
            }, 5, "test-thread");
            fut.get();
            return null;
        }
    });
    Thread.sleep(3000);
    assert latch1.countDown() == 1;
    assert latch1.countDown() == 0;
    // Ensure there are no hangs.
    fut.get();
    // Test operations on removed latch.
    latch1.close();
    checkRemovedLatch(latch1);
}
Also used : LoggerResource(org.apache.ignite.resources.LoggerResource) IgniteCountDownLatch(org.apache.ignite.IgniteCountDownLatch) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) Ignite(org.apache.ignite.Ignite) IgniteLogger(org.apache.ignite.IgniteLogger) Nullable(org.jetbrains.annotations.Nullable)

Example 48 with IgniteInstanceResource

use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.

the class IgniteTxOriginatingNodeFailureAbstractSelfTest method testTxOriginatingNodeFails.

/**
 * @param keys Keys to update.
 * @param partial Flag indicating whether to simulate partial prepared state.
 * @throws Exception If failed.
 */
protected void testTxOriginatingNodeFails(Collection<Integer> keys, final boolean partial) throws Exception {
    assertFalse(keys.isEmpty());
    final Collection<IgniteKernal> grids = new ArrayList<>();
    ClusterNode txNode = grid(originatingNode()).localNode();
    for (int i = 1; i < gridCount(); i++) grids.add((IgniteKernal) grid(i));
    final Map<Integer, String> map = new HashMap<>();
    final String initVal = "initialValue";
    for (Integer key : keys) {
        grid(originatingNode()).cache(DEFAULT_CACHE_NAME).put(key, initVal);
        map.put(key, String.valueOf(key));
    }
    Map<Integer, Collection<ClusterNode>> nodeMap = new HashMap<>();
    info("Node being checked: " + grid(1).localNode().id());
    for (Integer key : keys) {
        Collection<ClusterNode> nodes = new ArrayList<>();
        nodes.addAll(grid(1).affinity(DEFAULT_CACHE_NAME).mapKeyToPrimaryAndBackups(key));
        nodes.remove(txNode);
        nodeMap.put(key, nodes);
    }
    info("Starting optimistic tx " + "[values=" + map + ", topVer=" + (grid(1)).context().discovery().topologyVersion() + ']');
    if (partial)
        ignoreMessages(grid(1).localNode().id(), ignoreMessageClass());
    final Ignite txIgniteNode = G.ignite(txNode.id());
    GridTestUtils.runAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            IgniteCache<Integer, String> cache = txIgniteNode.cache(DEFAULT_CACHE_NAME);
            assertNotNull(cache);
            TransactionProxyImpl tx = (TransactionProxyImpl) txIgniteNode.transactions().txStart();
            GridNearTxLocal txEx = tx.tx();
            assertTrue(txEx.optimistic());
            cache.putAll(map);
            try {
                txEx.prepareNearTxLocal().get(3, TimeUnit.SECONDS);
            } catch (IgniteFutureTimeoutCheckedException ignored) {
                info("Failed to wait for prepare future completion: " + partial);
            }
            return null;
        }
    }).get();
    info("Stopping originating node " + txNode);
    G.stop(G.ignite(txNode.id()).name(), true);
    info("Stopped grid, waiting for transactions to complete.");
    boolean txFinished = GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            for (IgniteKernal g : grids) {
                GridCacheSharedContext<Object, Object> ctx = g.context().cache().context();
                int txNum = ctx.tm().idMapSize();
                if (txNum != 0)
                    return false;
            }
            return true;
        }
    }, 10000);
    assertTrue(txFinished);
    info("Transactions finished.");
    for (Map.Entry<Integer, Collection<ClusterNode>> e : nodeMap.entrySet()) {
        final Integer key = e.getKey();
        final String val = map.get(key);
        assertFalse(e.getValue().isEmpty());
        for (ClusterNode node : e.getValue()) {
            compute(G.ignite(node.id()).cluster().forNode(node)).call(new IgniteCallable<Void>() {

                /**
                 */
                @IgniteInstanceResource
                private Ignite ignite;

                @Override
                public Void call() throws Exception {
                    IgniteCache<Integer, String> cache = ignite.cache(DEFAULT_CACHE_NAME);
                    assertNotNull(cache);
                    assertEquals(partial ? initVal : val, cache.localPeek(key));
                    return null;
                }
            });
        }
    }
    for (Map.Entry<Integer, String> e : map.entrySet()) {
        for (Ignite g : G.allGrids()) {
            UUID locNodeId = g.cluster().localNode().id();
            assertEquals("Check failed for node: " + locNodeId, partial ? initVal : e.getValue(), g.cache(DEFAULT_CACHE_NAME).get(e.getKey()));
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GridCacheSharedContext(org.apache.ignite.internal.processors.cache.GridCacheSharedContext) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteCache(org.apache.ignite.IgniteCache) GridNearTxLocal(org.apache.ignite.internal.processors.cache.distributed.near.GridNearTxLocal) IgniteSpiException(org.apache.ignite.spi.IgniteSpiException) IgniteException(org.apache.ignite.IgniteException) IgniteFutureTimeoutCheckedException(org.apache.ignite.internal.IgniteFutureTimeoutCheckedException) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map) TransactionProxyImpl(org.apache.ignite.internal.processors.cache.transactions.TransactionProxyImpl)

Example 49 with IgniteInstanceResource

use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.

the class GridCacheDhtPreloadBigDataSelfTest method testLargeObjectsWithLifeCycleBean.

/**
 * @throws Exception If failed.
 */
@Test
public void testLargeObjectsWithLifeCycleBean() throws Exception {
    preloadMode = SYNC;
    partitions = 23;
    try {
        final int cnt = 10000;
        lbean = new LifecycleBean() {

            @IgniteInstanceResource
            private Ignite ignite;

            @Override
            public void onLifecycleEvent(LifecycleEventType evt) {
                if (evt == LifecycleEventType.AFTER_NODE_START) {
                    IgniteCache<Integer, byte[]> c = ignite.cache(DEFAULT_CACHE_NAME);
                    if (c.putIfAbsent(-1, new byte[1])) {
                        populate(c, cnt, KBSIZE);
                        info(">>> POPULATED GRID <<<");
                    }
                }
            }
        };
        int gridCnt = 3;
        for (int i = 0; i < gridCnt; i++) startGrid(i);
        for (int i = 0; i < gridCnt; i++) info("Grid size [i=" + i + ", size=" + grid(i).cache(DEFAULT_CACHE_NAME).size() + ']');
        Thread.sleep(10000);
        for (int i = 0; i < gridCnt; i++) {
            IgniteCache<Integer, String> c = grid(i).cache(DEFAULT_CACHE_NAME);
            if (backups + 1 <= gridCnt)
                assert c.localSize() < cnt;
            else
                assert c.localSize() == cnt;
        }
    } finally {
        stopAllGrids();
    }
}
Also used : IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteCache(org.apache.ignite.IgniteCache) LifecycleBean(org.apache.ignite.lifecycle.LifecycleBean) Ignite(org.apache.ignite.Ignite) LifecycleEventType(org.apache.ignite.lifecycle.LifecycleEventType) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 50 with IgniteInstanceResource

use of org.apache.ignite.resources.IgniteInstanceResource in project ignite by apache.

the class WalEnableDisableWithRestartsTest method startNodeWithMaintenance.

/**
 */
private Ignite startNodeWithMaintenance(String consistentId) throws Exception {
    Ignite node;
    try {
        node = Ignition.start(igniteCfg(false, consistentId));
    } catch (Exception ex) {
        if (!X.hasCause(ex, "Cache groups with potentially corrupted partition files", IgniteException.class))
            throw ex;
        node = Ignition.start(igniteCfg(false, consistentId));
        node.compute().run(new IgniteRunnable() {

            /**
             */
            @IgniteInstanceResource
            private Ignite ignite;

            /**
             */
            @Override
            public void run() {
                MaintenanceRegistry mntcRegistry = ((IgniteEx) ignite).context().maintenanceRegistry();
                List<MaintenanceAction<?>> actions = mntcRegistry.actionsForMaintenanceTask(CORRUPTED_DATA_FILES_MNTC_TASK_NAME);
                actions.stream().filter(a -> a.name().equals(CleanCacheStoresMaintenanceAction.ACTION_NAME)).findFirst().get().execute();
                mntcRegistry.unregisterMaintenanceTask(CORRUPTED_DATA_FILES_MNTC_TASK_NAME);
            }
        });
        node.close();
        node = Ignition.start(igniteCfg(false, consistentId));
    }
    return node;
}
Also used : CacheAtomicityMode(org.apache.ignite.cache.CacheAtomicityMode) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) IgniteException(org.apache.ignite.IgniteException) MaintenanceAction(org.apache.ignite.maintenance.MaintenanceAction) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteEx(org.apache.ignite.internal.IgniteEx) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) List(java.util.List) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Ignition(org.apache.ignite.Ignition) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) MaintenanceRegistry(org.apache.ignite.maintenance.MaintenanceRegistry) CleanCacheStoresMaintenanceAction(org.apache.ignite.internal.processors.cache.persistence.CleanCacheStoresMaintenanceAction) X(org.apache.ignite.internal.util.typedef.X) After(org.junit.After) DataStorageConfiguration(org.apache.ignite.configuration.DataStorageConfiguration) LinkedList(java.util.LinkedList) DataRegionConfiguration(org.apache.ignite.configuration.DataRegionConfiguration) CacheMode(org.apache.ignite.cache.CacheMode) CORRUPTED_DATA_FILES_MNTC_TASK_NAME(org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager.CORRUPTED_DATA_FILES_MNTC_TASK_NAME) MaintenanceAction(org.apache.ignite.maintenance.MaintenanceAction) CleanCacheStoresMaintenanceAction(org.apache.ignite.internal.processors.cache.persistence.CleanCacheStoresMaintenanceAction) Ignite(org.apache.ignite.Ignite) MaintenanceRegistry(org.apache.ignite.maintenance.MaintenanceRegistry) IgniteException(org.apache.ignite.IgniteException) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable)

Aggregations

IgniteInstanceResource (org.apache.ignite.resources.IgniteInstanceResource)59 Ignite (org.apache.ignite.Ignite)48 Test (org.junit.Test)40 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)28 IgniteException (org.apache.ignite.IgniteException)24 IgniteEx (org.apache.ignite.internal.IgniteEx)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 IgniteCache (org.apache.ignite.IgniteCache)15 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)11 IgniteRunnable (org.apache.ignite.lang.IgniteRunnable)11 List (java.util.List)9 UUID (java.util.UUID)9 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)9 ClusterNode (org.apache.ignite.cluster.ClusterNode)8 HashMap (java.util.HashMap)7 Map (java.util.Map)7 IgniteLogger (org.apache.ignite.IgniteLogger)7 IgniteCallable (org.apache.ignite.lang.IgniteCallable)7 Callable (java.util.concurrent.Callable)6