Search in sources :

Example 1 with ReadOnlyManyCommand

use of org.infinispan.commands.functional.ReadOnlyManyCommand in project infinispan by infinispan.

the class BaseDistributionInterceptor method fetchAndApplyValues.

private <C extends TopologyAffectedCommand & FlagAffectedCommand> MergingCompletableFuture<Object> fetchAndApplyValues(InvocationContext ctx, C command, ReadManyCommandHelper<C> helper, Collection<?> keys, List<Object> availableKeys, Map<Address, List<Object>> requestedKeys) {
    // TODO: while this works in a non-blocking way, the returned stream is not lazy as the functional
    // contract suggests. Traversable is also not honored as it is executed only locally on originator.
    // On FutureMode.ASYNC, there should be one command per target node going from the top level
    // to allow retries in StateTransferInterceptor in case of topology change.
    MergingCompletableFuture<Object> allFuture = new MergingCompletableFuture<>(requestedKeys.size() + (availableKeys.isEmpty() ? 0 : 1), new Object[keys.size()], helper::transformResult);
    handleLocallyAvailableKeys(ctx, command, availableKeys, allFuture, helper);
    int pos = availableKeys.size();
    for (Map.Entry<Address, List<Object>> addressKeys : requestedKeys.entrySet()) {
        List<Object> keysForAddress = addressKeys.getValue();
        ReadOnlyManyCommand remoteCommand = helper.copyForRemote(command, keysForAddress, ctx);
        remoteCommand.setTopologyId(command.getTopologyId());
        Address target = addressKeys.getKey();
        rpcManager.invokeCommand(target, remoteCommand, SingletonMapResponseCollector.ignoreLeavers(), rpcManager.getSyncRpcOptions()).whenComplete(new ReadManyHandler(target, allFuture, ctx, command, keysForAddress, null, pos, helper));
        pos += keysForAddress.size();
    }
    return allFuture;
}
Also used : ReadOnlyManyCommand(org.infinispan.commands.functional.ReadOnlyManyCommand) Address(org.infinispan.remoting.transport.Address) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with ReadOnlyManyCommand

use of org.infinispan.commands.functional.ReadOnlyManyCommand 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)

Aggregations

ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 ReadOnlyManyCommand (org.infinispan.commands.functional.ReadOnlyManyCommand)2 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletionStage (java.util.concurrent.CompletionStage)1 Function (java.util.function.Function)1 Predicate (java.util.function.Predicate)1 Stream (java.util.stream.Stream)1 AbstractVisitor (org.infinispan.commands.AbstractVisitor)1 FlagAffectedCommand (org.infinispan.commands.FlagAffectedCommand)1 MetadataAwareCommand (org.infinispan.commands.MetadataAwareCommand)1 SegmentSpecificCommand (org.infinispan.commands.SegmentSpecificCommand)1 TopologyAffectedCommand (org.infinispan.commands.TopologyAffectedCommand)1