Search in sources :

Example 1 with IgniteCountDownLatch

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

the class IgniteLockAbstractSelfTest method testLockInterruptiblyMultinode.

/**
 * @throws Exception If failed.
 */
private void testLockInterruptiblyMultinode(final boolean fair) throws Exception {
    if (gridCount() == 1)
        return;
    // Initialize reentrant lock.
    final IgniteLock lock0 = grid(0).reentrantLock("lock", true, fair, true);
    assertEquals(0, lock0.getHoldCount());
    assertFalse(lock0.hasQueuedThreads());
    lock0.lock();
    // Number of threads, one per node.
    final int threadCount = gridCount();
    final AtomicLong threadCounter = new AtomicLong(0);
    IgniteInternalFuture<?> fut = multithreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            final int localNodeId = (int) threadCounter.getAndIncrement();
            final Ignite grid = grid(localNodeId);
            IgniteClosure<Ignite, Void> closure = new IgniteClosure<Ignite, Void>() {

                @Override
                public Void apply(Ignite ignite) {
                    final IgniteLock l = ignite.reentrantLock("lock", true, true, true);
                    final AtomicReference<Thread> thread = new AtomicReference<>();
                    final AtomicBoolean done = new AtomicBoolean(false);
                    final AtomicBoolean exceptionThrown = new AtomicBoolean(false);
                    final IgniteCountDownLatch latch = ignite.countDownLatch("latch", threadCount, false, true);
                    IgniteInternalFuture<?> fut = GridTestUtils.runAsync(new Callable<Void>() {

                        @Override
                        public Void call() throws Exception {
                            try {
                                thread.set(Thread.currentThread());
                                l.lockInterruptibly();
                            } catch (IgniteInterruptedException ignored) {
                                exceptionThrown.set(true);
                            } finally {
                                done.set(true);
                            }
                            return null;
                        }
                    });
                    // Wait until l.lock() has been called.
                    while (!l.hasQueuedThreads()) {
                    // No-op.
                    }
                    latch.countDown();
                    latch.await();
                    thread.get().interrupt();
                    while (!done.get()) {
                    // No-op.
                    }
                    try {
                        fut.get();
                    } catch (IgniteCheckedException e) {
                        fail(e.getMessage());
                        throw new RuntimeException(e);
                    }
                    assertTrue(exceptionThrown.get());
                    return null;
                }
            };
            closure.apply(grid);
            return null;
        }
    }, threadCount);
    fut.get();
    lock0.unlock();
    info("Checking if interrupted threads are removed from global waiting queue...");
    // Check if interrupted threads are removed from global waiting queue.
    boolean locked = lock0.tryLock(1000, MILLISECONDS);
    info("Interrupted threads successfully removed from global waiting queue. ");
    assertTrue(locked);
    lock0.unlock();
    assertFalse(lock0.isLocked());
    lock0.close();
}
Also used : IgniteClosure(org.apache.ignite.lang.IgniteClosure) AtomicReference(java.util.concurrent.atomic.AtomicReference) IgniteCountDownLatch(org.apache.ignite.IgniteCountDownLatch) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) ExpectedException(org.junit.rules.ExpectedException) IOException(java.io.IOException) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteLock(org.apache.ignite.IgniteLock) Ignite(org.apache.ignite.Ignite)

Example 2 with IgniteCountDownLatch

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

the class IgniteCountDownLatchAbstractSelfTest method checkAwait.

/**
 * @throws Exception Exception.
 */
private void checkAwait() throws Exception {
    // Check only 'false' cases here. Successful await is tested over the grid.
    IgniteCountDownLatch latch = createLatch("await", 5, false);
    assert !latch.await(10);
    assert !latch.await(10, MILLISECONDS);
    removeLatch("await");
}
Also used : IgniteCountDownLatch(org.apache.ignite.IgniteCountDownLatch)

Example 3 with IgniteCountDownLatch

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

the class IgniteCountDownLatchAbstractSelfTest method testLatchMultinode1.

/**
 * @throws Exception If failed.
 */
@Test
public void testLatchMultinode1() throws Exception {
    if (gridCount() == 1)
        return;
    IgniteCountDownLatch latch = grid(0).countDownLatch("l1", 10, true, true);
    List<IgniteInternalFuture<?>> futs = new ArrayList<>();
    final AtomicBoolean countedDown = new AtomicBoolean();
    CountDownLatch allLatchesObtained = new CountDownLatch(gridCount());
    for (int i = 0; i < gridCount(); i++) {
        final Ignite ignite = grid(i);
        futs.add(GridTestUtils.runAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                IgniteCountDownLatch latch = ignite.countDownLatch("l1", 10, true, false);
                allLatchesObtained.countDown();
                assertNotNull(latch);
                boolean wait = latch.await(30_000);
                assertTrue(countedDown.get());
                assertEquals(0, latch.count());
                assertTrue(wait);
                return null;
            }
        }));
    }
    for (int i = 0; i < 10; i++) {
        if (i == 9) {
            countedDown.set(true);
            allLatchesObtained.await();
        }
        latch.countDown();
    }
    for (IgniteInternalFuture<?> fut : futs) fut.get(30_000);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArrayList(java.util.ArrayList) IgniteCountDownLatch(org.apache.ignite.IgniteCountDownLatch) Ignite(org.apache.ignite.Ignite) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) IgniteCountDownLatch(org.apache.ignite.IgniteCountDownLatch) CountDownLatch(java.util.concurrent.CountDownLatch) Callable(java.util.concurrent.Callable) IgniteCallable(org.apache.ignite.lang.IgniteCallable) Test(org.junit.Test)

Example 4 with IgniteCountDownLatch

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

the class IgniteCountDownLatchAbstractSelfTest method removeLatch.

/**
 * @param latchName Latch name.
 * @throws Exception If failed.
 */
private void removeLatch(String latchName) throws Exception {
    IgniteCountDownLatch latch = grid(RND.nextInt(NODES_CNT)).countDownLatch(latchName, 10, false, true);
    assert latch != null;
    if (latch.count() > 0)
        latch.countDownAll();
    // Remove latch on random node.
    IgniteCountDownLatch latch0 = grid(RND.nextInt(NODES_CNT)).countDownLatch(latchName, 0, false, false);
    assertNotNull(latch0);
    latch0.close();
    // Ensure latch is removed on all nodes.
    for (Ignite g : G.allGrids()) assertNull(((IgniteKernal) g).context().dataStructures().countDownLatch(latchName, null, 10, true, false));
    checkRemovedLatch(latch);
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) IgniteCountDownLatch(org.apache.ignite.IgniteCountDownLatch) Ignite(org.apache.ignite.Ignite)

Example 5 with IgniteCountDownLatch

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

the class DataStructures method countDownLatch.

@Test
void countDownLatch() {
    // tag::count-down-latch[]
    Ignite ignite = Ignition.start();
    IgniteCountDownLatch latch = // Latch name.
    ignite.countDownLatch(// Latch name.
    "latchName", // Initial count.
    10, // Auto remove, when counter has reached zero.
    false, // Create if it does not exist.
    true);
    // end::count-down-latch[]
    ignite.close();
}
Also used : Ignite(org.apache.ignite.Ignite) IgniteCountDownLatch(org.apache.ignite.IgniteCountDownLatch) Test(org.junit.jupiter.api.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