use of org.apache.ignite.IgniteSemaphore 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.IgniteSemaphore in project ignite by apache.
the class IgnitePersistentStoreDataStructuresTest method testSemaphoreVolatility.
/**
* @throws Exception If failed.
*/
@Test
public void testSemaphoreVolatility() throws Exception {
Ignite ignite = startGrids(4);
ignite.active(true);
IgniteSemaphore sem = ignite.semaphore("test", 10, false, true);
assert sem != null;
stopAllGrids();
ignite = startGrids(4);
ignite.active(true);
sem = ignite.semaphore("test", 10, false, false);
assert sem == null;
}
use of org.apache.ignite.IgniteSemaphore in project ignite by apache.
the class IgniteClientReconnectAtomicsTest method testSemaphoreReconnect.
/**
* @throws Exception If failed.
*/
@Test
public void testSemaphoreReconnect() throws Exception {
Ignite client = grid(serverCount());
assertTrue(client.cluster().localNode().isClient());
Ignite srv = ignite(0);
IgniteSemaphore clientSemaphore = client.semaphore("semaphore1", 3, false, true);
assertEquals(3, clientSemaphore.availablePermits());
final IgniteSemaphore srvSemaphore = srv.semaphore("semaphore1", 3, false, false);
assertEquals(3, srvSemaphore.availablePermits());
reconnectClientNode(client, srv, new Runnable() {
@Override
public void run() {
srvSemaphore.acquire();
}
});
assertEquals(2, srvSemaphore.availablePermits());
assertEquals(2, clientSemaphore.availablePermits());
srvSemaphore.acquire();
assertEquals(1, srvSemaphore.availablePermits());
assertEquals(1, clientSemaphore.availablePermits());
clientSemaphore.acquire();
assertEquals(0, srvSemaphore.availablePermits());
assertEquals(0, clientSemaphore.availablePermits());
assertFalse(srvSemaphore.tryAcquire());
assertFalse(srvSemaphore.tryAcquire());
}
use of org.apache.ignite.IgniteSemaphore in project ignite by apache.
the class IgniteInlineIndexBenchmark method setUp.
/**
* {@inheritDoc}
*/
@Override
public void setUp(BenchmarkConfiguration cfg) throws Exception {
super.setUp(cfg);
range = args.range();
isJavaObj = args.getBooleanParameter("javaObject", false);
if (isJavaObj) {
cacheName = "CACHE_POJO";
keyCls = TestKey.class;
} else {
cacheName = "CACHE_LONG";
keyCls = Integer.class;
}
printParameters();
IgniteSemaphore sem = ignite().semaphore("setup", 1, true, true);
try {
if (sem.tryAcquire()) {
println(cfg, "Create tables...");
init();
} else {
// Acquire (wait setup by other client) and immediately release/
println(cfg, "Waits for setup...");
sem.acquire();
}
} finally {
sem.release();
}
}
use of org.apache.ignite.IgniteSemaphore in project ignite by apache.
the class IgniteSqlQueryJoinBenchmark2 method setUp.
/**
* {@inheritDoc}
*/
@Override
public void setUp(BenchmarkConfiguration cfg) throws Exception {
super.setUp(cfg);
String subqueryName = args.getStringParameter(SUBQUERY_PARAM, null);
if (!F.isEmpty(subqueryName))
subqry = Subquery.valueOfSafe(subqueryName.trim().toUpperCase());
if (subqry == null)
throw new IllegalArgumentException("The \"" + SUBQUERY_PARAM + "\" is mandatory" + " and should be one of the " + Arrays.toString(Subquery.values()));
if (args.getBooleanParameter(SKIP_INIT_PARAM, false))
return;
IgniteSemaphore sem = ignite().semaphore("setup", 1, true, true);
try {
if (sem.tryAcquire()) {
println(cfg, "Create tables...");
init();
} else {
println(cfg, "Waits for setup...");
sem.acquire();
}
} finally {
sem.release();
}
println(cfg, "Done");
}
Aggregations