use of org.apache.ignite.IgniteAtomicSequence 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.IgniteAtomicSequence in project ignite by apache.
the class CacheClientsConcurrentStartTest method doTest.
/**
* @throws Exception If failed.
*/
private void doTest() throws Exception {
final AtomicBoolean failed = new AtomicBoolean();
startGrids(SRV_CNT);
for (int i = 0; i < SRV_CNT; i++) {
((TestRecordingCommunicationSpi) ignite(i).configuration().getCommunicationSpi()).blockMessages(new IgniteBiPredicate<ClusterNode, Message>() {
@Override
public boolean apply(ClusterNode node, Message msg) {
if (msg instanceof GridDhtPartitionsFullMessage) {
try {
U.sleep(ThreadLocalRandom.current().nextLong(500) + 100);
} catch (Exception e) {
e.printStackTrace();
}
}
return false;
}
});
}
List<IgniteInternalFuture<?>> futs = new ArrayList<>();
for (int i = 0; i < CLIENTS_CNT; i++) {
final int idx = i;
IgniteInternalFuture<?> fut = multithreadedAsync(new Runnable() {
@Override
public void run() {
Random rnd = new Random();
try {
Ignite ignite = startClientGrid(SRV_CNT + idx);
assertTrue(ignite.configuration().isClientMode());
for (int i = 0; i < CACHES / 2; i++) {
String cacheName = "cache-" + rnd.nextInt(CACHES);
IgniteCache<Object, Object> cache = getCache(ignite, cacheName);
cache.put(ignite.cluster().localNode().id(), UUID.randomUUID());
IgniteAtomicSequence seq = ignite.atomicSequence("seq-" + rnd.nextInt(20), 0, true);
seq.getAndIncrement();
}
while (!stopped) {
IgniteCache<Object, Object> cache = getCache(ignite, "cache-" + rnd.nextInt(CACHES));
int val = Math.abs(rnd.nextInt(100));
if (val >= 0 && val < 40)
cache.containsKey(ignite.cluster().localNode().id());
else if (val >= 40 && val < 80)
cache.get(ignite.cluster().localNode().id());
else
cache.put(ignite.cluster().localNode().id(), UUID.randomUUID());
Thread.sleep(10);
}
} catch (Exception e) {
log.error("Unexpected error: " + e, e);
failed.set(true);
}
}
}, 1, "client-thread");
futs.add(fut);
}
Thread.sleep(10_000);
stopped = true;
for (IgniteInternalFuture<?> fut : futs) fut.get();
assertFalse(failed.get());
}
use of org.apache.ignite.IgniteAtomicSequence in project ignite by apache.
the class GridCachePartitionedAtomicSequenceMultiThreadedTest method testGetAndIncrement.
/**
* @throws Exception If failed.
*/
@Test
public void testGetAndIncrement() throws Exception {
// Random sequence names.
String seqName = UUID.randomUUID().toString();
final IgniteAtomicSequence seq = grid(0).atomicSequence(seqName, 0L, true);
runSequenceClosure(new GridInUnsafeClosure<IgniteAtomicSequence>() {
@Override
public void apply(IgniteAtomicSequence t) {
t.getAndIncrement();
}
}, seq, ITERATION_NUM, THREAD_NUM);
assertEquals(ITERATION_NUM * THREAD_NUM, seq.get());
}
use of org.apache.ignite.IgniteAtomicSequence in project ignite by apache.
the class GridCachePartitionedAtomicSequenceMultiThreadedTest method testGetAndIncrementAsync.
/**
* @throws Exception If failed.
*/
@Test
public void testGetAndIncrementAsync() throws Exception {
// Random sequence names.
String seqName = UUID.randomUUID().toString();
final IgniteAtomicSequence seq = grid(0).atomicSequence(seqName, 0L, true);
runSequenceClosure(new GridInUnsafeClosure<IgniteAtomicSequence>() {
@Override
public void apply(IgniteAtomicSequence t) {
t.getAndIncrement();
}
}, seq, ITERATION_NUM, THREAD_NUM);
assertEquals(ITERATION_NUM * THREAD_NUM, seq.get());
}
use of org.apache.ignite.IgniteAtomicSequence in project ignite by apache.
the class GridCachePartitionedAtomicSequenceMultiThreadedTest method testAddAndGet.
/**
* @throws Exception If failed.
*/
@Test
public void testAddAndGet() throws Exception {
// Random sequence names.
String seqName = UUID.randomUUID().toString();
final IgniteAtomicSequence seq = grid(0).atomicSequence(seqName, 0L, true);
runSequenceClosure(new GridInUnsafeClosure<IgniteAtomicSequence>() {
@Override
public void apply(IgniteAtomicSequence t) {
t.addAndGet(5);
}
}, seq, ITERATION_NUM, THREAD_NUM);
assertEquals(5 * ITERATION_NUM * THREAD_NUM, seq.get());
}
Aggregations