use of org.apache.ignite.cache.affinity.AffinityFunction in project ignite by apache.
the class GridCachePartitionExchangeManager method clientTopology.
/**
* @param cacheId Cache ID.
* @param exchFut Exchange future.
* @return Topology.
*/
public GridDhtPartitionTopology clientTopology(int cacheId, GridDhtPartitionsExchangeFuture exchFut) {
GridClientPartitionTopology top = clientTops.get(cacheId);
if (top != null)
return top;
Object affKey = null;
DynamicCacheDescriptor desc = cctx.cache().cacheDescriptor(cacheId);
if (desc != null) {
CacheConfiguration ccfg = desc.cacheConfiguration();
AffinityFunction aff = ccfg.getAffinity();
affKey = cctx.kernalContext().affinity().similaryAffinityKey(aff, ccfg.getNodeFilter(), ccfg.getBackups(), aff.partitions());
}
GridClientPartitionTopology old = clientTops.putIfAbsent(cacheId, top = new GridClientPartitionTopology(cctx, cacheId, exchFut, affKey));
return old != null ? old : top;
}
use of org.apache.ignite.cache.affinity.AffinityFunction in project ignite by apache.
the class GridAffinityProcessor method affinityInfoFromNode.
/**
* Requests {@link AffinityFunction} and {@link AffinityKeyMapper} from remote node.
*
* @param cacheName Name of cache on which affinity is requested.
* @param topVer Topology version.
* @param n Node from which affinity is requested.
* @return Affinity cached function.
* @throws IgniteCheckedException If either local or remote node cannot get deployment for affinity objects.
*/
private AffinityInfo affinityInfoFromNode(String cacheName, AffinityTopologyVersion topVer, ClusterNode n) throws IgniteCheckedException {
GridTuple3<GridAffinityMessage, GridAffinityMessage, GridAffinityAssignment> t = ctx.closure().callAsyncNoFailover(BROADCAST, affinityJob(cacheName, topVer), F.asList(n), true, /*system pool*/
0, false).get();
AffinityFunction f = (AffinityFunction) unmarshall(ctx, n.id(), t.get1());
AffinityKeyMapper m = (AffinityKeyMapper) unmarshall(ctx, n.id(), t.get2());
assert m != null;
// Bring to initial state.
f.reset();
m.reset();
CacheConfiguration ccfg = ctx.cache().cacheConfiguration(cacheName);
return new AffinityInfo(f, m, t.get3(), ctx.cacheObjects().contextForCache(ccfg));
}
use of org.apache.ignite.cache.affinity.AffinityFunction in project ignite by apache.
the class RendezvousAffinityFunctionSelfTest method affinityFunction.
/**
* {@inheritDoc}
*/
@Override
protected AffinityFunction affinityFunction() {
AffinityFunction aff = new RendezvousAffinityFunction();
GridTestUtils.setFieldValue(aff, "ignite", ignite);
return aff;
}
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 GridCommonAbstractTest method awaitPartitionMapExchange.
/**
* @param waitEvicts If {@code true} will wait for evictions finished.
* @param waitNode2PartUpdate If {@code true} will wait for nodes node2part info update finished.
* @param nodes Optional nodes. If {@code null} method will wait for all nodes, for non null collection nodes will
* be filtered
* @param printPartState If {@code true} will print partition state if evictions not happened.
* @throws InterruptedException If interrupted.
*/
@SuppressWarnings("BusyWait")
protected void awaitPartitionMapExchange(boolean waitEvicts, boolean waitNode2PartUpdate, @Nullable Collection<ClusterNode> nodes, boolean printPartState) throws InterruptedException {
long timeout = getPartitionMapExchangeTimeout();
long startTime = -1;
Set<String> names = new HashSet<>();
Ignite crd = null;
for (Ignite g : G.allGrids()) {
ClusterNode node = g.cluster().localNode();
if (crd == null || node.order() < crd.cluster().localNode().order()) {
crd = g;
if (node.order() == 1)
break;
}
}
if (crd == null)
return;
AffinityTopologyVersion waitTopVer = ((IgniteKernal) crd).context().discovery().topologyVersionEx();
if (waitTopVer.topologyVersion() <= 0)
waitTopVer = new AffinityTopologyVersion(1, 0);
for (Ignite g : G.allGrids()) {
if (nodes != null && !nodes.contains(g.cluster().localNode()))
continue;
IgniteKernal g0 = (IgniteKernal) g;
names.add(g0.configuration().getIgniteInstanceName());
if (startTime != -1) {
if (startTime != g0.context().discovery().gridStartTime())
fail("Found nodes from different clusters, probable some test does not stop nodes " + "[allNodes=" + names + ']');
} else
startTime = g0.context().discovery().gridStartTime();
if (g.cluster().localNode().isDaemon())
continue;
IgniteInternalFuture<?> exchFut = g0.context().cache().context().exchange().affinityReadyFuture(waitTopVer);
if (exchFut != null && !exchFut.isDone()) {
try {
exchFut.get(timeout);
} catch (IgniteCheckedException e) {
log.error("Failed to wait for exchange [topVer=" + waitTopVer + ", node=" + g0.name() + ']', e);
}
}
for (IgniteCacheProxy<?, ?> c : g0.context().cache().jcaches()) {
CacheConfiguration cfg = c.context().config();
if (cfg == null)
continue;
if (cfg.getCacheMode() != LOCAL && cfg.getRebalanceMode() != NONE && g.cluster().nodes().size() > 1) {
AffinityFunction aff = cfg.getAffinity();
GridDhtCacheAdapter<?, ?> dht = dht(c);
GridDhtPartitionTopology top = dht.topology();
for (int p = 0; p < aff.partitions(); p++) {
long start = 0;
for (int i = 0; ; i++) {
boolean match = false;
GridCachePartitionExchangeManager<?, ?> exchMgr = dht.context().shared().exchange();
AffinityTopologyVersion readyVer = exchMgr.readyAffinityVersion();
// Otherwise, there may be an assertion when printing top.readyTopologyVersion().
try {
IgniteInternalFuture<?> fut = exchMgr.affinityReadyFuture(readyVer);
if (fut != null)
fut.get();
} catch (IgniteCheckedException e) {
throw new IgniteException(e);
}
if (readyVer.topologyVersion() > 0 && c.context().started()) {
// Must map on updated version of topology.
Collection<ClusterNode> affNodes = dht.context().affinity().assignment(readyVer).idealAssignment().get(p);
int affNodesCnt = affNodes.size();
GridDhtTopologyFuture topFut = top.topologyVersionFuture();
Collection<ClusterNode> owners = (topFut != null && topFut.isDone()) ? top.owners(p, AffinityTopologyVersion.NONE) : Collections.<ClusterNode>emptyList();
int ownerNodesCnt = owners.size();
GridDhtLocalPartition loc = top.localPartition(p, readyVer, false);
if (affNodesCnt != ownerNodesCnt || !affNodes.containsAll(owners) || (waitEvicts && loc != null && loc.state() != GridDhtPartitionState.OWNING)) {
LT.warn(log(), "Waiting for topology map update [" + "igniteInstanceName=" + g.name() + ", cache=" + cfg.getName() + ", cacheId=" + dht.context().cacheId() + ", topVer=" + top.readyTopologyVersion() + ", p=" + p + ", affNodesCnt=" + affNodesCnt + ", ownersCnt=" + ownerNodesCnt + ", affNodes=" + F.nodeIds(affNodes) + ", owners=" + F.nodeIds(owners) + ", topFut=" + topFut + ", locNode=" + g.cluster().localNode() + ']');
} else
match = true;
} else {
LT.warn(log(), "Waiting for topology map update [" + "igniteInstanceName=" + g.name() + ", cache=" + cfg.getName() + ", cacheId=" + dht.context().cacheId() + ", topVer=" + top.readyTopologyVersion() + ", started=" + dht.context().started() + ", p=" + p + ", readVer=" + readyVer + ", locNode=" + g.cluster().localNode() + ']');
}
if (!match) {
if (i == 0)
start = System.currentTimeMillis();
if (System.currentTimeMillis() - start > timeout) {
U.dumpThreads(log);
if (printPartState)
printPartitionState(c);
throw new IgniteException("Timeout of waiting for topology map update [" + "igniteInstanceName=" + g.name() + ", cache=" + cfg.getName() + ", cacheId=" + dht.context().cacheId() + ", topVer=" + top.readyTopologyVersion() + ", p=" + p + ", readVer=" + readyVer + ", locNode=" + g.cluster().localNode() + ']');
}
// Busy wait.
Thread.sleep(20);
continue;
}
if (i > 0)
log().warning("Finished waiting for topology map update [igniteInstanceName=" + g.name() + ", p=" + p + ", duration=" + (System.currentTimeMillis() - start) + "ms]");
break;
}
}
if (waitNode2PartUpdate) {
long start = System.currentTimeMillis();
boolean failed = true;
while (failed) {
failed = false;
for (GridDhtPartitionMap pMap : top.partitionMap(true).values()) {
if (failed)
break;
for (Map.Entry entry : pMap.entrySet()) {
if (System.currentTimeMillis() - start > timeout) {
U.dumpThreads(log);
throw new IgniteException("Timeout of waiting for partition state update [" + "igniteInstanceName=" + g.name() + ", cache=" + cfg.getName() + ", cacheId=" + dht.context().cacheId() + ", topVer=" + top.readyTopologyVersion() + ", locNode=" + g.cluster().localNode() + ']');
}
if (entry.getValue() != GridDhtPartitionState.OWNING) {
LT.warn(log(), "Waiting for correct partition state part=" + entry.getKey() + ", should be OWNING [state=" + entry.getValue() + "], node=" + g.name() + ", cache=" + c.getName());
// Busy wait.
Thread.sleep(200);
failed = true;
break;
}
}
}
}
}
}
}
}
log.info("awaitPartitionMapExchange finished");
}
Aggregations