Search in sources :

Example 11 with IgniteCountDownLatch

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

the class IgniteCountDownLatchAbstractSelfTest method testLatchBroadcast.

/**
     * @throws Exception If failed.
     */
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)

Example 12 with IgniteCountDownLatch

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

the class IgniteClientDataStructuresAbstractTest method testLatch.

/**
     * @param creator Creator node.
     * @param other Other node.
     * @throws Exception If failed.
     */
private void testLatch(Ignite creator, final Ignite other) throws Exception {
    assertNull(creator.countDownLatch("latch1", 1, true, false));
    assertNull(other.countDownLatch("latch1", 1, true, false));
    try (IgniteCountDownLatch latch = creator.countDownLatch("latch1", 1, true, true)) {
        assertNotNull(latch);
        assertEquals(1, latch.count());
        IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                U.sleep(1000);
                IgniteCountDownLatch latch0 = other.countDownLatch("latch1", 1, true, false);
                assertEquals(1, latch0.count());
                log.info("Count down latch.");
                latch0.countDown();
                assertEquals(0, latch0.count());
                return null;
            }
        });
        log.info("Await latch.");
        assertTrue(latch.await(5000, TimeUnit.MILLISECONDS));
        log.info("Finished wait.");
        fut.get();
    }
    assertNull(creator.countDownLatch("latch1", 1, true, false));
    assertNull(other.countDownLatch("latch1", 1, true, false));
}
Also used : IgniteCountDownLatch(org.apache.ignite.IgniteCountDownLatch)

Example 13 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.
     */
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)

Example 14 with IgniteCountDownLatch

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

the class IgniteCountDownLatchAbstractSelfTest method checkAutoDelete.

/**
     * @throws Exception Exception.
     */
private void checkAutoDelete() throws Exception {
    IgniteCountDownLatch latch = createLatch("rmv", 5, true);
    latch.countDownAll();
    // Latch should be removed since autoDelete = true
    checkRemovedLatch(latch);
    IgniteCountDownLatch latch1 = createLatch("rmv1", 5, false);
    latch1.countDownAll();
    // Latch should NOT be removed since autoDelete = false
    assert !latch1.removed();
    removeLatch("rmv1");
}
Also used : IgniteCountDownLatch(org.apache.ignite.IgniteCountDownLatch)

Example 15 with IgniteCountDownLatch

use of org.apache.ignite.IgniteCountDownLatch 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)

Aggregations

IgniteCountDownLatch (org.apache.ignite.IgniteCountDownLatch)20 Ignite (org.apache.ignite.Ignite)14 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)5 Callable (java.util.concurrent.Callable)4 IgniteCallable (org.apache.ignite.lang.IgniteCallable)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 IgniteException (org.apache.ignite.IgniteException)3 IgniteLock (org.apache.ignite.IgniteLock)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 IgniteAtomicLong (org.apache.ignite.IgniteAtomicLong)2 IgniteAtomicSequence (org.apache.ignite.IgniteAtomicSequence)2 IgniteInterruptedException (org.apache.ignite.IgniteInterruptedException)2 IgniteClosure (org.apache.ignite.lang.IgniteClosure)2 Nullable (org.jetbrains.annotations.Nullable)2 ExpectedException (org.junit.rules.ExpectedException)2 Closeable (java.io.Closeable)1