public CacheLocality(Cache<?, ?> cache) { DistributionManager dist = cache.getAdvancedCache().getDistributionManager(); this.locality = (dist != null) ? new ConsistentHashLocality(dist.getCacheTopology()) : new SimpleLocality(true); }
assertEquals(Arrays.asList(address(0), address(1), address(2)), advancedCache(0).getDistributionManager().locate(key)); Cache<Object, Object> primaryOwnerCache = cache(0); final Cache<Object, Object> newBackupOwnerCache = cache(3); int currentTopologyId = primaryOwnerCache.getAdvancedCache().getDistributionManager().getCacheTopology().getTopologyId(); Future<Object> secondCommitFuture = fork(() -> {
eventuallyEquals(address(2), () -> advancedCache(0).getDistributionManager().getReadConsistentHash().locatePrimaryOwnerForSegment(1)); dm0.getCacheTopology().getDistribution(k1).readOwners()); cache(0).put(k1, "v1"); MagicKey k2 = new MagicKey("k2", cache(2)); assertEquals(Arrays.asList(address(2), address(1), address(3)), dm0.getCacheTopology().getDistribution(k2).readOwners()); cache(0).put(k2, "v2"); assertNotNull(dm0.getCacheTopology().getPendingCH()); log.debugf("Blocked state response from %s, killing %s", firstResponseSender.get(), manager(nodeToKill)); cache(nodeToKill).stop(); eventuallyEquals(3, () -> dm0.getCacheTopology().getMembers().size()); assertTrue(dm0.getCacheTopology().isReadOwner(k1)); assertTrue(dm0.getCacheTopology().isReadOwner(k2)); assertEquals("v1", cache(0).get(k1)); assertEquals("v2", cache(0).get(k2));
private Future<Void> simulateTopologyUpdate(Cache<Object, Object> cache) { StateTransferLock stl4 = TestingUtil.extractComponent(cache, StateTransferLock.class); DistributionManager dm4 = cache.getAdvancedCache().getDistributionManager(); LocalizedCacheTopology cacheTopology = dm4.getCacheTopology(); int newTopologyId = cacheTopology.getTopologyId() + 1; CacheTopology newTopology = new CacheTopology(newTopologyId, cacheTopology.getRebalanceId(), cacheTopology.getCurrentCH(), cacheTopology.getPendingCH(), cacheTopology.getUnionCH(), cacheTopology.getPhase(), cacheTopology.getActualMembers(), cacheTopology.getMembersPersistentUUIDs()); dm4.setCacheTopology(newTopology); return fork(() -> stl4.notifyTransactionDataReceived(newTopologyId)); } }
private ShardDistribution getShardDistribution() { if (shardDistribution == null) { shardDistribution = this.buildShardDistribution( distributionManager == null ? null : distributionManager.getWriteConsistentHash()); } return shardDistribution; }
public static boolean hasOwners(Object key, Cache<?, ?> primaryOwner, Cache<?, ?>... backupOwners) { DistributionManager dm = primaryOwner.getAdvancedCache().getDistributionManager(); List<Address> ownerAddresses = dm.locate(key); if (!addressOf(primaryOwner).equals(ownerAddresses.get(0))) return false; for (int i = 0; i < backupOwners.length; i++) { if (!ownerAddresses.contains(addressOf(backupOwners[i]))) return false; } return true; }
private ConsistentHash getConsistentHash(Cache cache) { DistributionManager dm = cache.getAdvancedCache().getDistributionManager(); return (dm != null) ? dm.getReadConsistentHash() : null; }
if ((manager != null) && !manager.isRehashInProgress()) List<Address> addresses = manager.locate(sessionId);
return; System.out.println("Routing Table for the Hash:" + dMgr.getConsistentHash() .getRoutingTableAsString()); System.out.println("Get Members:" + dMgr.getConsistentHash() .getMembers());
private ShardDistribution getShardDistribution() { if (shardDistribution == null) { shardDistribution = this.buildShardDistribution( distributionManager == null ? null : distributionManager.getWriteConsistentHash()); } return shardDistribution; }
public static boolean isOwner(Cache<?, ?> c, Object key) { DistributionManager dm = c.getAdvancedCache().getDistributionManager(); List<Address> ownerAddresses = dm.locate(key); return ownerAddresses.contains(addressOf(c)); }
<S> CompletionRehashPublisherDecorator<S> createDecorator(ConsistentHash ch, Set<Integer> segmentsForOwner, Set<Integer> primarySegmentsForOwner, Consumer<Supplier<PrimitiveIterator.OfInt>> internalListener, Consumer<Object> entryConsumer) { Address address = Mockito.mock(Address.class); if (segmentsForOwner != null) { when(ch.getSegmentsForOwner(eq(address))).thenReturn(segmentsForOwner); } if (primarySegmentsForOwner != null) { when(ch.getPrimarySegmentsForOwner(eq(address))).thenReturn(primarySegmentsForOwner); } DistributionManager dm = mock(DistributionManager.class); when(dm.getReadConsistentHash()).thenReturn(ch); return new CompletionRehashPublisherDecorator<>(AbstractCacheStream.IteratorOperation.NO_MAP, dm, address, // Just ignore early completed segments and lost ones internalListener, i -> {}, i -> {}, new WithinThreadExecutor(), entryConsumer, e -> ((Map.Entry) e).getKey()); }
private void checkAffinity(Cache<IndexScopedKey, ?> indexCache) { AdvancedCache<IndexScopedKey, ?> advancedCache = indexCache.getAdvancedCache(); DataContainer<IndexScopedKey, ?> dataContainer = advancedCache.getDataContainer(); ConsistentHash consistentHash = advancedCache.getDistributionManager().getConsistentHash(); Address address = advancedCache.getRpcManager().getAddress(); Set<Integer> ownedSegments = consistentHash.getSegmentsForOwner(address); dataContainer.forEach(entry -> { int segmentAffinity = entry.getKey().getAffinitySegmentId(); assertTrue(ownedSegments.contains(segmentAffinity)); }); }
Node locatePrimaryOwner(String sessionId) { DistributionManager dist = this.cache.getAdvancedCache().getDistributionManager(); Address address = (dist != null) ? dist.getCacheTopology().getDistribution(new Key<>(sessionId)).primary() : null; Node node = (address != null) ? this.memberFactory.createNode(address) : null; return (node != null) ? node : this.group.getLocalMember(); }
@TopologyChanged @SuppressWarnings("unused") public void onTopologyChange(TopologyChangedEvent<?, ?> tce) { if (initialized) { logger.debugf("Updating shard allocation"); this.shardDistribution = this.buildShardDistribution(distributionManager.getWriteConsistentHash()); } }
public void testRepeatedRemoteGet() { String key = "key"; List<Address> owners = cache(0).getAdvancedCache().getDistributionManager().locate(key); Cache<Object, Object> nonOwnerCache = caches().stream() .filter(c -> !owners.contains(address(c))) .findAny() .orElse(null); cache(0).put(key, "value"); for (int i = 0; i < 50000; i++) { assertEquals("value", nonOwnerCache.get(key)); } } }
ConsistentHash ch = distManager.getReadConsistentHash(); int numSegments = ch.getNumSegments();
@Override public Membership getMembership() { if (this.isSingleton()) { return new SingletonMembership(this.getLocalMember()); } Transport transport = this.cache.getCacheManager().getTransport(); DistributionManager dist = this.cache.getAdvancedCache().getDistributionManager(); return (dist != null) ? new CacheMembership(transport.getAddress(), dist.getCacheTopology(), this) : new CacheMembership(transport, this); }
@TopologyChanged @SuppressWarnings("unused") public void onTopologyChange(TopologyChangedEvent<?, ?> tce) { if (initialized) { logger.debugf("Updating shard allocation"); this.shardDistribution = this.buildShardDistribution(distributionManager.getWriteConsistentHash()); } }
public void testInvalidation() throws Exception { assertEquals(Collections.singletonList(address(0)), advancedCache(0).getDistributionManager().locate(k0)); assertEquals(Collections.singletonList(address(0)), advancedCache(1).getDistributionManager().locate(k0)); advancedCache(1).put(k0, "k1"); assertTrue(advancedCache(1).getDataContainer().containsKey(k0)); assertTrue(advancedCache(0).getDataContainer().containsKey(k0)); tm(0).begin(); cache(0).put(k0, "v2"); tm(0).commit(); assertFalse(advancedCache(1).getDataContainer().containsKey(k0)); }