Search in sources :

Example 1 with DataWriteCommand

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

the class SingleKeyBackupWriteCommand method createWriteCommand.

@Override
WriteCommand createWriteCommand() {
    DataWriteCommand command;
    switch(operation) {
        case REMOVE:
            command = new RemoveCommand(key, null, segmentId, getFlags(), getCommandInvocationId());
            break;
        case WRITE:
            command = EnumUtil.containsAny(getFlags(), FlagBitSets.IRAC_UPDATE) ? new IracPutKeyValueCommand(key, segmentId, getCommandInvocationId(), valueOrFunction, metadata, internalMetadata) : new PutKeyValueCommand(key, valueOrFunction, false, metadata, segmentId, getFlags(), getCommandInvocationId());
            break;
        case COMPUTE:
            command = new ComputeCommand(key, (BiFunction) valueOrFunction, false, segmentId, getFlags(), getCommandInvocationId(), metadata);
            break;
        case REPLACE:
            command = new ReplaceCommand(key, null, valueOrFunction, metadata, segmentId, getFlags(), getCommandInvocationId());
            break;
        case REMOVE_EXPIRED:
            // Doesn't matter if it is max idle or not - important thing is that it raises expired event
            command = new RemoveExpiredCommand(key, valueOrFunction, null, false, segmentId, getFlags(), getCommandInvocationId());
            break;
        case COMPUTE_IF_PRESENT:
            command = new ComputeCommand(key, (BiFunction) valueOrFunction, true, segmentId, getFlags(), getCommandInvocationId(), metadata);
            break;
        case COMPUTE_IF_ABSENT:
            command = new ComputeIfAbsentCommand(key, (Function) valueOrFunction, segmentId, getFlags(), getCommandInvocationId(), metadata);
            break;
        default:
            throw new IllegalStateException("Unknown operation " + operation);
    }
    command.setInternalMetadata(internalMetadata);
    return command;
}
Also used : RemoveCommand(org.infinispan.commands.write.RemoveCommand) ComputeCommand(org.infinispan.commands.write.ComputeCommand) BiFunction(java.util.function.BiFunction) Function(java.util.function.Function) DataWriteCommand(org.infinispan.commands.write.DataWriteCommand) BiFunction(java.util.function.BiFunction) ReplaceCommand(org.infinispan.commands.write.ReplaceCommand) RemoveExpiredCommand(org.infinispan.commands.write.RemoveExpiredCommand) ComputeIfAbsentCommand(org.infinispan.commands.write.ComputeIfAbsentCommand) IracPutKeyValueCommand(org.infinispan.commands.write.IracPutKeyValueCommand) IracPutKeyValueCommand(org.infinispan.commands.write.IracPutKeyValueCommand) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand)

Example 2 with DataWriteCommand

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

the class ScatteredDistributionInterceptor method handleWriteOnOriginPrimary.

