Search in sources :

Example 1 with AbstractCacheTransaction

use of org.infinispan.transaction.impl.AbstractCacheTransaction in project infinispan by infinispan.

the class VersionedDistributionInterceptor method unwrapFunctionalResultOnOrigin.

@Override
protected Object unwrapFunctionalResultOnOrigin(InvocationContext ctx, Object key, Object responseValue) {
    VersionedResult vr = (VersionedResult) responseValue;
    // As an optimization, read-only single-key commands are executed in SingleKeyNonTxInvocationContext
    if (ctx.isInTxScope()) {
        AbstractCacheTransaction tx = ((TxInvocationContext) ctx).getCacheTransaction();
        checkAndAddReadVersion(tx, key, vr.version);
    }
    return vr.result;
}
Also used : AbstractCacheTransaction(org.infinispan.transaction.impl.AbstractCacheTransaction) TxInvocationContext(org.infinispan.context.impl.TxInvocationContext)

Example 2 with AbstractCacheTransaction

use of org.infinispan.transaction.impl.AbstractCacheTransaction in project infinispan by infinispan.

the class AbstractTxLockingInterceptor method lockAllOrRegisterBackupLock.

/**
 * Same as {@link #lockOrRegisterBackupLock(TxInvocationContext, VisitableCommand, Object, long)}
 *
 * @return a collection with the keys locked.
 */
final InvocationStage lockAllOrRegisterBackupLock(TxInvocationContext<?> ctx, VisitableCommand command, Collection<?> keys, long lockTimeout) {
    if (keys.isEmpty()) {
        return InvocationStage.completedNullStage();
    }
    Collection<Object> keysToLock = new ArrayList<>(keys.size());
    AbstractCacheTransaction cacheTransaction = ctx.getCacheTransaction();
    LocalizedCacheTopology cacheTopology = cdl.getCacheTopology();
    for (Object key : keys) {
        // Skip keys that are already locked (when retrying a lock/prepare command)
        if (cacheTransaction.ownsLock(key))
            continue;
        switch(cacheTopology.getDistribution(key).writeOwnership()) {
            case PRIMARY:
                keysToLock.add(key);
                break;
            case BACKUP:
                cacheTransaction.addBackupLockForKey(key);
                break;
            default:
                break;
        }
    }
    if (keysToLock.isEmpty()) {
        return InvocationStage.completedNullStage();
    }
    return checkPendingAndLockAllKeys(ctx, command, keysToLock, lockTimeout);
}
Also used : AbstractCacheTransaction(org.infinispan.transaction.impl.AbstractCacheTransaction) ArrayList(java.util.ArrayList) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology)

Example 3 with AbstractCacheTransaction

use of org.infinispan.transaction.impl.AbstractCacheTransaction in project infinispan by infinispan.

the class TxQueryInterceptor method commitModificationsToIndexFuture.

private InvocationStage commitModificationsToIndexFuture(InvocationContext ctx, VisitableCommand cmd, Object rv) {
    TxInvocationContext txCtx = (TxInvocationContext) ctx;
    Map<Object, Object> removed = txOldValues.remove(txCtx.getGlobalTransaction());
    final Map<Object, Object> oldValues = removed == null ? Collections.emptyMap() : removed;
    AbstractCacheTransaction transaction = txCtx.getCacheTransaction();
    return asyncValue(CompletableFuture.allOf(transaction.getAllModifications().stream().filter(mod -> !mod.hasAnyFlag(FlagBitSets.SKIP_INDEXING)).flatMap(mod -> mod.getAffectedKeys().stream()).map(key -> {
        CacheEntry<?, ?> entry = txCtx.lookupEntry(key);
        if (entry != null) {
            Object oldValue = oldValues.getOrDefault(key, QueryInterceptor.UNKNOWN);
            return queryInterceptor.processChange(ctx, null, key, oldValue, entry.getValue());
        }
        return CompletableFutures.completedNull();
    }).toArray(CompletableFuture[]::new)));
}
Also used : PrepareCommand(org.infinispan.commands.tx.PrepareCommand) CacheEntry(org.infinispan.container.entries.CacheEntry) CompletableFuture(java.util.concurrent.CompletableFuture) AbstractCacheTransaction(org.infinispan.transaction.impl.AbstractCacheTransaction) CommitCommand(org.infinispan.commands.tx.CommitCommand) InvocationSuccessFunction(org.infinispan.interceptors.InvocationSuccessFunction) ConcurrentMap(java.util.concurrent.ConcurrentMap) FlagBitSets(org.infinispan.context.impl.FlagBitSets) InvocationContext(org.infinispan.context.InvocationContext) CompletableFutures(org.infinispan.util.concurrent.CompletableFutures) TxInvocationContext(org.infinispan.context.impl.TxInvocationContext) GlobalTransaction(org.infinispan.transaction.xa.GlobalTransaction) Map(java.util.Map) VisitableCommand(org.infinispan.commands.VisitableCommand) DDAsyncInterceptor(org.infinispan.interceptors.DDAsyncInterceptor) InvocationStage(org.infinispan.interceptors.InvocationStage) Collections(java.util.Collections) TxInvocationContext(org.infinispan.context.impl.TxInvocationContext) AbstractCacheTransaction(org.infinispan.transaction.impl.AbstractCacheTransaction) CacheEntry(org.infinispan.container.entries.CacheEntry)

