use of org.apache.ignite.IgniteSet in project ignite by apache.
the class GridCacheSetAbstractSelfTest method testCleanup.
/**
* @param collocated Collocation flag.
* @throws Exception If failed.
*/
private void testCleanup(boolean collocated) throws Exception {
CollectionConfiguration colCfg = config(collocated);
final IgniteSet<Integer> set0 = grid(0).set(SET_NAME, colCfg);
assertNotNull(set0);
final Collection<Set<Integer>> sets = new ArrayList<>();
for (int i = 0; i < gridCount(); i++) {
IgniteSet<Integer> set = grid(i).set(SET_NAME, null);
assertNotNull(set);
sets.add(set);
}
Collection<Integer> items = new ArrayList<>(10_000);
for (int i = 0; i < 10_000; i++) items.add(i);
set0.addAll(items);
assertEquals(10_000, set0.size());
final AtomicBoolean stop = new AtomicBoolean();
final AtomicInteger val = new AtomicInteger(10_000);
IgniteInternalFuture<?> fut;
try {
fut = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {
@Override
public Object call() throws Exception {
try {
while (!stop.get()) {
for (Set<Integer> set : sets) set.add(val.incrementAndGet());
}
} catch (IllegalStateException e) {
log.info("Set removed: " + e);
}
return null;
}
}, 5, "set-add-thread");
set0.close();
} finally {
stop.set(true);
}
fut.get();
int cnt = 0;
GridCacheContext cctx = GridTestUtils.getFieldValue(set0, "cctx");
boolean separated = separated(set0);
if (separated)
awaitPartitionMapExchange();
for (int i = 0; i < gridCount(); i++) {
GridCacheAdapter cache = grid(i).context().cache().internalCache(cctx.name());
if (separated) {
assertNull("Cache " + cctx.name() + " was not destroyed.", cache);
continue;
}
for (Object e : cache.localEntries(new CachePeekMode[] { CachePeekMode.ALL })) {
cnt++;
log.info("Unexpected entry: " + e);
}
}
assertEquals("Found unexpected cache entries", 0, cnt);
for (final Set<Integer> set : sets) {
GridTestUtils.assertThrows(log, new Callable<Void>() {
@Override
public Void call() throws Exception {
set.add(10);
return null;
}
}, IllegalStateException.class, null);
}
}
use of org.apache.ignite.IgniteSet in project ignite by apache.
the class GridCacheSetAbstractSelfTest method testCacheReuse.
/**
* @param collocated Collocation flag.
*/
private void testCacheReuse(boolean collocated) {
Ignite ignite = grid(0);
CollectionConfiguration colCfg = collectionConfiguration().setCollocated(collocated);
colCfg.setAtomicityMode(ATOMIC);
colCfg.setGroupName("grp1");
IgniteSet set1 = ignite.set("set1", colCfg);
IgniteSet set2 = ignite.set("set2", colCfg);
assertEquals(separated(set1), cctx(set1).cacheId() != cctx(set2).cacheId());
colCfg.setAtomicityMode(TRANSACTIONAL);
IgniteSet set3 = ignite.set("set3", colCfg);
IgniteSet set4 = ignite.set("set4", colCfg);
assertEquals(separated(set3), cctx(set3).cacheId() != cctx(set4).cacheId());
assertTrue(cctx(set1).cacheId() != cctx(set3).cacheId());
assertTrue(cctx(set1).groupId() == cctx(set3).groupId());
colCfg.setGroupName("gtp2");
IgniteSet set5 = ignite.set("set5", colCfg);
IgniteSet set6 = ignite.set("set6", colCfg);
assertEquals(separated(set5), cctx(set5).cacheId() != cctx(set6).cacheId());
assertTrue(cctx(set1).groupId() != cctx(set5).groupId());
Stream.of(set1, set2, set3, set4, set5, set6).forEach(IgniteSet::close);
}
use of org.apache.ignite.IgniteSet in project ignite by apache.
the class GridCacheSetAbstractSelfTest method testCacheReuse.
/**
* Test that sets within the same group and compatible configurations are stored in the same cache.
*
* @throws Exception If failed.
*/
public void testCacheReuse() throws Exception {
Ignite ignite = grid(0);
CollectionConfiguration colCfg = collectionConfiguration();
colCfg.setAtomicityMode(ATOMIC);
colCfg.setGroupName("grp1");
IgniteSet set1 = ignite.set("set1", colCfg);
IgniteSet set2 = ignite.set("set2", colCfg);
assert cctx(set1).cacheId() == cctx(set2).cacheId();
colCfg.setAtomicityMode(TRANSACTIONAL);
IgniteSet set3 = ignite.set("set3", colCfg);
IgniteSet set4 = ignite.set("set4", colCfg);
assert cctx(set3).cacheId() == cctx(set4).cacheId();
assert cctx(set1).cacheId() != cctx(set3).cacheId();
assert cctx(set1).groupId() == cctx(set3).groupId();
colCfg.setGroupName("gtp2");
IgniteSet set5 = ignite.set("set5", colCfg);
IgniteSet set6 = ignite.set("set6", colCfg);
assert cctx(set5).cacheId() == cctx(set6).cacheId();
assert cctx(set1).groupId() != cctx(set5).groupId();
}
use of org.apache.ignite.IgniteSet 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.IgniteSet in project ignite by apache.
the class IgniteClientReconnectApiExceptionTest method dataStructureOperationsTest.
/**
* @throws Exception If failed.
*/
@SuppressWarnings("unchecked")
private void dataStructureOperationsTest() throws Exception {
final Ignite client = startClientGrid(serverCount());
doTestIgniteOperationOnDisconnect(client, Arrays.asList(// Check atomic long.
new T2<Callable, C1<Object, Boolean>>(new Callable() {
@Override
public Object call() throws Exception {
boolean failed = false;
try {
client.atomicLong("testAtomic", 41, true);
} catch (IgniteClientDisconnectedException e) {
failed = true;
checkAndWait(e);
}
assertTrue(failed);
return client.atomicLong("testAtomic", 41, true);
}
}, new C1<Object, Boolean>() {
@Override
public Boolean apply(Object o) {
assertNotNull(o);
IgniteAtomicLong atomicLong = (IgniteAtomicLong) o;
assertEquals(42, atomicLong.incrementAndGet());
return true;
}
}), // Check set.
new T2<Callable, C1<Object, Boolean>>(new Callable() {
@Override
public Object call() throws Exception {
boolean failed = false;
try {
client.set("testSet", getCollectionConfiguration());
} catch (IgniteClientDisconnectedException e) {
failed = true;
checkAndWait(e);
}
assertTrue(failed);
return client.set("testSet", getCollectionConfiguration());
}
}, new C1<Object, Boolean>() {
@Override
public Boolean apply(Object o) {
assertNotNull(o);
IgniteSet set = (IgniteSet) o;
String val = "testVal";
set.add(val);
assertEquals(1, set.size());
assertTrue(set.contains(val));
return true;
}
}), // Check ignite queue.
new T2<Callable, C1<Object, Boolean>>(new Callable() {
@Override
public Object call() throws Exception {
boolean failed = false;
try {
client.queue("TestQueue", 10, getCollectionConfiguration());
} catch (IgniteClientDisconnectedException e) {
failed = true;
checkAndWait(e);
}
assertTrue(failed);
return client.queue("TestQueue", 10, getCollectionConfiguration());
}
}, new C1<Object, Boolean>() {
@Override
public Boolean apply(Object o) {
assertNotNull(o);
IgniteQueue queue = (IgniteQueue) o;
String val = "Test";
queue.add(val);
assertEquals(val, queue.poll());
return true;
}
})));
}
Aggregations