Search in sources :

Example 11 with SuccessfulResponse

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

the class ScatteredDistributionInterceptor method handleWritePrimaryResponse.

private Object handleWritePrimaryResponse(InvocationContext ctx, DataWriteCommand command, Object rv) {
    Response response = (Response) rv;
    if (!response.isSuccessful()) {
        command.fail();
        if (response instanceof UnsuccessfulResponse) {
            return ((UnsuccessfulResponse) response).getResponseValue();
        } else {
            throw new CacheException("Unexpected response " + response);
        }
    }
    Object responseValue = ((SuccessfulResponse) response).getResponseValue();
    try {
        return command.acceptVisitor(ctx, new PrimaryResponseHandler(responseValue));
    } catch (Throwable throwable) {
        throw CompletableFutures.asCompletionException(throwable);
    }
}
Also used : CacheNotFoundResponse(org.infinispan.remoting.responses.CacheNotFoundResponse) UnsuccessfulResponse(org.infinispan.remoting.responses.UnsuccessfulResponse) SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) ExceptionResponse(org.infinispan.remoting.responses.ExceptionResponse) ValidResponse(org.infinispan.remoting.responses.ValidResponse) UnsureResponse(org.infinispan.remoting.responses.UnsureResponse) Response(org.infinispan.remoting.responses.Response) SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) CacheException(org.infinispan.commons.CacheException) UnsuccessfulResponse(org.infinispan.remoting.responses.UnsuccessfulResponse)

Example 12 with SuccessfulResponse

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

the class ScatteredDistributionInterceptor method visitReadOnlyKeyCommand.

