Search in sources :

Example 1 with ClusteredGetCommand

use of org.infinispan.commands.remote.ClusteredGetCommand in project infinispan by infinispan.

the class ScatteredDistributionInterceptor method handleReadCommand.

private Object handleReadCommand(InvocationContext ctx, AbstractDataCommand command) throws Throwable {
    LocalizedCacheTopology cacheTopology = checkTopology(command);
    // SKIP_OWNERSHIP_CHECK is added when the entry is prefetched from remote node
    // TODO [rvansa]: local lookup and hinted read, see improvements in package-info
    CacheEntry entry = ctx.lookupEntry(command.getKey());
    if (entry != null) {
        return invokeNext(ctx, command);
    }
    DistributionInfo info = cacheTopology.getSegmentDistribution(command.getSegment());
    if (info.isPrimary()) {
        if (log.isTraceEnabled()) {
            log.tracef("In topology %d this is primary owner", cacheTopology.getTopologyId());
        }
        return invokeNext(ctx, command);
    } else if (command.hasAnyFlag(FlagBitSets.SKIP_OWNERSHIP_CHECK)) {
        if (log.isTraceEnabled()) {
            log.trace("Ignoring ownership");
        }
        return invokeNext(ctx, command);
    } else if (info.primary() == null) {
        throw OutdatedTopologyException.RETRY_NEXT_TOPOLOGY;
    } else if (ctx.isOriginLocal()) {
        if (isLocalModeForced(command) || command.hasAnyFlag(FlagBitSets.SKIP_REMOTE_LOOKUP)) {
            entryFactory.wrapExternalEntry(ctx, command.getKey(), NullCacheEntry.getInstance(), false, false);
            return invokeNext(ctx, command);
        }
        ClusteredGetCommand clusteredGetCommand = cf.buildClusteredGetCommand(command.getKey(), info.segmentId(), command.getFlagsBitSet());
        clusteredGetCommand.setTopologyId(command.getTopologyId());
        ResponseCollector<Response> collector = PassthroughSingleResponseCollector.INSTANCE;
        CompletionStage<Response> rpcFuture = rpcManager.invokeCommand(info.primary(), clusteredGetCommand, collector, rpcManager.getSyncRpcOptions());
        Object key = clusteredGetCommand.getKey();
        return asyncInvokeNext(ctx, command, rpcFuture.thenAccept(response -> {
            if (response.isSuccessful()) {
                InternalCacheValue value = (InternalCacheValue) ((SuccessfulResponse) response).getResponseValue();
                if (value != null) {
                    InternalCacheEntry cacheEntry = value.toInternalCacheEntry(key);
                    entryFactory.wrapExternalEntry(ctx, key, cacheEntry, true, false);
                } else {
                    entryFactory.wrapExternalEntry(ctx, key, NullCacheEntry.getInstance(), false, false);
                }
            } else if (response instanceof UnsureResponse) {
                throw OutdatedTopologyException.RETRY_NEXT_TOPOLOGY;
            } else if (response instanceof CacheNotFoundResponse) {
                throw AllOwnersLostException.INSTANCE;
            } else if (response instanceof ExceptionResponse) {
                throw ResponseCollectors.wrapRemoteException(info.primary(), ((ExceptionResponse) response).getException());
            } else {
                throw new IllegalArgumentException("Unexpected response " + response);
            }
        }));
    } else {
        return UnsureResponse.INSTANCE;
    }
}
Also used : ExceptionResponse(org.infinispan.remoting.responses.ExceptionResponse) CacheNotFoundResponse(org.infinispan.remoting.responses.CacheNotFoundResponse) UnsureResponse(org.infinispan.remoting.responses.UnsureResponse) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) InternalCacheEntry(org.infinispan.container.entries.InternalCacheEntry) CacheEntry(org.infinispan.container.entries.CacheEntry) NullCacheEntry(org.infinispan.container.entries.NullCacheEntry) MetadataImmortalCacheEntry(org.infinispan.container.entries.metadata.MetadataImmortalCacheEntry) ClusteredGetCommand(org.infinispan.commands.remote.ClusteredGetCommand) DistributionInfo(org.infinispan.distribution.DistributionInfo) InternalCacheValue(org.infinispan.container.entries.InternalCacheValue) ResponseCollector(org.infinispan.remoting.transport.ResponseCollector) MapResponseCollector(org.infinispan.remoting.transport.impl.MapResponseCollector) PassthroughSingleResponseCollector(org.infinispan.remoting.transport.impl.PassthroughSingleResponseCollector) SingleResponseCollector(org.infinispan.remoting.transport.impl.SingleResponseCollector) SingletonMapResponseCollector(org.infinispan.remoting.transport.impl.SingletonMapResponseCollector) InternalCacheEntry(org.infinispan.container.entries.InternalCacheEntry) CompletionStage(java.util.concurrent.CompletionStage) AggregateCompletionStage(org.infinispan.util.concurrent.AggregateCompletionStage)

Example 2 with ClusteredGetCommand

