use of org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl 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.internal.processors.affinity.GridAffinityFunctionContextImpl in project ignite by apache.
the class CacheLateAffinityAssignmentTest method testDelayedAffinityCalculation.
/**
* Checks that new joined primary is not assigned immediately.
*
* @throws Exception If failed.
*/
public void testDelayedAffinityCalculation() throws Exception {
Ignite ignite0 = startServer(0, 1);
checkAffinity(1, topVer(1, 0), true);
GridCacheContext cctx = ((IgniteKernal) ignite0).context().cache().internalCache(CACHE_NAME1).context();
AffinityFunction func = cctx.config().getAffinity();
AffinityFunctionContext ctx = new GridAffinityFunctionContextImpl(new ArrayList<>(ignite0.cluster().nodes()), null, null, topVer(1, 0), cctx.config().getBackups());
List<List<ClusterNode>> calcAff1_0 = func.assignPartitions(ctx);
startServer(1, 2);
ctx = new GridAffinityFunctionContextImpl(new ArrayList<>(ignite0.cluster().nodes()), calcAff1_0, null, topVer(1, 0), cctx.config().getBackups());
List<List<ClusterNode>> calcAff2_0 = func.assignPartitions(ctx);
checkAffinity(2, topVer(2, 0), false);
List<List<ClusterNode>> aff2_0 = affinity(ignite0, topVer(2, 0), CACHE_NAME1);
for (int p = 0; p < calcAff1_0.size(); p++) {
List<ClusterNode> a1 = calcAff1_0.get(p);
List<ClusterNode> a2 = calcAff2_0.get(p);
List<ClusterNode> a = aff2_0.get(p);
// Primary did not change.
assertEquals(a1.get(0), a.get(0));
// New primary is backup.
if (!a1.get(0).equals(a2.get(0)))
assertTrue(a.contains(a2.get(0)));
}
checkAffinity(2, topVer(2, 1), true);
List<List<ClusterNode>> aff2_1 = affinity(ignite0, topVer(2, 1), CACHE_NAME1);
assertEquals(calcAff2_0, aff2_1);
}
use of org.apache.ignite.internal.processors.affinity.GridAffinityFunctionContextImpl 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.internal.processors.affinity.GridAffinityFunctionContextImpl 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.internal.processors.affinity.GridAffinityFunctionContextImpl in project ignite by apache.
the class GridCacheAffinityApiSelfTest method testPrimaryPartitionsOneNode.
/**
* JUnit.
*
* @throws Exception If failed.
*/
public void testPrimaryPartitionsOneNode() throws Exception {
AffinityFunctionContext ctx = new GridAffinityFunctionContextImpl(new ArrayList<>(grid(0).cluster().nodes()), null, null, new AffinityTopologyVersion(1), 1);
List<List<ClusterNode>> assignment = affinity().assignPartitions(ctx);
for (ClusterNode node : grid(0).cluster().nodes()) {
int[] parts = grid(0).affinity(DEFAULT_CACHE_NAME).primaryPartitions(node);
assert !F.isEmpty(parts);
for (int p : parts) {
Collection<ClusterNode> owners = nodes(assignment, p);
assert !F.isEmpty(owners);
ClusterNode primary = F.first(owners);
assert F.eqNodes(node, primary);
}
}
}
Aggregations