@Override
public Object visitReadOnlyKeyCommand(InvocationContext ctx, ReadOnlyKeyCommand command) throws Throwable {
    Object key = command.getKey();
    CacheEntry entry = ctx.lookupEntry(key);
    if (entry != null) {
        // the entry is owned locally (it is NullCacheEntry if it was not found), no need to go remote
        return invokeNext(ctx, command);
    }
    if (!ctx.isOriginLocal()) {
        return UnsureResponse.INSTANCE;
    }
    if (isLocalModeForced(command) || command.hasAnyFlag(FlagBitSets.SKIP_REMOTE_LOOKUP)) {
        if (ctx.lookupEntry(command.getKey()) == null) {
            entryFactory.wrapExternalEntry(ctx, command.getKey(), NullCacheEntry.getInstance(), false, false);
        }
        return invokeNext(ctx, command);
    }
    DistributionInfo info = checkTopology(command).getDistribution(command.getKey());
    if (info.primary() == null) {
        throw AllOwnersLostException.INSTANCE;
    }
    ResponseCollector<Response> collector = PassthroughSingleResponseCollector.INSTANCE;
    CompletionStage<Response> rpc = rpcManager.invokeCommand(info.primary(), command, collector, rpcManager.getSyncRpcOptions());
    return asyncValue(rpc.thenApply(response -> {
        if (response.isSuccessful()) {
            return ((SuccessfulResponse) response).getResponseValue();
        } 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);
        }
    }));
}
Also used : CacheNotFoundResponse(org.infinispan.remoting.responses.CacheNotFoundResponse) UnsuccessfulResponse(org.infinispan.remoting.responses.UnsuccessfulResponse) SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) ExceptionResponse(org.infinispan.remoting.responses.ExceptionResponse) ValidResponse(org.infinispan.remoting.responses.ValidResponse) UnsureResponse(org.infinispan.remoting.responses.UnsureResponse) Response(org.infinispan.remoting.responses.Response) 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) RemoteMetadata(org.infinispan.container.entries.RemoteMetadata) ClusteredGetAllCommand(org.infinispan.commands.remote.ClusteredGetAllCommand) Map(java.util.Map) RemoveCommand(org.infinispan.commands.write.RemoveCommand) ConsistentHash(org.infinispan.distribution.ch.ConsistentHash) EmbeddedMetadata(org.infinispan.metadata.EmbeddedMetadata) ReadWriteManyEntriesCommand(org.infinispan.commands.functional.ReadWriteManyEntriesCommand) UnsuccessfulResponse(org.infinispan.remoting.responses.UnsuccessfulResponse) Set(java.util.Set) GroupManager(org.infinispan.distribution.group.impl.GroupManager) CacheTopology(org.infinispan.topology.CacheTopology) ResponseCollector(org.infinispan.remoting.transport.ResponseCollector) DONT_LOAD(org.infinispan.commands.VisitableCommand.LoadType.DONT_LOAD) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) InternalCacheValue(org.infinispan.container.entries.InternalCacheValue) AbstractVisitor(org.infinispan.commands.AbstractVisitor) ArrayCollector(org.infinispan.commons.util.ArrayCollector) InternalCacheEntry(org.infinispan.container.entries.InternalCacheEntry) CacheEntryRemoved(org.infinispan.notifications.cachelistener.annotation.CacheEntryRemoved) PersistenceManager(org.infinispan.persistence.manager.PersistenceManager) ComputeCommand(org.infinispan.commands.write.ComputeCommand) MetadataAwareCommand(org.infinispan.commands.MetadataAwareCommand) EvictionManager(org.infinispan.eviction.EvictionManager) ReadOnlyKeyCommand(org.infinispan.commands.functional.ReadOnlyKeyCommand) ArrayList(java.util.ArrayList) WriteOnlyManyEntriesCommand(org.infinispan.commands.functional.WriteOnlyManyEntriesCommand) Start(org.infinispan.factories.annotations.Start) MapResponseCollector(org.infinispan.remoting.transport.impl.MapResponseCollector) PassthroughSingleResponseCollector(org.infinispan.remoting.transport.impl.PassthroughSingleResponseCollector) SingleResponseCollector(org.infinispan.remoting.transport.impl.SingleResponseCollector) DataWriteCommand(org.infinispan.commands.write.DataWriteCommand) FunctionalNotifier(org.infinispan.functional.impl.FunctionalNotifier) ValueMatcher(org.infinispan.commands.write.ValueMatcher) ClearCommand(org.infinispan.commands.write.ClearCommand) Address(org.infinispan.remoting.transport.Address) ExceptionResponse(org.infinispan.remoting.responses.ExceptionResponse) CacheEntry(org.infinispan.container.entries.CacheEntry) NotifyHelper(org.infinispan.notifications.cachelistener.NotifyHelper) WriteOnlyKeyValueCommand(org.infinispan.commands.functional.WriteOnlyKeyValueCommand) ValidResponse(org.infinispan.remoting.responses.ValidResponse) CacheNotifier(org.infinispan.notifications.cachelistener.CacheNotifier) Inject(org.infinispan.factories.annotations.Inject) ReadWriteKeyCommand(org.infinispan.commands.functional.ReadWriteKeyCommand) ReadWriteManyCommand(org.infinispan.commands.functional.ReadWriteManyCommand) NullCacheEntry(org.infinispan.container.entries.NullCacheEntry) AggregateCompletionStage(org.infinispan.util.concurrent.AggregateCompletionStage) ReplaceCommand(org.infinispan.commands.write.ReplaceCommand) EntryVersion(org.infinispan.container.versioning.EntryVersion) UnsureResponse(org.infinispan.remoting.responses.UnsureResponse) MultiTargetCollector(org.infinispan.util.concurrent.CommandAckCollector.MultiTargetCollector) GetKeysInGroupCommand(org.infinispan.commands.remote.GetKeysInGroupCommand) ActivationManager(org.infinispan.eviction.impl.ActivationManager) RepeatableReadEntry(org.infinispan.container.entries.RepeatableReadEntry) InvocationContext(org.infinispan.context.InvocationContext) CompletableFutures(org.infinispan.util.concurrent.CompletableFutures) OutdatedTopologyException(org.infinispan.statetransfer.OutdatedTopologyException) RpcOptions(org.infinispan.remoting.rpc.RpcOptions) Predicate(java.util.function.Predicate) Collection(java.util.Collection) ReadWriteKeyValueCommand(org.infinispan.commands.functional.ReadWriteKeyValueCommand) MetadataImmortalCacheEntry(org.infinispan.container.entries.metadata.MetadataImmortalCacheEntry) DataOperationOrderer(org.infinispan.util.concurrent.DataOperationOrderer) InequalVersionComparisonResult(org.infinispan.container.versioning.InequalVersionComparisonResult) InvocationSuccessFunction(org.infinispan.interceptors.InvocationSuccessFunction) List(java.util.List) SingletonMapResponseCollector(org.infinispan.remoting.transport.impl.SingletonMapResponseCollector) InvocationFinallyAction(org.infinispan.interceptors.InvocationFinallyAction) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand) FlagAffectedCommand(org.infinispan.commands.FlagAffectedCommand) TopologyAffectedCommand(org.infinispan.commands.TopologyAffectedCommand) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Metadata(org.infinispan.metadata.Metadata) CompletionStages(org.infinispan.util.concurrent.CompletionStages) GetAllCommand(org.infinispan.commands.read.GetAllCommand) Function(java.util.function.Function) AllOwnersLostException(org.infinispan.statetransfer.AllOwnersLostException) FlagBitSets(org.infinispan.context.impl.FlagBitSets) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) ClusteredGetCommand(org.infinispan.commands.remote.ClusteredGetCommand) IracPutKeyValueCommand(org.infinispan.commands.write.IracPutKeyValueCommand) Log(org.infinispan.util.logging.Log) AbstractDataCommand(org.infinispan.commands.read.AbstractDataCommand) SegmentSpecificCommand(org.infinispan.commands.SegmentSpecificCommand) MetadataImmortalCacheValue(org.infinispan.container.entries.metadata.MetadataImmortalCacheValue) ResponseCollectors(org.infinispan.remoting.transport.ResponseCollectors) Response(org.infinispan.remoting.responses.Response) ClusteringInterceptor(org.infinispan.interceptors.impl.ClusteringInterceptor) GetKeyValueCommand(org.infinispan.commands.read.GetKeyValueCommand) CacheException(org.infinispan.commons.CacheException) Iterator(java.util.Iterator) EvictCommand(org.infinispan.commands.write.EvictCommand) WriteOnlyManyCommand(org.infinispan.commands.functional.WriteOnlyManyCommand) PutMapCommand(org.infinispan.commands.write.PutMapCommand) ReadOnlyManyCommand(org.infinispan.commands.functional.ReadOnlyManyCommand) ComputeIfAbsentCommand(org.infinispan.commands.write.ComputeIfAbsentCommand) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) Configuration(org.infinispan.configuration.cache.Configuration) DistributionInfo(org.infinispan.distribution.DistributionInfo) Operation(org.infinispan.util.concurrent.DataOperationOrderer.Operation) WriteOnlyKeyCommand(org.infinispan.commands.functional.WriteOnlyKeyCommand) VisitableCommand(org.infinispan.commands.VisitableCommand) DeliverOrder(org.infinispan.remoting.inboundhandler.DeliverOrder) Collections(java.util.Collections) TimeService(org.infinispan.commons.time.TimeService) ScatteredVersionManager(org.infinispan.scattered.ScatteredVersionManager) ExceptionResponse(org.infinispan.remoting.responses.ExceptionResponse) CacheNotFoundResponse(org.infinispan.remoting.responses.CacheNotFoundResponse) UnsureResponse(org.infinispan.remoting.responses.UnsureResponse) 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) DistributionInfo(org.infinispan.distribution.DistributionInfo)