use of org.infinispan.commands.remote.ClusteredGetCommand in project infinispan by infinispan.

the class AnchoredFetchInterceptor method getRemoteValue.

private CompletionStage<CacheEntry<K, V>> getRemoteValue(Address keyLocation, K key, Integer segment, boolean isWrite) {
    ClusteredGetCommand getCommand = cf.buildClusteredGetCommand(key, segment, FlagBitSets.SKIP_OWNERSHIP_CHECK);
    getCommand.setTopologyId(0);
    getCommand.setWrite(isWrite);
    FetchResponseCollector<K, V> collector = new FetchResponseCollector<>(key);
    return rpcManager.invokeCommand(keyLocation, getCommand, collector, rpcManager.getSyncRpcOptions());
}
Also used : ClusteredGetCommand(org.infinispan.commands.remote.ClusteredGetCommand)

Example 3 with ClusteredGetCommand

use of org.infinispan.commands.remote.ClusteredGetCommand in project infinispan by infinispan.

the class VersionAwareMarshallerTest method testReplicableCommandsMarshalling.

public void testReplicableCommandsMarshalling() throws Exception {
    ByteString cacheName = ByteString.fromString(TestingUtil.getDefaultCacheName(cm));
    ClusteredGetCommand c2 = new ClusteredGetCommand("key", cacheName, 0, EnumUtil.EMPTY_BIT_SET);
    marshallAndAssertEquality(c2);
    // SizeCommand does not have an empty constructor, so doesn't look to be one that is marshallable.
    GetKeyValueCommand c4 = new GetKeyValueCommand("key", 0, EnumUtil.EMPTY_BIT_SET);
    marshallAndAssertEquality(c4);
    PutKeyValueCommand c5 = new PutKeyValueCommand("k", "v", false, new EmbeddedMetadata.Builder().build(), 0, EnumUtil.EMPTY_BIT_SET, CommandInvocationId.generateId(null));
    marshallAndAssertEquality(c5);
    RemoveCommand c6 = new RemoveCommand("key", null, 0, EnumUtil.EMPTY_BIT_SET, CommandInvocationId.generateId(null));
    marshallAndAssertEquality(c6);
    // EvictCommand does not have an empty constructor, so doesn't look to be one that is marshallable.
    InvalidateCommand c7 = new InvalidateCommand(EnumUtil.EMPTY_BIT_SET, CommandInvocationId.generateId(null), "key1", "key2");
    marshallAndAssertEquality(c7);
    InvalidateCommand c71 = new InvalidateL1Command(EnumUtil.EMPTY_BIT_SET, CommandInvocationId.generateId(null), "key1", "key2");
    marshallAndAssertEquality(c71);
    ReplaceCommand c8 = new ReplaceCommand("key", "oldvalue", "newvalue", new EmbeddedMetadata.Builder().build(), 0, EnumUtil.EMPTY_BIT_SET, CommandInvocationId.generateId(null));
    marshallAndAssertEquality(c8);
    ClearCommand c9 = new ClearCommand();
    marshallAndAssertEquality(c9);
    Map<Integer, GlobalTransaction> m1 = new HashMap<>();
    for (int i = 0; i < 10; i++) {
        GlobalTransaction gtx = gtf.newGlobalTransaction(new JGroupsAddress(UUID.randomUUID()), false);
        m1.put(1000 * i, gtx);
    }
    PutMapCommand c10 = new PutMapCommand(m1, new EmbeddedMetadata.Builder().build(), EnumUtil.EMPTY_BIT_SET, CommandInvocationId.generateId(null));
    marshallAndAssertEquality(c10);
    Address local = new JGroupsAddress(UUID.randomUUID());
    GlobalTransaction gtx = gtf.newGlobalTransaction(local, false);
    PrepareCommand c11 = new PrepareCommand(cacheName, gtx, true, c5, c6, c8, c10);
    marshallAndAssertEquality(c11);
    CommitCommand c12 = new CommitCommand(cacheName, gtx);
    marshallAndAssertEquality(c12);
    RollbackCommand c13 = new RollbackCommand(cacheName, gtx);
    marshallAndAssertEquality(c13);
}
Also used : RemoveCommand(org.infinispan.commands.write.RemoveCommand) IpAddress(org.jgroups.stack.IpAddress) JGroupsAddress(org.infinispan.remoting.transport.jgroups.JGroupsAddress) Address(org.infinispan.remoting.transport.Address) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) FastCopyHashMap(org.infinispan.commons.util.FastCopyHashMap) HashMap(java.util.HashMap) ByteString(org.infinispan.util.ByteString) JGroupsAddress(org.infinispan.remoting.transport.jgroups.JGroupsAddress) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) AutoProtoSchemaBuilder(org.infinispan.protostream.annotations.AutoProtoSchemaBuilder) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) GlobalTransaction(org.infinispan.transaction.xa.GlobalTransaction) PrepareCommand(org.infinispan.commands.tx.PrepareCommand) InvalidateL1Command(org.infinispan.commands.write.InvalidateL1Command) ClusteredGetCommand(org.infinispan.commands.remote.ClusteredGetCommand) PutMapCommand(org.infinispan.commands.write.PutMapCommand) GetKeyValueCommand(org.infinispan.commands.read.GetKeyValueCommand) ReplaceCommand(org.infinispan.commands.write.ReplaceCommand) RollbackCommand(org.infinispan.commands.tx.RollbackCommand) ClearCommand(org.infinispan.commands.write.ClearCommand) CommitCommand(org.infinispan.commands.tx.CommitCommand) InvalidateCommand(org.infinispan.commands.write.InvalidateCommand) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand)

