use of org.infinispan.distribution.MagicKey in project infinispan by infinispan.
the class EntryWrappingInterceptorDoesNotBlockTest method readWriteMany.
private Object readWriteMany(MagicKey key, int index) {
// make sure the other key is stable
MagicKey otherKey = new MagicKey("other", cache(0), cache(2));
FunctionalMap.ReadWriteMap<Object, Object> rwMap = ReadWriteMapImpl.create(FunctionalMapImpl.create(cache(0).getAdvancedCache()));
HashSet<MagicKey> keys = new HashSet<>(Arrays.asList(key, otherKey));
Traversable<Object> traversable = rwMap.evalMany(keys, view -> {
assertFalse(view.find().isPresent());
view.set("v" + index);
return "r" + index;
});
return traversable.findAny().orElseThrow(IllegalStateException::new);
}
use of org.infinispan.distribution.MagicKey in project infinispan by infinispan.
the class GetAllCacheNotFoundResponseTest method test.
public void test() throws InterruptedException, ExecutionException, TimeoutException {
ControlledRpcManager crm4 = ControlledRpcManager.replaceRpcManager(cache(4));
crm4.excludeCommands(StateResponseCommand.class);
MagicKey key1 = new MagicKey(cache(0), cache(1));
MagicKey key2 = new MagicKey(cache(0), cache(2));
MagicKey key3 = new MagicKey(cache(2), cache(3));
// We expect the targets of ClusteredGetAllCommands to be selected in a specific way and for that we need
// to iterate through the keys in certain order.
Set<Object> keys = new LinkedHashSet<>(Arrays.asList(key1, key2, key3));
Future<Map<Object, Object>> future = fork(() -> cache(4).getAdvancedCache().getAll(keys));
// Wait until the first two ClusteredGetAllCommands are sent
log.debugf("Expect first get all");
ControlledRpcManager.BlockedRequests round1 = crm4.expectCommands(ClusteredGetAllCommand.class, address(0), address(2));
// Provide fake responses for the 1st round
round1.skipSendAndReceive(address(0), CacheNotFoundResponse.INSTANCE);
round1.skipSendAndReceiveAsync(address(2), UnsureResponse.INSTANCE);
// Key retries are independent: will retry key1 on cache1, key2 on cache2, and key3 on cache3
log.debugf("Expect 1st retry");
ControlledRpcManager.BlockedRequests round2 = crm4.expectCommands(ClusteredGetAllCommand.class, address(1), address(2), address(3));
// Provide a response for the retry commands.
// We simulate that key1 is completely lost due to crashing nodes.
round2.skipSendAndReceive(address(1), CacheNotFoundResponse.INSTANCE);
round2.skipSendAndReceive(address(2), SuccessfulResponse.create(new InternalCacheValue[] { new ImmortalCacheValue("value2") }));
round2.skipSendAndReceiveAsync(address(3), SuccessfulResponse.create(new InternalCacheValue[] { new ImmortalCacheValue("value3") }));
// After all the owners are lost, we must wait for a new topology in case the key is still available
crm4.expectNoCommand(10, TimeUnit.MILLISECONDS);
log.debugf("Increment topology and expect 2nd retry");
Future<Void> topologyUpdateFuture = simulateTopologyUpdate(cache(4));
ControlledRpcManager.BlockedRequests round3 = crm4.expectCommands(ClusteredGetAllCommand.class, address(0));
// Provide a response for the 2nd retry
// Because we only simulated the loss of cache0, the primary owner is the same
round3.skipSendAndReceive(address(0), SuccessfulResponse.create(new InternalCacheValue[] { null }));
log.debugf("Expect final result");
topologyUpdateFuture.get(10, TimeUnit.SECONDS);
Map<Object, Object> values = future.get(10, TimeUnit.SECONDS);
// assertEquals is more verbose than assertNull in case of failure
assertEquals(null, values.get(key1));
assertEquals("value2", values.get(key2));
assertEquals("value3", values.get(key3));
}
use of org.infinispan.distribution.MagicKey in project infinispan by infinispan.
the class GetAllCommandNodeCrashTest method test.
public void test() throws Exception {
MagicKey key = new MagicKey(cache(0), cache(1));
cache(2).put(key, "value");
CheckPoint checkPoint = new CheckPoint();
ControlledRpcManager rpcManager = ControlledRpcManager.replaceRpcManager(cache(2));
rpcManager.excludeCommands(StateResponseCommand.class, StateTransferStartCommand.class, StateTransferCancelCommand.class);
StateConsumer stateConsumerSpy = spy(extractComponent(cache(2), StateConsumer.class));
doAnswer(invocation -> {
checkPoint.trigger("topology_update_blocked");
checkPoint.awaitStrict("topology_update_resumed", 10, TimeUnit.SECONDS);
return invocation.callRealMethod();
}).when(stateConsumerSpy).onTopologyUpdate(any(), anyBoolean());
replaceComponent(cache(2), StateConsumer.class, stateConsumerSpy, true);
Future<Map<Object, Object>> f = fork(() -> cache(2).getAdvancedCache().getAll(Collections.singleton(key)));
// Block the request before being sent
ControlledRpcManager.BlockedRequest<?> blockedGetAll = rpcManager.expectCommand(ClusteredGetAllCommand.class);
// it's necessary to stop whole cache manager, not just cache, because otherwise the exception would have
// suspect node defined
cacheManagers.get(0).stop();
checkPoint.awaitStrict("topology_update_blocked", 10, TimeUnit.SECONDS);
// Send the blocked request and wait for the CacheNotFoundResponse
blockedGetAll.send().receiveAll();
// The retry can't be sent at this point
rpcManager.expectNoCommand();
// Resume the topology update
checkPoint.trigger("topology_update_resumed");
// Now the command can be retried, and the operation can finish
rpcManager.expectCommand(ClusteredGetAllCommand.class).send().receiveAll();
try {
Map<Object, Object> map = f.get(10, TimeUnit.SECONDS);
assertNotNull(map);
assertFalse(map.isEmpty());
assertEquals("value", map.get(key));
} finally {
checkPoint.triggerForever("topology_update_resumed");
rpcManager.stopBlocking();
}
}
use of org.infinispan.distribution.MagicKey in project infinispan by infinispan.
the class ConflictManagerTest method testConflictsResolvedWithProvidedMergePolicy.
public void testConflictsResolvedWithProvidedMergePolicy() {
createCluster();
AdvancedCache<Object, Object> cache = getCache(0);
ConflictManager<Object, Object> cm = ConflictManagerFactory.get(cache);
MagicKey key = new MagicKey(cache(0), cache(1));
cache.put(key, 1);
cache.withFlags(Flag.CACHE_MODE_LOCAL).put(key, 2);
assertEquals(1, getConflicts(0).count());
cm.resolveConflicts(((preferredEntry, otherEntries) -> preferredEntry));
assertEquals(0, getConflicts(0).count());
}
use of org.infinispan.distribution.MagicKey in project infinispan by infinispan.
the class PutForExternalReadTest method testKeyOnlyWrittenOnceOnOriginator.
// This test executes PFER on cache1, and expects that it will be relayed to cache2 == primary
// and then sent to cache1 again for backup. In scattered cache there's only one RPC.
@InCacheMode({ CacheMode.DIST_SYNC, CacheMode.REPL_SYNC })
public void testKeyOnlyWrittenOnceOnOriginator() throws Exception {
final Cache<MagicKey, String> cache1 = cache(0, CACHE_NAME);
final Cache<MagicKey, String> cache2 = cache(1, CACHE_NAME);
final CyclicBarrier barrier = new CyclicBarrier(2);
cache1.getAdvancedCache().getAsyncInterceptorChain().addInterceptor(new BaseAsyncInterceptor() {
@Override
public Object visitCommand(InvocationContext ctx, VisitableCommand command) throws Throwable {
if (command instanceof PutKeyValueCommand) {
if (!ctx.isOriginLocal()) {
// wait first before the check
TestBlocking.await(barrier, 10, TimeUnit.SECONDS);
// and once more after the check
TestBlocking.await(barrier, 10, TimeUnit.SECONDS);
}
}
return invokeNext(ctx, command);
}
}, 0);
final MagicKey myKey = new MagicKey(cache2);
cache1.putForExternalRead(myKey, value);
// Verify that the key was not written on the origin by the time it was looped back
barrier.await(10, TimeUnit.SECONDS);
assertNull(cache1.get(myKey));
// Verify that the key is written on the origin afterwards
barrier.await(10, TimeUnit.SECONDS);
eventually(() -> value.equals(cache1.get(myKey)) && value.equals(cache2.get(myKey)));
}
Aggregations