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();
}
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");
}
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);
}
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);
}
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();
}
Aggregations