Search in sources :

Example 1 with PrepareCommand

use of org.infinispan.commands.tx.PrepareCommand in project infinispan by infinispan.

the class StateConsumerImpl method invoke1PCPrepare.

private CompletionStage<?> invoke1PCPrepare(LocalTransaction localTransaction) {
    PrepareCommand prepareCommand;
    if (Configurations.isTxVersioned(configuration)) {
        prepareCommand = commandsFactory.buildVersionedPrepareCommand(localTransaction.getGlobalTransaction(), localTransaction.getModifications(), true);
    } else {
        prepareCommand = commandsFactory.buildPrepareCommand(localTransaction.getGlobalTransaction(), localTransaction.getModifications(), true);
    }
    LocalTxInvocationContext ctx = icf.createTxInvocationContext(localTransaction);
    return interceptorChain.invokeAsync(ctx, prepareCommand);
}
Also used : LocalTxInvocationContext(org.infinispan.context.impl.LocalTxInvocationContext) PrepareCommand(org.infinispan.commands.tx.PrepareCommand)

Example 2 with PrepareCommand

use of org.infinispan.commands.tx.PrepareCommand 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 PrepareCommand

use of org.infinispan.commands.tx.PrepareCommand in project infinispan by infinispan.

the class PrepareProcessedAfterOriginatorCrashTest method testBelatedTransactionDoesntLeak.

