use of org.apache.ignite.cache.affinity.AffinityFunction in project ignite by apache.
the class RendezvousAffinityFunctionSimpleBenchmark method testAffinityBenchmarkChangeLast.
/**
*/
public void testAffinityBenchmarkChangeLast() {
mode = TopologyModificationMode.CHANGE_LAST_NODE;
AffinityFunction aff0 = new RendezvousAffinityFunctionOld(true, 1024);
GridTestUtils.setFieldValue(aff0, "ignite", ignite);
affinityBenchmark(aff0, new RendezvousAffinityFunction(true, 1024));
}
use of org.apache.ignite.cache.affinity.AffinityFunction in project ignite by apache.
the class CacheLateAffinityAssignmentTest method calculateAffinity.
/**
* @param topVer Topology version.
* @param filterByRcvd If {@code true} filters caches by 'receivedFrom' property.
* @param cur Optional current affinity.
* @throws Exception If failed.
* @return {@code True} if some primary node changed comparing to given affinity.
*/
private boolean calculateAffinity(long topVer, boolean filterByRcvd, @Nullable Map<String, List<List<ClusterNode>>> cur) throws Exception {
List<Ignite> all = G.allGrids();
IgniteKernal ignite = (IgniteKernal) Collections.min(all, new Comparator<Ignite>() {
@Override
public int compare(Ignite n1, Ignite n2) {
return Long.compare(n1.cluster().localNode().order(), n2.cluster().localNode().order());
}
});
assert all.size() > 0;
Map<Integer, List<List<ClusterNode>>> assignments = idealAff.get(topVer);
if (assignments == null)
idealAff.put(topVer, assignments = new HashMap<>());
GridKernalContext ctx = ignite.context();
GridCacheSharedContext cctx = ctx.cache().context();
AffinityTopologyVersion topVer0 = new AffinityTopologyVersion(topVer);
cctx.discovery().topologyFuture(topVer).get();
List<GridDhtPartitionsExchangeFuture> futs = cctx.exchange().exchangeFutures();
DiscoveryEvent evt = null;
long stopTime = System.currentTimeMillis() + 10_000;
boolean primaryChanged = false;
do {
for (int i = futs.size() - 1; i >= 0; i--) {
GridDhtPartitionsExchangeFuture fut = futs.get(i);
if (fut.initialVersion().equals(topVer0)) {
evt = fut.firstEvent();
break;
}
}
if (evt == null) {
U.sleep(500);
futs = cctx.exchange().exchangeFutures();
} else
break;
} while (System.currentTimeMillis() < stopTime);
assertNotNull("Failed to find exchange future:", evt);
Collection<ClusterNode> allNodes = ctx.discovery().serverNodes(topVer0);
for (DynamicCacheDescriptor cacheDesc : ctx.cache().cacheDescriptors().values()) {
if (assignments.get(cacheDesc.cacheId()) != null)
continue;
if (filterByRcvd && cacheDesc.receivedFrom() != null && ctx.discovery().node(topVer0, cacheDesc.receivedFrom()) == null)
continue;
AffinityFunction func = cacheDesc.cacheConfiguration().getAffinity();
func = cctx.cache().clone(func);
cctx.kernalContext().resource().injectGeneric(func);
List<ClusterNode> affNodes = new ArrayList<>();
IgnitePredicate<ClusterNode> filter = cacheDesc.cacheConfiguration().getNodeFilter();
for (ClusterNode n : allNodes) {
if (!CU.clientNode(n) && (filter == null || filter.apply(n)))
affNodes.add(n);
}
Collections.sort(affNodes, NodeOrderComparator.getInstance());
AffinityFunctionContext affCtx = new GridAffinityFunctionContextImpl(affNodes, previousAssignment(topVer, cacheDesc.cacheId()), evt, topVer0, cacheDesc.cacheConfiguration().getBackups());
List<List<ClusterNode>> assignment = func.assignPartitions(affCtx);
if (cur != null) {
List<List<ClusterNode>> prev = cur.get(cacheDesc.cacheConfiguration().getName());
assertEquals(prev.size(), assignment.size());
if (!primaryChanged) {
for (int p = 0; p < prev.size(); p++) {
List<ClusterNode> nodes0 = prev.get(p);
List<ClusterNode> nodes1 = assignment.get(p);
if (nodes0.size() > 0 && nodes1.size() > 0) {
ClusterNode p0 = nodes0.get(0);
ClusterNode p1 = nodes1.get(0);
if (allNodes.contains(p0) && !p0.equals(p1)) {
primaryChanged = true;
log.info("Primary changed [cache=" + cacheDesc.cacheConfiguration().getName() + ", part=" + p + ", prev=" + F.nodeIds(nodes0) + ", new=" + F.nodeIds(nodes1) + ']');
break;
}
}
}
}
}
assignments.put(cacheDesc.cacheId(), assignment);
}
return primaryChanged;
}
use of org.apache.ignite.cache.affinity.AffinityFunction in project ignite by apache.
the class GridCacheAffinityApiSelfTest method testMapPartitionsToNodeCollection.
/**
* JUnit.
*
* @throws Exception If failed.
*/
public void testMapPartitionsToNodeCollection() throws Exception {
Collection<Integer> parts = new LinkedList<>();
for (int p = 0; p < affinity().partitions(); p++) parts.add(p);
Map<Integer, ClusterNode> map = grid(0).affinity(DEFAULT_CACHE_NAME).mapPartitionsToNodes(parts);
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 GridCachePartitionExchangeManager method clientTopology.
/**
* @param grpId Cache group ID.
* @param discoCache Discovery data cache.
* @return Topology.
*/
public GridDhtPartitionTopology clientTopology(int grpId, DiscoCache discoCache) {
GridClientPartitionTopology top = clientTops.get(grpId);
if (top != null)
return top;
CacheGroupDescriptor grpDesc = cctx.affinity().cacheGroups().get(grpId);
assert grpDesc != null : grpId;
CacheConfiguration<?, ?> ccfg = grpDesc.config();
AffinityFunction aff = ccfg.getAffinity();
Object affKey = cctx.kernalContext().affinity().similaryAffinityKey(aff, ccfg.getNodeFilter(), ccfg.getBackups(), aff.partitions());
GridClientPartitionTopology old = clientTops.putIfAbsent(grpId, top = new GridClientPartitionTopology(cctx, discoCache, grpId, aff.partitions(), affKey));
return old != null ? old : top;
}
use of org.apache.ignite.cache.affinity.AffinityFunction in project ignite by apache.
the class PlatformConfigurationUtils method writeAffinityFunction.
/**
* Writes the affinity functions.
*
* @param out Stream.
* @param f Affinity.
*/
private static void writeAffinityFunction(BinaryRawWriter out, AffinityFunction f) {
if (f instanceof PlatformDotNetAffinityFunction)
f = ((PlatformDotNetAffinityFunction) f).getFunc();
if (f instanceof RendezvousAffinityFunction) {
out.writeByte((byte) 2);
RendezvousAffinityFunction f0 = (RendezvousAffinityFunction) f;
out.writeInt(f0.getPartitions());
out.writeBoolean(f0.isExcludeNeighbors());
// override flags
out.writeByte((byte) 0);
// user func
out.writeObject(null);
} else if (f instanceof PlatformAffinityFunction) {
PlatformAffinityFunction f0 = (PlatformAffinityFunction) f;
AffinityFunction baseFunc = f0.getBaseFunc();
if (baseFunc instanceof RendezvousAffinityFunction) {
out.writeByte((byte) 2);
out.writeInt(f0.partitions());
out.writeBoolean(((RendezvousAffinityFunction) baseFunc).isExcludeNeighbors());
out.writeByte(f0.getOverrideFlags());
out.writeObject(f0.getUserFunc());
} else {
out.writeByte((byte) 3);
out.writeInt(f0.partitions());
// exclude neighbors
out.writeBoolean(false);
out.writeByte(f0.getOverrideFlags());
out.writeObject(f0.getUserFunc());
}
} else
out.writeByte((byte) 0);
}
Aggregations