private Object handleWriteOnOriginPrimary(InvocationContext ctx, DataWriteCommand command, Object rv, RepeatableReadEntry cacheEntry, Object seenValue, EntryVersion seenVersion, CacheTopology cacheTopology, DistributionInfo info) {
    if (!command.isSuccessful()) {
        if (log.isTraceEnabled())
            log.tracef("Skipping the replication of the command as it did not succeed on primary owner (%s).", command);
        return rv;
    }
    // increment the version
    EntryVersion nextVersion = svm.incrementVersion(info.segmentId());
    Metadata metadata = addVersion(cacheEntry.getMetadata(), nextVersion);
    cacheEntry.setMetadata(metadata);
    CompletionStage<Void> stage;
    if (command.loadType() != DONT_LOAD) {
        stage = commitSingleEntryIfNoChange(cacheEntry, ctx, command);
    } else {
        stage = commitSingleEntryIfNewer(cacheEntry, ctx, command);
    }
    // When replicating to backup, we'll add skip ownership check since we're now on primary owner
    // and we have already committed the entry, reading the return value. If we got OTE from remote
    // site and the command would be retried, we could fail to do the retry/return wrong value.
    WriteCommand backupCommand;
    long flags = command.getFlagsBitSet() | FlagBitSets.SKIP_OWNERSHIP_CHECK;
    if (cacheEntry.isRemoved()) {
        backupCommand = cf.buildRemoveCommand(command.getKey(), null, info.segmentId(), flags);
        ((RemoveCommand) backupCommand).setMetadata(cacheEntry.getMetadata());
    } else {
        backupCommand = cf.buildPutKeyValueCommand(command.getKey(), cacheEntry.getValue(), info.segmentId(), cacheEntry.getMetadata(), flags);
    }
    backupCommand.setTopologyId(command.getTopologyId());
    Address backup = getNextMember(cacheTopology);
    if (backup != null) {
        // error responses throw exceptions from JGroupsTransport
        CompletionStage<?> rpcFuture = rpcManager.invokeCommand(backup, backupCommand, SingleResponseCollector.validOnly(), rpcManager.getSyncRpcOptions());
        rpcFuture.thenRun(() -> {
            if (cacheEntry.isCommitted() && !command.hasAnyFlag(FlagBitSets.PUT_FOR_STATE_TRANSFER)) {
                scheduleKeyInvalidation(command.getKey(), cacheEntry.getMetadata().version(), cacheEntry.isRemoved());
            }
        });
        rpcFuture = completeSingleWriteOnPrimaryOriginator(command, backup, rpcFuture);
        // Exception responses are thrown anyway and we don't expect any return values
        return delayedValue(CompletionStages.allOf(stage.toCompletableFuture(), rpcFuture.toCompletableFuture()), rv);
    } else {
        return delayedValue(stage, rv);
    }
}
Also used : WriteCommand(org.infinispan.commands.write.WriteCommand) DataWriteCommand(org.infinispan.commands.write.DataWriteCommand) RemoveCommand(org.infinispan.commands.write.RemoveCommand) EntryVersion(org.infinispan.container.versioning.EntryVersion) Address(org.infinispan.remoting.transport.Address) RemoteMetadata(org.infinispan.container.entries.RemoteMetadata) EmbeddedMetadata(org.infinispan.metadata.EmbeddedMetadata) Metadata(org.infinispan.metadata.Metadata)

Example 3 with DataWriteCommand

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

the class BaseBackupInterceptor method handleSingleKeyWriteReturn.

private Object handleSingleKeyWriteReturn(InvocationContext ctx, DataWriteCommand dataWriteCommand, Object rv) {
    if (!dataWriteCommand.isSuccessful()) {
        if (log.isTraceEnabled()) {
            log.tracef("Command %s is not successful, not replicating", dataWriteCommand);
        }
        return rv;
    }
    int segment = dataWriteCommand.getSegment();
    if (clusteringDependentLogic.getCacheTopology().getSegmentDistribution(segment).isPrimary()) {
        // primary owner always tracks updates to the remote sites (and sends the update in the background)
        iracManager.trackUpdatedKey(segment, dataWriteCommand.getKey(), dataWriteCommand.getCommandInvocationId());
        CacheEntry<?, ?> entry = ctx.lookupEntry(dataWriteCommand.getKey());
        WriteCommand crossSiteCommand = createCommandForXSite(entry, segment, dataWriteCommand.getFlagsBitSet());
        return backupSender.backupWrite(crossSiteCommand, dataWriteCommand).thenReturn(ctx, dataWriteCommand, rv);
    } else if (!ctx.isOriginLocal()) {
        // backup owners need to keep track of the update in the remote context for ASYNC cross-site
        // if backup owner == originator, we don't want to track the key again when ctx.isOriginLocal==true
        iracManager.trackUpdatedKey(segment, dataWriteCommand.getKey(), dataWriteCommand.getCommandInvocationId());
    }
    return rv;
}
Also used : WriteCommand(org.infinispan.commands.write.WriteCommand) DataWriteCommand(org.infinispan.commands.write.DataWriteCommand)

Example 4 with DataWriteCommand

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

the class ScatteredDistributionInterceptor method commitSingleEntryOnReturn.

private Object commitSingleEntryOnReturn(InvocationContext ctx, DataWriteCommand command, RepeatableReadEntry cacheEntry, EntryVersion nextVersion) {
    return invokeNextThenApply(ctx, command, (rCtx, rCommand, rv) -> {
        DataWriteCommand dataWriteCommand = (DataWriteCommand) rCommand;
        if (nextVersion != null) {
            cacheEntry.setMetadata(addVersion(cacheEntry.getMetadata(), nextVersion));
        }
        CompletionStage<Void> stage;
        if (command.loadType() != DONT_LOAD) {
            stage = commitSingleEntryIfNoChange(cacheEntry, rCtx, rCommand);
        } else {
            stage = commitSingleEntryIfNewer(cacheEntry, rCtx, dataWriteCommand);
        }
        if (cacheEntry.isCommitted() && rCtx.isOriginLocal() && nextVersion != null) {
            scheduleKeyInvalidation(dataWriteCommand.getKey(), nextVersion, cacheEntry.isRemoved());
        }
        return delayedValue(stage, rv);
    });
}
Also used : DataWriteCommand(org.infinispan.commands.write.DataWriteCommand)

