use of org.apache.ignite.IgniteAtomicStamped in project ignite by apache.
the class IgniteClientReconnectAtomicsTest method testAtomicStampedReconnect.
/**
* @throws Exception If failed.
*/
public void testAtomicStampedReconnect() throws Exception {
Ignite client = grid(serverCount());
assertTrue(client.cluster().localNode().isClient());
Ignite srv = clientRouter(client);
IgniteAtomicStamped clientAtomicStamped = client.atomicStamped("atomicStamped", 0, 0, true);
assertEquals(true, clientAtomicStamped.compareAndSet(0, 1, 0, 1));
assertEquals(1, clientAtomicStamped.value());
assertEquals(1, clientAtomicStamped.stamp());
final IgniteAtomicStamped srvAtomicStamped = srv.atomicStamped("atomicStamped", 0, 0, false);
assertEquals(true, srvAtomicStamped.compareAndSet(1, 2, 1, 2));
assertEquals(2, srvAtomicStamped.value());
assertEquals(2, srvAtomicStamped.stamp());
reconnectClientNode(client, srv, new Runnable() {
@Override
public void run() {
assertEquals(true, srvAtomicStamped.compareAndSet(2, 3, 2, 3));
assertEquals(3, srvAtomicStamped.value());
assertEquals(3, srvAtomicStamped.stamp());
}
});
assertEquals(true, clientAtomicStamped.compareAndSet(3, 4, 3, 4));
assertEquals(4, clientAtomicStamped.value());
assertEquals(4, clientAtomicStamped.stamp());
assertEquals(true, srvAtomicStamped.compareAndSet(4, 5, 4, 5));
assertEquals(5, srvAtomicStamped.value());
assertEquals(5, srvAtomicStamped.stamp());
srvAtomicStamped.close();
}
use of org.apache.ignite.IgniteAtomicStamped in project ignite by apache.
the class IgniteClientReconnectAtomicsTest method testAtomicStampedReconnectInProgress.
/**
* @throws Exception If failed.
*/
public void testAtomicStampedReconnectInProgress() throws Exception {
Ignite client = grid(serverCount());
assertTrue(client.cluster().localNode().isClient());
Ignite srv = clientRouter(client);
final IgniteAtomicStamped clientAtomicStamped = client.atomicStamped("atomicStampedInProgress", 0, 0, true);
assertEquals(true, clientAtomicStamped.compareAndSet(0, 1, 0, 1));
assertEquals(1, clientAtomicStamped.value());
assertEquals(1, clientAtomicStamped.stamp());
IgniteAtomicStamped srvAtomicStamped = srv.atomicStamped("atomicStampedInProgress", 0, 0, false);
assertEquals(true, srvAtomicStamped.compareAndSet(1, 2, 1, 2));
assertEquals(2, srvAtomicStamped.value());
assertEquals(2, srvAtomicStamped.stamp());
BlockTcpCommunicationSpi servCommSpi = commSpi(srv);
servCommSpi.blockMessage(GridNearTxPrepareResponse.class);
final IgniteInternalFuture<Object> fut = GridTestUtils.runAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
try {
clientAtomicStamped.compareAndSet(2, 3, 2, 3);
} catch (IgniteClientDisconnectedException e) {
checkAndWait(e);
return true;
}
return false;
}
});
// Check that client waiting operation.
GridTestUtils.assertThrows(log, new Callable<Object>() {
@Override
public Object call() throws Exception {
return fut.get(200);
}
}, IgniteFutureTimeoutCheckedException.class, null);
assertNotDone(fut);
servCommSpi.unblockMessage();
reconnectClientNode(client, srv, null);
assertTrue((Boolean) fut.get(2, TimeUnit.SECONDS));
// Check that after reconnect working.
assertEquals(false, clientAtomicStamped.compareAndSet(2, 3, 2, 3));
assertEquals(3, clientAtomicStamped.value());
assertEquals(3, clientAtomicStamped.stamp());
assertEquals(true, srvAtomicStamped.compareAndSet(3, 4, 3, 4));
assertEquals(4, srvAtomicStamped.value());
assertEquals(4, srvAtomicStamped.stamp());
srvAtomicStamped.close();
}
use of org.apache.ignite.IgniteAtomicStamped in project ignite by apache.
the class IgniteClientReconnectAtomicsTest method testAtomicStampedReconnectRemoved.
/**
* @throws Exception If failed.
*/
public void testAtomicStampedReconnectRemoved() throws Exception {
Ignite client = grid(serverCount());
assertTrue(client.cluster().localNode().isClient());
Ignite srv = clientRouter(client);
final IgniteAtomicStamped clientAtomicStamped = client.atomicStamped("atomicStampedRemoved", 0, 0, true);
assertEquals(true, clientAtomicStamped.compareAndSet(0, 1, 0, 1));
assertEquals(1, clientAtomicStamped.value());
assertEquals(1, clientAtomicStamped.stamp());
final IgniteAtomicStamped srvAtomicStamped = srv.atomicStamped("atomicStampedRemoved", 0, 0, false);
assertEquals(true, srvAtomicStamped.compareAndSet(1, 2, 1, 2));
assertEquals(2, srvAtomicStamped.value());
assertEquals(2, srvAtomicStamped.stamp());
reconnectClientNode(client, srv, new Runnable() {
@Override
public void run() {
srvAtomicStamped.close();
}
});
GridTestUtils.assertThrows(log, new Callable<Object>() {
@Override
public Object call() throws Exception {
clientAtomicStamped.compareAndSet(2, 3, 2, 3);
return null;
}
}, IllegalStateException.class, null);
IgniteAtomicStamped newClientAtomicStamped = client.atomicStamped("atomicStampedRemoved", 0, 0, true);
assertEquals(true, newClientAtomicStamped.compareAndSet(0, 1, 0, 1));
assertEquals(1, newClientAtomicStamped.value());
assertEquals(1, newClientAtomicStamped.stamp());
IgniteAtomicStamped newSrvAtomicStamped = srv.atomicStamped("atomicStampedRemoved", 0, 0, false);
assertEquals(true, newSrvAtomicStamped.compareAndSet(1, 2, 1, 2));
assertEquals(2, newSrvAtomicStamped.value());
assertEquals(2, newSrvAtomicStamped.stamp());
newClientAtomicStamped.close();
}
use of org.apache.ignite.IgniteAtomicStamped in project ignite by apache.
the class GridCacheAtomicReferenceMultiNodeAbstractTest method testAtomicStamped.
/**
* JUnit.
*
* @throws Exception If failed.
*/
public void testAtomicStamped() throws Exception {
// Get random name of stamped.
final String stampedName = UUID.randomUUID().toString();
// Get random value of atomic stamped.
final String val = UUID.randomUUID().toString();
// Get random value of atomic stamped.
final String stamp = UUID.randomUUID().toString();
// Get random new value of atomic stamped.
final String newVal = UUID.randomUUID().toString();
// Get random new stamp of atomic stamped.
final String newStamp = UUID.randomUUID().toString();
// Initialize atomicStamped in cache.
IgniteAtomicStamped<String, String> stamped = grid(0).atomicStamped(stampedName, val, stamp, true);
final Ignite ignite = grid(0);
// Execute task on all grid nodes.
ignite.compute().call(new IgniteCallable<String>() {
@Override
public String call() {
IgniteAtomicStamped<String, String> stamped = ignite.atomicStamped(stampedName, val, stamp, true);
assertEquals(val, stamped.value());
assertEquals(stamp, stamped.stamp());
return stamped.value();
}
});
stamped.compareAndSet("WRONG EXPECTED VALUE", newVal, "WRONG EXPECTED STAMP", newStamp);
// Execute task on all grid nodes.
ignite.compute().call(new IgniteCallable<String>() {
@Override
public String call() {
IgniteAtomicStamped<String, String> stamped = ignite.atomicStamped(stampedName, val, stamp, true);
assertEquals(val, stamped.value());
assertEquals(stamp, stamped.stamp());
return stamped.value();
}
});
stamped.compareAndSet(val, newVal, stamp, newStamp);
// Execute task on all grid nodes.
ignite.compute().call(new IgniteCallable<String>() {
@Override
public String call() {
IgniteAtomicStamped<String, String> stamped = ignite.atomicStamped(stampedName, val, stamp, true);
assertEquals(newVal, stamped.value());
assertEquals(newStamp, stamped.stamp());
return stamped.value();
}
});
}
use of org.apache.ignite.IgniteAtomicStamped 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 = 9;
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 latch, grid: " + ignite.name());
res = ignite.countDownLatch(name, 0, true, true);
break;
case 4:
log.info("Create atomic reference, grid: " + ignite.name());
res = ignite.atomicReference(name, null, true);
break;
case 5:
log.info("Create queue, grid: " + ignite.name());
res = ignite.queue(name, 0, config(false));
break;
case 6:
log.info("Create set, grid: " + ignite.name());
res = ignite.set(name, config(false));
break;
case 7:
log.info("Create atomic semaphore, grid: " + ignite.name());
res = ignite.semaphore(name, 0, false, true);
break;
case 8:
log.info("Create atomic reentrant lock, grid: " + ignite.name());
res = ignite.reentrantLock(name, true, true, true);
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();
}
}
Aggregations