public void testBelatedTransactionDoesntLeak() throws Throwable {
    CountDownLatch prepareReceived = new CountDownLatch(1);
    CountDownLatch prepareBlocked = new CountDownLatch(1);
    CountDownLatch prepareExecuted = new CountDownLatch(1);
    Cache receiver = cache(1);
    PerCacheInboundInvocationHandler originalInvocationHandler = TestingUtil.extractComponent(receiver, PerCacheInboundInvocationHandler.class);
    PerCacheInboundInvocationHandler blockingInvocationHandler = new AbstractDelegatingHandler(originalInvocationHandler) {

        @Override
        public void handle(CacheRpcCommand command, Reply reply, DeliverOrder order) {
            if (!(command instanceof PrepareCommand)) {
                delegate.handle(command, reply, order);
                return;
            }
            try {
                prepareReceived.countDown();
                prepareBlocked.await(10, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                throw new IllegalLifecycleStateException(e);
            }
            log.trace("Processing belated prepare");
            delegate.handle(command, returnValue -> {
                prepareExecuted.countDown();
                reply.reply(returnValue);
            }, order);
        }
    };
    TestingUtil.replaceComponent(receiver, PerCacheInboundInvocationHandler.class, blockingInvocationHandler, true);
    TestingUtil.extractComponentRegistry(receiver).cacheComponents();
    final Object key = getKeyForCache(1);
    fork(() -> {
        try {
            cache(0).put(key, "v");
        } catch (Throwable e) {
        // possible as the node is being killed
        }
    });
    prepareReceived.await(10, TimeUnit.SECONDS);
    killMember(0);
    // give TransactionTable.cleanupStaleTransactions some time to run
    Thread.sleep(5000);
    prepareBlocked.countDown();
    prepareExecuted.await(10, TimeUnit.SECONDS);
    log.trace("Finished waiting for belated prepare to complete");
    final TransactionTable transactionTable = TestingUtil.getTransactionTable(receiver);
    assertEquals(0, transactionTable.getRemoteTxCount());
    assertEquals(0, transactionTable.getLocalTxCount());
    assertFalse(receiver.getAdvancedCache().getLockManager().isLocked(key));
}
Also used : PerCacheInboundInvocationHandler(org.infinispan.remoting.inboundhandler.PerCacheInboundInvocationHandler) IllegalLifecycleStateException(org.infinispan.commons.IllegalLifecycleStateException) DeliverOrder(org.infinispan.remoting.inboundhandler.DeliverOrder) TransactionTable(org.infinispan.transaction.impl.TransactionTable) PrepareCommand(org.infinispan.commands.tx.PrepareCommand) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractDelegatingHandler(org.infinispan.remoting.inboundhandler.AbstractDelegatingHandler) CacheRpcCommand(org.infinispan.commands.remote.CacheRpcCommand) Reply(org.infinispan.remoting.inboundhandler.Reply) Cache(org.infinispan.Cache)

Example 4 with PrepareCommand

use of org.infinispan.commands.tx.PrepareCommand in project infinispan by infinispan.

the class VersionAwareMarshallerTest method testReplicableCommandsMarshalling.

public void testReplicableCommandsMarshalling() throws Exception {
    ByteString cacheName = ByteString.fromString(TestingUtil.getDefaultCacheName(cm));
    ClusteredGetCommand c2 = new ClusteredGetCommand("key", cacheName, 0, EnumUtil.EMPTY_BIT_SET);
    marshallAndAssertEquality(c2);
    // SizeCommand does not have an empty constructor, so doesn't look to be one that is marshallable.
    GetKeyValueCommand c4 = new GetKeyValueCommand("key", 0, EnumUtil.EMPTY_BIT_SET);
    marshallAndAssertEquality(c4);
    PutKeyValueCommand c5 = new PutKeyValueCommand("k", "v", false, new EmbeddedMetadata.Builder().build(), 0, EnumUtil.EMPTY_BIT_SET, CommandInvocationId.generateId(null));
    marshallAndAssertEquality(c5);
    RemoveCommand c6 = new RemoveCommand("key", null, 0, EnumUtil.EMPTY_BIT_SET, CommandInvocationId.generateId(null));
    marshallAndAssertEquality(c6);
    // EvictCommand does not have an empty constructor, so doesn't look to be one that is marshallable.
    InvalidateCommand c7 = new InvalidateCommand(EnumUtil.EMPTY_BIT_SET, CommandInvocationId.generateId(null), "key1", "key2");
    marshallAndAssertEquality(c7);
    InvalidateCommand c71 = new InvalidateL1Command(EnumUtil.EMPTY_BIT_SET, CommandInvocationId.generateId(null), "key1", "key2");
    marshallAndAssertEquality(c71);
    ReplaceCommand c8 = new ReplaceCommand("key", "oldvalue", "newvalue", new EmbeddedMetadata.Builder().build(), 0, EnumUtil.EMPTY_BIT_SET, CommandInvocationId.generateId(null));
    marshallAndAssertEquality(c8);
    ClearCommand c9 = new ClearCommand();
    marshallAndAssertEquality(c9);
    Map<Integer, GlobalTransaction> m1 = new HashMap<>();
    for (int i = 0; i < 10; i++) {
        GlobalTransaction gtx = gtf.newGlobalTransaction(new JGroupsAddress(UUID.randomUUID()), false);
        m1.put(1000 * i, gtx);
    }
    PutMapCommand c10 = new PutMapCommand(m1, new EmbeddedMetadata.Builder().build(), EnumUtil.EMPTY_BIT_SET, CommandInvocationId.generateId(null));
    marshallAndAssertEquality(c10);
    Address local = new JGroupsAddress(UUID.randomUUID());
    GlobalTransaction gtx = gtf.newGlobalTransaction(local, false);
    PrepareCommand c11 = new PrepareCommand(cacheName, gtx, true, c5, c6, c8, c10);
    marshallAndAssertEquality(c11);
    CommitCommand c12 = new CommitCommand(cacheName, gtx);
    marshallAndAssertEquality(c12);
    RollbackCommand c13 = new RollbackCommand(cacheName, gtx);
    marshallAndAssertEquality(c13);
}
Also used : RemoveCommand(org.infinispan.commands.write.RemoveCommand) IpAddress(org.jgroups.stack.IpAddress) JGroupsAddress(org.infinispan.remoting.transport.jgroups.JGroupsAddress) Address(org.infinispan.remoting.transport.Address) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) FastCopyHashMap(org.infinispan.commons.util.FastCopyHashMap) HashMap(java.util.HashMap) ByteString(org.infinispan.util.ByteString) JGroupsAddress(org.infinispan.remoting.transport.jgroups.JGroupsAddress) ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) AutoProtoSchemaBuilder(org.infinispan.protostream.annotations.AutoProtoSchemaBuilder) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) GlobalTransaction(org.infinispan.transaction.xa.GlobalTransaction) PrepareCommand(org.infinispan.commands.tx.PrepareCommand) InvalidateL1Command(org.infinispan.commands.write.InvalidateL1Command) ClusteredGetCommand(org.infinispan.commands.remote.ClusteredGetCommand) PutMapCommand(org.infinispan.commands.write.PutMapCommand) GetKeyValueCommand(org.infinispan.commands.read.GetKeyValueCommand) ReplaceCommand(org.infinispan.commands.write.ReplaceCommand) RollbackCommand(org.infinispan.commands.tx.RollbackCommand) ClearCommand(org.infinispan.commands.write.ClearCommand) CommitCommand(org.infinispan.commands.tx.CommitCommand) InvalidateCommand(org.infinispan.commands.write.InvalidateCommand) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand)

