Search in sources :

Example 21 with IgniteCountDownLatch

use of org.apache.ignite.IgniteCountDownLatch in project ignite by apache.

the class IgniteCountDownLatchAbstractSelfTest method checkCountDown.

/**
 * @throws Exception Exception.
 */
private void checkCountDown() throws Exception {
    IgniteCountDownLatch latch = createLatch("cnt", 10, true);
    assert latch.countDown() == 9;
    assert latch.countDown(2) == 7;
    latch.countDownAll();
    assert latch.count() == 0;
    checkRemovedLatch(latch);
    IgniteCountDownLatch latch1 = createLatch("cnt1", 10, true);
    assert latch1.countDown() == 9;
    assert latch1.countDown(2) == 7;
    latch1.countDownAll();
    assert latch1.count() == 0;
    checkRemovedLatch(latch1);
}
Also used : IgniteCountDownLatch(org.apache.ignite.IgniteCountDownLatch)

Example 22 with IgniteCountDownLatch

use of org.apache.ignite.IgniteCountDownLatch in project ignite by apache.

the class IgniteCountDownLatchAbstractSelfTest method testLatchBroadcast.

/**
 * @throws Exception If failed.
 */
@Test
public void testLatchBroadcast() throws Exception {
    Ignite ignite = grid(0);
    ClusterGroup srvsGrp = ignite.cluster().forServers();
    int numOfSrvs = srvsGrp.nodes().size();
    ignite.destroyCache("testCache");
    IgniteCache<Object, Object> cache = ignite.createCache("testCache");
    for (ClusterNode node : srvsGrp.nodes()) cache.put(String.valueOf(node.id()), 0);
    for (int i = 0; i < 500; i++) {
        IgniteCountDownLatch latch1 = createLatch1(ignite, numOfSrvs);
        IgniteCountDownLatch latch2 = createLatch2(ignite, numOfSrvs);
        ignite.compute(srvsGrp).broadcast(new IgniteRunnableJob(latch1, latch2, i));
        assertTrue(latch2.await(10000));
    }
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) Ignite(org.apache.ignite.Ignite) IgniteCountDownLatch(org.apache.ignite.IgniteCountDownLatch) Test(org.junit.Test)

Example 23 with IgniteCountDownLatch

use of org.apache.ignite.IgniteCountDownLatch in project ignite by apache.

the class IgniteCountDownLatchAbstractSelfTest method testIsolation.

/**
 * Implementation of ignite data structures internally uses special system caches, need make sure
 * that transaction on these system caches do not intersect with transactions started by user.
 *
 * @throws Exception If failed.
 */
@Test
public void testIsolation() throws Exception {
    Ignite ignite = grid(0);
    CacheConfiguration cfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
    cfg.setName("myCache");
    cfg.setAtomicityMode(TRANSACTIONAL);
    cfg.setWriteSynchronizationMode(FULL_SYNC);
    IgniteCache<Integer, Integer> cache = ignite.getOrCreateCache(cfg);
    try {
        IgniteCountDownLatch latch = ignite.countDownLatch("latch1", 10, false, true);
        assertNotNull(latch);
        try (Transaction tx = ignite.transactions().txStart()) {
            cache.put(1, 1);
            assertEquals(8, latch.countDown(2));
            tx.rollback();
        }
        assertEquals(0, cache.size());
        assertEquals(7, latch.countDown(1));
    } finally {
        ignite.destroyCache(cfg.getName());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) Ignite(org.apache.ignite.Ignite) IgniteCountDownLatch(org.apache.ignite.IgniteCountDownLatch) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) Test(org.junit.Test)

Example 24 with IgniteCountDownLatch

use of org.apache.ignite.IgniteCountDownLatch in project ignite by apache.

the class IgniteDataStructureUniqueNameTest method testUniqueName.

/**
 * @param singleGrid If {@code true} uses single grid.
 * @throws Exception If failed.
 */