Example 4 with ClusteredGetCommand

use of org.infinispan.commands.remote.ClusteredGetCommand in project infinispan by infinispan.

the class RpcManagerTest method testInvokeCommandCollection.

public void testInvokeCommandCollection() throws Exception {
    ClusteredGetCommand command = TestingUtil.extractCommandsFactory(cache(0)).buildClusteredGetCommand("key", 0, 0L);
    RpcManager rpcManager0 = cache(0).getAdvancedCache().getRpcManager();
    Exceptions.expectException(IllegalArgumentException.class, () -> rpcManager0.invokeCommand(Arrays.asList(address(0)), command, SingleResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions()));
    command.setTopologyId(rpcManager0.getTopologyId());
    CompletionStage<Map<Address, Response>> stage1 = rpcManager0.invokeCommand(Arrays.asList(address(0)), command, MapResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions());
    assertResponse(Collections.emptyMap(), stage1);
    CompletionStage<Map<Address, Response>> stage2 = rpcManager0.invokeCommand(Arrays.asList(address(1)), command, MapResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions());
    assertResponse(Collections.singletonMap(address(1), SUCCESSFUL_EMPTY_RESPONSE), stage2);
    CompletionStage<Map<Address, Response>> stage3 = rpcManager0.invokeCommand(Arrays.asList(address(0), address(1)), command, MapResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions());
    assertResponse(Collections.singletonMap(address(1), SUCCESSFUL_EMPTY_RESPONSE), stage3);
}
Also used : ClusteredGetCommand(org.infinispan.commands.remote.ClusteredGetCommand) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with ClusteredGetCommand

use of org.infinispan.commands.remote.ClusteredGetCommand in project infinispan by infinispan.

the class RpcManagerTest method testInvokeCommandCollectionSuspect.

public void testInvokeCommandCollectionSuspect() throws Exception {
    ClusteredGetCommand command = TestingUtil.extractCommandsFactory(cache(0)).buildClusteredGetCommand("key", 0, 0L);
    RpcManager rpcManager0 = cache(0).getAdvancedCache().getRpcManager();
    command.setTopologyId(rpcManager0.getTopologyId());
    CompletionStage<Map<Address, Response>> stage1 = rpcManager0.invokeCommand(Arrays.asList(SUSPECT), command, MapResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions());
    Exceptions.expectExecutionException(SuspectException.class, stage1.toCompletableFuture());
    CompletionStage<Map<Address, Response>> stage2 = rpcManager0.invokeCommand(Arrays.asList(address(0), SUSPECT), command, MapResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions());
    Exceptions.expectExecutionException(SuspectException.class, stage2.toCompletableFuture());
    CompletionStage<Map<Address, Response>> stage3 = rpcManager0.invokeCommand(Arrays.asList(address(0), address(1), SUSPECT), command, MapResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions());
    Exceptions.expectExecutionException(SuspectException.class, stage3.toCompletableFuture());
}
Also used : ClusteredGetCommand(org.infinispan.commands.remote.ClusteredGetCommand) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

ClusteredGetCommand (org.infinispan.commands.remote.ClusteredGetCommand)19 Map (java.util.Map)10 HashMap (java.util.HashMap)7 Address (org.infinispan.remoting.transport.Address)6 ReplicableCommand (org.infinispan.commands.ReplicableCommand)3 InternalCacheValue (org.infinispan.container.entries.InternalCacheValue)3 CommitCommand (org.infinispan.commands.tx.CommitCommand)2 PrepareCommand (org.infinispan.commands.tx.PrepareCommand)2 RollbackCommand (org.infinispan.commands.tx.RollbackCommand)2 InternalCacheEntry (org.infinispan.container.entries.InternalCacheEntry)2 DistributionInfo (org.infinispan.distribution.DistributionInfo)2 LocalizedCacheTopology (org.infinispan.distribution.LocalizedCacheTopology)2 CacheNotFoundResponse (org.infinispan.remoting.responses.CacheNotFoundResponse)2 Response (org.infinispan.remoting.responses.Response)2 SuccessfulResponse (org.infinispan.remoting.responses.SuccessfulResponse)2 ValidResponse (org.infinispan.remoting.responses.ValidResponse)2 JGroupsAddress (org.infinispan.remoting.transport.jgroups.JGroupsAddress)2 ByteString (org.infinispan.util.ByteString)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1