use of org.apache.ignite.cache.affinity.AffinityFunctionContext in project ignite by apache.
the class GridCacheAffinityApiSelfTest method testMapPartitionsToNode.
/**
* JUnit.
*
* @throws Exception If failed.
*/
public void testMapPartitionsToNode() 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.AffinityFunctionContext in project ignite by apache.
the class PlatformAffinityFunctionTarget method processOutStream.
/**
* {@inheritDoc}
*/
@Override
public void processOutStream(int type, BinaryRawWriterEx writer) throws IgniteCheckedException {
if (type == OP_ASSIGN_PARTITIONS) {
AffinityFunctionContext affCtx = currentAffCtx.get();
if (affCtx == null)
throw new IgniteException("Thread-local AffinityFunctionContext is null. " + "This may indicate an unsupported call to the base AffinityFunction.");
final List<List<ClusterNode>> partitions = baseFunc.assignPartitions(affCtx);
PlatformAffinityUtils.writePartitionAssignment(partitions, writer, platformContext());
return;
}
super.processOutStream(type, writer);
}
use of org.apache.ignite.cache.affinity.AffinityFunctionContext 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.AffinityFunctionContext in project ignite by apache.
the class GridCachePartitionedPreloadEventsSelfTest method cacheConfiguration.
/** {@inheritDoc} */
@Override
protected CacheConfiguration cacheConfiguration() {
CacheConfiguration cacheCfg = super.cacheConfiguration();
if (replicatedAffinity)
// replicate entries to all nodes
cacheCfg.setAffinity(notSerializableProxy(new AffinityFunction() {
/** {@inheritDoc} */
@Override
public void reset() {
}
/** {@inheritDoc} */
@Override
public int partitions() {
return 1;
}
/** {@inheritDoc} */
@Override
public int partition(Object key) {
return 0;
}
/** {@inheritDoc} */
@Override
public List<List<ClusterNode>> assignPartitions(AffinityFunctionContext affCtx) {
List<ClusterNode> nodes = new ArrayList<>(affCtx.currentTopologySnapshot());
return Collections.singletonList(nodes);
}
/** {@inheritDoc} */
@Override
public void removeNode(UUID nodeId) {
}
}, AffinityFunction.class));
cacheCfg.setRebalanceDelay(rebalanceDelay);
return cacheCfg;
}
use of org.apache.ignite.cache.affinity.AffinityFunctionContext in project ignite by apache.
the class GridCacheAffinityApiSelfTest method testAllPartitions.
/**
* JUnit.
*
* @throws Exception If failed.
*/
public void testAllPartitions() throws Exception {
// Pick 2 nodes and create a projection over them.
ClusterNode n0 = grid(0).localNode();
int[] parts = grid(0).affinity(DEFAULT_CACHE_NAME).allPartitions(n0);
assert !F.isEmpty(parts);
AffinityFunctionContext ctx = new GridAffinityFunctionContextImpl(new ArrayList<>(grid(0).cluster().nodes()), null, null, new AffinityTopologyVersion(1), 1);
List<List<ClusterNode>> assignment = affinity().assignPartitions(ctx);
for (int p : parts) {
Collection<ClusterNode> owners = nodes(assignment, p);
assert !F.isEmpty(owners);
assert owners.contains(n0);
}
}
Aggregations