Search in sources :

Example 46 with MagicKey

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);
}
Also used : FunctionalMap(org.infinispan.functional.FunctionalMap) MagicKey(org.infinispan.distribution.MagicKey) HashSet(java.util.HashSet)

Example 47 with MagicKey

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));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) InternalCacheValue(org.infinispan.container.entries.InternalCacheValue) ControlledRpcManager(org.infinispan.util.ControlledRpcManager) MagicKey(org.infinispan.distribution.MagicKey) Map(java.util.Map) ImmortalCacheValue(org.infinispan.container.entries.ImmortalCacheValue)

Example 48 with MagicKey

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();
    }
}
Also used : StateConsumer(org.infinispan.statetransfer.StateConsumer) MagicKey(org.infinispan.distribution.MagicKey) ControlledRpcManager(org.infinispan.util.ControlledRpcManager) Map(java.util.Map) CheckPoint(org.infinispan.test.fwk.CheckPoint)

Example 49 with MagicKey

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());
}
Also used : IntStream(java.util.stream.IntStream) DataRehashed(org.infinispan.notifications.cachelistener.annotation.DataRehashed) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) Reply(org.infinispan.remoting.inboundhandler.Reply) PartitionHandling(org.infinispan.partitionhandling.PartitionHandling) Test(org.testng.annotations.Test) AssertJUnit.assertTrue(org.testng.AssertJUnit.assertTrue) ArrayList(java.util.ArrayList) AbstractDelegatingHandler(org.infinispan.remoting.inboundhandler.AbstractDelegatingHandler) StateChunk(org.infinispan.statetransfer.StateChunk) Future(java.util.concurrent.Future) AdvancedCache(org.infinispan.AdvancedCache) Map(java.util.Map) ClusteredGetCommand(org.infinispan.commands.remote.ClusteredGetCommand) BasePartitionHandlingTest(org.infinispan.partitionhandling.BasePartitionHandlingTest) TestingUtil(org.infinispan.test.TestingUtil) AssertJUnit.assertNotSame(org.testng.AssertJUnit.assertNotSame) Address(org.infinispan.remoting.transport.Address) MagicKey(org.infinispan.distribution.MagicKey) CacheException(org.infinispan.commons.CacheException) Listener(org.infinispan.notifications.Listener) CacheEntry(org.infinispan.container.entries.CacheEntry) Collection(java.util.Collection) StateResponseCommand(org.infinispan.commands.statetransfer.StateResponseCommand) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) ConflictManager(org.infinispan.conflict.ConflictManager) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) CacheRpcCommand(org.infinispan.commands.remote.CacheRpcCommand) CacheMode(org.infinispan.configuration.cache.CacheMode) Stream(java.util.stream.Stream) PerCacheInboundInvocationHandler(org.infinispan.remoting.inboundhandler.PerCacheInboundInvocationHandler) Exceptions(org.infinispan.commons.test.Exceptions) Flag(org.infinispan.context.Flag) TestingUtil.wrapInboundInvocationHandler(org.infinispan.test.TestingUtil.wrapInboundInvocationHandler) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) InternalCacheValue(org.infinispan.container.entries.InternalCacheValue) DataRehashedEvent(org.infinispan.notifications.cachelistener.event.DataRehashedEvent) DeliverOrder(org.infinispan.remoting.inboundhandler.DeliverOrder) AssertJUnit.assertEquals(org.testng.AssertJUnit.assertEquals) ConflictManagerFactory(org.infinispan.conflict.ConflictManagerFactory) NullCacheEntry(org.infinispan.container.entries.NullCacheEntry) MagicKey(org.infinispan.distribution.MagicKey)

Example 50 with MagicKey

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)));
}
Also used : VisitableCommand(org.infinispan.commands.VisitableCommand) BaseAsyncInterceptor(org.infinispan.interceptors.BaseAsyncInterceptor) InvocationContext(org.infinispan.context.InvocationContext) MagicKey(org.infinispan.distribution.MagicKey) CyclicBarrier(java.util.concurrent.CyclicBarrier) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand) InCacheMode(org.infinispan.test.fwk.InCacheMode)

Aggregations

MagicKey (org.infinispan.distribution.MagicKey)185 Test (org.testng.annotations.Test)74 TransactionManager (javax.transaction.TransactionManager)23 CheckPoint (org.infinispan.test.fwk.CheckPoint)20 HashMap (java.util.HashMap)16 Map (java.util.Map)16 Transaction (javax.transaction.Transaction)16 Cache (org.infinispan.Cache)16 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)15 CountDownLatch (java.util.concurrent.CountDownLatch)10 CacheMode (org.infinispan.configuration.cache.CacheMode)10 CacheEntry (org.infinispan.container.entries.CacheEntry)10 Address (org.infinispan.remoting.transport.Address)10 StateSequencer (org.infinispan.test.concurrent.StateSequencer)9 AssertJUnit.assertEquals (org.testng.AssertJUnit.assertEquals)9 ArrayList (java.util.ArrayList)8 CyclicBarrier (java.util.concurrent.CyclicBarrier)8 DistributionManager (org.infinispan.distribution.DistributionManager)8 MultipleCacheManagersTest (org.infinispan.test.MultipleCacheManagersTest)8 RollbackException (javax.transaction.RollbackException)7