Example 5 with DataWriteCommand

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

the class L1NonTxInterceptor method handleDataWriteCommand.

private Object handleDataWriteCommand(InvocationContext ctx, DataWriteCommand command, boolean assumeOriginKeptEntryInL1) {
    if (command.hasAnyFlag(FlagBitSets.CACHE_MODE_LOCAL)) {
        if (log.isTraceEnabled()) {
            log.tracef("local mode forced, suppressing L1 calls.");
        }
        return invokeNext(ctx, command);
    }
    CompletableFuture<?> l1InvalidationFuture = invalidateL1InCluster(ctx, command, assumeOriginKeptEntryInL1);
    return invokeNextAndHandle(ctx, command, (InvocationContext rCtx, DataWriteCommand dataWriteCommand, Object rv, Throwable ex) -> {
        if (ex != null) {
            if (mustSyncInvalidation(l1InvalidationFuture, dataWriteCommand)) {
                return asyncValue(l1InvalidationFuture).thenApply(rCtx, dataWriteCommand, (rCtx1, rCommand1, rv1) -> {
                    throw ex;
                });
            }
            throw ex;
        } else {
            if (mustSyncInvalidation(l1InvalidationFuture, dataWriteCommand)) {
                if (shouldRemoveFromLocalL1(rCtx, dataWriteCommand)) {
                    CompletionStage<VisitableCommand> removeFromL1CommandStage = removeFromL1Command(rCtx, dataWriteCommand.getKey(), dataWriteCommand.getSegment());
                    // TODO: To be fixed in https://issues.redhat.com/browse/ISPN-11125
                    VisitableCommand removeFromL1Command = CompletionStages.join(removeFromL1CommandStage);
                    return makeStage(asyncInvokeNext(rCtx, removeFromL1Command, l1InvalidationFuture)).thenApply(null, null, (rCtx2, rCommand2, rv2) -> rv);
                } else {
                    return asyncValue(l1InvalidationFuture).thenApply(rCtx, dataWriteCommand, (rCtx1, rCommand1, rv1) -> rv);
                }
            } else if (shouldRemoveFromLocalL1(rCtx, dataWriteCommand)) {
                CompletionStage<VisitableCommand> removeFromL1CommandStage = removeFromL1Command(rCtx, dataWriteCommand.getKey(), dataWriteCommand.getSegment());
                // TODO: To be fixed in https://issues.redhat.com/browse/ISPN-11125
                VisitableCommand removeFromL1Command = CompletionStages.join(removeFromL1CommandStage);
                return invokeNextThenApply(rCtx, removeFromL1Command, (rCtx2, rCommand2, rv2) -> rv);
            } else if (log.isTraceEnabled()) {
                log.trace("Allowing entry to commit as local node is owner");
            }
        }
        return rv;
    });
}
Also used : VisitableCommand(org.infinispan.commands.VisitableCommand) WriteCommand(org.infinispan.commands.write.WriteCommand) GetCacheEntryCommand(org.infinispan.commands.read.GetCacheEntryCommand) MultiSubCommandInvoker(org.infinispan.interceptors.impl.MultiSubCommandInvoker) LogFactory(org.infinispan.util.logging.LogFactory) ReplaceCommand(org.infinispan.commands.write.ReplaceCommand) TimeoutException(java.util.concurrent.TimeoutException) InvocationContext(org.infinispan.context.InvocationContext) CompletableFutures(org.infinispan.util.concurrent.CompletableFutures) RemoveCommand(org.infinispan.commands.write.RemoveCommand) CommandsFactory(org.infinispan.commands.CommandsFactory) Collection(java.util.Collection) ReadWriteManyEntriesCommand(org.infinispan.commands.functional.ReadWriteManyEntriesCommand) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ReadWriteKeyValueCommand(org.infinispan.commands.functional.ReadWriteKeyValueCommand) Set(java.util.Set) InvalidateL1Command(org.infinispan.commands.write.InvalidateL1Command) L1Manager(org.infinispan.distribution.L1Manager) CompletionStage(java.util.concurrent.CompletionStage) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand) FlagAffectedCommand(org.infinispan.commands.FlagAffectedCommand) DataCommand(org.infinispan.commands.DataCommand) InternalCacheEntry(org.infinispan.container.entries.InternalCacheEntry) ComputeCommand(org.infinispan.commands.write.ComputeCommand) CompletableFuture(java.util.concurrent.CompletableFuture) CompletionStages(org.infinispan.util.concurrent.CompletionStages) EnumUtil(org.infinispan.commons.util.EnumUtil) StateTransferLock(org.infinispan.statetransfer.StateTransferLock) ConcurrentMap(java.util.concurrent.ConcurrentMap) WriteOnlyManyEntriesCommand(org.infinispan.commands.functional.WriteOnlyManyEntriesCommand) Start(org.infinispan.factories.annotations.Start) HashSet(java.util.HashSet) FlagBitSets(org.infinispan.context.impl.FlagBitSets) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) Log(org.infinispan.util.logging.Log) AbstractDataCommand(org.infinispan.commands.read.AbstractDataCommand) DataWriteCommand(org.infinispan.commands.write.DataWriteCommand) BaseRpcInterceptor(org.infinispan.interceptors.impl.BaseRpcInterceptor) ClusteringDependentLogic(org.infinispan.interceptors.locking.ClusteringDependentLogic) GetKeyValueCommand(org.infinispan.commands.read.GetKeyValueCommand) Iterator(java.util.Iterator) InternalDataContainer(org.infinispan.container.impl.InternalDataContainer) WriteOnlyKeyValueCommand(org.infinispan.commands.functional.WriteOnlyKeyValueCommand) WriteOnlyManyCommand(org.infinispan.commands.functional.WriteOnlyManyCommand) PutMapCommand(org.infinispan.commands.write.PutMapCommand) EntryFactory(org.infinispan.container.impl.EntryFactory) Inject(org.infinispan.factories.annotations.Inject) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) ReadWriteKeyCommand(org.infinispan.commands.functional.ReadWriteKeyCommand) ComputeIfAbsentCommand(org.infinispan.commands.write.ComputeIfAbsentCommand) WriteOnlyKeyCommand(org.infinispan.commands.functional.WriteOnlyKeyCommand) VisitableCommand(org.infinispan.commands.VisitableCommand) ReadWriteManyCommand(org.infinispan.commands.functional.ReadWriteManyCommand) Collections(java.util.Collections) ClusteringConfiguration(org.infinispan.configuration.cache.ClusteringConfiguration) DataWriteCommand(org.infinispan.commands.write.DataWriteCommand) InvocationContext(org.infinispan.context.InvocationContext) CompletionStage(java.util.concurrent.CompletionStage)

