/** * Reports the existence of a Transaction for this thread * */ @Override public boolean exists() { return null != getTXState(); }
public static TXStateProxy getCurrentTXState() { if (currentInstance == null) { return null; } return currentInstance.getTXState(); }
@Override public boolean tryResume(TransactionId transactionId) { if (transactionId == null || getTXState() != null) { return false; } TXStateProxy txProxy = this.suspendedTXs.remove(transactionId); if (txProxy != null) { resumeProxy(txProxy); return true; } return false; }
/** * Gets the current transaction identifier or null if no transaction exists * */ @Override public TransactionId getTransactionId() { TXStateProxy t = getTXState(); TransactionId ret = null; if (t != null) { ret = t.getTransactionId(); } return ret; }
@Override public void resume(TransactionId transactionId) { if (transactionId == null) { throw new IllegalStateException( "Trying to resume unknown transaction, or transaction resumed by another thread"); } if (getTXState() != null) { throw new IllegalStateException( "Cannot resume transaction, current thread has an active transaction"); } TXStateProxy txProxy = this.suspendedTXs.remove(transactionId); if (txProxy == null) { throw new IllegalStateException( "Trying to resume unknown transaction, or transaction resumed by another thread"); } resumeProxy(txProxy); }
/** * @return the node which a transaction is already is progress, null otherwise */ private DistributedMember getTransactionalNode() { if (cache.getTxManager().getTXState() != null) { return cache.getTxManager().getTXState().getTarget(); } return null; }
/** * prepare for transaction replay by assigning a new tx id to the current proxy */ private void _incrementTXUniqueIDForReplay() { TXStateProxyImpl tx = (TXStateProxyImpl) getTXState(); assert tx != null : "expected a transaction to be in progress"; TXId id = new TXId(this.distributionMgrId, this.uniqId.incrementAndGet()); tx.setTXIDForReplay(id); }
@Override public void setDistributed(boolean flag) { checkClosed(); TXStateProxy tx = getTXState(); // Check whether given flag and current flag are different and whether a transaction is in // progress if (tx != null && flag != isDistributed()) { // Cannot change mode in the middle of a transaction throw new java.lang.IllegalStateException( "Transaction mode cannot be changed when the thread has an active transaction"); } else { isTXDistributed.set(flag); } }
private static boolean isTransactionInternalSuspendNeeded(TXManagerImpl txManager) { TXStateProxy txState = txManager.getTXState(); // handle distributed transaction when needed. return txState != null && txState.isRealDealLocal() && !txState.isDistTx(); }
@Override public void validateExecution(Function function, Set targetMembers) { InternalCache cache = GemFireCacheImpl.getInstance(); if (cache != null && cache.getTxManager().getTXState() != null) { TXStateProxyImpl tx = (TXStateProxyImpl) cache.getTxManager().getTXState(); tx.getRealDeal(null, region); tx.incOperationCount(); } }
public void precommit() throws CommitConflictException { checkClosed(); final TXStateProxy tx = getTXState(); if (tx == null) { throw new IllegalStateException( "Thread does not have an active transaction"); } tx.checkJTA( "Can not commit this transaction because it is enlisted with a JTA transaction, use the JTA manager to perform the commit."); tx.precommit(); }
@Override protected boolean operateOnTx(TXId txId, ClusterDistributionManager dm) { InternalCache cache = dm.getCache(); TXManagerImpl txMgr = cache.getTXMgr(); if (logger.isDebugEnabled()) { logger.debug("JTA: Calling beforeCompletion for :{}", txId); } // Check if jta has been completed, possible due to tx failover. // No need to execute beforeCompletion if already completed. if (txMgr.isHostedTxRecentlyCompleted(txId)) { return true; } txMgr.getTXState().beforeCompletion(); return true; }
@Override public void validateExecution(Function function, Set targetMembers) { InternalCache cache = pr.getGemFireCache(); if (cache.getTxManager().getTXState() != null) { if (targetMembers.size() > 1) { throw new TransactionException( "Function inside a transaction cannot execute on more than one node"); } else { assert targetMembers.size() == 1; DistributedMember funcTarget = (DistributedMember) targetMembers.iterator().next(); DistributedMember target = cache.getTxManager().getTXState().getTarget(); if (target == null) { cache.getTxManager().getTXState().setTarget(funcTarget); } else if (!target.equals(funcTarget)) { throw new TransactionDataRebalancedException( String.format( "Function execution is not colocated with transaction. The transactional data is hosted on node %s, but you are trying to target node %s", target, funcTarget)); } } } cache.getInternalResourceManager().getHeapMonitor().checkForLowMemory(function, targetMembers); } }
public TXEntryState createReadEntry(LocalRegion r, Object entryKey, RegionEntry re, Object vId, Object pendingValue) { InternalCache cache = r.getCache(); boolean isDistributed = false; if (cache.getTxManager().getTXState() != null) { isDistributed = cache.getTxManager().getTXState().isDistTx(); } else { // TXCoordinator and datanode are same isDistributed = cache.getTxManager().isDistributed(); } TXEntryState result = cache.getTXEntryStateFactory().createEntry(re, vId, pendingValue, entryKey, this, isDistributed); this.entryMods.put(entryKey, result); return result; }
private void resumeProxy(TXStateProxy txProxy) { assert txProxy != null; assert getTXState() == null; setTXState(txProxy); txProxy.resume(); SystemTimerTask task = this.expiryTasks.remove(txProxy.getTransactionId()); if (task != null) { if (task.cancel()) { this.cache.purgeCCPTimer(); } } }
@Test public void testBeforeCompletionNotInvokedIfJTACompleted() throws Exception { InternalCache cache = mock(InternalCache.class); TXManagerImpl txMgr = mock(TXManagerImpl.class); ClusterDistributionManager distributionManager = mock(ClusterDistributionManager.class); TXId txId = mock(TXId.class); when(distributionManager.getCache()).thenReturn(cache); when(cache.getTXMgr()).thenReturn(txMgr); when(txMgr.isHostedTxRecentlyCompleted(txId)).thenReturn(true); when(txMgr.getTXState()).thenReturn(mock(TXStateProxyImpl.class)); JtaBeforeCompletionMessage message = new JtaBeforeCompletionMessage(1, mock(InternalDistributedMember.class), mock(ReplyProcessor21.class)); message.operateOnTx(txId, distributionManager); verify(txMgr, never()).getTXState(); }
@Test public void testAfterCompletionNotInvokedIfJTACompleted() throws Exception { InternalCache cache = mock(InternalCache.class); TXManagerImpl txMgr = mock(TXManagerImpl.class); ClusterDistributionManager distributionManager = mock(ClusterDistributionManager.class); TXId txId = mock(TXId.class); when(distributionManager.getCache()).thenReturn(cache); when(cache.getTXMgr()).thenReturn(txMgr); when(txMgr.getRecentlyCompletedMessage(txId)).thenReturn(mock(TXCommitMessage.class)); when(txMgr.getTXState()).thenReturn(mock(TXStateProxyImpl.class)); JtaAfterCompletionMessage message = new JtaAfterCompletionMessage(); JtaAfterCompletionMessage spyMessage = spy(message); when(spyMessage.getSender()).thenReturn(mock(InternalDistributedMember.class)); spyMessage.operateOnTx(txId, distributionManager); verify(txMgr, never()).getTXState(); }
@Before public void setup() { clientMessage = mock(Message.class); serverConnection = mock(ServerConnection.class); txManager = mock(TXManagerImpl.class); member = mock(InternalDistributedMember.class); txStateProxy = mock(TXStateProxyImpl.class); txId = mock(TXId.class); txCommitMessage = mock(TXCommitMessage.class); part0 = mock(Part.class); part1 = mock(Part.class); part2 = mock(Part.class); exception = new RuntimeException(); command = mock(TXSynchronizationCommand.class); when(clientMessage.getPart(0)).thenReturn(part0); when(clientMessage.getPart(1)).thenReturn(part1); when(clientMessage.getPart(2)).thenReturn(part2); doReturn(txManager).when(command).getTXManager(serverConnection); doReturn(member).when(command).getDistributedMember(serverConnection); when(txManager.getTXState()).thenReturn(txStateProxy); when(txStateProxy.getTxId()).thenReturn(txId); }
/** * Roll back the transaction associated with the current thread. When this method completes, the * thread is no longer associated with a transaction. */ @Override public void rollback() { checkClosed(); TXStateProxy tx = getTXState(); if (tx == null) { throw new IllegalStateException( "Thread does not have an active transaction"); } tx.checkJTA( "Can not rollback this transaction is enlisted with a JTA transaction, use the JTA manager to perform the rollback."); final long opStart = CachePerfStats.getStatTime(); final long lifeTime = opStart - tx.getBeginTime(); setTXState(null); tx.rollback(); saveTXStateForClientFailover(tx); cleanup(tx.getTransactionId()); noteRollbackSuccess(opStart, lifeTime, tx); }
@Override protected boolean operateOnTx(TXId txId, ClusterDistributionManager dm) throws RemoteOperationException { TXManagerImpl txMgr = dm.getCache().getTXMgr(); if (logger.isDebugEnabled()) { logger.debug("JTA: Calling afterCompletion for :{}", txId); } TXCommitMessage commitMessage = txMgr.getRecentlyCompletedMessage(txId); if (commitMessage != null) { TXCommitMessage message = commitMessage == TXCommitMessage.ROLLBACK_MSG ? null : commitMessage; TXRemoteCommitReplyMessage.send(getSender(), getProcessorId(), message, getReplySender(dm)); return false; } TXStateProxy txState = txMgr.getTXState(); txState.setCommitOnBehalfOfRemoteStub(true); txState.afterCompletion(status); commitMessage = txState.getCommitMessage(); TXRemoteCommitReplyMessage.send(getSender(), getProcessorId(), commitMessage, getReplySender(dm)); txMgr.removeHostedTXState(txId); return false; }