Search in sources :

Example 1 with VersionedPrepareCommand

use of org.infinispan.commands.tx.VersionedPrepareCommand in project infinispan by infinispan.

the class WriteSkewHelper method performWriteSkewCheckAndReturnNewVersions.

public static CompletionStage<Map<Object, IncrementableEntryVersion>> performWriteSkewCheckAndReturnNewVersions(VersionedPrepareCommand prepareCommand, EntryLoader<?, ?> entryLoader, VersionGenerator versionGenerator, TxInvocationContext<?> context, KeySpecificLogic ksl, KeyPartitioner keyPartitioner) {
    if (prepareCommand.getVersionsSeen() == null) {
        // Do not perform the write skew check if this prepare command is being replayed for state transfer
        return CompletableFutures.completedEmptyMap();
    }
    Map<Object, IncrementableEntryVersion> uv = new HashMap<>();
    AggregateCompletionStage<Map<Object, IncrementableEntryVersion>> aggregateCompletionStage = CompletionStages.aggregateCompletionStage(uv);
    for (WriteCommand c : prepareCommand.getModifications()) {
        for (Object k : c.getAffectedKeys()) {
            int segment = SegmentSpecificCommand.extractSegment(c, k, keyPartitioner);
            if (ksl.performCheckOnSegment(segment)) {
                CacheEntry<?, ?> cacheEntry = context.lookupEntry(k);
                if (!(cacheEntry instanceof VersionedRepeatableReadEntry)) {
                    continue;
                }
                VersionedRepeatableReadEntry entry = (VersionedRepeatableReadEntry) cacheEntry;
                CompletionStage<Boolean> skewStage = entry.performWriteSkewCheck(entryLoader, segment, context, prepareCommand.getVersionsSeen().get(k), versionGenerator, c.hasAnyFlag(FlagBitSets.ROLLING_UPGRADE));
                aggregateCompletionStage.dependsOn(skewStage.thenAccept(passSkew -> {
                    if (!passSkew) {
                        throw new WriteSkewException("Write skew detected on key " + entry.getKey() + " for transaction " + context.getCacheTransaction(), entry.getKey());
                    }
                    IncrementableEntryVersion newVersion = incrementVersion(entry, versionGenerator);
                    // Have to synchronize as we could have returns on different threads due to notifications/loaders etc
                    synchronized (uv) {
                        uv.put(entry.getKey(), newVersion);
                    }
                }));
            }
        }
    }
    return aggregateCompletionStage.freeze();
}
Also used : WriteCommand(org.infinispan.commands.write.WriteCommand) IncrementableEntryVersion(org.infinispan.container.versioning.IncrementableEntryVersion) Response(org.infinispan.remoting.responses.Response) CacheEntry(org.infinispan.container.entries.CacheEntry) VersionGenerator(org.infinispan.container.versioning.VersionGenerator) HashMap(java.util.HashMap) CompletionStages(org.infinispan.util.concurrent.CompletionStages) EntryLoader(org.infinispan.persistence.util.EntryLoader) VersionedPrepareCommand(org.infinispan.commands.tx.VersionedPrepareCommand) FlagBitSets(org.infinispan.context.impl.FlagBitSets) CompletionStage(java.util.concurrent.CompletionStage) KeyPartitioner(org.infinispan.distribution.ch.KeyPartitioner) PrepareResponse(org.infinispan.remoting.responses.PrepareResponse) CompletableFutures(org.infinispan.util.concurrent.CompletableFutures) TxInvocationContext(org.infinispan.context.impl.TxInvocationContext) Map(java.util.Map) PrivateMetadata(org.infinispan.metadata.impl.PrivateMetadata) SegmentSpecificCommand(org.infinispan.commands.SegmentSpecificCommand) VersionedRepeatableReadEntry(org.infinispan.container.entries.VersionedRepeatableReadEntry) WriteSkewException(org.infinispan.transaction.WriteSkewException) AggregateCompletionStage(org.infinispan.util.concurrent.AggregateCompletionStage) WriteCommand(org.infinispan.commands.write.WriteCommand) HashMap(java.util.HashMap) VersionedRepeatableReadEntry(org.infinispan.container.entries.VersionedRepeatableReadEntry) WriteSkewException(org.infinispan.transaction.WriteSkewException) IncrementableEntryVersion(org.infinispan.container.versioning.IncrementableEntryVersion) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with VersionedPrepareCommand

use of org.infinispan.commands.tx.VersionedPrepareCommand in project infinispan by infinispan.

the class VersionedEntryWrappingInterceptor method prepareHandler.

