Search in sources :

Example 11 with InvocationStage

use of org.infinispan.interceptors.InvocationStage in project infinispan by infinispan.

the class OptimisticLockingInterceptor method visitPrepareCommand.

@Override
public Object visitPrepareCommand(TxInvocationContext ctx, PrepareCommand command) throws Throwable {
    final Collection<?> keysToLock = command.getKeysToLock();
    InvocationStage lockStage = InvocationStage.completedNullStage();
    ((TxInvocationContext<?>) ctx).addAllAffectedKeys(command.getAffectedKeys());
    if (!keysToLock.isEmpty()) {
        if (command.isRetriedCommand()) {
            // Don't keep backup locks if the local node is the primary owner in the current topology
            // The lock/prepare command is being retried, so it's not a "pending" transaction
            // However, keep the backup locks if the prepare command is being replayed because of state transfer
            ctx.getCacheTransaction().cleanupBackupLocks();
        }
        lockStage = lockAllOrRegisterBackupLock(ctx, command, keysToLock, command.hasZeroLockAcquisition() ? 0 : cacheConfiguration.locking().lockAcquisitionTimeout());
    }
    if (command.isOnePhaseCommit()) {
        return lockStage.thenApply(ctx, command, onePhaseCommitFunction);
    } else {
        return asyncInvokeNext(ctx, command, lockStage);
    }
}
Also used : InvocationStage(org.infinispan.interceptors.InvocationStage) TxInvocationContext(org.infinispan.context.impl.TxInvocationContext)

Example 12 with InvocationStage

use of org.infinispan.interceptors.InvocationStage in project infinispan by infinispan.

the class ProtobufMetadataManagerInterceptor method visitPutMapCommand.

@Override
public Object visitPutMapCommand(InvocationContext ctx, PutMapCommand command) {
    if (!ctx.isOriginLocal()) {
        return invokeNext(ctx, command);
    }
    final Map<Object, Object> map = command.getMap();
    FileDescriptorSource source = new FileDescriptorSource();
    for (Object key : map.keySet()) {
        final Object value = map.get(key);
        if (!(key instanceof String)) {
            throw log.keyMustBeString(key.getClass());
        }
        if (!(value instanceof String)) {
            throw log.valueMustBeString(value.getClass());
        }
        if (shouldIntercept(key)) {
            if (!((String) key).endsWith(PROTO_KEY_SUFFIX)) {
                throw log.keyMustBeStringEndingWithProto(key);
            }
            source.addProtoFile((String) key, (String) value);
        }
    }
    // lock global errors key
    VisitableCommand cmd = commandsFactory.buildLockControlCommand(ERRORS_KEY_SUFFIX, command.getFlagsBitSet(), null);
    InvocationStage stage = invoker.running().invokeStage(ctx, cmd);
    return makeStage(asyncInvokeNext(ctx, command, stage)).thenApply(ctx, command, (rCtx, rCommand, rv) -> {
        long flagsBitSet = copyFlags(rCommand);
        ProgressCallback progressCallback = null;
        if (rCtx.isOriginLocal()) {
            progressCallback = new ProgressCallback();
            source.withProgressCallback(progressCallback);
        } else {
            source.withProgressCallback(EMPTY_CALLBACK);
        }
        registerFileDescriptorSource(source, source.getFiles().keySet().toString());
        if (progressCallback != null) {
            List<KeyValuePair<String, String>> errorUpdates = computeErrorUpdates(progressCallback);
            return updateSchemaErrorsIterator(rCtx, flagsBitSet, errorUpdates.iterator());
        }
        return InvocationStage.completedNullStage();
    });
}
Also used : VisitableCommand(org.infinispan.commands.VisitableCommand) InvocationStage(org.infinispan.interceptors.InvocationStage) SyncInvocationStage(org.infinispan.interceptors.SyncInvocationStage) KeyValuePair(org.infinispan.util.KeyValuePair) FileDescriptorSource(org.infinispan.protostream.FileDescriptorSource)

Example 13 with InvocationStage

use of org.infinispan.interceptors.InvocationStage 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 14 with InvocationStage

use of org.infinispan.interceptors.InvocationStage in project infinispan by infinispan.

the class CacheMgmtInterceptorTest method testVisitGetCacheEntryCommand.

public void testVisitGetCacheEntryCommand() throws Throwable {
    GetCacheEntryCommand command = new GetCacheEntryCommand(KEY, 0, 0);
    InvocationStage stage = makeStage(interceptor.visitGetCacheEntryCommand(ctx, command));
    assertFalse(stage.isDone());
    timeService.advance(1);
    nextInterceptor.completeLastInvocation(null);
    assertNull(stage.get());
    assertEquals(1, interceptor.getAverageReadTime());
}
Also used : InvocationStage(org.infinispan.interceptors.InvocationStage) GetCacheEntryCommand(org.infinispan.commands.read.GetCacheEntryCommand)

Example 15 with InvocationStage

use of org.infinispan.interceptors.InvocationStage in project infinispan by infinispan.

the class CacheMgmtInterceptorTest method testVisitPutKeyValueCommandException.

public void testVisitPutKeyValueCommandException() throws Throwable {
    PutKeyValueCommand command = new PutKeyValueCommand(KEY, VALUE, false, null, 0, 0, null);
    InvocationStage stage = makeStage(interceptor.visitPutKeyValueCommand(ctx, command));
    assertFalse(stage.isDone());
    timeService.advance(1);
    nextInterceptor.completeLastInvocationExceptionally(new TestException());
    expectInvocationException(stage);
    assertEquals(1, interceptor.getAverageWriteTime());
}
Also used : InvocationStage(org.infinispan.interceptors.InvocationStage) TestException(org.infinispan.test.TestException) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand)

Aggregations

InvocationStage (org.infinispan.interceptors.InvocationStage)29 TestException (org.infinispan.test.TestException)7 TxInvocationContext (org.infinispan.context.impl.TxInvocationContext)6 SyncInvocationStage (org.infinispan.interceptors.SyncInvocationStage)6 PutKeyValueCommand (org.infinispan.commands.write.PutKeyValueCommand)5 Map (java.util.Map)4 VisitableCommand (org.infinispan.commands.VisitableCommand)4 LockControlCommand (org.infinispan.commands.control.LockControlCommand)4 PrepareCommand (org.infinispan.commands.tx.PrepareCommand)4 PutMapCommand (org.infinispan.commands.write.PutMapCommand)4 RemoveCommand (org.infinispan.commands.write.RemoveCommand)4 ReplaceCommand (org.infinispan.commands.write.ReplaceCommand)4 WriteCommand (org.infinispan.commands.write.WriteCommand)4 InvocationContext (org.infinispan.context.InvocationContext)4 KeyValuePair (org.infinispan.util.KeyValuePair)4 FlagAffectedCommand (org.infinispan.commands.FlagAffectedCommand)3 CommitCommand (org.infinispan.commands.tx.CommitCommand)3 CacheEntry (org.infinispan.container.entries.CacheEntry)3 FlagBitSets (org.infinispan.context.impl.FlagBitSets)3 Inject (org.infinispan.factories.annotations.Inject)3