private void getRemoteVersion() { VersionedValue<Long> versioned = remoteCache.getVersioned( id ); if ( versioned == null ) { throw log.criticalDataLossDetected(); } lastKnownVersion = versioned.getVersion(); lastKnownRemoteValue = (Long) versioned.getValue(); }
public void testHotRodReplace() throws Exception { final String key = "5"; final byte[] value1 = "v1".getBytes(); final byte[] value2 = "v2".getBytes(); // 1. Put with HotRod RemoteCache<Object, Object> remote = cacheFactory1.getHotRodCache(); assertNull(remote.withFlags(Flag.FORCE_RETURN_VALUE).put(key, value1)); // 2. Replace with HotRod VersionedValue versioned = cacheFactory1.getHotRodCache().getVersioned(key); assertTrue(cacheFactory1.getHotRodCache().replaceWithVersion(key, value2, versioned.getVersion())); }
protected void createRemoteEntityInCache(K key, long eventVersion) { VersionedValue<SessionEntityWrapper<V>> remoteSessionVersioned = remoteCache.getWithMetadata(key); // Maybe can happen under some circumstances that remoteCache doesn't yet contain the value sent in the event (maybe just theoretically...) if (remoteSessionVersioned == null || remoteSessionVersioned.getValue() == null) { logger.debugf("Entity '%s' not present in remoteCache. Ignoring create", key.toString()); return; } V remoteSession = remoteSessionVersioned.getValue().getEntity(); SessionEntityWrapper<V> newWrapper = new SessionEntityWrapper<>(remoteSession); logger.debugf("Read session entity wrapper from the remote cache: %s", remoteSession.toString()); // Using putIfAbsent. Theoretic possibility that entity was already put to cache by someone else cache.getAdvancedCache().withFlags(Flag.SKIP_CACHE_STORE, Flag.SKIP_CACHE_LOAD, Flag.IGNORE_RETURN_VALUES) .putIfAbsent(key, newWrapper); }
public void testHotRodRemove() throws Exception { final byte[] key = "7".getBytes(); final String value = "v1"; // 1. Put with HotRod RemoteCache<Object, Object> remote = cacheFactory1.getHotRodCache(); assertNull(remote.withFlags(Flag.FORCE_RETURN_VALUE).put(key, value)); // 2. Removed with HotRod VersionedValue versioned = cacheFactory1.getHotRodCache().getVersioned(key); assertTrue(cacheFactory1.getHotRodCache().removeWithVersion(key, versioned.getVersion())); }
@Override public boolean replace(K key, V oldValue, V newValue) { VersionedValue<V> versioned = delegate.getWithMetadata(key); if (versioned == null) { return false; } if (!oldValue.equals(versioned.getValue())) { return false; } return delegate.replaceWithVersion(key, newValue, versioned.getVersion()); }
public void testHotRodReplace() throws Exception { final byte[] key = "5".getBytes(); final String value1 = "v1"; final String value2 = "v2"; // 1. Put with HotRod RemoteCache<Object, Object> remote = cacheFactory1.getHotRodCache(); assertNull(remote.withFlags(Flag.FORCE_RETURN_VALUE).put(key, value1)); // 2. Replace with HotRod VersionedValue versioned = cacheFactory1.getHotRodCache().getVersioned(key); assertTrue(cacheFactory1.getHotRodCache().replaceWithVersion(key, value2, versioned.getVersion())); }
@Override public boolean remove(Object key, Object oldValue) { @SuppressWarnings("unchecked") K k = (K) key; VersionedValue<V> versioned = delegate.getWithMetadata(k); if (versioned == null) { return false; } if (!oldValue.equals(versioned.getValue())) { return false; } return delegate.removeWithVersion(k, versioned.getVersion()); }
public void testHotRodRemove() throws Exception { final String key = "7"; final byte[] value = "v1".getBytes(); // 1. Put with HotRod RemoteCache<Object, Object> remote = cacheFactory1.getHotRodCache(); assertNull(remote.withFlags(Flag.FORCE_RETURN_VALUE).put(key, value)); // 2. Remove with HotRod VersionedValue versioned = cacheFactory1.getHotRodCache().getVersioned(key); assertTrue(cacheFactory1.getHotRodCache().removeWithVersion(key, versioned.getVersion())); }
if (remoteSessionVersioned == null || remoteSessionVersioned.getValue() == null) { logger.debugf("Entity '%s' not present in remoteCache. Ignoring replace", key.toString()); if (remoteSessionVersioned.getVersion() < eventVersion) { try { logger.debugf("Got replace remote entity event prematurely for entity '%s', will try again. Event version: %d, got: %d", key.toString(), eventVersion, remoteSessionVersioned == null ? -1 : remoteSessionVersioned.getVersion()); Thread.sleep(new Random().nextInt(sleepInterval)); // using exponential backoff continue; SessionEntity remoteSession = remoteSessionVersioned.getValue().getEntity();
public void testHotRodReplaceMemcachedCASTest() throws Exception { final String key1 = "6"; // 1. Put with Memcached Future<Boolean> f = cacheFactory.getMemcachedClient().set(key1, 0, "v1"); assertTrue(f.get(60, TimeUnit.SECONDS)); CASValue oldValue = cacheFactory.getMemcachedClient().gets(key1); // 2. Replace with Hot Rod VersionedValue versioned = cacheFactory.getHotRodCache().getVersioned(key1); assertTrue(cacheFactory.getHotRodCache().replaceWithVersion(key1, "v2", versioned.getVersion())); // 4. Get with Memcached and verify value/CAS CASValue newValue = cacheFactory.getMemcachedClient().gets(key1); assertEquals("v2", newValue.getValue()); assertTrue("The version (CAS) should have changed", oldValue.getCas() != newValue.getCas()); }
SessionEntityWrapper<V> sessionWrapper = versioned.getValue(); final V session = sessionWrapper.getEntity(); versioned.getVersion(), session); replaced = remoteCache.replaceWithVersion(key, SessionEntityWrapper.forTransport(session), versioned.getVersion(), lifespanMs, TimeUnit.MILLISECONDS, maxIdleMs, TimeUnit.MILLISECONDS); logger.debugf("%s: Failed to replace entity '%s' version %d. Will retry again", logTopologyData(topology, replaceIteration), key, versioned.getVersion()); } else { if (logger.isTraceEnabled()) { logger.tracef("%s: Replaced entity version %d in remote cache: %s", logTopologyData(topology, replaceIteration), versioned.getVersion(), session);
public void testEventReceiveConditional() { EventLogListener<Integer> l = new EventLogListener<>(cacheFactory.getHotRodCache()); withClientListener(l, remote -> { l.expectNoEvents(); // Put if absent remote.putIfAbsent(1, "one"); l.expectOnlyCreatedEvent(1); remote.putIfAbsent(1, "again"); l.expectNoEvents(); // Replace remote.replace(1, "newone"); l.expectOnlyModifiedEvent(1); // Replace with version remote.replaceWithVersion(1, "one", 0); l.expectNoEvents(); VersionedValue<?> versioned = remote.getVersioned(1); remote.replaceWithVersion(1, "one", versioned.getVersion()); l.expectOnlyModifiedEvent(1); // Remove with version remote.removeWithVersion(1, 0); l.expectNoEvents(); versioned = remote.getVersioned(1); remote.removeWithVersion(1, versioned.getVersion()); l.expectOnlyRemovedEvent(1); }); }
public static void testReplaceWithVersionAsync(RemoteCache<String, String> remoteCache) throws Exception { remoteCache.clear(); remoteCache.put(KEY1, VALUE1); VersionedValue<String> v = remoteCache.getVersioned(KEY1); assertEquals(VALUE1, v.getValue()); long ver = v.getVersion(); CompletableFuture<Boolean> f = remoteCache.replaceWithVersionAsync(KEY1, VALUE2, ver); f.get(ASYNC_TIMEOUT, TimeUnit.SECONDS); v = remoteCache.getVersioned(KEY1); assertEquals(VALUE2, v.getValue()); assertTrue(ver != v.getVersion()); }
public void testHotRodRemoveConditionalEmbeddedGet() { final Integer key = 12; Cache<Integer, String> embedded = getEmbeddedCache(); RemoteCache<Integer, String> remote = cacheFactory.getHotRodCache(); assertEquals(null, remote.withFlags(Flag.FORCE_RETURN_VALUE).put(key, "v1")); VersionedValue<String> versioned = remote.getVersioned(key); assertFalse(remote.withFlags(Flag.FORCE_RETURN_VALUE).removeWithVersion(key, Long.MAX_VALUE)); assertTrue(remote.withFlags(Flag.FORCE_RETURN_VALUE).removeWithVersion(key, versioned.getVersion())); assertEquals(null, embedded.get(key)); }
public static void testReplaceWithVersioned(RemoteCache<String, String> remoteCache) { remoteCache.clear(); remoteCache.put(KEY1, VALUE1); VersionedValue<String> v = remoteCache.getVersioned(KEY1); assertEquals(VALUE1, v.getValue()); long ver = v.getVersion(); remoteCache.replaceWithVersion(KEY1, VALUE2, ver); v = remoteCache.getVersioned(KEY1); assertEquals(VALUE2, v.getValue()); assertTrue(ver != v.getVersion()); }
@Test public void testEventReceiveConditional() { final EventLogListener eventListener = new EventLogListener(); remoteCache.addClientListener(eventListener); try { expectNoEvents(eventListener); // Put if absent remoteCache.putIfAbsent(1, "one"); expectOnlyCreatedEvent(1, eventListener); remoteCache.putIfAbsent(1, "again"); expectNoEvents(eventListener); // Replace remoteCache.replace(1, "newone"); expectOnlyModifiedEvent(1, eventListener); // Replace with version remoteCache.replaceWithVersion(1, "one", 0); expectNoEvents(eventListener); VersionedValue<String> versioned = remoteCache.getVersioned(1); remoteCache.replaceWithVersion(1, "one", versioned.getVersion()); expectOnlyModifiedEvent(1, eventListener); // Remove with version remoteCache.removeWithVersion(1, 0); expectNoEvents(eventListener); versioned = remoteCache.getVersioned(1); remoteCache.removeWithVersion(1, versioned.getVersion()); expectOnlyRemovedEvent(1, eventListener); } finally { remoteCache.removeClientListener(eventListener); } }
public static void testRemoveWithVersionAsync(RemoteCache<String, String> remoteCache) throws Exception { remoteCache.clear(); remoteCache.put(KEY1, VALUE1); VersionedValue<String> v = remoteCache.getVersioned(KEY1); assertEquals(VALUE1, v.getValue()); long ver = v.getVersion(); CompletableFuture<Boolean> f = remoteCache.removeWithVersionAsync(KEY1, ver); f.get(ASYNC_TIMEOUT, TimeUnit.SECONDS); v = remoteCache.getVersioned(KEY1); if (v != null) assertTrue(ver != v.getVersion()); }
public void testEmbeddedHotRodReplaceMemcachedCASTest() throws Exception { final String key1 = "7"; // 1. Put with Memcached Future<Boolean> f = cacheFactory.getMemcachedClient().set(key1, 0, "v1"); assertTrue(f.get(60, TimeUnit.SECONDS)); CASValue oldValue = cacheFactory.getMemcachedClient().gets(key1); // 2. Replace with Hot Rod VersionedValue versioned = cacheFactory.getHotRodCache().getVersioned(key1); assertTrue(cacheFactory.getHotRodCache().replaceWithVersion(key1, "v2", versioned.getVersion())); // 3. Replace with Embedded assertTrue(cacheFactory.getEmbeddedCache().replace(key1, "v2", "v3")); // 4. Get with Memcached and verify value/CAS CASValue newValue = cacheFactory.getMemcachedClient().gets(key1); assertEquals("v3", newValue.getValue()); assertTrue("The version (CAS) should have changed", oldValue.getCas() != newValue.getCas()); }
private static Tuple getTuple(InfinispanRemoteDatastoreProvider provider, EntityKey key) { final String cacheName = cacheName( key ); ProtoStreamMappingAdapter mapper = provider.getDataMapperForCache( cacheName ); ProtostreamId idBuffer = mapper.createIdPayload( key.getColumnNames(), key.getColumnValues() ); VersionedValue<ProtostreamPayload> v = mapper.withinCacheEncodingContext( c -> c.getVersioned( idBuffer ) ); if ( v == null ) { return null; } ProtostreamPayload payload = v.getValue(); if ( payload == null ) { return null; } long version = v.getVersion(); VersionedTuple versionedTuple = payload.toVersionedTuple( SnapshotType.UPDATE ); versionedTuple.setVersion( version ); return versionedTuple; }
@Test public void testReplaceWithVersion() { assertNull(remoteCache.replace("aKey", "aValue")); remoteCache.put("aKey", "aValue"); VersionedValue valueBinary = remoteCache.getVersioned("aKey"); // replacement should take place (and so return true) assertTrue(remoteCache.replaceWithVersion("aKey", "aNewValue", valueBinary.getVersion())); // version should have changed; value should have changed VersionedValue entry2 = remoteCache.getVersioned("aKey"); assertNotEquals(entry2.getVersion(), valueBinary.getVersion()); assertEquals(entry2.getValue(), "aNewValue"); // replacement should not take place because we have changed the value assertTrue(!remoteCache.replaceWithVersion("aKey", "aNewValue", valueBinary.getVersion())); }