Search in sources :

Example 1 with ValidResponse

use of org.infinispan.remoting.responses.ValidResponse in project infinispan by infinispan.

the class ClusteringInterceptor method visitTouchCommand.

@Override
public Object visitTouchCommand(InvocationContext ctx, TouchCommand command) throws Throwable {
    if (command.hasAnyFlag(FlagBitSets.CACHE_MODE_LOCAL | FlagBitSets.SKIP_REMOTE_LOOKUP)) {
        return invokeNext(ctx, command);
    }
    LocalizedCacheTopology cacheTopology = checkTopologyId(command);
    DistributionInfo info = cacheTopology.getSegmentDistribution(command.getSegment());
    // Scattered any node could be a backup, so we have to touch all members
    List<Address> owners = isScattered ? cacheTopology.getActualMembers() : info.readOwners();
    if (touchMode == TouchMode.ASYNC) {
        if (ctx.isOriginLocal()) {
            // Send to all the owners
            rpcManager.sendToMany(owners, command, DeliverOrder.NONE);
        }
        return invokeNext(ctx, command);
    }
    if (info.isPrimary()) {
        AbstractTouchResponseCollector collector = isScattered ? ScatteredTouchResponseCollector.INSTANCE : TouchResponseCollector.INSTANCE;
        CompletionStage<Boolean> remoteInvocation = rpcManager.invokeCommand(owners, command, collector, rpcManager.getSyncRpcOptions());
        return invokeNextThenApply(ctx, command, (rCtx, rCommand, rValue) -> {
            Boolean touchedLocally = (Boolean) rValue;
            if (touchedLocally) {
                return asyncValue(remoteInvocation);
            }
            // If primary can't touch - it doesn't matter about others
            return Boolean.FALSE;
        });
    } else if (ctx.isOriginLocal()) {
        // Send to the primary owner
        CompletionStage<ValidResponse> remoteInvocation = rpcManager.invokeCommand(info.primary(), command, SingleResponseCollector.validOnly(), rpcManager.getSyncRpcOptions());
        return asyncValue(remoteInvocation).thenApply(ctx, command, (rCtx, rCommand, rResponse) -> ((ValidResponse) rResponse).getResponseValue());
    }
    return invokeNext(ctx, command);
}
Also used : CacheNotFoundResponse(org.infinispan.remoting.responses.CacheNotFoundResponse) UnsureResponse(org.infinispan.remoting.responses.UnsureResponse) CompletableFuture(java.util.concurrent.CompletableFuture) AllOwnersLostException(org.infinispan.statetransfer.AllOwnersLostException) LockManager(org.infinispan.util.concurrent.locks.LockManager) FlagBitSets(org.infinispan.context.impl.FlagBitSets) InvocationContext(org.infinispan.context.InvocationContext) SingleResponseCollector(org.infinispan.remoting.transport.impl.SingleResponseCollector) OutdatedTopologyException(org.infinispan.statetransfer.OutdatedTopologyException) Map(java.util.Map) Address(org.infinispan.remoting.transport.Address) Response(org.infinispan.remoting.responses.Response) CommandsFactory(org.infinispan.commands.CommandsFactory) CacheException(org.infinispan.commons.CacheException) Iterator(java.util.Iterator) InternalDataContainer(org.infinispan.container.impl.InternalDataContainer) ValidResponseCollector(org.infinispan.remoting.transport.ValidResponseCollector) TouchMode(org.infinispan.expiration.TouchMode) ValidResponse(org.infinispan.remoting.responses.ValidResponse) EntryFactory(org.infinispan.container.impl.EntryFactory) Inject(org.infinispan.factories.annotations.Inject) Consumer(java.util.function.Consumer) List(java.util.List) TouchCommand(org.infinispan.expiration.impl.TouchCommand) CompletionStage(java.util.concurrent.CompletionStage) SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) CLUSTER(org.infinispan.util.logging.Log.CLUSTER) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) DistributionInfo(org.infinispan.distribution.DistributionInfo) DeliverOrder(org.infinispan.remoting.inboundhandler.DeliverOrder) FlagAffectedCommand(org.infinispan.commands.FlagAffectedCommand) TopologyAffectedCommand(org.infinispan.commands.TopologyAffectedCommand) DistributionManager(org.infinispan.distribution.DistributionManager) Address(org.infinispan.remoting.transport.Address) ValidResponse(org.infinispan.remoting.responses.ValidResponse) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) DistributionInfo(org.infinispan.distribution.DistributionInfo) CompletionStage(java.util.concurrent.CompletionStage)

