/** * @param part Partition. * @param topVer Topology version. * @return Primary node for given key. */ @Nullable public GridNode primary(int part, long topVer) { Collection<GridNode> nodes = nodes(part, topVer); if (nodes.isEmpty()) return null; return nodes.iterator().next(); }
/** * @param part Partition. * @param topVer Topology version. * @return Backup nodes. */ public Collection<GridNode> backups(int part, long topVer) { Collection<GridNode> nodes = nodes(part, topVer); assert !F.isEmpty(nodes); if (nodes.size() <= 1) return Collections.emptyList(); return F.view(nodes, F.notEqualTo(nodes.iterator().next())); }
/** {@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() + ']'); } }