Search in sources :

Example 6 with IgniteEx

use of org.apache.ignite.internal.IgniteEx in project ignite by apache.

the class GridCacheAbstractDataStructuresFailoverSelfTest method doTestReentrantLock.

/**
     * @throws Exception If failed.
     */
private void doTestReentrantLock(final ConstantTopologyChangeWorker topWorker, final boolean failoverSafe, final boolean fair) throws Exception {
    IgniteEx ig = grid(0);
    try (IgniteLock lock = ig.reentrantLock(STRUCTURE_NAME, failoverSafe, fair, true)) {
        IgniteInternalFuture<?> fut = topWorker.startChangingTopology(new IgniteClosure<Ignite, Void>() {

            @Override
            public Void apply(Ignite ignite) {
                final IgniteLock l = ignite.reentrantLock(STRUCTURE_NAME, failoverSafe, fair, false);
                final AtomicBoolean done = new AtomicBoolean(false);
                GridTestUtils.runAsync(new Callable<Void>() {

                    @Override
                    public Void call() throws Exception {
                        try {
                            l.lock();
                        } finally {
                            done.set(true);
                        }
                        return null;
                    }
                });
                // Wait until l.lock() has been called.
                while (!l.hasQueuedThreads() && !done.get()) {
                // No-op.
                }
                return null;
            }
        });
        while (!fut.isDone()) {
            try {
                lock.lock();
            } catch (IgniteException e) {
                // Exception may happen in non-failoversafe mode.
                if (failoverSafe)
                    throw e;
            } finally {
                // Broken lock cannot be used in non-failoversafe mode.
                if (!lock.isBroken() || failoverSafe) {
                    assertTrue(lock.isHeldByCurrentThread());
                    lock.unlock();
                    assertFalse(lock.isHeldByCurrentThread());
                }
            }
        }
        fut.get();
        for (Ignite g : G.allGrids()) {
            IgniteLock l = g.reentrantLock(STRUCTURE_NAME, failoverSafe, fair, false);
            assertTrue(g.name(), !l.isHeldByCurrentThread() || lock.isBroken());
        }
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteException(org.apache.ignite.IgniteException) IgniteEx(org.apache.ignite.internal.IgniteEx) IgniteLock(org.apache.ignite.IgniteLock) Ignite(org.apache.ignite.Ignite) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable)

Example 7 with IgniteEx

use of org.apache.ignite.internal.IgniteEx in project ignite by apache.

the class GridCacheQueueApiSelfAbstractTest method testAffinityCall.

/**
     * @throws Exception If failed.
     */
public void testAffinityCall() throws Exception {
    final CollectionConfiguration colCfg = collectionConfiguration();
    colCfg.setCollocated(false);
    colCfg.setCacheMode(CacheMode.PARTITIONED);
    try (final IgniteQueue<Integer> queue1 = grid(0).queue("Queue1", 0, colCfg)) {
        GridTestUtils.assertThrows(log, new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                queue1.affinityCall(new IgniteCallable<Object>() {

                    @Override
                    public Object call() {
                        return null;
                    }
                });
                return null;
            }
        }, IgniteException.class, "Failed to execute affinityCall() for non-collocated queue: " + queue1.name() + ". This operation is supported only for collocated queues.");
    }
    colCfg.setCollocated(true);
    try (final IgniteQueue<Integer> queue2 = grid(0).queue("Queue2", 0, colCfg)) {
        queue2.add(100);
        Integer res = queue2.affinityCall(new IgniteCallable<Integer>() {

            @IgniteInstanceResource
            private IgniteEx ignite;

            @Override
            public Integer call() {
                assertTrue(ignite.cachex("datastructures_0").affinity().isPrimaryOrBackup(ignite.cluster().localNode(), "Queue2"));
                return queue2.take();
            }
        });
        assertEquals(100, res.intValue());
    }
}
Also used : IgniteInstanceResource(org.apache.ignite.resources.IgniteInstanceResource) IgniteCallable(org.apache.ignite.lang.IgniteCallable) IgniteEx(org.apache.ignite.internal.IgniteEx) CollectionConfiguration(org.apache.ignite.configuration.CollectionConfiguration) IgniteException(org.apache.ignite.IgniteException)

Example 8 with IgniteEx

use of org.apache.ignite.internal.IgniteEx in project ignite by apache.

the class IgniteCachePartitionLossPolicySelfTest method checkLostPartition.

/**
     * @param canWrite {@code True} if writes are allowed.
     * @param safe {@code True} if lost partition should trigger exception.
     * @throws Exception if failed.
     */
