use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.
the class IgniteCacheGroupsTest method testCacheIdSort.
/**
* @throws Exception If failed.
*/
@Test
public void testCacheIdSort() throws Exception {
Ignite node = startGrid(0);
final List<IgniteCache> caches = new ArrayList<>(3);
caches.add(node.createCache(cacheConfiguration(GROUP1, "c1", PARTITIONED, ATOMIC, 1, false).setAffinity(new RendezvousAffinityFunction(false, 8))));
caches.add(node.createCache(cacheConfiguration(GROUP1, "c2", PARTITIONED, ATOMIC, 1, false).setAffinity(new RendezvousAffinityFunction(false, 8))));
caches.add(node.createCache(cacheConfiguration(GROUP1, "c3", PARTITIONED, ATOMIC, 1, false).setAffinity(new RendezvousAffinityFunction(false, 8))));
Affinity aff = node.affinity("c1");
final List<Integer> keys = new ArrayList<>();
for (int i = 0; i < 1_000_000; i++) {
if (aff.partition(i) == 0) {
keys.add(i);
if (keys.size() >= 10_000)
break;
}
}
assertEquals(10_000, keys.size());
final long stopTime = System.currentTimeMillis() + 10_000;
GridTestUtils.runMultiThreaded(new Callable<Void>() {
@Override
public Void call() throws Exception {
ThreadLocalRandom rnd = ThreadLocalRandom.current();
while (System.currentTimeMillis() < stopTime) {
for (int i = 0; i < 100; i++) {
IgniteCache cache = caches.get(rnd.nextInt(3));
Integer key = keys.get(rnd.nextInt(10_000));
if (rnd.nextFloat() > 0.8f)
cache.remove(key);
else
cache.put(key, key);
}
}
return null;
}
}, 5, "update-thread");
CacheGroupContext grp = cacheGroup(node, GROUP1);
Integer cacheId = null;
GridIterator<CacheDataRow> it = grp.offheap().partitionIterator(0);
int c = 0;
while (it.hasNext()) {
CacheDataRow row = it.next();
if (cacheId == null || cacheId != row.cacheId()) {
cacheId = row.cacheId();
c++;
}
}
assertEquals(3, c);
}
use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.
the class IgniteCachePeekModesAbstractTest method swapKeys.
/**
* @param nodeIdx Node index.
* @return Tuple with primary and backup keys.
*/
private T2<List<Integer>, List<Integer>> swapKeys(int nodeIdx) {
// TODO: GG-11148.
// SwapSpaceSpi swap = ignite(nodeIdx).configuration().getSwapSpaceSpi();
//
// IgniteSpiCloseableIterator<KeyCacheObject> it = swap.keyIterator(SPACE_NAME, null);
IgniteSpiCloseableIterator<KeyCacheObject> it = new GridEmptyCloseableIterator<>();
assertNotNull(it);
Affinity aff = ignite(nodeIdx).affinity(DEFAULT_CACHE_NAME);
ClusterNode node = ignite(nodeIdx).cluster().localNode();
List<Integer> primary = new ArrayList<>();
List<Integer> backups = new ArrayList<>();
CacheObjectContext coctx = ((IgniteEx) ignite(nodeIdx)).context().cache().internalCache(DEFAULT_CACHE_NAME).context().cacheObjectContext();
while (it.hasNext()) {
Integer key = it.next().value(coctx, false);
if (aff.isPrimary(node, key))
primary.add(key);
else {
assertTrue(aff.isBackup(node, key));
backups.add(key);
}
}
return new T2<>(primary, backups);
}
use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.
the class GridCacheInterceptorAbstractSelfTest method nearKey.
/**
* @param idx Grid index.
* @return Key which does not belong to the grid.
*/
private String nearKey(int idx) {
Affinity aff = ignite(0).affinity(DEFAULT_CACHE_NAME);
String key = null;
for (int i = 0; i < 10_000; i++) {
if (!aff.isPrimaryOrBackup(grid(idx).localNode(), String.valueOf(i))) {
key = String.valueOf(i);
break;
}
}
assertNotNull(key);
return key;
}
use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.
the class GridCacheInterceptorAbstractSelfTest method backupKey.
/**
* @param idx Grid index.
* @return Primary key for grid.
*/
private String backupKey(int idx) {
Affinity aff = ignite(0).affinity(DEFAULT_CACHE_NAME);
String key = null;
for (int i = 0; i < 10_000; i++) {
if (aff.isBackup(grid(idx).localNode(), String.valueOf(i))) {
key = String.valueOf(i);
break;
}
}
assertNotNull(key);
return key;
}
use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.
the class CacheMvccSqlTxQueriesAbstractTest method testUpdateExplicitPartitionsWithReducer.
/**
* @throws Exception If failed.
*/
@Test
public void testUpdateExplicitPartitionsWithReducer() throws Exception {
ccfg = cacheConfiguration(cacheMode(), FULL_SYNC, 2, 10).setIndexedTypes(Integer.class, Integer.class);
Ignite ignite = startGridsMultiThreaded(4);
awaitPartitionMapExchange();
IgniteCache<Object, Object> cache = ignite.cache(DEFAULT_CACHE_NAME);
Affinity<Object> affinity = internalCache0(cache).affinity();
int keysCnt = 10, retryCnt = 0;
Integer test = 0;
Map<Integer, Integer> vals = new LinkedHashMap<>();
while (vals.size() < keysCnt) {
int partition = affinity.partition(test);
if (partition == 1 || partition == 2)
vals.put(test, 0);
else
assertTrue("Maximum retry number exceeded", ++retryCnt < 1000);
test++;
}
cache.putAll(vals);
SqlFieldsQuery qry = new SqlFieldsQuery("UPDATE Integer set _val=(SELECT 2 FROM DUAL)").setPartitions(1, 2);
List<List<?>> all = cache.query(qry).getAll();
assertEquals(Long.valueOf(keysCnt), all.stream().findFirst().orElseThrow(AssertionError::new).get(0));
List<List<?>> rows = cache.query(new SqlFieldsQuery("SELECT _val FROM Integer")).getAll();
assertEquals(keysCnt, rows.size());
assertTrue(rows.stream().map(r -> r.get(0)).map(Integer.class::cast).allMatch(v -> v == 2));
}
Aggregations