Example 13 with SuccessfulResponse

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

the class ScatteredDistributionInterceptor method visitReadOnlyManyCommand.

@Override
public Object visitReadOnlyManyCommand(InvocationContext ctx, ReadOnlyManyCommand command) throws Throwable {
    if (command.hasAnyFlag(FlagBitSets.CACHE_MODE_LOCAL | FlagBitSets.SKIP_REMOTE_LOOKUP)) {
        return handleLocalOnlyReadManyCommand(ctx, command, command.getKeys());
    }
    LocalizedCacheTopology cacheTopology = checkTopology(command);
    if (!ctx.isOriginLocal()) {
        return handleRemoteReadManyCommand(ctx, command, command.getKeys());
    }
    if (command.getKeys().isEmpty()) {
        return Stream.empty();
    }
    ConsistentHash ch = cacheTopology.getReadConsistentHash();
    int estimateForOneNode = 2 * command.getKeys().size() / ch.getMembers().size();
    Function<Address, List<Object>> createList = k -> new ArrayList<>(estimateForOneNode);
    Map<Address, List<Object>> requestedKeys = new HashMap<>();
    List<Object> localKeys = null;
    for (Object key : command.getKeys()) {
        if (ctx.lookupEntry(key) != null) {
            if (localKeys == null) {
                localKeys = new ArrayList<>();
            }
            localKeys.add(key);
            continue;
        }
        DistributionInfo info = cacheTopology.getDistribution(key);
        assert !info.isPrimary();
        if (info.primary() == null) {
            throw AllOwnersLostException.INSTANCE;
        }
        requestedKeys.computeIfAbsent(info.primary(), createList).add(key);
    }
    MergingCompletableFuture<Object> allFuture = new MergingCompletableFuture<>(requestedKeys.size() + (localKeys == null ? 0 : 1), new Object[command.getKeys().size()], Arrays::stream);
    int offset = 0;
    if (localKeys != null) {
        offset += localKeys.size();
        ReadOnlyManyCommand localCommand = new ReadOnlyManyCommand(command).withKeys(localKeys);
        invokeNextAndFinally(ctx, localCommand, (rCtx, rCommand, rv, throwable) -> {
            if (throwable != null) {
                allFuture.completeExceptionally(throwable);
            } else {
                try {
                    ((Stream) rv).collect(new ArrayCollector(allFuture.results));
                    allFuture.countDown();
                } catch (Throwable t) {
                    allFuture.completeExceptionally(t);
                }
            }
        });
    }
    for (Map.Entry<Address, List<Object>> addressKeys : requestedKeys.entrySet()) {
        List<Object> keysForAddress = addressKeys.getValue();
        ReadOnlyManyCommand remoteCommand = new ReadOnlyManyCommand(command).withKeys(keysForAddress);
        remoteCommand.setTopologyId(command.getTopologyId());
        Set<Address> target = Collections.singleton(addressKeys.getKey());
        int myOffset = offset;
        SingletonMapResponseCollector collector = SingletonMapResponseCollector.ignoreLeavers();
        CompletionStage<Map<Address, Response>> rpc = rpcManager.invokeCommand(target, remoteCommand, collector, rpcManager.getSyncRpcOptions());
        rpc.whenComplete((responseMap, throwable) -> {
            if (throwable != null) {
                allFuture.completeExceptionally(throwable);
                return;
            }
            SuccessfulResponse response = getSuccessfulResponseOrFail(responseMap, allFuture, rsp -> allFuture.completeExceptionally(rsp instanceof UnsureResponse ? OutdatedTopologyException.RETRY_NEXT_TOPOLOGY : AllOwnersLostException.INSTANCE));
            if (response == null) {
                return;
            }
            try {
                Object[] values = (Object[]) response.getResponseValue();
                if (values != null) {
                    System.arraycopy(values, 0, allFuture.results, myOffset, values.length);
                    allFuture.countDown();
                } else {
                    allFuture.completeExceptionally(new IllegalStateException("Unexpected response value " + response.getResponseValue()));
                }
            } catch (Throwable t) {
                allFuture.completeExceptionally(t);
            }
        });
        offset += keysForAddress.size();
    }
    return asyncValue(allFuture);
}
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) RemoteMetadata(org.infinispan.container.entries.RemoteMetadata) ClusteredGetAllCommand(org.infinispan.commands.remote.ClusteredGetAllCommand) Map(java.util.Map) RemoveCommand(org.infinispan.commands.write.RemoveCommand) ConsistentHash(org.infinispan.distribution.ch.ConsistentHash) EmbeddedMetadata(org.infinispan.metadata.EmbeddedMetadata) ReadWriteManyEntriesCommand(org.infinispan.commands.functional.ReadWriteManyEntriesCommand) UnsuccessfulResponse(org.infinispan.remoting.responses.UnsuccessfulResponse) Set(java.util.Set) GroupManager(org.infinispan.distribution.group.impl.GroupManager) CacheTopology(org.infinispan.topology.CacheTopology) ResponseCollector(org.infinispan.remoting.transport.ResponseCollector) DONT_LOAD(org.infinispan.commands.VisitableCommand.LoadType.DONT_LOAD) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) InternalCacheValue(org.infinispan.container.entries.InternalCacheValue) AbstractVisitor(org.infinispan.commands.AbstractVisitor) ArrayCollector(org.infinispan.commons.util.ArrayCollector) InternalCacheEntry(org.infinispan.container.entries.InternalCacheEntry) CacheEntryRemoved(org.infinispan.notifications.cachelistener.annotation.CacheEntryRemoved) PersistenceManager(org.infinispan.persistence.manager.PersistenceManager) ComputeCommand(org.infinispan.commands.write.ComputeCommand) MetadataAwareCommand(org.infinispan.commands.MetadataAwareCommand) EvictionManager(org.infinispan.eviction.EvictionManager) ReadOnlyKeyCommand(org.infinispan.commands.functional.ReadOnlyKeyCommand) ArrayList(java.util.ArrayList) WriteOnlyManyEntriesCommand(org.infinispan.commands.functional.WriteOnlyManyEntriesCommand) Start(org.infinispan.factories.annotations.Start) MapResponseCollector(org.infinispan.remoting.transport.impl.MapResponseCollector) PassthroughSingleResponseCollector(org.infinispan.remoting.transport.impl.PassthroughSingleResponseCollector) SingleResponseCollector(org.infinispan.remoting.transport.impl.SingleResponseCollector) DataWriteCommand(org.infinispan.commands.write.DataWriteCommand) FunctionalNotifier(org.infinispan.functional.impl.FunctionalNotifier) ValueMatcher(org.infinispan.commands.write.ValueMatcher) ClearCommand(org.infinispan.commands.write.ClearCommand) Address(org.infinispan.remoting.transport.Address) ExceptionResponse(org.infinispan.remoting.responses.ExceptionResponse) CacheEntry(org.infinispan.container.entries.CacheEntry) NotifyHelper(org.infinispan.notifications.cachelistener.NotifyHelper) WriteOnlyKeyValueCommand(org.infinispan.commands.functional.WriteOnlyKeyValueCommand) ValidResponse(org.infinispan.remoting.responses.ValidResponse) CacheNotifier(org.infinispan.notifications.cachelistener.CacheNotifier) Inject(org.infinispan.factories.annotations.Inject) ReadWriteKeyCommand(org.infinispan.commands.functional.ReadWriteKeyCommand) ReadWriteManyCommand(org.infinispan.commands.functional.ReadWriteManyCommand) NullCacheEntry(org.infinispan.container.entries.NullCacheEntry) AggregateCompletionStage(org.infinispan.util.concurrent.AggregateCompletionStage) ReplaceCommand(org.infinispan.commands.write.ReplaceCommand) EntryVersion(org.infinispan.container.versioning.EntryVersion) UnsureResponse(org.infinispan.remoting.responses.UnsureResponse) MultiTargetCollector(org.infinispan.util.concurrent.CommandAckCollector.MultiTargetCollector) GetKeysInGroupCommand(org.infinispan.commands.remote.GetKeysInGroupCommand) ActivationManager(org.infinispan.eviction.impl.ActivationManager) RepeatableReadEntry(org.infinispan.container.entries.RepeatableReadEntry) InvocationContext(org.infinispan.context.InvocationContext) CompletableFutures(org.infinispan.util.concurrent.CompletableFutures) OutdatedTopologyException(org.infinispan.statetransfer.OutdatedTopologyException) RpcOptions(org.infinispan.remoting.rpc.RpcOptions) Predicate(java.util.function.Predicate) Collection(java.util.Collection) ReadWriteKeyValueCommand(org.infinispan.commands.functional.ReadWriteKeyValueCommand) MetadataImmortalCacheEntry(org.infinispan.container.entries.metadata.MetadataImmortalCacheEntry) DataOperationOrderer(org.infinispan.util.concurrent.DataOperationOrderer) InequalVersionComparisonResult(org.infinispan.container.versioning.InequalVersionComparisonResult) InvocationSuccessFunction(org.infinispan.interceptors.InvocationSuccessFunction) List(java.util.List) SingletonMapResponseCollector(org.infinispan.remoting.transport.impl.SingletonMapResponseCollector) InvocationFinallyAction(org.infinispan.interceptors.InvocationFinallyAction) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand) FlagAffectedCommand(org.infinispan.commands.FlagAffectedCommand) TopologyAffectedCommand(org.infinispan.commands.TopologyAffectedCommand) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Metadata(org.infinispan.metadata.Metadata) CompletionStages(org.infinispan.util.concurrent.CompletionStages) GetAllCommand(org.infinispan.commands.read.GetAllCommand) Function(java.util.function.Function) AllOwnersLostException(org.infinispan.statetransfer.AllOwnersLostException) FlagBitSets(org.infinispan.context.impl.FlagBitSets) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) ClusteredGetCommand(org.infinispan.commands.remote.ClusteredGetCommand) IracPutKeyValueCommand(org.infinispan.commands.write.IracPutKeyValueCommand) Log(org.infinispan.util.logging.Log) AbstractDataCommand(org.infinispan.commands.read.AbstractDataCommand) SegmentSpecificCommand(org.infinispan.commands.SegmentSpecificCommand) MetadataImmortalCacheValue(org.infinispan.container.entries.metadata.MetadataImmortalCacheValue) ResponseCollectors(org.infinispan.remoting.transport.ResponseCollectors) Response(org.infinispan.remoting.responses.Response) ClusteringInterceptor(org.infinispan.interceptors.impl.ClusteringInterceptor) GetKeyValueCommand(org.infinispan.commands.read.GetKeyValueCommand) CacheException(org.infinispan.commons.CacheException) Iterator(java.util.Iterator) EvictCommand(org.infinispan.commands.write.EvictCommand) WriteOnlyManyCommand(org.infinispan.commands.functional.WriteOnlyManyCommand) PutMapCommand(org.infinispan.commands.write.PutMapCommand) ReadOnlyManyCommand(org.infinispan.commands.functional.ReadOnlyManyCommand) ComputeIfAbsentCommand(org.infinispan.commands.write.ComputeIfAbsentCommand) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) Configuration(org.infinispan.configuration.cache.Configuration) DistributionInfo(org.infinispan.distribution.DistributionInfo) Operation(org.infinispan.util.concurrent.DataOperationOrderer.Operation) WriteOnlyKeyCommand(org.infinispan.commands.functional.WriteOnlyKeyCommand) VisitableCommand(org.infinispan.commands.VisitableCommand) DeliverOrder(org.infinispan.remoting.inboundhandler.DeliverOrder) Collections(java.util.Collections) TimeService(org.infinispan.commons.time.TimeService) ScatteredVersionManager(org.infinispan.scattered.ScatteredVersionManager) ConsistentHash(org.infinispan.distribution.ch.ConsistentHash) ReadOnlyManyCommand(org.infinispan.commands.functional.ReadOnlyManyCommand) Address(org.infinispan.remoting.transport.Address) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayCollector(org.infinispan.commons.util.ArrayCollector) ArrayList(java.util.ArrayList) List(java.util.List) Stream(java.util.stream.Stream) SingletonMapResponseCollector(org.infinispan.remoting.transport.impl.SingletonMapResponseCollector) SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) UnsureResponse(org.infinispan.remoting.responses.UnsureResponse) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) DistributionInfo(org.infinispan.distribution.DistributionInfo) Arrays(java.util.Arrays) Map(java.util.Map) HashMap(java.util.HashMap)

