private Object[] getKeys(boolean isOwner, int num) { return IntStream.iterate(0, i -> i + 1) .mapToObj(i -> "key" + i) .filter(k -> cache(0, DIST).getAdvancedCache().getDistributionManager().getLocality(k).isLocal() == isOwner) .limit(num).toArray(Object[]::new); } }
protected Object getKey(boolean isOwner, String cacheName) { Object key; if (isOwner) { // this is simple: find a key that is local to the originating node key = getKeyForCache(0, cacheName); } else { // this is more complicated: we need a key that is *not* local to the originating node key = IntStream.iterate(0, i -> i + 1) .mapToObj(i -> "key" + i) .filter(k -> !cache(0, cacheName).getAdvancedCache().getDistributionManager().getLocality(k).isLocal()) .findAny() .get(); } return key; }
@Test(dataProvider = "owningModeAndReadMethod") public void testReadLoad(boolean isOwner, ReadMethod method) { Object key = getKey(isOwner, DIST); assertTrue(method.eval(key, ro, view -> { assertFalse(view.find().isPresent()); return true; })); // we can't add from read-only cache, so we put manually: cache(0, DIST).put(key, "value"); caches(DIST).forEach(cache -> assertEquals(cache.get(key), "value", getAddress(cache).toString())); caches(DIST).forEach(cache -> { if (cache.getAdvancedCache().getDistributionManager().getLocality(key).isLocal()) { assertTrue(cacheContainsKey(key, cache), getAddress(cache).toString()); } else { assertFalse(cacheContainsKey(key, cache), getAddress(cache).toString()); } }); assertEquals(method.eval(key, ro, view -> { assertTrue(view.find().isPresent()); assertEquals(view.get(), "value"); return "OK"; }), "OK"); }
@Test(dataProvider = "owningModeAndReadMethod") public void testReadLoad(boolean isSourceOwner, ReadMethod method) { Object key = getKey(isSourceOwner, DIST); List<Cache<Object, Object>> owners = caches(DIST).stream() .filter(cache -> cache.getAdvancedCache().getDistributionManager().getLocality(key).isLocal()) .collect(Collectors.toList()); assertTrue(method.eval(key, ro, view -> { assertFalse(view.find().isPresent()); return true; })); // we can't add from read-only cache, so we put manually: cache(0, DIST).put(key, "value"); caches(DIST).forEach(cache -> assertEquals(cache.get(key), "value", getAddress(cache).toString())); caches(DIST).forEach(cache -> cache.evict(key)); caches(DIST).forEach(cache -> assertFalse(cache.getAdvancedCache().getDataContainer().containsKey(key), getAddress(cache).toString())); owners.forEach(cache -> { Set<DummyInMemoryStore> stores = cache.getAdvancedCache().getComponentRegistry().getComponent(PersistenceManager.class).getStores(DummyInMemoryStore.class); DummyInMemoryStore store = stores.iterator().next(); assertTrue(store.contains(key), getAddress(cache).toString()); }); assertEquals(method.eval(key, ro, view -> { assertTrue(view.find().isPresent()); assertEquals(view.get(), "value"); return "OK"; }), "OK"); } }
if (cache.getAdvancedCache().getDistributionManager().getLocality(key).isLocal()) { assertTrue(cacheContainsKey(key, cache), getAddress(cache).toString()); } else {
.filter(cache -> cache.getAdvancedCache().getDistributionManager().getLocality(key).isLocal()) .collect(Collectors.toList());
if (!dm.getLocality(key).isLocal()) { if (!cacheMode.isScattered()) { assertFalse("Key '" + key + "' is not owned by node " + address(i) + " but it still appears there",
private void assertEvenDistribution() { for (int i = 0; i < getCacheManagers().size(); i++) { Cache<String, String> testCache = manager(i).getCache(testCacheName); DistributionManager dm = testCache.getAdvancedCache().getDistributionManager(); DataContainer dataContainer = testCache.getAdvancedCache().getDataContainer(); // Note there is stale data in the cache store that this owner no longer owns for (int j = 0; j < NUM_KEYS; j++) { String key = "key" + j; // each key must only occur once (numOwners is one) if (dm.getLocality(key).isLocal()) { assertTrue("Key '" + key + "' is owned by node " + address(i) + " but it doesn't appear there", dataContainer.containsKey(key)); } else { assertTrue("Key '" + key + "' is not owned by node " + address(i) + " but it still appears there", !dataContainer.containsKey(key)); } } } }