Search in sources :

Example 1 with VersionGenerator

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

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

the class MarshalledEntryUtil method createWithVersion.

public static <K, V> MarshallableEntry<K, V> createWithVersion(K key, V value, Cache cache) {
    ComponentRegistry registry = cache.getAdvancedCache().getComponentRegistry();
    MarshallableEntryFactory<K, V> entryFactory = registry.getComponent(MarshallableEntryFactory.class);
    VersionGenerator versionGenerator = registry.getVersionGenerator();
    PrivateMetadata metadata = new PrivateMetadata.Builder().entryVersion(versionGenerator.generateNew()).build();
    return entryFactory.create(key, value, null, metadata, -1, -1);
}
Also used : ComponentRegistry(org.infinispan.factories.ComponentRegistry) VersionGenerator(org.infinispan.container.versioning.VersionGenerator) PrivateMetadata(org.infinispan.metadata.impl.PrivateMetadata)

Example 3 with VersionGenerator

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

the class PartialResponse method generateVersion.

private EntryVersion generateVersion(Cache<byte[], byte[]> cache) {
    ComponentRegistry registry = getCacheRegistry();
    VersionGenerator cacheVersionGenerator = registry.getComponent(VersionGenerator.class);
    if (cacheVersionGenerator == null) {
        NumericVersionGenerator newVersionGenerator = new NumericVersionGenerator();
        registry.registerComponent(newVersionGenerator, VersionGenerator.class);
        return newVersionGenerator.generateNew();
    } else {
        return cacheVersionGenerator.generateNew();
    }
}
Also used : ComponentRegistry(org.infinispan.factories.ComponentRegistry) NumericVersionGenerator(org.infinispan.container.versioning.NumericVersionGenerator) VersionGenerator(org.infinispan.container.versioning.VersionGenerator) NumericVersionGenerator(org.infinispan.container.versioning.NumericVersionGenerator)

Aggregations

VersionGenerator (org.infinispan.container.versioning.VersionGenerator)3 ComponentRegistry (org.infinispan.factories.ComponentRegistry)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 VersionedRepeatableReadEntry (org.infinispan.container.entries.VersionedRepeatableReadEntry)1 IncrementableEntryVersion (org.infinispan.container.versioning.IncrementableEntryVersion)1 NumericVersionGenerator (org.infinispan.container.versioning.NumericVersionGenerator)1 FlagBitSets (org.infinispan.context.impl.FlagBitSets)1 TxInvocationContext (org.infinispan.context.impl.TxInvocationContext)1 KeyPartitioner (org.infinispan.distribution.ch.KeyPartitioner)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