use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.
the class IgniteCachePeekModesAbstractTest method offheapKeys.
/**
* @param nodeIdx Node index.
* @return Tuple with primary and backup keys.
*/
private T2<List<Integer>, List<Integer>> offheapKeys(int nodeIdx) {
GridCacheAdapter<Integer, String> internalCache = ((IgniteKernal) ignite(nodeIdx)).context().cache().internalCache(DEFAULT_CACHE_NAME);
// TODO GG-11148.
Iterator<Map.Entry<Integer, String>> offheapIt = Collections.EMPTY_MAP.entrySet().iterator();
// if (internalCache.context().isNear())
// offheapIt = internalCache.context().near().dht().context().swap().lazyOffHeapIterator(false);
// else
// offheapIt = internalCache.context().swap().lazyOffHeapIterator(false);
Affinity aff = ignite(nodeIdx).affinity(DEFAULT_CACHE_NAME);
ClusterNode node = ignite(nodeIdx).cluster().localNode();
List<Integer> primary = new ArrayList<>();
List<Integer> backups = new ArrayList<>();
while (offheapIt.hasNext()) {
Map.Entry<Integer, String> e = offheapIt.next();
if (aff.isPrimary(node, e.getKey()))
primary.add(e.getKey());
else {
assertTrue(aff.isBackup(node, e.getKey()));
backups.add(e.getKey());
}
}
return new T2<>(primary, backups);
}
use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.
the class IgniteTxMultiNodeAbstractTest method primaryId.
/**
* @param ignite Grid
* @param key Key.
* @return Primary node id.
*/
@SuppressWarnings("unchecked")
private static UUID primaryId(Ignite ignite, Object key) {
Affinity aff = ignite.affinity(DEFAULT_CACHE_NAME);
Collection<ClusterNode> affNodes = aff.mapPartitionToPrimaryAndBackups(aff.partition(key));
ClusterNode first = F.first(affNodes);
assert first != null;
return first.id();
}
use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.
the class GridCachePartitionedReloadAllAbstractSelfTest method testReloadAll.
/**
* @throws Exception If test failed.
*/
@Test
public void testReloadAll() throws Exception {
// Fill caches with values.
for (IgniteCache<Integer, String> cache : caches) {
Iterable<Integer> keys = primaryKeys(cache, 100);
info("Values [cache=" + caches.indexOf(cache) + ", size=" + F.size(keys.iterator()) + ", keys=" + keys + "]");
for (Integer key : keys) map.put(key, "val" + key);
}
CompletionListenerFuture fut = new CompletionListenerFuture();
caches.get(0).loadAll(map.keySet(), false, fut);
fut.get();
Affinity aff = ignite(0).affinity(DEFAULT_CACHE_NAME);
for (IgniteCache<Integer, String> cache : caches) {
for (Integer key : map.keySet()) {
if (aff.isPrimaryOrBackup(grid(caches.indexOf(cache)).localNode(), key))
assertEquals(map.get(key), cache.localPeek(key));
else
assertNull(cache.localPeek(key));
}
}
}
use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.
the class EvictionPolicyFailureHandlerTest method testFailureHandlerShouldNotCallOnRuntimeException.
/**
*/
@Test
public void testFailureHandlerShouldNotCallOnRuntimeException() throws Exception {
IgniteEx node1 = startGrid(0);
IgniteEx node2 = startGrid(1);
GridCacheAdapter<Object, Object> cache = ((IgniteKernal) node2).internalCache(DEFAULT_CACHE_NAME);
Affinity<Object> affinity = cache.affinity();
for (int i = 0; i < 1000; i++) {
if (affinity.isPrimary(node1.localNode(), i))
cache.put(i, 1);
}
for (int i = 0; i < 1000; i++) {
if (affinity.isPrimary(node1.localNode(), (double) i))
cache.put((double) i, 1);
}
assertFalse(cache.map().entrySet(cache.context().cacheId()).stream().anyMatch(e -> e.key().value(null, false) instanceof Double));
assertFalse(nodeFailure.get());
}
use of org.apache.ignite.cache.affinity.Affinity in project ignite by apache.
the class ClientCacheNodePartitionsRequest method process.
/**
* {@inheritDoc}
*/
@Override
public ClientResponse process(ClientConnectionContext ctx) {
IgniteCache cache = cache(ctx);
GridDiscoveryManager discovery = ctx.kernalContext().discovery();
Collection<ClusterNode> nodes = discovery.discoCache().cacheNodes(cache.getName());
Affinity aff = ctx.kernalContext().affinity().affinityProxy(cache.getName());
ArrayList<ClientConnectableNodePartitions> res = new ArrayList<>();
for (ClusterNode node : nodes) {
Integer port = node.attribute(ClientListenerProcessor.CLIENT_LISTENER_PORT);
if (port == null)
continue;
Collection<String> addrs = node.addresses();
int[] parts = aff.primaryPartitions(node);
res.add(new ClientConnectableNodePartitions(port, addrs, parts));
}
return new ClientCacheNodePartitionsResponse(requestId(), res);
}
Aggregations