Example 14 with SuccessfulResponse

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

the class ScatteredDistributionInterceptor method visitGetKeysInGroupCommand.

@Override
public final Object visitGetKeysInGroupCommand(InvocationContext ctx, GetKeysInGroupCommand command) throws Throwable {
    final Object groupName = command.getGroupName();
    if (command.isGroupOwner()) {
        // don't go remote if we are an owner.
        return invokeNext(ctx, command);
    }
    Address primary = distributionManager.getCacheTopology().getDistribution(groupName).primary();
    CompletionStage<Void> future = rpcManager.invokeCommand(primary, command, SingleResponseCollector.validOnly(), rpcManager.getSyncRpcOptions()).thenAccept(response -> {
        if (response instanceof SuccessfulResponse) {
            // noinspection unchecked
            List<CacheEntry> cacheEntries = (List<CacheEntry>) response.getResponseValue();
            for (CacheEntry entry : cacheEntries) {
                entryFactory.wrapExternalEntry(ctx, entry.getKey(), entry, true, false);
            }
        }
    });
    return asyncInvokeNext(ctx, command, future);
}
Also used : SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) Address(org.infinispan.remoting.transport.Address) ArrayList(java.util.ArrayList) List(java.util.List) 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)

Example 15 with SuccessfulResponse

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

