/** {@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() + ']'); } }
/** * 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); }