/** * @param id Node ID to check. * @return {@code True} if node ID is local. */ public boolean isLocalNode(UUID id) { assert id != null; return localNode().id().equals(id); }
/** * Initialize this future for local execution. */ public synchronized void initLocal() { doInit(null, Collections.singletonList(cctx.localNode()), true); }
/** * @param part Partition number to check. * @param topVer Topology version. * @return {@code true} if given partition belongs to local node. */ public boolean partitionLocalNode(int part, AffinityTopologyVersion topVer) { assert part >= 0 : "Invalid partition: " + part; return nodesByPartition(part, topVer).contains(cctx.localNode()); }
/** * @param n Node to check. * @return {@code True} if node is local. */ public boolean isLocalNode(ClusterNode n) { assert n != null; return localNode().id().equals(n.id()); }
/** * @return Client first flag. */ public boolean clientFirst() { return cctx.localNode().isClient() && !topLocked && !tx.hasRemoteLocks(); }
/** * @param topVer Topology version. * @return Nodes where set data request should be sent. * @throws IgniteCheckedException If all cache nodes left grid. */ private Collection<ClusterNode> dataNodes(AffinityTopologyVersion topVer) throws IgniteCheckedException { assert ctx.isPartitioned() || collocated : "Non-collocated mode is supported only for PARTITIONED caches."; if (ctx.isLocal() || (ctx.isReplicated() && ctx.affinityNode())) return Collections.singleton(ctx.localNode()); Collection<ClusterNode> nodes; if (collocated) { List<ClusterNode> nodes0 = ctx.affinity().nodesByPartition(hdrPart, topVer); nodes = !nodes0.isEmpty() ? Collections.singleton(nodes0.contains(ctx.localNode()) ? ctx.localNode() : F.first(nodes0)) : nodes0; } else nodes = CU.affinityNodes(ctx, topVer); if (nodes.isEmpty()) throw new IgniteCheckedException("Failed to get set data, all cache nodes left grid."); return nodes; }
/** * @param e Transaction entry. * @param topVer Topology version. * @return {@code True} if entry is locally mapped as a primary or back up node. */ protected boolean isNearLocallyMapped(GridCacheEntryEx e, AffinityTopologyVersion topVer) { return ctx.affinity().partitionBelongs(ctx.localNode(), e.partition(), topVer); }
@Override public void onEvent(Event evt) { DiscoveryEvent e = (DiscoveryEvent)evt; ClusterNode loc = ctx.localNode(); assert e.type() == EVT_NODE_LEFT || e.type() == EVT_NODE_FAILED : e; final ClusterNode n = e.eventNode(); assert !loc.id().equals(n.id()); for (GridDhtForceKeysFuture<?, ?> f : forceKeyFuts.values()) f.onDiscoveryEvent(e); } };
/** */ private boolean isLocalBackup(EnlistOperation op, KeyCacheObject key) { if (!cctx.affinityNode() || op == EnlistOperation.LOCK) return false; else if (cctx.isReplicated()) return true; return cctx.topology().nodes(key.partition(), tx.topologyVersion()).indexOf(cctx.localNode()) > 0; }
/** */ private boolean isLocalBackup(EnlistOperation op, KeyCacheObject key) { if (!cctx.affinityNode() || op == EnlistOperation.LOCK) return false; else if (cctx.isReplicated()) return true; return cctx.topology().nodes(key.partition(), tx.topologyVersion()).contains(cctx.localNode()); }
/** {@inheritDoc} */ @Override public boolean apply(K k, V v) { return !filter || ctx.affinity().primaryByKey(ctx.localNode(), k, ctx.affinity().affinityTopologyVersion()); }
/** * Called when cache was restored during recovery and node has joined to topology. * * @param topVer Cache topology join version. * @param clusterWideDesc Cluster-wide cache descriptor received during exchange. */ public void finishRecovery(AffinityTopologyVersion topVer, DynamicCacheDescriptor clusterWideDesc) { assert recoveryMode : this; recoveryMode = false; locStartTopVer = topVer; locMacs = localNode().attribute(ATTR_MACS); assert locMacs != null; this.statisticsEnabled = clusterWideDesc.cacheConfiguration().isStatisticsEnabled(); this.dynamicDeploymentId = clusterWideDesc.deploymentId(); }
/** * 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); }
/** {@inheritDoc} */ @Override public long localSizeLong(int part, CachePeekMode[] peekModes) throws IgniteCheckedException { PeekModes modes = parsePeekModes(peekModes, true); long size = 0; if (modes.near) size += nearSize(); // Swap and offheap are disabled for near cache. if (modes.offheap) { AffinityTopologyVersion topVer = ctx.affinity().affinityTopologyVersion(); IgniteCacheOffheapManager offheap = ctx.offheap(); if (ctx.affinity().primaryByPartition(ctx.localNode(), part, topVer) && modes.primary || ctx.affinity().backupByPartition(ctx.localNode(), part, topVer) && modes.backup) size += offheap.cacheEntriesCount(ctx.cacheId(), part); } return size; }
/** * * @param node Node. * @param batch Batch. * @param first First mapping flag. */ private void sendBatch(ClusterNode node, Batch batch, boolean first) throws IgniteCheckedException { updateMappings(node); boolean clientFirst = first && cctx.localNode().isClient() && !topLocked && !tx.hasRemoteLocks(); int batchId = batchCntr.incrementAndGet(); if (node.isLocal()) enlistLocal(batchId, node.id(), batch); else sendBatch(batchId, node.id(), batch, clientFirst); }
/** * * @param node Node. * @param batch Batch. * @param first First mapping flag. */ private void sendBatch(ClusterNode node, Batch batch, boolean first) throws IgniteCheckedException { updateMappings(node); boolean clientFirst = first && cctx.localNode().isClient() && !topLocked && !tx.hasRemoteLocks(); int batchId = batchCntr.incrementAndGet(); if (node.isLocal()) enlistLocal(batchId, node.id(), batch); else sendBatch(batchId, node.id(), batch, clientFirst); }
/** * Fill cache. * * @throws IgniteCheckedException if failed. */ private static Set<Integer> populateCache(IgniteEx ignite, boolean loc, int cnt, IgnitePredicate<Integer> expectedEntryFilter) throws IgniteCheckedException { IgniteInternalCache<Integer, Person> cache = ignite.cachex(PERSON_CACHE); assertNotNull(cache); Random rand = new Random(); HashSet<Integer> exp = new HashSet<>(); Affinity<Integer> aff = cache.affinity(); ClusterNode localNode = cache.context().localNode(); for (int i = 0; i < cnt; i++) { int val = rand.nextInt(cnt); cache.put(val, new Person(String.valueOf(val), val)); if (expectedEntryFilter.apply(val) && (!loc || aff.isPrimary(localNode, val))) exp.add(val); } return exp; }
/** * @param key Key. * @param idx Node index. * @return {@code True} if key belongs to node with index idx. */ protected boolean belongs(String key, int idx) { return context(idx).cache().affinity().isPrimaryOrBackup(context(idx).localNode(), key); }
/** * Adds partition unload event. * * @param part Partition. */ public void addUnloadEvent(int part) { if (!eventRecordable(EVT_CACHE_REBALANCE_PART_UNLOADED)) LT.warn(log, "Added event without checking if event is recordable: " + U.gridEventName(EVT_CACHE_REBALANCE_PART_UNLOADED)); List<GridCacheContext> caches = this.caches; for (GridCacheContext cctx : caches) if (!cctx.config().isEventsDisabled()) cctx.gridEvents().record(new CacheRebalancingEvent(cctx.name(), cctx.localNode(), "Cache unloading event.", EVT_CACHE_REBALANCE_PART_UNLOADED, part, null, 0, 0)); }
/** * @param nodeIdx Node index. * @param part Cache partition. * @return Tuple with number of primary and backup keys (one or both will be zero). */ private T2<Integer, Integer> offheapKeysCount(int nodeIdx, int part) throws IgniteCheckedException { GridCacheContext ctx = ((IgniteEx)ignite(nodeIdx)).context().cache().internalCache(DEFAULT_CACHE_NAME).context(); // Swap and offheap are disabled for near cache. IgniteCacheOffheapManager offheapManager = ctx.isNear() ? ctx.near().dht().context().offheap() : ctx.offheap(); //First count entries... int cnt = (int)offheapManager.cacheEntriesCount(ctx.cacheId(), part); GridCacheAffinityManager affinity = ctx.affinity(); AffinityTopologyVersion topVer = affinity.affinityTopologyVersion(); //And then find out whether they are primary or backup ones. int primaryCnt = 0; int backupCnt = 0; if (affinity.primaryByPartition(ctx.localNode(), part, topVer)) primaryCnt = cnt; else if (affinity.backupByPartition(ctx.localNode(), part, topVer)) backupCnt = cnt; return new T2<>(primaryCnt, backupCnt); }