@Override public boolean isLocal(Object key) { return this.topology.getDistribution(key).isPrimary(); } }
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(); }
@Override public boolean shouldModifyIndexes(FlagAffectedCommand command, InvocationContext ctx, DistributionManager distributionManager, RpcManager rpcManager, Object key) { if (key == null || distributionManager == null) { return true; } DistributionInfo info = distributionManager.getCacheTopology().getDistribution(key); // If this is a backup node we should modify the entry in the remote context return info.isPrimary() || info.isWriteOwner() && (ctx.isInTxScope() || !ctx.isOriginLocal() || command != null && command.hasAnyFlag(FlagBitSets.PUT_FOR_STATE_TRANSFER)); } },
private <K, V> CacheHelper<K, V> create(List<Cache<K, V>> cacheList) { CacheHelper<K, V> cacheHelper = new CacheHelper<>(); for (Cache<K, V> cache : cacheList) { ClusteringDependentLogic clusteringDependentLogic = extractComponent(cache, ClusteringDependentLogic.class); DistributionInfo distributionInfo = clusteringDependentLogic.getCacheTopology().getDistribution(key); log.debugf("owners for key %s are %s", key, distributionInfo.writeOwners()); if (distributionInfo.isPrimary()) { log.debug("Cache " + address(cache) + " is the primary owner"); assertTrue(cacheHelper.addCache(Ownership.PRIMARY, cache)); } else if (distributionInfo.isWriteBackup()) { log.debug("Cache " + address(cache) + " is the backup owner"); assertTrue(cacheHelper.addCache(Ownership.BACKUP, cache)); } else { log.debug("Cache " + address(cache) + " is the non owner"); assertTrue(cacheHelper.addCache(Ownership.NON_OWNER, cache) || cacheMode.isScattered()); } } return cacheHelper; }
if (di.writeOwners().contains(ownerAddress)) { return valueToTest; if (di.primary().equals(ownerAddress)) { return valueToTest; if (di.writeOwners().contains(ownerAddress)) { return valueToTest;
private Optional<Cache<Object, Person>> findCache(Ownership ownership, Object key) { List<Cache<Object, Person>> caches = caches(); ClusteringDependentLogic cdl = cache1.getAdvancedCache().getComponentRegistry().getComponent(ClusteringDependentLogic.class); DistributionInfo distribution = cdl.getCacheTopology().getDistribution(key); Predicate<Cache<?, ?>> predicate = null; switch (ownership) { case PRIMARY: predicate = c -> c.getAdvancedCache().getRpcManager().getAddress().equals(distribution.primary()); break; case BACKUP: predicate = c -> distribution.writeBackups().contains(c.getAdvancedCache().getRpcManager().getAddress()); break; case NON_OWNER: predicate = c -> !distribution.writeOwners().contains(c.getAdvancedCache().getRpcManager().getAddress()); } return caches.stream().filter(predicate).findFirst(); }
private void doTest(Operation operation, boolean init) throws Exception { final MagicKey key = new MagicKey(cache(1), cache(2)); if (init) { cache(0).put(key, "v1"); assertInAllCache(key, "v1"); } BlockingLocalTopologyManager bltm0 = replaceTopologyManagerDefaultCache(manager(0)); BlockingLocalTopologyManager bltm1 = replaceTopologyManagerDefaultCache(manager(1)); killMember(2, null, false); //CH_UPDATE + REBALANCE_START + CH_UPDATE(blocked) bltm0.expectRebalanceStartAfterLeave().unblock(); bltm1.expectRebalanceStartAfterLeave().unblock(); bltm0.expectPhaseConfirmation().unblock(); bltm1.expectPhaseConfirmation().unblock(); //check if we are in the correct state LocalizedCacheTopology cacheTopology = TestingUtil.extractComponent(cache(0), DistributionManager.class).getCacheTopology(); DistributionInfo distributionInfo = cacheTopology.getDistribution(key); assertFalse(distributionInfo.isReadOwner()); assertTrue(distributionInfo.isWriteOwner()); assertEquals(address(1), distributionInfo.primary()); operation.put(key, "v2", cache(1)); BlockingLocalTopologyManager.finishRebalance(CacheTopology.Phase.READ_ALL_WRITE_ALL, bltm0, bltm1); waitForClusterToForm(); //let the cluster finish the state transfer assertInAllCache(key, "v2"); }
private void assertOwnershipConsensus(String key) { List l1 = getCacheTopology(c1).getDistribution(key).writeOwners(); List l2 = getCacheTopology(c2).getDistribution(key).writeOwners(); List l3 = getCacheTopology(c3).getDistribution(key).writeOwners(); List l4 = getCacheTopology(c4).getDistribution(key).writeOwners(); assertEquals("L1 "+l1+" and L2 "+l2+" don't agree.", l1, l2); assertEquals("L2 "+l2+" and L3 "+l3+" don't agree.", l2, l3); assertEquals("L3 "+l3+" and L4 "+l4+" don't agree.", l3, l4); }
dm0.getCacheTopology().getDistribution("k1").readOwners()); assertNull(dm0.getCacheTopology().getPendingCH()); dm0.getCacheTopology().getPendingCH().locateOwnersForSegment(0)); assertEquals(Arrays.asList(address(1), address(2), address(3), address(0)), dm0.getCacheTopology().getDistribution("k1").writeOwners());
@Override public boolean shouldModifyIndexes(FlagAffectedCommand command, InvocationContext ctx, DistributionManager distributionManager, RpcManager rpcManager, Object key) { return command instanceof ClearCommand || !(command.hasAnyFlag(FlagBitSets.SKIP_INDEXING)) && (distributionManager == null || distributionManager.getCacheTopology().getDistribution(key).isWriteOwner()); } },
private void testBeforeTopology(BiFunction<FunctionalMap.ReadWriteMap<String, Integer>, String, Integer> op, int expectedIncrement) throws Exception { cache(0).put("key", 1); // Blocking on receiver side. We cannot block the StateResponseCommand on the server side since // the InternalCacheEntries in its state are the same instances of data stored in DataContainer // - therefore when the command is blocked on sender the command itself would be mutated by applying // the transaction below. BlockingStateConsumer bsc2 = TestingUtil.wrapComponent(cache(2), StateConsumer.class, BlockingStateConsumer::new); tm(2).begin(); FunctionalMap.ReadWriteMap<String, Integer> rw = ReadWriteMapImpl.create( FunctionalMapImpl.create(this.<String, Integer>cache(2).getAdvancedCache())); assertEquals(new Integer(1), op.apply(rw, "key")); Transaction tx = tm(2).suspend(); chf.setOwnerIndexes(0, 2); Future<?> future = fork(() -> { TestResourceTracker.testThreadStarted(this); addClusterEnabledCacheManager(cb).getCache(); }); bsc2.await(); DistributionInfo distributionInfo = cache(2).getAdvancedCache().getDistributionManager().getCacheTopology().getDistribution("key"); assertFalse(distributionInfo.isReadOwner()); assertTrue(distributionInfo.isWriteBackup()); tm(2).resume(tx); tm(2).commit(); bsc2.unblock(); future.get(10, TimeUnit.SECONDS); InternalCacheEntry<Object, Object> ice = cache(2).getAdvancedCache().getDataContainer().get("key"); assertEquals("Current ICE: " + ice, 1 + expectedIncrement, ice.getValue()); }
@Override public TestCache create(String groupName, List<Cache<GroupKey, String>> cacheList) { Cache<GroupKey, String> primaryOwner = null; AdvancedCache<GroupKey, String> backupOwner = null; for (Cache<GroupKey, String> cache : cacheList) { DistributionManager distributionManager = TestingUtil.extractComponent(cache, DistributionManager.class); DistributionInfo distributionInfo = distributionManager.getCacheTopology().getDistribution(groupName); if (primaryOwner == null && distributionInfo.isPrimary()) { primaryOwner = cache; } else if (backupOwner == null && distributionInfo.isWriteOwner()) { backupOwner = cache.getAdvancedCache(); } if (primaryOwner != null && backupOwner != null) { return new TestCache(primaryOwner, backupOwner); } } throw new IllegalStateException("didn't find a cache... should never happen!"); } },
protected void assertMapsToAddress(Object o, Address addr) { LocalizedCacheTopology cacheTopology = caches.get(0).getAdvancedCache().getDistributionManager().getCacheTopology(); List<Address> addresses = cacheTopology.getDistribution(o).writeOwners(); assertEquals("Expected key " + o + " to map to address " + addr + ". List of addresses is" + addresses, true, addresses.contains(addr)); }
private void testAfterTopology(BiFunction<FunctionalMap.ReadWriteMap<String, Integer>, String, Integer> op, int expectedIncrement) throws Exception { cache(0).put("key", 1); // Blocking on receiver side. We cannot block the StateResponseCommand on the server side since // the InternalCacheEntries in its state are the same instances of data stored in DataContainer // - therefore when the command is blocked on sender the command itself would be mutated by applying // the transaction below. BlockingStateConsumer bsc2 = TestingUtil.wrapComponent(cache(2), StateConsumer.class, BlockingStateConsumer::new); chf.setOwnerIndexes(0, 2); Future<?> future = fork(() -> { TestResourceTracker.testThreadStarted(this); addClusterEnabledCacheManager(cb).getCache(); }); bsc2.await(); DistributionInfo distributionInfo = cache(2).getAdvancedCache().getDistributionManager().getCacheTopology().getDistribution("key"); assertFalse(distributionInfo.isReadOwner()); assertTrue(distributionInfo.isWriteBackup()); withTx(tm(2), () -> { FunctionalMap.ReadWriteMap<String, Integer> rw = ReadWriteMapImpl.create( FunctionalMapImpl.create(this.<String, Integer>cache(2).getAdvancedCache())); assertEquals(new Integer(1), op.apply(rw, "key")); return null; }); bsc2.unblock(); future.get(10, TimeUnit.SECONDS); InternalCacheEntry<Object, Object> ice = cache(2).getAdvancedCache().getDataContainer().get("key"); assertEquals("Current ICE: " + ice, 1 + expectedIncrement, ice.getValue()); }
@Override public boolean isLocal(Object key) { return this.topology.getDistribution(key).isPrimary(); } }
@Override public String locate(String sessionId) { DistributionManager dist = this.cache.getAdvancedCache().getDistributionManager(); Address address = (dist != null) && !this.cache.getCacheConfiguration().clustering().cacheMode().isScattered() ? dist.getCacheTopology().getDistribution(new Key<>(sessionId)).primary() : this.cache.getCacheManager().getAddress(); Node node = (address != null) ? this.factory.createNode(address) : null; Map.Entry<String, Void> entry = (node != null) ? this.registry.getEntry(node) : null; if (entry == null) { entry = this.registry.getEntry(this.registry.getGroup().getLocalMember()); } return (entry != null) ? entry.getKey() : null; } }
@Override public TestCache create(String groupName, List<Cache<GroupKey, String>> cacheList) { Cache<GroupKey, String> primaryOwner = null; AdvancedCache<GroupKey, String> nonOwner = null; for (Cache<GroupKey, String> cache : cacheList) { DistributionManager distributionManager = TestingUtil.extractComponent(cache, DistributionManager.class); DistributionInfo distributionInfo = distributionManager.getCacheTopology().getDistribution(groupName); if (primaryOwner == null && distributionInfo.isPrimary()) { primaryOwner = cache; } else if (nonOwner == null && !distributionInfo.isWriteOwner()) { nonOwner = cache.getAdvancedCache(); } if (primaryOwner != null && nonOwner != null) { return new TestCache(primaryOwner, nonOwner); } } throw new IllegalStateException("didn't find a cache... should never happen!"); } };
@Test (dependsOnMethods = "testServersDropped") public void testCollocatedKey() { LocalizedCacheTopology cacheTopology = manager(0).getCache(cacheName).getAdvancedCache().getDistributionManager().getCacheTopology(); for (int i = 0; i < 1000; i++) { List<Address> addresses = cacheTopology.getDistribution(i).writeOwners(); Object collocatedKey = keyAffinityService.getCollocatedKey(i); List<Address> addressList = cacheTopology.getDistribution(collocatedKey).writeOwners(); assertEquals(addresses, addressList); } }
@Override public boolean accept(Object key, Object value, Metadata metadata) { return clusteringDependentLogic.getCacheTopology().getDistribution(key).isPrimary(); } }
Node locatePrimaryOwner(I id) { DistributionManager dist = this.cache.getAdvancedCache().getDistributionManager(); Address address = (dist != null) && !this.cache.getCacheConfiguration().clustering().cacheMode().isScattered() ? dist.getCacheTopology().getDistribution(new Key<>(id)).primary() : null; Node member = (address != null) ? this.nodeFactory.createNode(address) : null; return (member != null) ? member : this.registry.getGroup().getLocalMember(); }