Search in sources :

Example 1 with EntryLoader

use of org.infinispan.persistence.util.EntryLoader 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)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 CompletionStage (java.util.concurrent.CompletionStage)1 SegmentSpecificCommand (org.infinispan.commands.SegmentSpecificCommand)1 VersionedPrepareCommand (org.infinispan.commands.tx.VersionedPrepareCommand)1 WriteCommand (org.infinispan.commands.write.WriteCommand)1 CacheEntry (org.infinispan.container.entries.CacheEntry)1 VersionedRepeatableReadEntry (org.infinispan.container.entries.VersionedRepeatableReadEntry)1 IncrementableEntryVersion (org.infinispan.container.versioning.IncrementableEntryVersion)1 VersionGenerator (org.infinispan.container.versioning.VersionGenerator)1 FlagBitSets (org.infinispan.context.impl.FlagBitSets)1 TxInvocationContext (org.infinispan.context.impl.TxInvocationContext)1 KeyPartitioner (org.infinispan.distribution.ch.KeyPartitioner)1 PrivateMetadata (org.infinispan.metadata.impl.PrivateMetadata)1 EntryLoader (org.infinispan.persistence.util.EntryLoader)1 PrepareResponse (org.infinispan.remoting.responses.PrepareResponse)1 Response (org.infinispan.remoting.responses.Response)1 WriteSkewException (org.infinispan.transaction.WriteSkewException)1 AggregateCompletionStage (org.infinispan.util.concurrent.AggregateCompletionStage)1 CompletableFutures (org.infinispan.util.concurrent.CompletableFutures)1