Aggregations

DataWriteCommand (org.infinispan.commands.write.DataWriteCommand)8 RemoveCommand (org.infinispan.commands.write.RemoveCommand)4 WriteCommand (org.infinispan.commands.write.WriteCommand)4 ComputeCommand (org.infinispan.commands.write.ComputeCommand)3 ComputeIfAbsentCommand (org.infinispan.commands.write.ComputeIfAbsentCommand)3 PutKeyValueCommand (org.infinispan.commands.write.PutKeyValueCommand)3 ReplaceCommand (org.infinispan.commands.write.ReplaceCommand)3 Collection (java.util.Collection)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 CompletionStage (java.util.concurrent.CompletionStage)2 TimeUnit (java.util.concurrent.TimeUnit)2 DataCommand (org.infinispan.commands.DataCommand)2 FlagAffectedCommand (org.infinispan.commands.FlagAffectedCommand)2 ReadWriteKeyCommand (org.infinispan.commands.functional.ReadWriteKeyCommand)2 ReadWriteKeyValueCommand (org.infinispan.commands.functional.ReadWriteKeyValueCommand)2 ReadWriteManyCommand (org.infinispan.commands.functional.ReadWriteManyCommand)2 ReadWriteManyEntriesCommand (org.infinispan.commands.functional.ReadWriteManyEntriesCommand)2 WriteOnlyKeyCommand (org.infinispan.commands.functional.WriteOnlyKeyCommand)2 WriteOnlyKeyValueCommand (org.infinispan.commands.functional.WriteOnlyKeyValueCommand)2 WriteOnlyManyCommand (org.infinispan.commands.functional.WriteOnlyManyCommand)2