Search in sources :

Example 1 with AbstractDataWriteCommand

use of org.infinispan.commands.write.AbstractDataWriteCommand in project infinispan by infinispan.

the class BaseDistributionInterceptor method primaryReturnHandler.

protected Object primaryReturnHandler(InvocationContext ctx, AbstractDataWriteCommand command, Object localResult) {
    if (!command.isSuccessful()) {
        if (log.isTraceEnabled())
            log.tracef("Skipping the replication of the conditional command as it did not succeed on primary owner (%s).", command);
        return localResult;
    }
    LocalizedCacheTopology cacheTopology = checkTopologyId(command);
    int segment = SegmentSpecificCommand.extractSegment(command, command.getKey(), keyPartitioner);
    DistributionInfo distributionInfo = cacheTopology.getSegmentDistribution(segment);
    Collection<Address> owners = distributionInfo.writeOwners();
    if (owners.size() == 1) {
        // There are no backups, skip the replication part.
        return localResult;
    }
    // Cache the matcher and reset it if we get OOTE (or any other exception) from backup
    ValueMatcher originalMatcher = command.getValueMatcher();
    // Ignore the previous value on the backup owners
    command.setValueMatcher(ValueMatcher.MATCH_ALWAYS);
    if (!isSynchronous(command)) {
        if (isReplicated) {
            rpcManager.sendToAll(command, DeliverOrder.PER_SENDER);
        } else {
            rpcManager.sendToMany(owners, command, DeliverOrder.PER_SENDER);
        }
        // Switch to the retry policy, in case the primary owner changes before we commit locally
        command.setValueMatcher(originalMatcher.matcherForRetry());
        return localResult;
    }
    VoidResponseCollector collector = VoidResponseCollector.ignoreLeavers();
    RpcOptions rpcOptions = rpcManager.getSyncRpcOptions();
    // Mark the command as a backup write so it can skip some checks
    command.addFlags(FlagBitSets.BACKUP_WRITE);
    CompletionStage<Void> remoteInvocation = isReplicated ? rpcManager.invokeCommandOnAll(command, collector, rpcOptions) : rpcManager.invokeCommand(owners, command, collector, rpcOptions);
    return asyncValue(remoteInvocation.handle((ignored, t) -> {
        // Unset the backup write bit as the command will be retried
        command.setFlagsBitSet(command.getFlagsBitSet() & ~FlagBitSets.BACKUP_WRITE);
        // Switch to the retry policy, in case the primary owner changed and the write already succeeded on the new primary
        command.setValueMatcher(originalMatcher.matcherForRetry());
        CompletableFutures.rethrowExceptionIfPresent(t);
        return localResult;
    }));
}
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) RpcOptions(org.infinispan.remoting.rpc.RpcOptions) Address(org.infinispan.remoting.transport.Address) ValueMatcher(org.infinispan.commands.write.ValueMatcher) VoidResponseCollector(org.infinispan.remoting.transport.impl.VoidResponseCollector) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) DistributionInfo(org.infinispan.distribution.DistributionInfo)

Example 2 with AbstractDataWriteCommand

use of org.infinispan.commands.write.AbstractDataWriteCommand in project infinispan by infinispan.

the class AnchoredDistributionInterceptor method primaryReturnHandler.