Example 2 with ValidResponse

use of org.infinispan.remoting.responses.ValidResponse in project infinispan by infinispan.

the class ScatteredDistributionInterceptor method handleWriteManyOnOrigin.

private <C extends WriteCommand, Container, Item> Object handleWriteManyOnOrigin(InvocationContext ctx, C command, WriteManyCommandHelper<C, Container, Item> helper) {
    LocalizedCacheTopology cacheTopology = checkTopology(command);
    Map<Address, Container> remoteEntries = new HashMap<>();
    for (Item item : helper.getItems(command)) {
        Object key = helper.item2key(item);
        DistributionInfo info = cacheTopology.getDistribution(key);
        Address primary = info.primary();
        if (primary == null) {
            throw AllOwnersLostException.INSTANCE;
        } else {
            Container currentEntries = remoteEntries.computeIfAbsent(primary, k -> helper.newContainer());
            helper.accumulate(currentEntries, item);
        }
    }
    Object[] results = command.loadType() == DONT_LOAD ? null : new Object[command.getAffectedKeys().size()];
    MergingCompletableFuture<Object> allFuture = new SyncMergingCompletableFuture<>(remoteEntries.size(), results, helper::transformResult);
    int offset = 0;
    Container localEntries = remoteEntries.remove(rpcManager.getAddress());
    if (localEntries != null) {
        helper.containerSize(localEntries);
        C localCommand = helper.copyForLocal(command, localEntries);
        localCommand.setTopologyId(command.getTopologyId());
        LocalWriteManyHandler handler = new LocalWriteManyHandler(allFuture, localCommand.getAffectedKeys(), cacheTopology);
        invokeNextAndFinally(ctx, localCommand, handler);
    }
    // This will be null in a non-biased variant
    MultiTargetCollector multiTargetCollector = createMultiTargetCollector(command, remoteEntries.size());
    for (Map.Entry<Address, Container> ownerEntry : remoteEntries.entrySet()) {
        Address owner = ownerEntry.getKey();
        // TODO: copyForLocal just creates the command with given entries, not using the segment-aware map
        Container container = ownerEntry.getValue();
        C toPrimary = helper.copyForLocal(command, container);
        toPrimary.setTopologyId(command.getTopologyId());
        CompletionStage<ValidResponse> rpcFuture = manyWriteOnRemotePrimary(owner, toPrimary, multiTargetCollector);
        int myOffset = offset;
        offset += helper.containerSize(container);
        rpcFuture.whenComplete((response, t) -> {
            if (t != null) {
                allFuture.completeExceptionally(t);
                return;
            }
            Object responseValue = response.getResponseValue();
            // Note: we could use PrimaryResponseHandler, but we would have to add the reference to allFuture, offset...
            InternalCacheValue[] values;
            try {
                if (command.loadType() == DONT_LOAD) {
                    if (!(responseValue instanceof InternalCacheValue[])) {
                        allFuture.completeExceptionally(new CacheException("Response from " + owner + ": expected InternalCacheValue[] but it is " + responseValue));
                        return;
                    }
                    values = (InternalCacheValue[]) responseValue;
                } else {
                    if (!(responseValue instanceof Object[]) || (((Object[]) responseValue).length != 2)) {
                        allFuture.completeExceptionally(new CacheException("Response from " + owner + ": expected Object[2] but it is " + responseValue));
                        return;
                    }
                    // We use Object[] { InternalCacheValue[], Object[] } structure to get benefit of same-type array marshalling
                    // TODO optimize returning entry itself
                    // Note: some interceptors relying on the return value *could* have a problem interpreting this
                    values = (InternalCacheValue[]) ((Object[]) responseValue)[0];
                    MergingCompletableFuture.moveListItemsToFuture(((Object[]) responseValue)[1], allFuture, myOffset);
                }
                AggregateCompletionStage<Void> aggregateCompletionStage = CompletionStages.aggregateCompletionStage();
                synchronized (allFuture) {
                    if (allFuture.isDone()) {
                        return;
                    }
                    int i = 0;
                    for (Object key : helper.toKeys(container)) {
                        // we will serve as the backup
                        InternalCacheEntry ice = values[i++].toInternalCacheEntry(key);
                        entryFactory.wrapExternalEntry(ctx, key, ice, true, true);
                        RepeatableReadEntry entry = (RepeatableReadEntry) ctx.lookupEntry(key);
                        // we don't care about setCreated() since backup owner should not fire listeners
                        entry.setChanged(true);
                        aggregateCompletionStage.dependsOn(commitSingleEntryIfNewer(entry, ctx, command));
                        if (entry.isCommitted() && !command.hasAnyFlag(FlagBitSets.PUT_FOR_STATE_TRANSFER)) {
                            scheduleKeyInvalidation(entry.getKey(), entry.getMetadata().version(), entry.isRemoved());
                        }
                    }
                    assert i == values.length;
                }
                aggregateCompletionStage.freeze().thenRun(allFuture::countDown);
            } catch (Throwable t2) {
                allFuture.completeExceptionally(t2);
            }
        });
    }
    return asyncValue(allFuture);
}
Also used : Address(org.infinispan.remoting.transport.Address) HashMap(java.util.HashMap) CacheException(org.infinispan.commons.CacheException) ValidResponse(org.infinispan.remoting.responses.ValidResponse) RepeatableReadEntry(org.infinispan.container.entries.RepeatableReadEntry) MultiTargetCollector(org.infinispan.util.concurrent.CommandAckCollector.MultiTargetCollector) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) DistributionInfo(org.infinispan.distribution.DistributionInfo) InternalCacheValue(org.infinispan.container.entries.InternalCacheValue) InternalCacheEntry(org.infinispan.container.entries.InternalCacheEntry) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with ValidResponse

