/** * @param key Key. * @return Partition. */ public int partition(Object key) { return partition(key, true); }
/** * @param p Partition. * @param cctx Cache context. * @return Owning node ID. */ public UUID nodeForPartition(int p, GridCacheContext<?, ?> cctx) { UUID[] nodeIds = partsNodes; if (nodeIds == null) { assert partsMap != null; nodeIds = new UUID[cctx.affinity().partitions()]; for (Map.Entry<UUID, int[]> e : partsMap.entrySet()) { UUID nodeId = e.getKey(); int[] nodeParts = e.getValue(); assert nodeId != null; assert !F.isEmpty(nodeParts); for (int part : nodeParts) { assert nodeIds[part] == null; nodeIds[part] = nodeId; } } partsNodes = nodeIds; } return nodeIds[p]; }
/** * Gets current topology version. * * @return Topology version. */ private AffinityTopologyVersion topologyVersion() { return cctx.affinity().affinityTopologyVersion(); } }
/** * @param key Key to check. * @param topVer Topology version. * @return {@code true} if given key belongs to local node. */ public boolean keyLocalNode(Object key, AffinityTopologyVersion topVer) { return partitionLocalNode(partition(key), topVer); }
/** * @param key Key to check. * @param topVer Topology version. * @return Backup nodes. */ public Collection<ClusterNode> backupsByKey(Object key, AffinityTopologyVersion topVer) { return backupsByPartition(partition(key), topVer); }
/** * @param key Key to check. * @param topVer Topology version. * @return Primary node for given key. */ @Nullable public ClusterNode primaryByKey(Object key, AffinityTopologyVersion topVer) { return primaryByPartition(partition(key), topVer); }
int part = cctx.affinity().partition(key); for (int i = 0; i < cctx.affinity().partitions(); i++) { try { Map<Object, Object> exp = entries.get(i);
/** * @param n Node to check. * @param key Key to check. * @param topVer Topology version. * @return {@code True} if checked node is primary for given key. */ public boolean primaryByKey(ClusterNode n, Object key, AffinityTopologyVersion topVer) { return F.eq(primaryByKey(key, topVer), n); }
/** * @param cctx Cctx. * @return Remote partition. */ private IgniteBiTuple<Integer, UUID> remotePartition(final GridCacheContext cctx) { ClusterNode node = F.first(cctx.kernalContext().grid().cluster().forRemotes().nodes()); GridCacheAffinityManager affMgr = cctx.affinity(); AffinityTopologyVersion topVer = affMgr.affinityTopologyVersion(); Set<Integer> parts = affMgr.primaryPartitions(node.id(), topVer); return new IgniteBiTuple<>(F.first(parts), node.id()); }
/** * @param expVer Expected topology version. * @param curVer Current topology version. * @return {@code True} if cache affinity changed and operation should be remapped. */ protected final boolean needRemap(AffinityTopologyVersion expVer, AffinityTopologyVersion curVer, Collection<KeyCacheObject> keys) { if (curVer.equals(expVer)) return false; AffinityTopologyVersion lastAffChangedTopVer = ctx.shared().exchange().lastAffinityChangedTopologyVersion(expVer); if (curVer.compareTo(lastAffChangedTopVer) >= 0 && curVer.compareTo(expVer) <= 0) return false; // TODO IGNITE-7164 check mvcc crd for mvcc enabled txs. Collection<ClusterNode> cacheNodes0 = ctx.discovery().cacheGroupAffinityNodes(ctx.groupId(), expVer); Collection<ClusterNode> cacheNodes1 = ctx.discovery().cacheGroupAffinityNodes(ctx.groupId(), curVer); if (!cacheNodes0.equals(cacheNodes1) || ctx.affinity().affinityTopologyVersion().compareTo(curVer) < 0) return true; try { List<List<ClusterNode>> aff1 = ctx.affinity().assignments(expVer); List<List<ClusterNode>> aff2 = ctx.affinity().assignments(curVer); return !aff1.equals(aff2); } catch (IllegalStateException ignored) { return true; } }
/** * @param cctx Cache context. * @param prj Projection (optional). * @return Collection of data nodes in provided projection (if any). * @throws IgniteCheckedException If partition number is invalid. */ private static Collection<ClusterNode> nodes(final GridCacheContext<?, ?> cctx, @Nullable final ClusterGroup prj, @Nullable final Integer part) throws IgniteCheckedException { assert cctx != null; final AffinityTopologyVersion topVer = cctx.affinity().affinityTopologyVersion(); Collection<ClusterNode> affNodes = CU.affinityNodes(cctx, topVer); if (prj == null && part == null) return affNodes; if (part != null && part >= cctx.affinity().partitions()) throw new IgniteCheckedException("Invalid partition number: " + part); final Set<ClusterNode> owners = part == null ? Collections.<ClusterNode>emptySet() : new HashSet<>(cctx.topology().owners(part, topVer)); return F.view(affNodes, new P1<ClusterNode>() { @Override public boolean apply(ClusterNode n) { return cctx.discovery().cacheAffinityNode(n, cctx.name()) && (prj == null || prj.node(n.id()) != null) && (part == null || owners.contains(n)); } }); }
for (ClusterNode clusterNode : cctx.affinity().assignment(topVer).nodes()) mapping.put(clusterNode, null); List<List<ClusterNode>> assignment = cctx.affinity().assignment(topVer).assignment(); U.forRange(0, cctx.affinity().partitions());
AffinityTopologyVersion topVer = cctx.affinity().affinityTopologyVersion(); Set<Integer> primaries = cctx.affinity().primaryPartitions(cctx.localNodeId(), topVer); Set<Integer> backups = cctx.affinity().backupPartitions(cctx.localNodeId(), topVer);
int partsCnt = cctx.affinity().partitions(); partIds = cctx.affinity().primaryPartitions(ctx.localNodeId(), topVer);
topVer = ctx.cache().context().exchange().readyAffinityVersion(); List<List<ClusterNode>> assignments = cctx.affinity().assignments(topVer); key.partition(cctx.affinity().partition(key, false));
/** * Get affinity assignment for the given topology version. * * @param topVer Topology version. * @return Affinity assignment. */ public AffinityAssignment assignment(AffinityTopologyVersion topVer) { return assignment(topVer, cctx.shared().exchange().lastAffinityChangedTopologyVersion(topVer)); }
int partition = cctx.affinity().partition(affKeyObj); if (!cctx.affinity().primaryByKey(cctx.localNode(), partition, qctx.topologyVersion())) return null; node = cctx.affinity().primaryByKey(affKeyObj, qctx.topologyVersion());
/** {@inheritDoc} */ @Override public GridTuple3<GridAffinityMessage, GridAffinityMessage, GridAffinityAssignment> call() throws Exception { assert ignite != null; assert log != null; IgniteKernal kernal = ((IgniteKernal) ignite); GridCacheContext<Object, Object> cctx = kernal.internalCache(cacheName).context(); assert cctx != null; GridKernalContext ctx = kernal.context(); cctx.affinity().affinityReadyFuture(topVer).get(); AffinityAssignment assign0 = cctx.affinity().assignment(topVer); //using legacy GridAffinityAssignment for compatibility. return F.t( affinityMessage(ctx, cctx.config().getAffinity()), affinityMessage(ctx, cctx.config().getAffinityMapper()), new GridAffinityAssignment(topVer, assign0.assignment(), assign0.idealAssignment()) ); }
/** {@inheritDoc} */ @Override public boolean apply(K k, V v) { return !filter || ctx.affinity().primaryByKey(ctx.localNode(), k, ctx.affinity().affinityTopologyVersion()); }
cctx.affinity().assignment(cctx.affinity().affinityTopologyVersion()).idealAssignment();