/** * @param sb String builder. * @param ctx Context. */ static void dumpExchangeInfo(StringBuilder sb, GridKernalContext ctx) { GridCachePartitionExchangeManager exchMgr = ctx.cache().context().exchange(); GridDhtTopologyFuture fut = exchMgr.lastTopologyFuture(); sb.append("Partitions exchange info [readyVer=").append(exchMgr.readyAffinityVersion()).append(']').append(U.nl()) .append("Last initialized exchange future: ").append(fut); }
/** * @param sb String builder. */ public void dumpPendingMessages(StringBuilder sb) { synchronized (pendingMsgs) { if (pendingMsgs.isEmpty()) return; sb.append("Pending cache messages waiting for exchange [readyVer="). append(cctx.exchange().readyAffinityVersion()). append(", discoVer="). append(cctx.discovery().topologyVersion()).append(']'); sb.append(nl()); for (GridCacheMessage msg : pendingMsgs) { sb.append("Message [waitVer=").append(msg.topologyVersion()).append(", msg=").append(msg).append(']'); sb.append(nl()); } } }
/** * @return Ready topology version. */ public AffinityTopologyVersion readyTopologyVersion() { return ctx.cache().context().exchange().readyAffinityVersion(); }
/** * @param cacheName Cache name. * @param keys Keys. * @return Affinity map. * @throws IgniteCheckedException If failed. */ private <K> Map<ClusterNode, Collection<K>> keysToNodes(@Nullable final String cacheName, Collection<? extends K> keys) throws IgniteCheckedException { return keysToNodes(cacheName, keys, ctx.cache().context().exchange().readyAffinityVersion()); }
/** * Gets affinity key for cache key. * * @param cacheName Cache name. * @param key Cache key. * @return Affinity key. * @throws IgniteCheckedException In case of error. */ @Nullable public Object affinityKey(String cacheName, @Nullable Object key) throws IgniteCheckedException { assert cacheName != null; if (key == null) return null; AffinityInfo affInfo = affinityCache(cacheName, ctx.cache().context().exchange().readyAffinityVersion()); if (affInfo == null) return null; return affInfo.affinityKey(key); }
/** {@inheritDoc} */ @Override public AffinityTopologyVersion topologyVersion() { AffinityTopologyVersion res = topVer; if (res == null || res.equals(AffinityTopologyVersion.NONE)) { if (system()) { AffinityTopologyVersion topVer = cctx.tm().lockedTopologyVersion(Thread.currentThread().getId(), this); if (topVer != null) return topVer; } return cctx.exchange().readyAffinityVersion(); } return res; }
/** * @return Near entries. */ public Set<Cache.Entry<K, V>> nearEntries() { final AffinityTopologyVersion topVer = ctx.shared().exchange().readyAffinityVersion(); return super.entrySet(new CacheEntryPredicateAdapter() { @Override public boolean apply(GridCacheEntryEx entry) { GridNearCacheEntry nearEntry = (GridNearCacheEntry)entry; return !nearEntry.deleted() && nearEntry.visitable(CU.empty0()) && nearEntry.valid(topVer); } }); }
/** * @return Affinity info for current topology version. * @throws IgniteCheckedException If failed. */ private AffinityInfo cache() throws IgniteCheckedException { AffinityInfo aff = affinityCache(cacheName, ctx.cache().context().exchange().readyAffinityVersion()); if (aff == null) throw new IgniteException("Failed to find cache (cache was not started " + "yet or cache was already stopped): " + cacheName); return aff; } }
/** {@inheritDoc} */ @Override public IgniteInternalFuture<MvccSnapshot> requestSnapshot() { SnapshotFuture fut; synchronized (this) { if (state == null) state = fut = new SnapshotFuture(); else if (state.getClass() == SnapshotFuture.class) return (IgniteInternalFuture<MvccSnapshot>)state; else return new GridFinishedFuture<>((MvccSnapshot)state); } requestSnapshot0(cctx.shared().exchange().readyAffinityVersion(), fut); return fut; }
/** * @param cacheName Cache name (needed only if {@code aff} is not provided. * @param key Key. * @param aff Affinity. * @return Key partition. * @throws IgniteCheckedException If failed. */ public int partition0(String cacheName, Object key, @Nullable AffinityInfo aff) throws IgniteCheckedException { assert cacheName != null; if (aff == null) { aff = affinityCache(cacheName, ctx.cache().context().exchange().readyAffinityVersion()); if (aff == null) throw new IgniteCheckedException("Failed to get cache affinity (cache was not started " + "yet or cache was already stopped): " + cacheName); } return aff.affFunc.partition(aff.affinityKey(key)); }
/** * Process client cache start/close requests, called from exchange thread. * * @param msg Change request. */ void processClientCachesChanges(ClientCacheChangeDummyDiscoveryMessage msg) { AffinityTopologyVersion topVer = cctx.exchange().readyAffinityVersion(); DiscoCache discoCache = cctx.discovery().discoCache(topVer); boolean crd = cctx.localNode().equals(discoCache.oldestAliveServerNode()); Map<Integer, Boolean> startedCaches = processClientCacheStartRequests(msg, crd, topVer, discoCache); Set<Integer> closedCaches = processCacheCloseRequests(msg, crd, topVer); if (startedCaches != null || closedCaches != null) scheduleClientChangeMessage(startedCaches, closedCaches); }
/** * Initialize future. */ public void init() { AffinityTopologyVersion mappingtopVermappingtopVer; if (topVer.topologyVersion() > 0) mappingtopVermappingtopVer = topVer; else { mappingtopVermappingtopVer = canRemap ? cctx.affinity().affinityTopologyVersion() : cctx.shared().exchange().readyAffinityVersion(); } map(mappingtopVermappingtopVer); }
@Override public void apply(IgniteEx ig) { while (true) { AffinityTopologyVersion locTopVer = ig.context().cache().context() .exchange().readyAffinityVersion(); if (locTopVer.compareTo(ver) < 0){ System.out.println("Top ready " + locTopVer + " on " + ig.localNode().id()); try { Thread.sleep(100); } catch (InterruptedException e) { break; } } else break; } } }).run();
/** * @param part Partition. * @param qry Query. * @param qryMgr Query manager. * @param cctx Cache context. */ private ScanQueryFallbackClosableIterator(int part, GridCacheQueryAdapter qry, GridCacheQueryManager qryMgr, GridCacheContext cctx) { this.qry = qry; this.qryMgr = qryMgr; this.cctx = cctx; this.part = part; nodes = fallbacks(cctx.shared().exchange().readyAffinityVersion()); if (F.isEmpty(nodes)) throw new ClusterTopologyException("Failed to execute the query " + "(all affinity nodes left the grid) [cache=" + cctx.name() + ", qry=" + qry + ", startTopVer=" + cctx.versions().last().topologyVersion() + ", curTopVer=" + qryMgr.queryTopologyVersion().topologyVersion() + ']'); init(); }
/** * Holds (suspends) job execution until our cache version becomes equal to remote cache's version. * * @return {@code True} if topology check passed. */ private boolean waitAffinityReadyFuture() { GridCacheProcessor cacheProc = ((IgniteEx)ignite).context().cache(); AffinityTopologyVersion locTopVer = cacheProc.context().exchange().readyAffinityVersion(); if (locTopVer.compareTo(topVer) < 0) { IgniteInternalFuture<?> fut = cacheProc.context().exchange().affinityReadyFuture(topVer); if (fut != null && !fut.isDone()) { jobCtx.holdcc(); fut.listen(new CI1<IgniteInternalFuture<?>>() { @Override public void apply(IgniteInternalFuture<?> t) { ((IgniteEx)ignite).context().closure().runLocalSafe(new Runnable() { @Override public void run() { jobCtx.callcc(); } }, false); } }); return false; } } return true; } }
/** {@inheritDoc} */ @Override public boolean isLockedByThread(K key) { A.notNull(key, "key"); if (keyCheck) validateCacheKey(key); try { KeyCacheObject cacheKey = ctx.toCacheKeyObject(key); GridCacheEntryEx e = entry0(cacheKey, ctx.shared().exchange().readyAffinityVersion(), false, false); if (e == null) return false; // Delegate to near if dht. if (e.isDht() && CU.isNearEnabled(ctx)) { IgniteInternalCache<K, V> near = ctx.isDht() ? ctx.dht().near() : ctx.near(); return near.isLockedByThread(key) || e.lockedByThread(); } return e.lockedByThread(); } catch (GridCacheEntryRemovedException ignore) { return false; } }
/** * @throws Exception If failed. */ private void checkAffinity() throws Exception { List<Ignite> nodes = G.allGrids(); ClusterNode crdNode = null; for (Ignite node : nodes) { ClusterNode locNode = node.cluster().localNode(); if (crdNode == null || locNode.order() < crdNode.order()) crdNode = locNode; } AffinityTopologyVersion topVer = ((IgniteKernal)grid(crdNode)). context().cache().context().exchange().readyAffinityVersion(); Map<String, List<List<ClusterNode>>> affMap = new HashMap<>(); for (Ignite node : nodes) { IgniteKernal node0 = (IgniteKernal)node; for (IgniteInternalCache cache : node0.context().cache().caches()) { List<List<ClusterNode>> aff = affMap.get(cache.name()); List<List<ClusterNode>> aff0 = cache.context().affinity().assignments(topVer); if (aff != null) assertEquals(aff, aff0); else affMap.put(cache.name(), aff0); } } }
/** * Waits for the topology version to be not less than one registered on source node. * * @param src Source node. * @throws IgniteCheckedException If failed to wait on affinity ready future. */ protected void alignCacheTopologyVersion(Ignite src) throws IgniteCheckedException { AffinityTopologyVersion topVer = ((IgniteEx)src).context().cache().context().exchange().readyAffinityVersion(); info("Will wait for topology version on all nodes: " + topVer); for (Ignite ignite : Ignition.allGrids()) { IgniteInternalFuture<?> ready = ((IgniteEx)ignite).context().cache().context().exchange() .affinityReadyFuture(topVer); if (ready != null) ready.get(); } }
/** * @throws IgniteInterruptedCheckedException If interrupted. */ private void awaitTopologyChange() throws IgniteInterruptedCheckedException { for (Ignite g : G.allGrids()) { final GridKernalContext ctx = ((IgniteKernal)g).context(); if (ctx.isStopping() || ctx.gateway().getState() == DISCONNECTED || !g.active()) continue; AffinityTopologyVersion topVer = ctx.discovery().topologyVersionEx(); AffinityTopologyVersion exchVer = ctx.cache().context().exchange().readyAffinityVersion(); if (!topVer.equals(exchVer)) { info("Topology version mismatch [node=" + g.name() + ", exchVer=" + exchVer + ", topVer=" + topVer + ']'); GridTestUtils.waitForCondition(new GridAbsPredicate() { @Override public boolean apply() { AffinityTopologyVersion topVer = ctx.discovery().topologyVersionEx(); AffinityTopologyVersion exchVer = ctx.cache().context().exchange().readyAffinityVersion(); return exchVer.equals(topVer); } }, DFLT_TOP_WAIT_TIMEOUT); } } }
/** * @param cacheName Cache name. * @param expDone Expected rebalance finish flag. */ private void checkRebalance(String cacheName, boolean expDone) { for (Ignite node : G.allGrids()) { IgniteKernal node0 = (IgniteKernal)node; GridCacheAdapter cache = node0.context().cache().internalCache(cacheName); AffinityTopologyVersion topVer = node0.context().cache().context().exchange().readyAffinityVersion(); if (cache != null) assertEquals(expDone, cache.context().topology().rebalanceFinished(topVer)); else node0.context().discovery().cacheAffinityNode(node0.localNode(), cacheName); } }