the class ScatteredDistributionInterceptor method handleGetAllResponse.

private void handleGetAllResponse(Map<Address, Response> responseMap, Throwable throwable, InvocationContext ctx, List<?> keys, ClusteredGetAllFuture allFuture) {
    if (throwable != null) {
        allFuture.completeExceptionally(throwable);
        return;
    }
    // While upon lost owners in dist/repl mode we only return a map with less entries, in scattered mode
    // we need to retry the operation in next topology which should have the new primary owners assigned
    SuccessfulResponse response = getSuccessfulResponseOrFail(responseMap, allFuture, rsp -> allFuture.completeExceptionally(rsp instanceof UnsureResponse ? OutdatedTopologyException.RETRY_NEXT_TOPOLOGY : AllOwnersLostException.INSTANCE));
    if (response == null) {
        return;
    }
    Object responseValue = response.getResponseValue();
    if (!(responseValue instanceof InternalCacheValue[])) {
        allFuture.completeExceptionally(new IllegalStateException("Unexpected response value: " + responseValue));
        return;
    }
    InternalCacheValue[] values = (InternalCacheValue[]) responseValue;
    if (keys.size() != values.length) {
        allFuture.completeExceptionally(new CacheException("Request and response lengths differ: keys=" + keys + ", response=" + Arrays.toString(values)));
        return;
    }
    synchronized (allFuture) {
        if (allFuture.isDone()) {
            return;
        }
        for (int i = 0; i < values.length; ++i) {
            Object key = keys.get(i);
            InternalCacheValue value = values[i];
            CacheEntry entry = value == null ? NullCacheEntry.getInstance() : value.toInternalCacheEntry(key);
            entryFactory.wrapExternalEntry(ctx, key, entry, true, false);
        }
        if (--allFuture.counter == 0) {
            allFuture.complete(null);
        }
    }
}
Also used : SuccessfulResponse(org.infinispan.remoting.responses.SuccessfulResponse) CacheException(org.infinispan.commons.CacheException) UnsureResponse(org.infinispan.remoting.responses.UnsureResponse) 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) InternalCacheValue(org.infinispan.container.entries.InternalCacheValue)

Aggregations

SuccessfulResponse (org.infinispan.remoting.responses.SuccessfulResponse)22 Address (org.infinispan.remoting.transport.Address)16 Response (org.infinispan.remoting.responses.Response)15 Map (java.util.Map)11 HashMap (java.util.HashMap)8 CacheException (org.infinispan.commons.CacheException)7 CacheEntry (org.infinispan.container.entries.CacheEntry)7 InternalCacheValue (org.infinispan.container.entries.InternalCacheValue)7 InternalCacheEntry (org.infinispan.container.entries.InternalCacheEntry)6 CacheNotFoundResponse (org.infinispan.remoting.responses.CacheNotFoundResponse)6 UnsureResponse (org.infinispan.remoting.responses.UnsureResponse)6 ValidResponse (org.infinispan.remoting.responses.ValidResponse)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 CompletionStage (java.util.concurrent.CompletionStage)5 NullCacheEntry (org.infinispan.container.entries.NullCacheEntry)5 LocalizedCacheTopology (org.infinispan.distribution.LocalizedCacheTopology)5 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 ReadOnlyKeyCommand (org.infinispan.commands.functional.ReadOnlyKeyCommand)4