use of org.infinispan.remoting.responses.ValidResponse in project infinispan by infinispan.

the class BaseDistributionInterceptor method invokeRemotely.

protected Object invokeRemotely(InvocationContext ctx, DataWriteCommand command, Address primaryOwner) {
    if (log.isTraceEnabled())
        getLog().tracef("I'm not the primary owner, so sending the command to the primary owner(%s) in order to be forwarded", primaryOwner);
    boolean isSyncForwarding = isSynchronous(command) || command.isReturnValueExpected();
    if (!isSyncForwarding) {
        rpcManager.sendTo(primaryOwner, command, DeliverOrder.PER_SENDER);
        return null;
    }
    CompletionStage<ValidResponse> remoteInvocation;
    try {
        remoteInvocation = rpcManager.invokeCommand(primaryOwner, command, SingleResponseCollector.validOnly(), rpcManager.getSyncRpcOptions());
    } catch (Throwable t) {
        command.setValueMatcher(command.getValueMatcher().matcherForRetry());
        throw t;
    }
    return asyncValue(remoteInvocation).andHandle(ctx, command, (rCtx, dataWriteCommand, rv, t) -> {
        dataWriteCommand.setValueMatcher(dataWriteCommand.getValueMatcher().matcherForRetry());
        CompletableFutures.rethrowExceptionIfPresent(t);
        Response response = ((Response) rv);
        if (!response.isSuccessful()) {
            dataWriteCommand.fail();
        // FIXME A response cannot be successful and not valid
        } else if (!(response instanceof ValidResponse)) {
            throw unexpected(primaryOwner, response);
        }
        // We expect only successful/unsuccessful responses, not unsure
        return ((ValidResponse) response).getResponseValue();
    });
}
Also used : CacheNotFoundResponse(org.infinispan.remoting.responses.CacheNotFoundResponse) UnsureResponse(org.infinispan.remoting.responses.UnsureResponse) SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) ExceptionResponse(org.infinispan.remoting.responses.ExceptionResponse) Response(org.infinispan.remoting.responses.Response) ValidResponse(org.infinispan.remoting.responses.ValidResponse) ValidResponse(org.infinispan.remoting.responses.ValidResponse)

Example 4 with ValidResponse

use of org.infinispan.remoting.responses.ValidResponse in project infinispan by infinispan.

the class BaseDistributionInterceptor method visitGetKeysInGroupCommand.