private Object prepareHandler(InvocationContext nonTxCtx, VersionedPrepareCommand command, Object nil) {
    TxInvocationContext<?> ctx = (TxInvocationContext<?>) nonTxCtx;
    CompletionStage<Map<Object, IncrementableEntryVersion>> originVersionData;
    if (ctx.getCacheTransaction().isFromStateTransfer()) {
        storeEntryVersionForStateTransfer(ctx);
        originVersionData = CompletableFutures.completedNull();
    } else if (ctx.isOriginLocal()) {
        originVersionData = checkWriteSkew(ctx, command);
    } else {
        originVersionData = CompletableFutures.completedNull();
    }
    InvocationStage originVersionStage = makeStage(asyncInvokeNext(ctx, command, originVersionData));
    InvocationStage newVersionStage = originVersionStage.thenApplyMakeStage(ctx, command, (rCtx, rCommand, rv) -> {
        CompletionStage<Map<Object, IncrementableEntryVersion>> stage = rCtx.isOriginLocal() ? originVersionData : checkWriteSkew((TxInvocationContext<?>) rCtx, rCommand);
        return asyncValue(stage.thenApply(vMap -> mergeInPrepareResponse(vMap, asPrepareResponse(rv))));
    });
    return newVersionStage.thenApply(ctx, command, afterPrepareHandler);
}
Also used : PrepareCommand(org.infinispan.commands.tx.PrepareCommand) WriteCommand(org.infinispan.commands.write.WriteCommand) IncrementableEntryVersion(org.infinispan.container.versioning.IncrementableEntryVersion) CacheEntry(org.infinispan.container.entries.CacheEntry) LogFactory(org.infinispan.util.logging.LogFactory) VersionGenerator(org.infinispan.container.versioning.VersionGenerator) VersionedCommitCommand(org.infinispan.commands.tx.VersionedCommitCommand) WriteSkewHelper.mergeInPrepareResponse(org.infinispan.transaction.impl.WriteSkewHelper.mergeInPrepareResponse) CommitCommand(org.infinispan.commands.tx.CommitCommand) VersionedPrepareCommand(org.infinispan.commands.tx.VersionedPrepareCommand) Inject(org.infinispan.factories.annotations.Inject) InvocationSuccessFunction(org.infinispan.interceptors.InvocationSuccessFunction) PrepareResponse.asPrepareResponse(org.infinispan.remoting.responses.PrepareResponse.asPrepareResponse) InvocationContext(org.infinispan.context.InvocationContext) CompletionStage(java.util.concurrent.CompletionStage) CompletableFutures(org.infinispan.util.concurrent.CompletableFutures) Flag(org.infinispan.context.Flag) TxInvocationContext(org.infinispan.context.impl.TxInvocationContext) Map(java.util.Map) Log(org.infinispan.util.logging.Log) PrivateMetadata(org.infinispan.metadata.impl.PrivateMetadata) InvocationStage(org.infinispan.interceptors.InvocationStage) InvocationFinallyFunction(org.infinispan.interceptors.InvocationFinallyFunction) FlagAffectedCommand(org.infinispan.commands.FlagAffectedCommand) InvocationStage(org.infinispan.interceptors.InvocationStage) TxInvocationContext(org.infinispan.context.impl.TxInvocationContext) Map(java.util.Map)

Aggregations

Map (java.util.Map)2 CompletionStage (java.util.concurrent.CompletionStage)2 VersionedPrepareCommand (org.infinispan.commands.tx.VersionedPrepareCommand)2 WriteCommand (org.infinispan.commands.write.WriteCommand)2 CacheEntry (org.infinispan.container.entries.CacheEntry)2 IncrementableEntryVersion (org.infinispan.container.versioning.IncrementableEntryVersion)2 VersionGenerator (org.infinispan.container.versioning.VersionGenerator)2 TxInvocationContext (org.infinispan.context.impl.TxInvocationContext)2 PrivateMetadata (org.infinispan.metadata.impl.PrivateMetadata)2 CompletableFutures (org.infinispan.util.concurrent.CompletableFutures)2 HashMap (java.util.HashMap)1 FlagAffectedCommand (org.infinispan.commands.FlagAffectedCommand)1 SegmentSpecificCommand (org.infinispan.commands.SegmentSpecificCommand)1 CommitCommand (org.infinispan.commands.tx.CommitCommand)1 PrepareCommand (org.infinispan.commands.tx.PrepareCommand)1 VersionedCommitCommand (org.infinispan.commands.tx.VersionedCommitCommand)1 VersionedRepeatableReadEntry (org.infinispan.container.entries.VersionedRepeatableReadEntry)1 Flag (org.infinispan.context.Flag)1 InvocationContext (org.infinispan.context.InvocationContext)1 FlagBitSets (org.infinispan.context.impl.FlagBitSets)1