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