Search in sources :

Example 1 with VersionedRepeatableReadEntry

use of org.infinispan.container.entries.VersionedRepeatableReadEntry 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 VersionedRepeatableReadEntry

use of org.infinispan.container.entries.VersionedRepeatableReadEntry in project infinispan by infinispan.

the class EntryFactoryImpl method createWrappedEntry.

protected MVCCEntry<?, ?> createWrappedEntry(Object key, CacheEntry<?, ?> cacheEntry) {
    Object value = null;
    Metadata metadata = null;
    PrivateMetadata internalMetadata = null;
    if (cacheEntry != null) {
        synchronized (cacheEntry) {
            value = cacheEntry.getValue();
            metadata = cacheEntry.getMetadata();
            internalMetadata = cacheEntry.getInternalMetadata();
        }
    }
    if (log.isTraceEnabled())
        log.tracef("Creating new entry for key %s", toStr(key));
    MVCCEntry<?, ?> mvccEntry;
    if (useRepeatableRead) {
        if (useVersioning) {
            if (internalMetadata == null) {
                internalMetadata = nonExistingPrivateMetadata;
            }
            mvccEntry = new VersionedRepeatableReadEntry(key, value, metadata);
        } else {
            mvccEntry = new RepeatableReadEntry(key, value, metadata);
        }
    } else {
        mvccEntry = new ReadCommittedEntry(key, value, metadata);
    }
    mvccEntry.setInternalMetadata(internalMetadata);
    if (cacheEntry != null) {
        mvccEntry.setCreated(cacheEntry.getCreated());
        mvccEntry.setLastUsed(cacheEntry.getLastUsed());
    }
    return mvccEntry;
}
Also used : ReadCommittedEntry(org.infinispan.container.entries.ReadCommittedEntry) RepeatableReadEntry(org.infinispan.container.entries.RepeatableReadEntry) VersionedRepeatableReadEntry(org.infinispan.container.entries.VersionedRepeatableReadEntry) Metadata(org.infinispan.metadata.Metadata) PrivateMetadata(org.infinispan.metadata.impl.PrivateMetadata) PrivateMetadata(org.infinispan.metadata.impl.PrivateMetadata) VersionedRepeatableReadEntry(org.infinispan.container.entries.VersionedRepeatableReadEntry)

Aggregations

VersionedRepeatableReadEntry (org.infinispan.container.entries.VersionedRepeatableReadEntry)2 PrivateMetadata (org.infinispan.metadata.impl.PrivateMetadata)2 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 ReadCommittedEntry (org.infinispan.container.entries.ReadCommittedEntry)1 RepeatableReadEntry (org.infinispan.container.entries.RepeatableReadEntry)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 Metadata (org.infinispan.metadata.Metadata)1 EntryLoader (org.infinispan.persistence.util.EntryLoader)1 PrepareResponse (org.infinispan.remoting.responses.PrepareResponse)1 Response (org.infinispan.remoting.responses.Response)1