@Override
protected Object primaryReturnHandler(InvocationContext ctx, AbstractDataWriteCommand command, Object localResult) {
    if (!command.isSuccessful()) {
        if (log.isTraceEnabled())
            log.tracef("Skipping the replication of the conditional command as it did not succeed on primary owner (%s).", command);
        return localResult;
    }
    LocalizedCacheTopology cacheTopology = checkTopologyId(command);
    DistributionInfo distributionInfo = cacheTopology.getSegmentDistribution(command.getSegment());
    Collection<Address> owners = distributionInfo.writeOwners();
    if (owners.size() == 1) {
        // There are no backups, skip the replication part.
        return localResult;
    }
    // Match always on the backups, but save the original matcher for retries
    ValueMatcher originalMatcher = command.getValueMatcher();
    command.setValueMatcher(ValueMatcher.MATCH_ALWAYS);
    CommandCopier commandCopier = new CommandCopier(ctx, command);
    // Ignore the previous value on the backup owners
    assert isSynchronous(command);
    MapResponseCollector collector = MapResponseCollector.ignoreLeavers(isReplicated, owners.size());
    RpcOptions rpcOptions = rpcManager.getSyncRpcOptions();
    CompletionStage<Map<Address, Response>> remoteInvocation = rpcManager.invokeCommands(distributionInfo.writeBackups(), commandCopier, collector, rpcOptions);
    return asyncValue(remoteInvocation.handle((responses, t) -> {
        // Switch to the retry policy, in case the primary owner changed
        // and the write already succeeded on the new primary
        command.setValueMatcher(originalMatcher.matcherForRetry());
        CompletableFutures.rethrowExceptionIfPresent(t instanceof RemoteException ? t.getCause() : t);
        return localResult;
    }));
}
Also used : WriteCommand(org.infinispan.commands.write.WriteCommand) AbstractVisitor(org.infinispan.commands.AbstractVisitor) LogFactory(org.infinispan.util.logging.LogFactory) ReplaceCommand(org.infinispan.commands.write.ReplaceCommand) RemoteMetadata(org.infinispan.container.entries.RemoteMetadata) HashMap(java.util.HashMap) Metadata(org.infinispan.metadata.Metadata) Function(java.util.function.Function) MapResponseCollector(org.infinispan.remoting.transport.impl.MapResponseCollector) AbstractDataWriteCommand(org.infinispan.commands.write.AbstractDataWriteCommand) InvocationContext(org.infinispan.context.InvocationContext) CompletableFutures(org.infinispan.util.concurrent.CompletableFutures) Map(java.util.Map) Log(org.infinispan.util.logging.Log) DataWriteCommand(org.infinispan.commands.write.DataWriteCommand) ValueMatcher(org.infinispan.commands.write.ValueMatcher) Address(org.infinispan.remoting.transport.Address) RpcOptions(org.infinispan.remoting.rpc.RpcOptions) Response(org.infinispan.remoting.responses.Response) RemoveCommand(org.infinispan.commands.write.RemoveCommand) WriteManyCommandHelper(org.infinispan.interceptors.distribution.WriteManyCommandHelper) CommandsFactory(org.infinispan.commands.CommandsFactory) ReplicableCommand(org.infinispan.commands.ReplicableCommand) CacheException(org.infinispan.commons.CacheException) CacheEntry(org.infinispan.container.entries.CacheEntry) Collection(java.util.Collection) PutMapCommand(org.infinispan.commands.write.PutMapCommand) Inject(org.infinispan.factories.annotations.Inject) IntSet(org.infinispan.commons.util.IntSet) CompletionStage(java.util.concurrent.CompletionStage) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand) NonTxDistributionInterceptor(org.infinispan.interceptors.distribution.NonTxDistributionInterceptor) DistributionInfo(org.infinispan.distribution.DistributionInfo) VisitableCommand(org.infinispan.commands.VisitableCommand) RemoteException(org.infinispan.remoting.RemoteException) RpcOptions(org.infinispan.remoting.rpc.RpcOptions) Address(org.infinispan.remoting.transport.Address) ValueMatcher(org.infinispan.commands.write.ValueMatcher) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) RemoteException(org.infinispan.remoting.RemoteException) HashMap(java.util.HashMap) Map(java.util.Map) DistributionInfo(org.infinispan.distribution.DistributionInfo) MapResponseCollector(org.infinispan.remoting.transport.impl.MapResponseCollector)

Aggregations

Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CompletionStage (java.util.concurrent.CompletionStage)2 Function (java.util.function.Function)2 ReplicableCommand (org.infinispan.commands.ReplicableCommand)2 VisitableCommand (org.infinispan.commands.VisitableCommand)2 AbstractDataWriteCommand (org.infinispan.commands.write.AbstractDataWriteCommand)2 DataWriteCommand (org.infinispan.commands.write.DataWriteCommand)2 ValueMatcher (org.infinispan.commands.write.ValueMatcher)2 WriteCommand (org.infinispan.commands.write.WriteCommand)2 CacheEntry (org.infinispan.container.entries.CacheEntry)2 InvocationContext (org.infinispan.context.InvocationContext)2 DistributionInfo (org.infinispan.distribution.DistributionInfo)2 LocalizedCacheTopology (org.infinispan.distribution.LocalizedCacheTopology)2 Inject (org.infinispan.factories.annotations.Inject)2 Response (org.infinispan.remoting.responses.Response)2 RpcOptions (org.infinispan.remoting.rpc.RpcOptions)2 Address (org.infinispan.remoting.transport.Address)2 MapResponseCollector (org.infinispan.remoting.transport.impl.MapResponseCollector)2