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);
}
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;
}
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));
}
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);
}
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;
}
});
}
Aggregations