public void testDistributionFromSecondaryOwner() throws Exception { Object key = "testDistributionFromSecondaryOwner"; doTestDistribution(key, cacheManagers.stream() .map(cm -> cm.<Object, Integer>getCache(DIST).getAdvancedCache()) // owner... .filter(cache -> cache.getDistributionManager().getCacheTopology().getDistribution(key).isWriteBackup()) .findAny() .get()); }
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()); }
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()); }
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; }