public MagicKey(String name, Cache<?, ?> primaryOwner, Cache<?, ?>... backupOwners) {
this.name = name;
Address primaryAddress = addressOf(primaryOwner);
this.address = primaryAddress.toString();
LocalizedCacheTopology cacheTopology = primaryOwner.getAdvancedCache().getDistributionManager().getCacheTopology();
ConsistentHash ch = cacheTopology.getWriteConsistentHash();
segment = findSegment(ch.getNumSegments(), s -> {
List<Address> owners = ch.locateOwnersForSegment(s);
if (!primaryAddress.equals(owners.get(0))) return false;
for (Cache<?, ?> backup : backupOwners) {
if (!owners.contains(addressOf(backup))) return false;
}
return true;
});
if (segment < 0) {
throw new IllegalStateException("Could not find any segment owned by " + primaryOwner + ", "
+ Arrays.toString(backupOwners) + ", primary segments: " + segments(primaryOwner)
+ ", backup segments: " + Stream.of(backupOwners).collect(Collectors.toMap(Function.identity(), this::segments)));
}
hashcode = getHashCodeForSegment(cacheTopology, segment);
unique = counter.getAndIncrement();
}