@Override
public final Object visitGetKeysInGroupCommand(InvocationContext ctx, GetKeysInGroupCommand command) throws Throwable {
    if (command.isGroupOwner()) {
        // don't go remote if we are an owner.
        return invokeNext(ctx, command);
    }
    Address primaryOwner = distributionManager.getCacheTopology().getDistribution(command.getGroupName()).primary();
    CompletionStage<ValidResponse> future = rpcManager.invokeCommand(primaryOwner, command, SingleResponseCollector.validOnly(), rpcManager.getSyncRpcOptions());
    return asyncInvokeNext(ctx, command, future.thenAccept(response -> {
        if (response instanceof SuccessfulResponse) {
            // noinspection unchecked
            List<CacheEntry> cacheEntries = (List<CacheEntry>) response.getResponseValue();
            for (CacheEntry entry : cacheEntries) {
                wrapRemoteEntry(ctx, entry.getKey(), entry, false);
            }
        }
    }));
}
Also used : WriteCommand(org.infinispan.commands.write.WriteCommand) Arrays(java.util.Arrays) GetCacheEntryCommand(org.infinispan.commands.read.GetCacheEntryCommand) CacheNotFoundResponse(org.infinispan.remoting.responses.CacheNotFoundResponse) LogFactory(org.infinispan.util.logging.LogFactory) UnsureResponse(org.infinispan.remoting.responses.UnsureResponse) GetKeysInGroupCommand(org.infinispan.commands.remote.GetKeysInGroupCommand) InvocationContext(org.infinispan.context.InvocationContext) CompletableFutures(org.infinispan.util.concurrent.CompletableFutures) TxInvocationContext(org.infinispan.context.impl.TxInvocationContext) OutdatedTopologyException(org.infinispan.statetransfer.OutdatedTopologyException) Map(java.util.Map) RpcOptions(org.infinispan.remoting.rpc.RpcOptions) ConsistentHash(org.infinispan.distribution.ch.ConsistentHash) Collection(java.util.Collection) InvocationSuccessFunction(org.infinispan.interceptors.InvocationSuccessFunction) ResponseCollector(org.infinispan.remoting.transport.ResponseCollector) List(java.util.List) SingletonMapResponseCollector(org.infinispan.remoting.transport.impl.SingletonMapResponseCollector) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) InternalCacheValue(org.infinispan.container.entries.InternalCacheValue) FlagAffectedCommand(org.infinispan.commands.FlagAffectedCommand) TopologyAffectedCommand(org.infinispan.commands.TopologyAffectedCommand) ArrayCollector(org.infinispan.commons.util.ArrayCollector) InternalCacheEntry(org.infinispan.container.entries.InternalCacheEntry) HashMap(java.util.HashMap) GetAllCommand(org.infinispan.commands.read.GetAllCommand) RemoteValueRetrievedListener(org.infinispan.distribution.RemoteValueRetrievedListener) Function(java.util.function.Function) ReadOnlyKeyCommand(org.infinispan.commands.functional.ReadOnlyKeyCommand) InternalExpirationManager(org.infinispan.expiration.impl.InternalExpirationManager) ArrayList(java.util.ArrayList) Start(org.infinispan.factories.annotations.Start) MapResponseCollector(org.infinispan.remoting.transport.impl.MapResponseCollector) FlagBitSets(org.infinispan.context.impl.FlagBitSets) AbstractDataWriteCommand(org.infinispan.commands.write.AbstractDataWriteCommand) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) SingleResponseCollector(org.infinispan.remoting.transport.impl.SingleResponseCollector) ClusteredGetCommand(org.infinispan.commands.remote.ClusteredGetCommand) Log(org.infinispan.util.logging.Log) BiConsumer(java.util.function.BiConsumer) VoidResponseCollector(org.infinispan.remoting.transport.impl.VoidResponseCollector) AbstractDataCommand(org.infinispan.commands.read.AbstractDataCommand) SegmentSpecificCommand(org.infinispan.commands.SegmentSpecificCommand) DataWriteCommand(org.infinispan.commands.write.DataWriteCommand) ValueMatcher(org.infinispan.commands.write.ValueMatcher) ClearCommand(org.infinispan.commands.write.ClearCommand) Address(org.infinispan.remoting.transport.Address) ExceptionResponse(org.infinispan.remoting.responses.ExceptionResponse) Response(org.infinispan.remoting.responses.Response) ClusteringInterceptor(org.infinispan.interceptors.impl.ClusteringInterceptor) ReplicableCommand(org.infinispan.commands.ReplicableCommand) GetKeyValueCommand(org.infinispan.commands.read.GetKeyValueCommand) CacheEntry(org.infinispan.container.entries.CacheEntry) ValidResponse(org.infinispan.remoting.responses.ValidResponse) Inject(org.infinispan.factories.annotations.Inject) ReadOnlyManyCommand(org.infinispan.commands.functional.ReadOnlyManyCommand) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) GlobalTransaction(org.infinispan.transaction.xa.GlobalTransaction) DistributionInfo(org.infinispan.distribution.DistributionInfo) VisitableCommand(org.infinispan.commands.VisitableCommand) DeliverOrder(org.infinispan.remoting.inboundhandler.DeliverOrder) Collections(java.util.Collections) BaseClusteredReadCommand(org.infinispan.commands.remote.BaseClusteredReadCommand) TimeService(org.infinispan.commons.time.TimeService) NullCacheEntry(org.infinispan.container.entries.NullCacheEntry) SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) Address(org.infinispan.remoting.transport.Address) ValidResponse(org.infinispan.remoting.responses.ValidResponse) List(java.util.List) ArrayList(java.util.ArrayList) InternalCacheEntry(org.infinispan.container.entries.InternalCacheEntry) CacheEntry(org.infinispan.container.entries.CacheEntry) NullCacheEntry(org.infinispan.container.entries.NullCacheEntry)

