/** {@inheritDoc} */ @Override public void start0() throws GridException { aff = new GridAffinityAssignmentCache( cctx, cctx.namex(), cctx.config().getAffinity(), cctx.config().getAffinityMapper(), cctx.config().getBackups()); // Generate internal keys for partitions. int partCnt = partitions(); partAffKeys = new GridPartitionLockKey[partCnt]; Collection<Integer> found = new HashSet<>(); long affKey = 0; while (true) { GridPartitionLockKey key = new GridPartitionLockKey(affKey); int part = aff.partition(key); if (found.add(part)) { // This is a key for not yet calculated partition. key.partitionId(part); partAffKeys[part] = key; if (found.size() == partCnt) break; } affKey++; if (affKey > partCnt * MAX_PARTITION_KEY_ATTEMPT_RATIO) throw new IllegalStateException( "Failed to calculate partition affinity keys for given affinity " + "function [attemptCnt=" + affKey + ", found=" + found + ", cacheName=" + cctx.name() + ']'); } }
/** {@inheritDoc} */ @Override protected void onKernalStart0() throws GridException { if (cctx.isLocal()) // No discovery event needed for local affinity. aff.calculate(1, null); }
/** @return Affinity-ready topology version. */ public long affinityTopologyVersion() { return aff.lastVersion(); }
/** * @param part Partition. * @param topVer Topology version. * @return Affinity nodes. */ public Collection<GridNode> nodes(int part, long topVer) { if (cctx.isLocal()) topVer = 1; return aff.nodes(part, topVer); }
/** * @param nodeId Node ID. * @param topVer Topology version to calculate affinity. * @return Partitions for which given node is backup. */ public Set<Integer> backupPartitions(UUID nodeId, long topVer) { if (cctx.isLocal()) topVer = 1; return aff.backupPartitions(nodeId, topVer); }
/** @return Partition count. */ public int partitions() { return aff.partitions(); }
/** * NOTE: Use this method always when you need to calculate partition id for a key provided by * user. It's required since we should apply affinity mapper logic in order to find a key that * will eventually be passed to affinity function. * * @param key Key. * @return Partition. */ public <T> int partition(T key) { return aff.partition(key); }
/** * Calculates affinity cache for given topology version. * * @param topVer Topology version to calculate affinity for. * @param discoEvt Discovery event that causes this topology change. */ public List<List<GridNode>> calculateAffinity(long topVer, GridDiscoveryEvent discoEvt) { assert !cctx.isLocal(); return aff.calculate(topVer, discoEvt); }
/** * @param topVer Topology version. * @return Affinity assignments. */ public List<List<GridNode>> assignments(long topVer) { if (cctx.isLocal()) topVer = 1; return aff.assignments(topVer); }
/** * Initializes affinity for joined node. * * @param topVer Topology version. * @param affAssignment Affinity assignment for this topology version. */ public void initializeAffinity(long topVer, List<List<GridNode>> affAssignment) { assert !cctx.isLocal(); aff.initialize(topVer, affAssignment); }
/** * Clean up outdated cache items. * * @param topVer Actual topology version, older versions will be removed. */ public void cleanUpCache(long topVer) { assert !cctx.isLocal(); aff.cleanUpCache(topVer); }
/** * Gets affinity ready future, a future that will be completed after affinity with given topology * version is calculated. * * @param topVer Topology version to affinity for. * @return Affinity ready future. */ public GridFuture<Long> affinityReadyFuture(long topVer) { assert !cctx.isLocal(); return aff.readyFuture(topVer); }