use of org.apache.ignite.configuration.CollectionConfiguration in project ignite by apache.
the class GridCacheSetAbstractSelfTest 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.
*/
public void testIsolation() throws Exception {
CollectionConfiguration colCfg = collectionConfiguration();
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 {
IgniteSet<Integer> set0 = ignite.set(SET_NAME, colCfg);
assertNotNull(set0);
try (Transaction tx = ignite.transactions().txStart()) {
cache.put(1, 1);
Collection<Integer> items = new ArrayList<>(100);
for (int i = 0; i < 100; i++) items.add(i);
set0.addAll(items);
tx.rollback();
}
assertEquals(0, cache.size());
assertEquals(100, set0.size());
set0.close();
} finally {
ignite.destroyCache(cfg.getName());
}
}
use of org.apache.ignite.configuration.CollectionConfiguration in project ignite by apache.
the class GridCacheSetAbstractSelfTest method testAffinityCall.
/**
* @throws Exception If failed.
*/
public void testAffinityCall() throws Exception {
final CollectionConfiguration colCfg = collectionConfiguration();
colCfg.setCollocated(false);
colCfg.setCacheMode(CacheMode.PARTITIONED);
try (final IgniteSet<Integer> set1 = grid(0).set("Set1", colCfg)) {
GridTestUtils.assertThrows(log, new Callable<Void>() {
@Override
public Void call() throws Exception {
set1.affinityCall(new IgniteCallable<Object>() {
@Override
public Object call() {
return null;
}
});
return null;
}
}, IgniteException.class, "Failed to execute affinityCall() for non-collocated set: " + set1.name() + ". This operation is supported only for collocated sets.");
}
colCfg.setCollocated(true);
try (final IgniteSet<Integer> set2 = grid(0).set("Set2", colCfg)) {
set2.add(100);
Integer res = set2.affinityCall(new IgniteCallable<Integer>() {
@IgniteInstanceResource
private IgniteEx ignite;
@Override
public Integer call() {
assertTrue(ignite.cachex("datastructures_0").affinity().isPrimaryOrBackup(ignite.cluster().localNode(), "Set2"));
return set2.iterator().next();
}
});
assertEquals(100, res.intValue());
}
}
use of org.apache.ignite.configuration.CollectionConfiguration in project ignite by apache.
the class GridCacheSetAbstractSelfTest method testMultithreaded.
/**
* @param collocated Collocation flag.
* @throws Exception If failed.
*/
private void testMultithreaded(final boolean collocated) throws Exception {
CollectionConfiguration colCfg = config(collocated);
Set<Integer> set0 = grid(0).set(SET_NAME, colCfg);
assertNotNull(set0);
Collection<IgniteInternalFuture> futs = new ArrayList<>();
final int THREADS_PER_NODE = 5;
final int KEY_RANGE = 10_000;
final int ITERATIONS = 3000;
for (int i = 0; i < gridCount(); i++) {
final int idx = i;
futs.add(GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {
@Override
public Void call() throws Exception {
IgniteSet<Integer> set = grid(idx).set(SET_NAME, null);
assertNotNull(set);
ThreadLocalRandom rnd = ThreadLocalRandom.current();
for (int i = 0; i < ITERATIONS; i++) {
switch(rnd.nextInt(4)) {
case 0:
set.add(rnd.nextInt(KEY_RANGE));
break;
case 1:
set.remove(rnd.nextInt(KEY_RANGE));
break;
case 2:
set.contains(rnd.nextInt(KEY_RANGE));
break;
case 3:
for (Integer val : set) assertNotNull(val);
break;
default:
fail();
}
if ((i + 1) % 500 == 0)
log.info("Executed iterations: " + (i + 1));
}
return null;
}
}, THREADS_PER_NODE, "testSetMultithreaded"));
}
for (IgniteInternalFuture fut : futs) fut.get();
}
use of org.apache.ignite.configuration.CollectionConfiguration in project ignite by apache.
the class GridCacheSetAbstractSelfTest method testApi.
/**
* @param collocated Collocation flag.
* @throws Exception If failed.
*/
private void testApi(boolean collocated) throws Exception {
CollectionConfiguration colCfg = config(collocated);
assertNotNull(grid(0).set(SET_NAME, colCfg));
for (int i = 0; i < gridCount(); i++) {
Set<Integer> set = grid(i).set(SET_NAME, null);
assertNotNull(set);
assertFalse(set.contains(1));
assertEquals(0, set.size());
assertTrue(set.isEmpty());
}
// Add, isEmpty.
assertTrue(grid(0).set(SET_NAME, null).add(1));
for (int i = 0; i < gridCount(); i++) {
Set<Integer> set = grid(i).set(SET_NAME, null);
assertEquals(1, set.size());
assertFalse(set.isEmpty());
assertTrue(set.contains(1));
assertFalse(set.add(1));
assertFalse(set.contains(100));
}
// Remove.
assertTrue(grid(0).set(SET_NAME, null).remove(1));
for (int i = 0; i < gridCount(); i++) {
Set<Integer> set = grid(i).set(SET_NAME, null);
assertEquals(0, set.size());
assertTrue(set.isEmpty());
assertFalse(set.contains(1));
assertFalse(set.remove(1));
}
// Contains all.
Collection<Integer> col1 = new ArrayList<>();
Collection<Integer> col2 = new ArrayList<>();
final int ITEMS = 100;
for (int i = 0; i < ITEMS; i++) {
assertTrue(grid(i % gridCount()).set(SET_NAME, null).add(i));
col1.add(i);
col2.add(i);
}
col2.add(ITEMS);
for (int i = 0; i < gridCount(); i++) {
Set<Integer> set = grid(i).set(SET_NAME, null);
assertEquals(ITEMS, set.size());
assertTrue(set.containsAll(col1));
assertFalse(set.containsAll(col2));
}
for (int i = 0; i < gridCount(); i++) {
Set<Integer> set = grid(i).set(SET_NAME, null);
assertArrayContent(set.toArray(), ITEMS);
assertArrayContent(set.toArray(new Integer[ITEMS]), ITEMS);
}
// Remove all.
Collection<Integer> rmvCol = new ArrayList<>();
for (int i = ITEMS - 10; i < ITEMS; i++) rmvCol.add(i);
assertTrue(grid(0).set(SET_NAME, null).removeAll(rmvCol));
for (int i = 0; i < gridCount(); i++) {
Set<Integer> set = grid(i).set(SET_NAME, null);
assertFalse(set.removeAll(rmvCol));
for (Integer val : rmvCol) assertFalse(set.contains(val));
assertArrayContent(set.toArray(), ITEMS - 10);
assertArrayContent(set.toArray(new Integer[ITEMS - 10]), ITEMS - 10);
}
// Add all.
assertTrue(grid(0).set(SET_NAME, null).addAll(rmvCol));
for (int i = 0; i < gridCount(); i++) {
Set<Integer> set = grid(i).set(SET_NAME, null);
assertEquals(ITEMS, set.size());
assertFalse(set.addAll(rmvCol));
for (Integer val : rmvCol) assertTrue(set.contains(val));
}
// Retain all.
assertTrue(grid(0).set(SET_NAME, null).retainAll(rmvCol));
for (int i = 0; i < gridCount(); i++) {
Set<Integer> set = grid(i).set(SET_NAME, null);
assertEquals(rmvCol.size(), set.size());
assertFalse(set.retainAll(rmvCol));
for (int val = 0; val < 10; val++) assertFalse(set.contains(val));
for (int val : rmvCol) assertTrue(set.contains(val));
}
// Clear.
grid(0).set(SET_NAME, null).clear();
for (int i = 0; i < gridCount(); i++) {
Set<Integer> set = grid(i).set(SET_NAME, null);
assertEquals(0, set.size());
assertTrue(set.isEmpty());
assertFalse(set.contains(0));
}
}
use of org.apache.ignite.configuration.CollectionConfiguration in project ignite by apache.
the class GridCacheQueueApiSelfAbstractTest method testNotReuseCache.
/**
* @throws Exception If failed.
*/
public void testNotReuseCache() throws Exception {
CollectionConfiguration colCfg1 = collectionConfiguration();
CollectionConfiguration colCfg2 = collectionConfiguration();
if (colCfg2.getAtomicityMode() == ATOMIC)
colCfg2.setAtomicityMode(TRANSACTIONAL);
else
colCfg2.setAtomicityMode(ATOMIC);
IgniteQueue queue1 = grid(0).queue("Queue1", 0, colCfg1);
IgniteQueue queue2 = grid(0).queue("Queue2", 0, colCfg2);
assertNotSame(getQueueCache(queue1), getQueueCache(queue2));
}
Aggregations