/** * Init affinity. * * @param cacheName Cache name. * @param affKey Affinity key. * @param ctx Kernal context. */ public void affinity(String cacheName, Object affKey, GridKernalContext ctx) { try { final Object affKey0 = ctx.affinity().affinityKey(cacheName, affKey); node = ctx.affinity().mapKeyToNode(cacheName, affKey0); } catch (IgniteCheckedException e) { throw U.convertException(e); } } }
/** * Cleaning the affinity history. */ private void onHistoryAdded() { if (fullHistSize.incrementAndGet() > MAX_HIST_SIZE) { Iterator<HistoryAffinityAssignment> it = affCache.values().iterator(); int rmvCnt = MAX_HIST_SIZE / 2; AffinityTopologyVersion topVerRmv = null; while (it.hasNext() && rmvCnt > 0) { AffinityAssignment aff0 = it.next(); it.remove(); rmvCnt--; fullHistSize.decrementAndGet(); topVerRmv = aff0.topologyVersion(); } topVerRmv = it.hasNext() ? it.next().topologyVersion() : topVerRmv; ctx.affinity().removeCachedAffinity(topVerRmv); } }
/** {@inheritDoc} */ @Override public int partition(Object arg, int dataType, String cacheName) throws IgniteCheckedException { Object param = H2Utils.convert(arg, idx, dataType); return idx.kernalContext().affinity().partition(cacheName, param); } }
/** * @param ctx Cache objects context. * @param cctx Cache context. * @param obj Object. * @return Object partition. */ private int partition(CacheObjectContext ctx, @Nullable GridCacheContext cctx, Object obj) { try { return cctx != null ? cctx.affinity().partition(obj, false) : ctx.kernalContext().affinity().partition0(ctx.cacheName(), obj, null); } catch (IgniteCheckedException e) { U.error(log, "Failed to get partition", e); return -1; } }
head = new AtomicReference<>(new GridAffinityAssignmentV2(AffinityTopologyVersion.NONE)); similarAffKey = ctx.affinity().similaryAffinityKey(aff, nodeFilter, backups, partsCnt);
/** * @param grpId Cache group ID. * @param discoCache Discovery data cache. * @return Topology. */ public GridDhtPartitionTopology clientTopology(int grpId, DiscoCache discoCache) { GridClientPartitionTopology top = clientTops.get(grpId); if (top != null) return top; CacheGroupDescriptor grpDesc = cctx.affinity().cacheGroups().get(grpId); assert grpDesc != null : grpId; CacheConfiguration<?, ?> ccfg = grpDesc.config(); AffinityFunction aff = ccfg.getAffinity(); Object affKey = cctx.kernalContext().affinity().similaryAffinityKey(aff, ccfg.getNodeFilter(), ccfg.getBackups(), aff.partitions()); GridClientPartitionTopology old = clientTops.putIfAbsent(grpId, top = new GridClientPartitionTopology(cctx, discoCache, grpId, aff.partitions(), affKey)); return old != null ? old : top; }
/** * Affinity run implementation. * * @param cacheName Cache name. * @param affKey Affinity key. * @param job Job. * @return Internal future. */ private IgniteInternalFuture<?> affinityRunAsync0(String cacheName, Object affKey, IgniteRunnable job) { A.notNull(affKey, "affKey"); A.notNull(job, "job"); guard(); try { // In case cache key is passed instead of affinity key. final Object affKey0 = ctx.affinity().affinityKey(cacheName, affKey); int partId = ctx.affinity().partition(cacheName, affKey0); if (partId < 0) throw new IgniteCheckedException("Failed map key to partition: [cache=" + cacheName + " key=" + affKey + ']'); return ctx.closure().affinityRun(Collections.singletonList(cacheName), partId, job, prj.nodes(), execName); } catch (IgniteCheckedException e) { throw U.convertException(e); } finally { unguard(); } }
/** * Affinity call implementation. * @param cacheName Cache name. * @param affKey Affinity key. * @param job Job. * @return Internal future. */ private <R> IgniteInternalFuture<R> affinityCallAsync0(String cacheName, Object affKey, IgniteCallable<R> job) { A.notNull(affKey, "affKey"); A.notNull(job, "job"); guard(); try { // In case cache key is passed instead of affinity key. final Object affKey0 = ctx.affinity().affinityKey(cacheName, affKey); int partId = ctx.affinity().partition(cacheName, affKey0); if (partId < 0) throw new IgniteCheckedException("Failed map key to partition: [cache=" + cacheName + " key=" + affKey + ']'); return ctx.closure().affinityCall(Collections.singletonList(cacheName), partId, job, prj.nodes(), execName); } catch (IgniteCheckedException e) { throw U.convertException(e); } finally { unguard(); } }
/** * Affinity call implementation. * @param cacheNames Cache names collection. * @param affKey Affinity key. * @param job Job. * @return Internal future. */ private <R> IgniteInternalFuture<R> affinityCallAsync0(@NotNull Collection<String> cacheNames, Object affKey, IgniteCallable<R> job) { A.notNull(affKey, "affKey"); A.notNull(job, "job"); A.ensure(!cacheNames.isEmpty(), "cachesNames mustn't be empty"); guard(); try { final String cacheName = F.first(cacheNames); // In case cache key is passed instead of affinity key. final Object affKey0 = ctx.affinity().affinityKey(cacheName, affKey); int partId = ctx.affinity().partition(cacheName, affKey0); if (partId < 0) throw new IgniteCheckedException("Failed map key to partition: [cache=" + cacheName + " key=" + affKey + ']'); return ctx.closure().affinityCall(cacheNames, partId, job, prj.nodes(), execName); } catch (IgniteCheckedException e) { throw U.convertException(e); } finally { unguard(); } }
/** {@inheritDoc} */ @Override public ClientResponse process(ClientConnectionContext ctx) { IgniteCache cache = cache(ctx); GridDiscoveryManager discovery = ctx.kernalContext().discovery(); Collection<ClusterNode> nodes = discovery.discoCache().cacheNodes(cache.getName()); Affinity aff = ctx.kernalContext().affinity().affinityProxy(cache.getName()); ArrayList<ClientConnectableNodePartitions> res = new ArrayList<>(); for (ClusterNode node : nodes) { Integer port = node.attribute(ClientListenerProcessor.CLIENT_LISTENER_PORT); if (port == null) continue; Collection<String> addrs = node.addresses(); int[] parts = aff.primaryPartitions(node); res.add(new ClientConnectableNodePartitions(port, addrs, parts)); } return new ClientCacheNodePartitionsResponse(requestId(), res); } }
public void testPerformance() throws Exception { IgniteKernal grid = (IgniteKernal)grid(0); GridAffinityProcessor aff = grid.context().affinity();
/** * @param key Key to map. * @param topVer Topology version. * @param cctx Context. * @return Nodes to send requests to. * @throws IgniteCheckedException If failed. */ private List<ClusterNode> nodes(KeyCacheObject key, AffinityTopologyVersion topVer, GridCacheContext cctx) throws IgniteCheckedException { GridAffinityProcessor aff = ctx.affinity(); List<ClusterNode> res = null; if (!allowOverwrite()) res = cctx.isLocal() ? aff.mapKeyToPrimaryAndBackups(cacheName, key, topVer) : cctx.topology().nodes(cctx.affinity().partition(key), topVer); else { ClusterNode node = aff.mapKeyToNode(cacheName, key, topVer); if (node != null) res = Collections.singletonList(node); } if (F.isEmpty(res)) throw new ClusterTopologyServerNotFoundException("Failed to find server node for cache (all affinity " + "nodes have left the grid or cache was stopped): " + cacheName); return res; }
/** * @throws Exception If failed. */ @Test public void testPartitionReservation() throws Exception { int orgId = 0; cancelAllJobs = true; // Workaround for initial update job metadata. try { grid(0).compute().affinityRun( Arrays.asList(Organization.class.getSimpleName(), Person.class.getSimpleName()), new Integer(orgId), new TestRun(orgId)); } catch (Exception ignored) { // No-op. Swallow exceptions on run (e.g. job canceling etc.). // The test checks only correct partition release in case CollisionSpi is used. } // All partition must be released in spite of any exceptions during the job executions. cancelAllJobs = false; ClusterNode n = grid(0).context().affinity() .mapKeyToNode(Organization.class.getSimpleName(), orgId); checkPartitionsReservations((IgniteEx)grid(n), orgId, 0); }
final ClusterNode node = ctx.affinity().mapPartitionToNode(cacheName, partId, mapTopVer);
final ClusterNode node = ctx.affinity().mapPartitionToNode(cacheName, partId, mapTopVer);
return ((IgniteEx)ignite).context().affinity().mapPartitionToNode(ctx.affinityCacheName(), ctx.partition(), ((GridFailoverContextImpl)ctx).affinityTopologyVersion());
size = ((ConcurrentSkipListMap)GridTestUtils.getFieldValue(grid.context().affinity(), "affMap")).size();
/** * @param ignite Ignite. * @throws Exception If failed. */ private void checkAffinity(Ignite ignite) throws Exception { Affinity<Object> aff = ignite.affinity(DEFAULT_CACHE_NAME); GridAffinityProcessor affProc = ((IgniteKernal)ignite).context().affinity(); IgniteCacheObjectProcessor cacheObjProc = ((IgniteKernal)ignite).context().cacheObjects(); CacheObjectContext cacheObjCtx = cacheObjProc.contextForCache( ignite.cache(DEFAULT_CACHE_NAME).getConfiguration(CacheConfiguration.class)); for (int i = 0; i < 1000; i++) { assertEquals(i, aff.affinityKey(i)); assertEquals(i, aff.affinityKey(new TestObject(i))); assertEquals(i, aff.affinityKey(ignite.binary().toBinary(new TestObject(i)))); assertEquals(i, aff.affinityKey(new AffinityKey(0, i))); BinaryObjectBuilder bldr = ignite.binary().builder("TestObject2"); bldr.setField("affKey", i); assertEquals(i, aff.affinityKey(bldr.build())); CacheObject cacheObj = cacheObjProc.toCacheObject(cacheObjCtx, new TestObject(i), true); assertEquals(i, aff.affinityKey(cacheObj)); assertEquals(aff.mapKeyToNode(i), aff.mapKeyToNode(new TestObject(i))); assertEquals(aff.mapKeyToNode(i), aff.mapKeyToNode(cacheObj)); assertEquals(i, affProc.affinityKey(DEFAULT_CACHE_NAME, i)); assertEquals(i, affProc.affinityKey(DEFAULT_CACHE_NAME, new TestObject(i))); assertEquals(i, affProc.affinityKey(DEFAULT_CACHE_NAME, cacheObj)); assertEquals(affProc.mapKeyToNode(DEFAULT_CACHE_NAME, i), affProc.mapKeyToNode(DEFAULT_CACHE_NAME, new TestObject(i))); assertEquals(affProc.mapKeyToNode(DEFAULT_CACHE_NAME, i), affProc.mapKeyToNode(DEFAULT_CACHE_NAME, cacheObj)); assertEquals(affProc.mapKeyToNode(DEFAULT_CACHE_NAME, new AffinityKey(0, i)), affProc.mapKeyToNode(DEFAULT_CACHE_NAME, i)); } }