private void checkLostPartition(boolean canWrite, boolean safe) throws Exception {
    assert partLossPlc != null;
    int part = prepareTopology();
    for (Ignite ig : G.allGrids()) {
        info("Checking node: " + ig.cluster().localNode().id());
        IgniteCache<Integer, Integer> cache = ig.cache(CACHE_NAME);
        verifyCacheOps(canWrite, safe, part, ig);
        // Check we can read and write to lost partition in recovery mode.
        IgniteCache<Integer, Integer> recoverCache = cache.withPartitionRecover();
        for (int lostPart : recoverCache.lostPartitions()) {
            recoverCache.get(lostPart);
            recoverCache.put(lostPart, lostPart);
        }
        // Check that writing in recover mode does not clear partition state.
        verifyCacheOps(canWrite, safe, part, ig);
    }
    // Check that partition state does not change after we start a new node.
    IgniteEx grd = startGrid(3);
    info("Newly started node: " + grd.cluster().localNode().id());
    for (Ignite ig : G.allGrids()) verifyCacheOps(canWrite, safe, part, ig);
    ignite(0).resetLostPartitions(Collections.singletonList(CACHE_NAME));
    awaitPartitionMapExchange(true, true, null);
    for (Ignite ig : G.allGrids()) {
        IgniteCache<Integer, Integer> cache = ig.cache(CACHE_NAME);
        assertTrue(cache.lostPartitions().isEmpty());
        int parts = ig.affinity(CACHE_NAME).partitions();
        for (int i = 0; i < parts; i++) {
            cache.get(i);
            cache.put(i, i);
        }
    }
}
Also used : IgniteEx(org.apache.ignite.internal.IgniteEx) Ignite(org.apache.ignite.Ignite)

Example 9 with IgniteEx

use of org.apache.ignite.internal.IgniteEx in project ignite by apache.

the class IgniteCrossCacheTxStoreSelfTest method testDifferentStores.

/**
     * @throws Exception If failed.
     */
public void testDifferentStores() throws Exception {
    IgniteEx grid = grid(0);
    TestStore firstStore = (TestStore) firstStores.get(grid.name());
    TestStore secondStore = (TestStore) secondStores.get(grid.name());
    assertNotNull(firstStore);
    assertNotNull(secondStore);
    Collection<String> firstStoreEvts = firstStore.events();
    Collection<String> secondStoreEvts = secondStore.events();
    try (Transaction tx = grid.transactions().txStart()) {
        IgniteCache<Object, Object> cacheA = grid.cache("cacheA");
        IgniteCache<Object, Object> cacheC = grid.cache("cacheC");
        cacheA.put("1", "1");
        cacheA.put("2", "2");
        cacheC.put("1", "1");
        cacheC.put("2", "2");
        cacheA.remove("3");
        cacheA.remove("4");
        cacheC.remove("3");
        cacheC.remove("4");
        cacheA.put("5", "5");
        cacheA.remove("6");
        cacheC.put("7", "7");
        tx.commit();
    }
    assertEqualsCollections(F.asList("writeAll cacheA 2", "deleteAll cacheA 2", "write cacheA", "delete cacheA", "sessionEnd true"), firstStoreEvts);
    assertEqualsCollections(F.asList("writeAll cacheC 2", "deleteAll cacheC 2", "write cacheC", "sessionEnd true"), secondStoreEvts);
}
Also used : Transaction(org.apache.ignite.transactions.Transaction) IgniteEx(org.apache.ignite.internal.IgniteEx)

Example 10 with IgniteEx

use of org.apache.ignite.internal.IgniteEx in project ignite by apache.

the class GridCacheNearMultiNodeSelfTest method mapKeys.

/** @param cnt Count. */
private Map<UUID, T2<Set<Integer>, Set<Integer>>> mapKeys(int cnt) {
    Affinity<Object> aff = affinity(0);
    //Mapping primary and backup keys on node
    Map<UUID, T2<Set<Integer>, Set<Integer>>> map = new HashMap<>();
    for (int i = 0; i < GRID_CNT; i++) {
        IgniteEx grid = grid(i);
        map.put(grid.cluster().localNode().id(), new T2<Set<Integer>, Set<Integer>>(new HashSet<Integer>(), new HashSet<Integer>()));
    }
    for (int key = 1; key <= cnt; key++) {
        Integer part = aff.partition(key);
        assert part != null;
        Collection<ClusterNode> nodes = aff.mapPartitionToPrimaryAndBackups(part);
        ClusterNode primary = F.first(nodes);
        map.get(primary.id()).get1().add(key);
        if (mapDebug)
            info("Mapped key to primary node [key=" + key + ", node=" + U.toShortString(primary));
        for (ClusterNode n : nodes) {
            if (n != primary) {
                map.get(n.id()).get2().add(key);
                if (mapDebug)
                    info("Mapped key to backup node [key=" + key + ", node=" + U.toShortString(n));
            }
        }
    }
    return map;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteEx(org.apache.ignite.internal.IgniteEx) UUID(java.util.UUID) T2(org.apache.ignite.internal.util.typedef.T2) HashSet(java.util.HashSet)

Aggregations

IgniteEx (org.apache.ignite.internal.IgniteEx)198 Ignite (org.apache.ignite.Ignite)42 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)40 IgniteException (org.apache.ignite.IgniteException)36 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)32 NearCacheConfiguration (org.apache.ignite.configuration.NearCacheConfiguration)26 Transaction (org.apache.ignite.transactions.Transaction)25 HashMap (java.util.HashMap)18 CacheException (javax.cache.CacheException)18 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)17 ArrayList (java.util.ArrayList)16 LinkedHashMap (java.util.LinkedHashMap)16 IgniteCache (org.apache.ignite.IgniteCache)16 CountDownLatch (java.util.concurrent.CountDownLatch)14 IgniteKernal (org.apache.ignite.internal.IgniteKernal)14 UUID (java.util.UUID)13 ClusterNode (org.apache.ignite.cluster.ClusterNode)13 IgniteSpiException (org.apache.ignite.spi.IgniteSpiException)12 IOException (java.io.IOException)11 Map (java.util.Map)11