private void populateKeys(Cache primaryOwner) { for (int i = 0; i < keys.length; ++i) { keys[i] = new MagicKey("k" + i, primaryOwner); } } }
public MagicKey(String name, Cache<?, ?> primaryOwner) { this.name = name; Address primaryAddress = addressOf(primaryOwner); this.address = primaryAddress.toString(); LocalizedCacheTopology cacheTopology = primaryOwner.getAdvancedCache().getDistributionManager().getCacheTopology(); ConsistentHash ch = cacheTopology.getWriteConsistentHash(); int segment = findSegment(ch.getNumSegments(), s -> primaryAddress.equals(ch.locatePrimaryOwnerForSegment(s))); if (segment < 0) { throw new IllegalStateException("Could not find any segment owned by " + primaryOwner + ", primary segments: " + segments(primaryOwner)); } this.segment = segment; hashcode = getHashCodeForSegment(cacheTopology, segment); unique = counter.getAndIncrement(); }
public void testGetEntry() throws Exception { Cache<MagicKey, String> cache = cache(0); MagicKey localKey = new MagicKey(cache(0)); MagicKey remoteKey = new MagicKey(cache(1)); cache.put(localKey, localKey.toString()); cache.put(remoteKey, remoteKey.toString()); assertNotNull(cache.getAdvancedCache().getCacheEntry(localKey)); assertNotNull(cache.getAdvancedCache().getCacheEntry(remoteKey)); }
public void testLifespanExpiredOnBoth() { MagicKey key = createKey(cache0, cache1); cache0.put(key, key.toString(), 10, TimeUnit.MINUTES); assertEquals(key.toString(), cache0.get(key)); assertEquals(key.toString(), cache1.get(key)); // Now we expire on cache0, it should still exist on cache1 ts0.advance(TimeUnit.MINUTES.toMillis(10) + 1); ts1.advance(TimeUnit.MINUTES.toMillis(10) + 1); // Both should be null assertNull(cache0.get(key)); assertNull(cache1.get(key)); }
public void testMaxIdleExpiredOnBoth() { MagicKey key = createKey(cache0, cache1); cache0.put(key, key.toString(), -1, null, 10, TimeUnit.MINUTES); assertEquals(key.toString(), cache0.get(key)); assertEquals(key.toString(), cache1.get(key)); // Now we expire on cache0, it should still exist on cache1 incrementAllTimeServices(11, TimeUnit.MINUTES); // Both should be null assertNull(cache0.get(key)); assertNull(cache1.get(key)); }
@Override protected Object getKeyTiedToCache(Cache<?, ?> cache) { return new MagicKey(cache); } }
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(); }
private void testMaxIdleExpireExpireIteration(boolean expireOnPrimary, boolean iterateOnPrimary) { // Cache0 is always the primary and cache1 is backup MagicKey key = createKey(cache0, cache1); cache1.put(key, key.toString(), -1, null, 10, TimeUnit.SECONDS); ControlledTimeService expiredTimeService; if (expireOnPrimary) { expiredTimeService = ts0; } else { expiredTimeService = ts1; } expiredTimeService.advance(TimeUnit.SECONDS.toMillis(11)); Cache<Object, String> cacheToIterate; if (iterateOnPrimary) { cacheToIterate = cache0; } else { cacheToIterate = cache1; } // Iteration always works with max idle expired entries try (CloseableIterator<Map.Entry<Object, String>> iterator = cacheToIterate.entrySet().iterator()) { assertTrue(iterator.hasNext()); Map.Entry<Object, String> entry = iterator.next(); assertEquals(key, entry.getKey()); assertEquals(key.toString(), entry.getValue()); } }
private void populateKeys(Cache primaryOwner) { for (int i = 0; i < KEYS.length; ++i) { KEYS[i] = new MagicKey("k" + i, primaryOwner); } }
private void testLifespanExpiredEntryRetrieval(Cache<Object, String> primaryOwner, Cache<Object, String> backupOwner, ControlledTimeService timeService, boolean expireOnPrimary) throws Exception { MagicKey key = createKey(primaryOwner, backupOwner); primaryOwner.put(key, key.toString(), 10, TimeUnit.MILLISECONDS); assertEquals(key.toString(), primaryOwner.get(key)); assertEquals(key.toString(), backupOwner.get(key)); other = otherCache; assertEquals(key.toString(), other.get(key)); assertEquals(key.toString(), expiredValue); } else { assertNull(expiredValue);
protected MagicKey getKeyForCache(Cache<?, ?> cache) { return new MagicKey(cache); }
/** * This test verifies that an entry is refreshed properly when the originator thinks the entry is expired * but another node accessed recently, but not same timestamp */ public void testMaxIdleAccessSuspectedExpiredEntryRefreshesProperly() { MagicKey key = createKey(cache0, cache1); String value = key.toString(); cache0.put(key, value, -1, null, 10, TimeUnit.SECONDS); // Now proceed half way in the max idle period before we access it on backup node incrementAllTimeServices(5, TimeUnit.SECONDS); // Access it on the backup to update the last access time (primary still has old access time only) assertEquals(value, cache1.get(key)); // Note now the entry would have been expired, if not for access above incrementAllTimeServices(5, TimeUnit.SECONDS); assertEquals(value, cache0.get(key)); // Now we try to access just before it expires, but it still should be available incrementAllTimeServices(9, TimeUnit.SECONDS); assertEquals(value, cache0.get(key)); }
private List<MagicKey> init() { List<MagicKey> keys = new ArrayList<>(Arrays.asList( new MagicKey("k1", c1), new MagicKey("k2", c2), new MagicKey("k3", c1), new MagicKey("k4", c2) )); int i = 0; for (Cache<Object, String> c : caches) c.put(keys.get(i++), "v" + i); log.infof("Initialized with keys %s", keys); return keys; }