/** * @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); }
/** {@inheritDoc} */ @Override public boolean isPrimary(ClusterNode n, K key) { A.notNull(n, "n", key, "key"); return cctx.affinity().primaryByKey(n, key, topologyVersion()); }
/** {@inheritDoc} */ @Override public Map<ClusterNode, Collection<K>> mapKeysToNodes(@Nullable Collection<? extends K> keys) { A.notNull(keys, "keys"); AffinityTopologyVersion topVer = topologyVersion(); int nodesCnt; if (!cctx.isLocal()) nodesCnt = cctx.discovery().cacheGroupAffinityNodes(cctx.groupId(), topVer).size(); else nodesCnt = 1; // Must return empty map if no alive nodes present or keys is empty. Map<ClusterNode, Collection<K>> res = new HashMap<>(nodesCnt, 1.0f); for (K key : keys) { ClusterNode primary = cctx.affinity().primaryByKey(key, topVer); if (primary == null) throw new IgniteException("Failed to get primary node [topVer=" + topVer + ", key=" + key + ']'); Collection<K> mapped = res.get(primary); if (mapped == null) { mapped = new ArrayList<>(Math.max(keys.size() / nodesCnt, 16)); res.put(primary, mapped); } mapped.add(key); } return res; }
/** {@inheritDoc} */ @Override public boolean apply(K k, V v) { return !filter || ctx.affinity().primaryByKey(ctx.localNode(), k, ctx.affinity().affinityTopologyVersion()); }
/** * @param txKey Tx key. * @return Primary node ID. */ private UUID primary(IgniteTxKey txKey) { GridCacheContext ctx = cctx.cacheContext(txKey.cacheId()); ClusterNode node = ctx.affinity().primaryByKey(txKey.key(), topVer); assert node != null : topVer; return node.id(); }
/** * Gets or creates entry for given key and given topology version. * * @param key Key for entry. * @param topVer Topology version. * @param allowDetached Whether to allow detached entries. If {@code true} and node is not primary * for given key, a new detached entry will be created. Otherwise, entry will be obtained from * dht cache map. * @return Cache entry. * @throws GridDhtInvalidPartitionException If {@code allowDetached} is false and node is not primary * for given key. */ public GridDistributedCacheEntry entryExx( KeyCacheObject key, AffinityTopologyVersion topVer, boolean allowDetached ) { return allowDetached && !ctx.affinity().primaryByKey(ctx.localNode(), key, topVer) ? createEntry(key) : entryExx(key, topVer); }
/** * Add entry to batch. * * @param key Key. * @param proc Processor. * @param rowNum Row number. * @throws IgniteCheckedException If failed. */ public void add(Object key, EntryProcessor<Object, Object, Boolean> proc, int rowNum) throws IgniteCheckedException { assert key != null; assert proc != null; ClusterNode node = cctx.affinity().primaryByKey(key, AffinityTopologyVersion.NONE); if (node == null) throw new IgniteCheckedException("Failed to map key to node."); assert rowNum < cntPerRow.length; UUID nodeId = node.id(); Batch batch = batches.get(nodeId); if (batch == null) { batch = new Batch(); batches.put(nodeId, batch); } if (batch.containsKey(key)) { // Force cache update if duplicates found. sendBatch(batch); } batch.put(key, rowNum, proc); if (batch.size() >= size) sendBatch(batch); }
/** {@inheritDoc} */ @Override public Object call() throws IgniteCheckedException { if (topVer != null) { GridCacheAffinityManager aff = ((IgniteKernal)ignite).context().cache().internalCache(CACHE_NAME).context().affinity(); ClusterNode loc = ignite.cluster().localNode(); if (loc.equals(aff.primaryByKey(key, topVer))) return true; AffinityTopologyVersion topVer0 = new AffinityTopologyVersion(topVer.topologyVersion() + 1, 0); assertEquals(loc, aff.primaryByKey(key, topVer0)); } return null; } }
/** * @param mapping Mappings. * @param key Key to map. * @param topVer Topology version. * @return Near lock mapping. * @throws IgniteCheckedException If mapping failed. */ private GridNearLockMapping map( KeyCacheObject key, @Nullable GridNearLockMapping mapping, AffinityTopologyVersion topVer ) throws IgniteCheckedException { assert mapping == null || mapping.node() != null; ClusterNode primary = cctx.affinity().primaryByKey(key, topVer); if (primary == null) throw new ClusterTopologyServerNotFoundException("Failed to lock keys " + "(all partition nodes left the grid)."); if (cctx.discovery().node(primary.id()) == null) // If primary node left the grid before lock acquisition, fail the whole future. throw newTopologyException(null, primary.id()); if (mapping == null || !primary.id().equals(mapping.node().id())) mapping = new GridNearLockMapping(primary, key); else mapping.addKey(key); return mapping; }
/** * @param mapping Mappings. * @param key Key to map. * @param topVer Topology version. * @return Near lock mapping. * @throws IgniteCheckedException If mapping for key failed. */ private GridNearLockMapping map( KeyCacheObject key, @Nullable GridNearLockMapping mapping, AffinityTopologyVersion topVer ) throws IgniteCheckedException { assert mapping == null || mapping.node() != null; ClusterNode primary = cctx.affinity().primaryByKey(key, topVer); if (primary == null) throw new ClusterTopologyServerNotFoundException("Failed to lock keys " + "(all partition nodes left the grid)."); if (cctx.discovery().node(primary.id()) == null) // If primary node left the grid before lock acquisition, fail the whole future. throw newTopologyException(null, primary.id()); if (mapping == null || !primary.id().equals(mapping.node().id())) mapping = new GridNearLockMapping(primary, key); else mapping.addKey(key); return mapping; }
if (!cctx.affinity().primaryByKey(cctx.localNode(), key, topVer)) {
if (!cctx.affinity().primaryByKey(cctx.localNode(), partition, qctx.topologyVersion())) return null; node = cctx.affinity().primaryByKey(affKeyObj, qctx.topologyVersion());
ClusterNode node = cctx.affinity().primaryByKey(key, topVer);
ClusterNode primary = ctx.affinity().primaryByKey(key, cand.topologyVersion());
((IgniteCacheProxy)cache).context().affinity().primaryByKey(key, NONE);
((IgniteCacheProxy)cache).context().affinity().primaryByKey(key, NONE);
if (cache.context().affinity().primaryByKey(ctx.grid().localNode(), e.getKey(), topVer)) { String name = ((GridServiceAssignmentsKey)e.getKey()).name();
if (!txInitiatorPrimary && node.equals(affinity.primaryByKey(i, NONE))) continue;
/** * @param queue Queue. * @return Primary node for queue's header. * @throws Exception If failed. */ private int primaryQueueNode(IgniteQueue queue) throws Exception { GridCacheContext cctx = GridTestUtils.getFieldValue(queue, "cctx"); GridCacheAffinityManager aff = cctx.affinity(); CachePeekMode[] modes = new CachePeekMode[]{CachePeekMode.ALL}; for (int i = 0; i < gridCount(); i++) { for (Cache.Entry e : grid(i).context().cache().internalCache(cctx.name()).localEntries(modes)) { Object key = e.getKey(); if (aff.primaryByKey(grid(i).localNode(), key, AffinityTopologyVersion.NONE) && key instanceof GridCacheQueueHeaderKey) return i; } } fail("Failed to find primary node for queue header."); return -1; } }
ClusterNode primaryNode = cctx.affinity().primaryByKey(key, topVer);