private void testUniqueName(final boolean singleGrid) throws Exception {
    final String name = IgniteUuid.randomUuid().toString();
    final int DS_TYPES = 6;
    final int THREADS = DS_TYPES * 3;
    for (int iter = 0; iter < 20; iter++) {
        log.info("Iteration: " + iter);
        List<IgniteInternalFuture<Object>> futs = new ArrayList<>(THREADS);
        final CyclicBarrier barrier = new CyclicBarrier(THREADS);
        for (int i = 0; i < THREADS; i++) {
            final int idx = i;
            IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    try {
                        Thread.currentThread().setName("test thread-" + idx);
                        barrier.await();
                        Ignite ignite = singleGrid ? ignite(0) : ignite(idx % gridCount());
                        Object res;
                        switch(idx % DS_TYPES) {
                            case 0:
                                log.info("Create atomic long, grid: " + ignite.name());
                                res = ignite.atomicLong(name, 0, true);
                                break;
                            case 1:
                                log.info("Create atomic sequence, grid: " + ignite.name());
                                res = ignite.atomicSequence(name, 0, true);
                                break;
                            case 2:
                                log.info("Create atomic stamped, grid: " + ignite.name());
                                res = ignite.atomicStamped(name, 0, true, true);
                                break;
                            case 3:
                                log.info("Create atomic reference, grid: " + ignite.name());
                                res = ignite.atomicReference(name, null, true);
                                break;
                            case 4:
                                log.info("Create queue, grid: " + ignite.name());
                                res = ignite.queue(name, 0, config(false));
                                break;
                            case 5:
                                log.info("Create set, grid: " + ignite.name());
                                res = ignite.set(name, config(false));
                                break;
                            default:
                                fail();
                                return null;
                        }
                        log.info("Thread created: " + res);
                        return res;
                    } catch (IgniteException e) {
                        log.info("Failed: " + e);
                        return e;
                    }
                }
            });
            futs.add(fut);
        }
        Closeable dataStructure = null;
        int createdCnt = 0;
        for (IgniteInternalFuture<Object> fut : futs) {
            Object res = fut.get();
            if (res instanceof IgniteException || res instanceof IgniteCheckedException)
                continue;
            assertTrue("Unexpected object: " + res, res instanceof IgniteAtomicLong || res instanceof IgniteAtomicSequence || res instanceof IgniteAtomicReference || res instanceof IgniteAtomicStamped || res instanceof IgniteCountDownLatch || res instanceof IgniteQueue || res instanceof IgniteSet || res instanceof IgniteSemaphore || res instanceof IgniteLock);
            log.info("Data structure created: " + dataStructure);
            createdCnt++;
            if (dataStructure != null)
                assertEquals(dataStructure.getClass(), res.getClass());
            else
                dataStructure = (Closeable) res;
        }
        assertNotNull(dataStructure);
        assertEquals(3, createdCnt);
        dataStructure.close();
    }
}
Also used : IgniteAtomicReference(org.apache.ignite.IgniteAtomicReference) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) IgniteAtomicLong(org.apache.ignite.IgniteAtomicLong) IgniteCountDownLatch(org.apache.ignite.IgniteCountDownLatch) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) CyclicBarrier(java.util.concurrent.CyclicBarrier) IgniteSet(org.apache.ignite.IgniteSet) IgniteAtomicStamped(org.apache.ignite.IgniteAtomicStamped) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IgniteQueue(org.apache.ignite.IgniteQueue) IgniteAtomicSequence(org.apache.ignite.IgniteAtomicSequence) Ignite(org.apache.ignite.Ignite) IgniteSemaphore(org.apache.ignite.IgniteSemaphore) IgniteLock(org.apache.ignite.IgniteLock)

Example 25 with IgniteCountDownLatch

use of org.apache.ignite.IgniteCountDownLatch in project ignite by apache.

the class IgniteLocalCountDownLatchSelfTest method testLatch.

/**
 * {@inheritDoc}
 */
@Test
@Override
public void testLatch() throws Exception {
    // Test main functionality.
    IgniteCountDownLatch latch = grid(0).countDownLatch("latch", 2, false, true);
    assertNotNull(latch);
    assertEquals(2, latch.count());
    IgniteInternalFuture<?> fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Nullable
        @Override
        public Object call() throws Exception {
            IgniteCountDownLatch latch = grid(0).countDownLatch("latch", 2, false, true);
            assert latch != null && latch.count() == 2;
            info("Thread is going to wait on latch: " + Thread.currentThread().getName());
            assert latch.await(1, MINUTES);
            info("Thread is again runnable: " + Thread.currentThread().getName());
            return null;
        }
    }, THREADS_CNT, "test-thread");
    Thread.sleep(3000);
    assert latch.countDown() == 1;
    assert latch.countDown() == 0;
    assert latch.await(1, SECONDS);
    // Ensure there are no hangs.
    fut.get();
    // Test operations on removed latch.
    IgniteCountDownLatch latch0 = grid(0).countDownLatch("latch", 0, false, false);
    assertNotNull(latch0);
    latch0.close();
    checkRemovedLatch(latch);
}
Also used : IgniteCountDownLatch(org.apache.ignite.IgniteCountDownLatch) Nullable(org.jetbrains.annotations.Nullable) IgniteCountDownLatchAbstractSelfTest(org.apache.ignite.internal.processors.cache.datastructures.IgniteCountDownLatchAbstractSelfTest) Test(org.junit.Test)

Aggregations

IgniteCountDownLatch (org.apache.ignite.IgniteCountDownLatch)29 Ignite (org.apache.ignite.Ignite)20 Test (org.junit.Test)14 IgniteException (org.apache.ignite.IgniteException)6 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)6 ArrayList (java.util.ArrayList)5 Callable (java.util.concurrent.Callable)5 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)4 IgniteLock (org.apache.ignite.IgniteLock)4 IgniteCallable (org.apache.ignite.lang.IgniteCallable)4 IOException (java.io.IOException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 IgniteAtomicLong (org.apache.ignite.IgniteAtomicLong)3 IgniteAtomicSequence (org.apache.ignite.IgniteAtomicSequence)3 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 IgniteAtomicReference (org.apache.ignite.IgniteAtomicReference)2