Example 5 with ValidResponse

use of org.infinispan.remoting.responses.ValidResponse in project infinispan by infinispan.

the class RpcManagerTest method testInvokeCommand1.

public void testInvokeCommand1() throws Exception {
    ClusteredGetCommand command = TestingUtil.extractCommandsFactory(cache(0)).buildClusteredGetCommand("key", 0, 0L);
    RpcManager rpcManager0 = cache(0).getAdvancedCache().getRpcManager();
    Exceptions.expectException(IllegalArgumentException.class, () -> rpcManager0.invokeCommand(address(0), command, SingleResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions()));
    command.setTopologyId(rpcManager0.getTopologyId());
    CompletionStage<ValidResponse> stage1 = rpcManager0.invokeCommand(address(0), command, SingleResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions());
    assertResponse(null, stage1);
    CompletionStage<ValidResponse> stage2 = rpcManager0.invokeCommand(address(1), command, SingleResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions());
    assertResponse(SUCCESSFUL_EMPTY_RESPONSE, stage2);
    CompletionStage<ValidResponse> stage3 = rpcManager0.invokeCommand(SUSPECT, command, SingleResponseCollector.validOnly(), rpcManager0.getSyncRpcOptions());
    Exceptions.expectExecutionException(SuspectException.class, stage3.toCompletableFuture());
}
Also used : ValidResponse(org.infinispan.remoting.responses.ValidResponse) ClusteredGetCommand(org.infinispan.commands.remote.ClusteredGetCommand)

Aggregations

ValidResponse (org.infinispan.remoting.responses.ValidResponse)12 Address (org.infinispan.remoting.transport.Address)6 HashMap (java.util.HashMap)4 Map (java.util.Map)4 DistributionInfo (org.infinispan.distribution.DistributionInfo)4 LocalizedCacheTopology (org.infinispan.distribution.LocalizedCacheTopology)4 DeliverOrder (org.infinispan.remoting.inboundhandler.DeliverOrder)4 CacheNotFoundResponse (org.infinispan.remoting.responses.CacheNotFoundResponse)4 Response (org.infinispan.remoting.responses.Response)4 SuccessfulResponse (org.infinispan.remoting.responses.SuccessfulResponse)4 CompletableFuture (java.util.concurrent.CompletableFuture)3 ClusteredGetCommand (org.infinispan.commands.remote.ClusteredGetCommand)3 ExceptionResponse (org.infinispan.remoting.responses.ExceptionResponse)3 UnsureResponse (org.infinispan.remoting.responses.UnsureResponse)3 List (java.util.List)2 CompletionStage (java.util.concurrent.CompletionStage)2 FlagAffectedCommand (org.infinispan.commands.FlagAffectedCommand)2 TopologyAffectedCommand (org.infinispan.commands.TopologyAffectedCommand)2 CacheException (org.infinispan.commons.CacheException)2 InternalCacheEntry (org.infinispan.container.entries.InternalCacheEntry)2