use of org.apache.ignite.cache.affinity.AffinityFunction in project ignite by apache.
the class GridCacheAffinityApiSelfTest method testMapPartitionsToNodeArray.
/**
* JUnit.
*
* @throws Exception If failed.
*/
public void testMapPartitionsToNodeArray() throws Exception {
Map<Integer, ClusterNode> map = grid(0).affinity(DEFAULT_CACHE_NAME).mapPartitionsToNodes(F.asList(0, 1, 5, 19, 12));
AffinityFunctionContext ctx = new GridAffinityFunctionContextImpl(new ArrayList<>(grid(0).cluster().nodes()), null, null, new AffinityTopologyVersion(1), 1);
AffinityFunction aff = affinity();
List<List<ClusterNode>> assignment = aff.assignPartitions(ctx);
for (Map.Entry<Integer, ClusterNode> e : map.entrySet()) assert F.eqNodes(F.first(nodes(assignment, aff, e.getKey())), e.getValue());
}
use of org.apache.ignite.cache.affinity.AffinityFunction in project ignite by apache.
the class GridCacheAffinityApiSelfTest method testMapPartitionToNode.
/**
* JUnit.
*
* @throws Exception If failed.
*/
public void testMapPartitionToNode() throws Exception {
int part = RND.nextInt(affinity().partitions());
AffinityFunctionContext ctx = new GridAffinityFunctionContextImpl(new ArrayList<>(grid(0).cluster().nodes()), null, null, new AffinityTopologyVersion(1), 1);
AffinityFunction aff = affinity();
List<List<ClusterNode>> assignment = aff.assignPartitions(ctx);
assertEquals(F.first(nodes(assignment, aff, part)), grid(0).affinity(DEFAULT_CACHE_NAME).mapPartitionToNode(part));
}
use of org.apache.ignite.cache.affinity.AffinityFunction in project ignite by apache.
the class IgniteCacheClientNodeChangingTopologyTest method findKeys.
/**
* Tries to find keys for two partitions: for one partition assignment should not change after node join,
* for another primary node should change.
*
* @param ignite Ignite.
* @param nodes Current nodes.
* @return Found keys.
*/
private IgniteBiTuple<Integer, Integer> findKeys(Ignite ignite, ClusterNode... nodes) {
ClusterNode newNode = new TcpDiscoveryNode();
GridTestUtils.setFieldValue(newNode, "consistentId", getTestIgniteInstanceName(4));
GridTestUtils.setFieldValue(newNode, "id", UUID.randomUUID());
List<ClusterNode> topNodes = new ArrayList<>();
Collections.addAll(topNodes, nodes);
topNodes.add(newNode);
DiscoveryEvent discoEvt = new DiscoveryEvent(newNode, "", EventType.EVT_NODE_JOINED, newNode);
final long topVer = ignite.cluster().topologyVersion();
GridAffinityFunctionContextImpl ctx = new GridAffinityFunctionContextImpl(topNodes, null, discoEvt, new AffinityTopologyVersion(topVer + 1), 1);
AffinityFunction affFunc = ignite.cache(DEFAULT_CACHE_NAME).getConfiguration(CacheConfiguration.class).getAffinity();
List<List<ClusterNode>> newAff = affFunc.assignPartitions(ctx);
List<List<ClusterNode>> curAff = ((IgniteKernal) ignite).context().cache().internalCache(DEFAULT_CACHE_NAME).context().affinity().assignments(new AffinityTopologyVersion(topVer));
Integer key1 = null;
Integer key2 = null;
Affinity<Integer> aff = ignite.affinity(DEFAULT_CACHE_NAME);
for (int i = 0; i < curAff.size(); i++) {
if (key1 == null) {
List<ClusterNode> oldNodes = curAff.get(i);
List<ClusterNode> newNodes = newAff.get(i);
if (oldNodes.equals(newNodes))
key1 = findKey(aff, i);
}
if (key2 == null) {
ClusterNode oldPrimary = F.first(curAff.get(i));
ClusterNode newPrimary = F.first(newAff.get(i));
if (!oldPrimary.equals(newPrimary))
key2 = findKey(aff, i);
}
if (key1 != null && key2 != null)
break;
}
if (key1 == null || key2 == null)
fail("Failed to find nodes required for test.");
return new IgniteBiTuple<>(key1, key2);
}
use of org.apache.ignite.cache.affinity.AffinityFunction in project ignite by apache.
the class GridCommonAbstractTest method movingKeysAfterJoin.
/**
* Return list of keys that are primary for given node on current topology,
* but primary node will change after new node will be added.
*
* @param ign Ignite.
* @param cacheName Cache name.
* @param size Number of keys.
* @return List of keys.
*/
protected final List<Integer> movingKeysAfterJoin(Ignite ign, String cacheName, int size) {
assertEquals("Expected consistentId is set to node name", ign.name(), ign.cluster().localNode().consistentId());
GridCacheContext<Object, Object> cctx = ((IgniteKernal) ign).context().cache().internalCache(cacheName).context();
ArrayList<ClusterNode> nodes = new ArrayList<>(ign.cluster().nodes());
AffinityFunction func = cctx.config().getAffinity();
AffinityFunctionContext ctx = new GridAffinityFunctionContextImpl(nodes, null, null, AffinityTopologyVersion.NONE, cctx.config().getBackups());
List<List<ClusterNode>> calcAff = func.assignPartitions(ctx);
GridTestNode fakeNode = new GridTestNode(UUID.randomUUID(), null);
fakeNode.consistentId(getTestIgniteInstanceName(nodes.size()));
nodes.add(fakeNode);
ctx = new GridAffinityFunctionContextImpl(nodes, null, null, AffinityTopologyVersion.NONE, cctx.config().getBackups());
List<List<ClusterNode>> calcAff2 = func.assignPartitions(ctx);
Set<Integer> movedParts = new HashSet<>();
UUID locId = ign.cluster().localNode().id();
for (int i = 0; i < calcAff.size(); i++) {
if (calcAff.get(i).get(0).id().equals(locId) && !calcAff2.get(i).get(0).id().equals(locId))
movedParts.add(i);
}
List<Integer> keys = new ArrayList<>();
Affinity<Integer> aff = ign.affinity(cacheName);
for (int i = 0; i < 10_000; i++) {
int keyPart = aff.partition(i);
if (movedParts.contains(keyPart)) {
keys.add(i);
if (keys.size() == size)
break;
}
}
assertEquals("Failed to find moving keys [movedPats=" + movedParts + ", keys=" + keys + ']', size, keys.size());
return keys;
}
use of org.apache.ignite.cache.affinity.AffinityFunction in project ignite by apache.
the class GridOffHeapPartitionedMapAbstractSelfTest method testPutRandomKeys.
/**
* @throws Exception If failed.
*/
@SuppressWarnings("unchecked")
public void testPutRandomKeys() throws Exception {
map = newMap();
AffinityFunction aff = new RendezvousAffinityFunction(parts, null);
getTestResources().inject(aff);
GridByteArrayWrapper[] keys = new GridByteArrayWrapper[512];
Random rnd = new Random();
for (int i = 0; i < keys.length; i++) {
byte[] key = new byte[rnd.nextInt(64) + 1];
rnd.nextBytes(key);
keys[i] = new GridByteArrayWrapper(key);
}
for (int i = 0; i < 10000; i++) {
int idx = rnd.nextInt(keys.length);
GridByteArrayWrapper key = keys[idx];
map.put(aff.partition(key), key.hashCode(), key.array(), new byte[64]);
}
}
Aggregations