Example 4 with AbstractCacheTransaction

use of org.infinispan.transaction.impl.AbstractCacheTransaction in project infinispan by infinispan.

the class VersionedDistributionInterceptor method wrapRemoteEntry.

@Override
protected void wrapRemoteEntry(InvocationContext ctx, Object key, CacheEntry ice, boolean isWrite) {
    if (ctx.isInTxScope()) {
        AbstractCacheTransaction cacheTransaction = ((TxInvocationContext<?>) ctx).getCacheTransaction();
        EntryVersion seenVersion = cacheTransaction.getVersionsRead().get(key);
        if (seenVersion != null) {
            IncrementableEntryVersion newVersion = versionFromEntry(ice);
            if (newVersion == null) {
                throw new IllegalStateException("Wrapping entry without version");
            }
            if (seenVersion.compareTo(newVersion) != InequalVersionComparisonResult.EQUAL) {
                if (ctx.lookupEntry(key) == null) {
                    // the full entry, but we cannot provide the same version as the already read one.
                    throw CONTAINER.writeSkewOnRead(key, key, seenVersion, newVersion);
                } else {
                    // We can safely ignore the newly fetched value.
                    return;
                }
            }
        }
    }
    super.wrapRemoteEntry(ctx, key, ice, isWrite);
}
Also used : EntryVersion(org.infinispan.container.versioning.EntryVersion) IncrementableEntryVersion(org.infinispan.container.versioning.IncrementableEntryVersion) AbstractCacheTransaction(org.infinispan.transaction.impl.AbstractCacheTransaction) TxInvocationContext(org.infinispan.context.impl.TxInvocationContext) IncrementableEntryVersion(org.infinispan.container.versioning.IncrementableEntryVersion)

Example 5 with AbstractCacheTransaction

use of org.infinispan.transaction.impl.AbstractCacheTransaction in project infinispan by infinispan.

the class CacheWriterInterceptor method commitModifications.

protected InvocationStage commitModifications(TxInvocationContext<AbstractCacheTransaction> ctx) throws Throwable {
    List<WriteCommand> allModifications = ctx.getCacheTransaction().getAllModifications();
    if (!allModifications.isEmpty()) {
        GlobalTransaction tx = ctx.getGlobalTransaction();
        if (log.isTraceEnabled())
            getLog().tracef("Persisting transaction %s modifications: %s", tx, allModifications);
        Transaction xaTx = null;
        try {
            xaTx = suspendRunningTx(ctx);
            return store(ctx);
        } finally {
            resumeRunningTx(xaTx);
        }
    } else {
        return null;
    }
}
Also used : WriteCommand(org.infinispan.commands.write.WriteCommand) DataWriteCommand(org.infinispan.commands.write.DataWriteCommand) Transaction(javax.transaction.Transaction) AbstractCacheTransaction(org.infinispan.transaction.impl.AbstractCacheTransaction) GlobalTransaction(org.infinispan.transaction.xa.GlobalTransaction) GlobalTransaction(org.infinispan.transaction.xa.GlobalTransaction)

Aggregations

AbstractCacheTransaction (org.infinispan.transaction.impl.AbstractCacheTransaction)5 TxInvocationContext (org.infinispan.context.impl.TxInvocationContext)3 GlobalTransaction (org.infinispan.transaction.xa.GlobalTransaction)2 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Transaction (javax.transaction.Transaction)1 VisitableCommand (org.infinispan.commands.VisitableCommand)1 CommitCommand (org.infinispan.commands.tx.CommitCommand)1 PrepareCommand (org.infinispan.commands.tx.PrepareCommand)1 DataWriteCommand (org.infinispan.commands.write.DataWriteCommand)1 WriteCommand (org.infinispan.commands.write.WriteCommand)1 CacheEntry (org.infinispan.container.entries.CacheEntry)1 EntryVersion (org.infinispan.container.versioning.EntryVersion)1 IncrementableEntryVersion (org.infinispan.container.versioning.IncrementableEntryVersion)1 InvocationContext (org.infinispan.context.InvocationContext)1 FlagBitSets (org.infinispan.context.impl.FlagBitSets)1 LocalizedCacheTopology (org.infinispan.distribution.LocalizedCacheTopology)1