Example 5 with PrepareCommand

use of org.infinispan.commands.tx.PrepareCommand in project infinispan by infinispan.

the class TransactionCoordinator method prepare.

public final CompletionStage<Integer> prepare(LocalTransaction localTransaction, boolean replayEntryWrapping) {
    CompletionStage<Integer> markRollbackStage = validateNotMarkedForRollback(localTransaction);
    if (markRollbackStage != null) {
        return markRollbackStage;
    }
    if (isOnePhaseCommit(localTransaction)) {
        if (log.isTraceEnabled())
            log.tracef("Received prepare for tx: %s. Skipping call as 1PC will be used.", localTransaction);
        return XA_OKAY_STAGE;
    }
    PrepareCommand prepareCommand = commandCreator.createPrepareCommand(localTransaction.getGlobalTransaction(), localTransaction.getModifications(), false);
    if (log.isTraceEnabled())
        log.tracef("Sending prepare command through the chain: %s", prepareCommand);
    LocalTxInvocationContext ctx = icf.running().createTxInvocationContext(localTransaction);
    prepareCommand.setReplayEntryWrapping(replayEntryWrapping);
    CompletionStage<Object> prepareStage = invoker.running().invokeAsync(ctx, prepareCommand);
    return CompletionStages.handleAndCompose(prepareStage, (ignore, prepareThrowable) -> {
        if (prepareThrowable != null) {
            if (shuttingDown)
                log.trace("Exception while preparing back, probably because we're shutting down.");
            else
                log.errorProcessingPrepare(prepareThrowable);
            // after prepare failed.
            return CompletionStages.handleAndCompose(rollback(localTransaction), (ignore2, rollbackThrowable) -> {
                // XA_RBROLLBACK tells the TM that we've rolled back already: the TM shouldn't call rollback after this.
                XAException xe = new XAException(XAException.XA_RBROLLBACK);
                if (rollbackThrowable != null) {
                    rollbackThrowable.addSuppressed(prepareThrowable);
                    xe.initCause(rollbackThrowable);
                } else {
                    xe.initCause(prepareThrowable);
                }
                return CompletableFutures.completedExceptionFuture(xe);
            });
        }
        if (localTransaction.isReadOnly()) {
            if (log.isTraceEnabled())
                log.tracef("Readonly transaction: %s", localTransaction.getGlobalTransaction());
            // force a cleanup to release any objects held.  Some TMs don't call commit if it is a READ ONLY tx.  See ISPN-845
            return commitInternal(ctx).thenApply(XA_RDONLY_APPLY);
        } else {
            txTable.running().localTransactionPrepared(localTransaction);
            return XA_OKAY_STAGE;
        }
    });
}
Also used : XAException(javax.transaction.xa.XAException) LocalTxInvocationContext(org.infinispan.context.impl.LocalTxInvocationContext) PrepareCommand(org.infinispan.commands.tx.PrepareCommand)

Aggregations

PrepareCommand (org.infinispan.commands.tx.PrepareCommand)16 CommitCommand (org.infinispan.commands.tx.CommitCommand)5 WriteCommand (org.infinispan.commands.write.WriteCommand)5 LocalTxInvocationContext (org.infinispan.context.impl.LocalTxInvocationContext)5 RollbackCommand (org.infinispan.commands.tx.RollbackCommand)4 PutKeyValueCommand (org.infinispan.commands.write.PutKeyValueCommand)3 InvocationContext (org.infinispan.context.InvocationContext)3 HashMap (java.util.HashMap)2 List (java.util.List)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 CommandsFactory (org.infinispan.commands.CommandsFactory)2 ClusteredGetCommand (org.infinispan.commands.remote.ClusteredGetCommand)2 TxCompletionNotificationCommand (org.infinispan.commands.remote.recovery.TxCompletionNotificationCommand)2 ClearCommand (org.infinispan.commands.write.ClearCommand)2 FlagBitSets (org.infinispan.context.impl.FlagBitSets)2 TxInvocationContext (org.infinispan.context.impl.TxInvocationContext)2 InvocationSuccessFunction (org.infinispan.interceptors.InvocationSuccessFunction)2 EmbeddedCacheManager (org.infinispan.manager.EmbeddedCacheManager)2 TransportFlags (org.infinispan.test.fwk.TransportFlags)2 GlobalTransaction (org.infinispan.transaction.xa.GlobalTransaction)2