Search in sources :

Example 1 with IncrementableEntryVersion

use of org.infinispan.container.versioning.IncrementableEntryVersion 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 IncrementableEntryVersion

use of org.infinispan.container.versioning.IncrementableEntryVersion in project infinispan by infinispan.

the class TxDistributionInterceptor method checkCacheNotFoundResponseInPartitionHandling.

private boolean checkCacheNotFoundResponseInPartitionHandling(TransactionBoundaryCommand command, TxInvocationContext<LocalTransaction> context, Collection<Address> recipients) {
    final GlobalTransaction globalTransaction = command.getGlobalTransaction();
    final Collection<Object> lockedKeys = context.getLockedKeys();
    if (command instanceof RollbackCommand) {
        return partitionHandlingManager.addPartialRollbackTransaction(globalTransaction, recipients, lockedKeys);
    } else if (command instanceof PrepareCommand) {
        if (((PrepareCommand) command).isOnePhaseCommit()) {
            return partitionHandlingManager.addPartialCommit1PCTransaction(globalTransaction, recipients, lockedKeys, Arrays.asList(((PrepareCommand) command).getModifications()));
        }
    } else if (command instanceof CommitCommand) {
        Map<Object, IncrementableEntryVersion> newVersion = null;
        if (command instanceof VersionedCommitCommand) {
            newVersion = ((VersionedCommitCommand) command).getUpdatedVersions();
        }
        return partitionHandlingManager.addPartialCommit2PCTransaction(globalTransaction, recipients, lockedKeys, newVersion);
    }
    return false;
}
Also used : VersionedCommitCommand(org.infinispan.commands.tx.VersionedCommitCommand) IncrementableEntryVersion(org.infinispan.container.versioning.IncrementableEntryVersion) GlobalTransaction(org.infinispan.transaction.xa.GlobalTransaction) RollbackCommand(org.infinispan.commands.tx.RollbackCommand) PrepareCommand(org.infinispan.commands.tx.PrepareCommand) VersionedCommitCommand(org.infinispan.commands.tx.VersionedCommitCommand) CommitCommand(org.infinispan.commands.tx.CommitCommand)

Example 3 with IncrementableEntryVersion

use of org.infinispan.container.versioning.IncrementableEntryVersion in project infinispan by infinispan.

the class EntryWrappingInterceptor method addVersionRead.

private void addVersionRead(TxInvocationContext<?> rCtx, CacheEntry<?, ?> cacheEntry, Object key) {
    IncrementableEntryVersion version = versionFromEntry(cacheEntry);
    if (version == null) {
        version = versionGenerator.nonExistingVersion();
        if (log.isTraceEnabled()) {
            log.tracef("Adding non-existent version read for key %s", key);
        }
    } else if (log.isTraceEnabled()) {
        log.tracef("Adding version read %s for key %s", version, key);
    }
    rCtx.getCacheTransaction().addVersionRead(key, version);
}
Also used : IncrementableEntryVersion(org.infinispan.container.versioning.IncrementableEntryVersion)

Example 4 with IncrementableEntryVersion

use of org.infinispan.container.versioning.IncrementableEntryVersion in project infinispan by infinispan.

the class IracWriteSkewTest method checkKey.

private void checkKey(String key, String value) {
    // irac version is the same in all nodes & sites. extract it from one site and check everywhere.
    IracEntryVersion iracVersion = extractIracEntryVersion(key);
    assertNotNull(iracVersion);
    assertIracEntryVersion(key, value, iracVersion);
    // NYC has the EntryVersion for write-skew check
    IncrementableEntryVersion entryVersion = extractEntryVersion(key);
    assertNotNull(entryVersion);
    assertIracEntryVersion(key, value, iracVersion, entryVersion);
}
Also used : IracEntryVersion(org.infinispan.container.versioning.irac.IracEntryVersion) IncrementableEntryVersion(org.infinispan.container.versioning.IncrementableEntryVersion)

Example 5 with IncrementableEntryVersion

use of org.infinispan.container.versioning.IncrementableEntryVersion in project infinispan by infinispan.

the class VersionedDistributionInterceptor method wrapFunctionalManyResultOnNonOrigin.

@Override
protected Object wrapFunctionalManyResultOnNonOrigin(InvocationContext ctx, Collection<?> keys, Object[] values) {
    // note: this relies on the fact that keys are already ordered on remote node
    EntryVersion[] versions = new EntryVersion[keys.size()];
    int i = 0;
    for (Object key : keys) {
        IncrementableEntryVersion version = versionFromEntry(ctx.lookupEntry(key));
        versions[i++] = version == null ? versionGenerator.nonExistingVersion() : version;
    }
    return new VersionedResults(values, versions);
}
Also used : EntryVersion(org.infinispan.container.versioning.EntryVersion) IncrementableEntryVersion(org.infinispan.container.versioning.IncrementableEntryVersion) IncrementableEntryVersion(org.infinispan.container.versioning.IncrementableEntryVersion)

Aggregations

IncrementableEntryVersion (org.infinispan.container.versioning.IncrementableEntryVersion)9 PrivateMetadata (org.infinispan.metadata.impl.PrivateMetadata)4 WriteCommand (org.infinispan.commands.write.WriteCommand)3 TxInvocationContext (org.infinispan.context.impl.TxInvocationContext)3 Map (java.util.Map)2 CompletionStage (java.util.concurrent.CompletionStage)2 CommitCommand (org.infinispan.commands.tx.CommitCommand)2 PrepareCommand (org.infinispan.commands.tx.PrepareCommand)2 VersionedCommitCommand (org.infinispan.commands.tx.VersionedCommitCommand)2 VersionedPrepareCommand (org.infinispan.commands.tx.VersionedPrepareCommand)2 CacheEntry (org.infinispan.container.entries.CacheEntry)2 EntryVersion (org.infinispan.container.versioning.EntryVersion)2 VersionGenerator (org.infinispan.container.versioning.VersionGenerator)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 RollbackCommand (org.infinispan.commands.tx.RollbackCommand)1 VersionedRepeatableReadEntry (org.infinispan.container.entries.VersionedRepeatableReadEntry)1 IracEntryVersion (org.infinispan.container.versioning.irac.IracEntryVersion)1