Search in sources :

Example 1 with TXCommitMessage

use of org.apache.geode.internal.cache.TXCommitMessage in project geode by apache.

the class CommitCommand method cmdExecute.

@Override
public void cmdExecute(Message clientMessage, ServerConnection serverConnection, long start) throws IOException {
    serverConnection.setAsTrue(REQUIRES_RESPONSE);
    TXManagerImpl txMgr = (TXManagerImpl) serverConnection.getCache().getCacheTransactionManager();
    InternalDistributedMember client = (InternalDistributedMember) serverConnection.getProxyID().getDistributedMember();
    int uniqId = clientMessage.getTransactionId();
    TXId txId = new TXId(client, uniqId);
    TXCommitMessage commitMsg = null;
    if (txMgr.isHostedTxRecentlyCompleted(txId)) {
        commitMsg = txMgr.getRecentlyCompletedMessage(txId);
        if (logger.isDebugEnabled()) {
            logger.debug("TX: returning a recently committed txMessage for tx: {}", txId);
        }
        if (!txMgr.isExceptionToken(commitMsg)) {
            writeCommitResponse(commitMsg, clientMessage, serverConnection);
            // fixes bug 46529
            commitMsg.setClientVersion(null);
            serverConnection.setAsTrue(RESPONDED);
        } else {
            sendException(clientMessage, serverConnection, txMgr.getExceptionForToken(commitMsg, txId));
        }
        txMgr.removeHostedTXState(txId);
        return;
    }
    // fixes bug 43350
    boolean wasInProgress = txMgr.setInProgress(true);
    final TXStateProxy txProxy = txMgr.getTXState();
    Assert.assertTrue(txProxy != null);
    if (logger.isDebugEnabled()) {
        logger.debug("TX: committing client tx: {}", txId);
    }
    try {
        txId = txProxy.getTxId();
        txProxy.setCommitOnBehalfOfRemoteStub(true);
        txMgr.commit();
        commitMsg = txProxy.getCommitMessage();
        writeCommitResponse(commitMsg, clientMessage, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
    } catch (Exception e) {
        sendException(clientMessage, serverConnection, e);
    } finally {
        if (txId != null) {
            txMgr.removeHostedTXState(txId);
        }
        if (!wasInProgress) {
            txMgr.setInProgress(false);
        }
        if (commitMsg != null) {
            // fixes bug 46529
            commitMsg.setClientVersion(null);
        }
    }
}
Also used : TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) TXId(org.apache.geode.internal.cache.TXId) TXCommitMessage(org.apache.geode.internal.cache.TXCommitMessage) IOException(java.io.IOException) CommitConflictException(org.apache.geode.cache.CommitConflictException)

Example 2 with TXCommitMessage

use of org.apache.geode.internal.cache.TXCommitMessage in project geode by apache.

the class CommitCommand method writeCommitResponse.

protected static void writeCommitResponse(TXCommitMessage response, Message origMsg, ServerConnection servConn) throws IOException {
    Message responseMsg = servConn.getResponseMessage();
    responseMsg.setMessageType(MessageType.RESPONSE);
    responseMsg.setTransactionId(origMsg.getTransactionId());
    responseMsg.setNumberOfParts(1);
    if (response != null) {
        response.setClientVersion(servConn.getClientVersion());
    }
    responseMsg.addObjPart(response, false);
    servConn.getCache().getCancelCriterion().checkCancelInProgress(null);
    if (logger.isDebugEnabled()) {
        logger.debug("TX: sending a nonNull response for transaction: {}", new TXId((InternalDistributedMember) servConn.getProxyID().getDistributedMember(), origMsg.getTransactionId()));
    }
    responseMsg.send(servConn);
    origMsg.clearParts();
}
Also used : TXCommitMessage(org.apache.geode.internal.cache.TXCommitMessage) Message(org.apache.geode.internal.cache.tier.sockets.Message) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) TXId(org.apache.geode.internal.cache.TXId)

Example 3 with TXCommitMessage

use of org.apache.geode.internal.cache.TXCommitMessage in project geode by apache.

the class TXSynchronizationCommand method cmdExecute.

/*
   * (non-Javadoc)
   * 
   * @see
   * org.apache.geode.internal.cache.tier.sockets.BaseCommand#cmdExecute(org.apache.geode.internal.
   * cache.tier.sockets.Message, org.apache.geode.internal.cache.tier.sockets.ServerConnection,
   * long)
   */
@Override
public void cmdExecute(final Message clientMessage, final ServerConnection serverConnection, long start) throws IOException, ClassNotFoundException, InterruptedException {
    serverConnection.setAsTrue(REQUIRES_RESPONSE);
    CompletionType type = CompletionType.values()[clientMessage.getPart(0).getInt()];
    /* int txIdInt = */
    // [bruce] not sure if we need to
    clientMessage.getPart(1).getInt();
    // transmit this
    final Part statusPart;
    if (type == CompletionType.AFTER_COMPLETION) {
        statusPart = clientMessage.getPart(2);
    } else {
        statusPart = null;
    }
    final TXManagerImpl txMgr = (TXManagerImpl) serverConnection.getCache().getCacheTransactionManager();
    final InternalDistributedMember member = (InternalDistributedMember) serverConnection.getProxyID().getDistributedMember();
    // get the tx state without associating it with this thread. That's done later
    final TXStateProxy txProxy = txMgr.masqueradeAs(clientMessage, member, true);
    // releases them
    if (txProxy != null) {
        final boolean isDebugEnabled = logger.isDebugEnabled();
        try {
            if (type == CompletionType.BEFORE_COMPLETION) {
                Runnable beforeCompletion = new Runnable() {

                    @SuppressWarnings("synthetic-access")
                    public void run() {
                        TXStateProxy txState = null;
                        Throwable failureException = null;
                        try {
                            txState = txMgr.masqueradeAs(clientMessage, member, false);
                            if (isDebugEnabled) {
                                logger.debug("Executing beforeCompletion() notification for transaction {}", clientMessage.getTransactionId());
                            }
                            txState.setIsJTA(true);
                            txState.beforeCompletion();
                            try {
                                writeReply(clientMessage, serverConnection);
                            } catch (IOException e) {
                                if (isDebugEnabled) {
                                    logger.debug("Problem writing reply to client", e);
                                }
                            }
                            serverConnection.setAsTrue(RESPONDED);
                        } catch (ReplyException e) {
                            failureException = e.getCause();
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                        } catch (Exception e) {
                            failureException = e;
                        } finally {
                            txMgr.unmasquerade(txState);
                        }
                        if (failureException != null) {
                            try {
                                writeException(clientMessage, failureException, false, serverConnection);
                            } catch (IOException ioe) {
                                if (isDebugEnabled) {
                                    logger.debug("Problem writing reply to client", ioe);
                                }
                            }
                            serverConnection.setAsTrue(RESPONDED);
                        }
                    }
                };
                TXSynchronizationRunnable sync = new TXSynchronizationRunnable(beforeCompletion);
                txProxy.setSynchronizationRunnable(sync);
                Executor exec = InternalDistributedSystem.getConnectedInstance().getDistributionManager().getWaitingThreadPool();
                exec.execute(sync);
                sync.waitForFirstExecution();
            } else {
                Runnable afterCompletion = new Runnable() {

                    @SuppressWarnings("synthetic-access")
                    public void run() {
                        TXStateProxy txState = null;
                        try {
                            txState = txMgr.masqueradeAs(clientMessage, member, false);
                            int status = statusPart.getInt();
                            if (isDebugEnabled) {
                                logger.debug("Executing afterCompletion({}) notification for transaction {}", status, clientMessage.getTransactionId());
                            }
                            txState.setIsJTA(true);
                            txState.afterCompletion(status);
                            // GemFire commits during afterCompletion - send the commit info back to the client
                            // where it can be applied to the local cache
                            TXCommitMessage cmsg = txState.getCommitMessage();
                            try {
                                CommitCommand.writeCommitResponse(cmsg, clientMessage, serverConnection);
                                txMgr.removeHostedTXState(txState.getTxId());
                            } catch (IOException e) {
                                // not much can be done here
                                if (isDebugEnabled || (e instanceof MessageTooLargeException)) {
                                    logger.warn("Problem writing reply to client", e);
                                }
                            }
                            serverConnection.setAsTrue(RESPONDED);
                        } catch (RuntimeException e) {
                            try {
                                writeException(clientMessage, e, false, serverConnection);
                            } catch (IOException ioe) {
                                if (isDebugEnabled) {
                                    logger.debug("Problem writing reply to client", ioe);
                                }
                            }
                            serverConnection.setAsTrue(RESPONDED);
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                        } finally {
                            txMgr.unmasquerade(txState);
                        }
                    }
                };
                // if there was a beforeCompletion call then there will be a thread
                // sitting in the waiting pool to execute afterCompletion. Otherwise
                // we have failed-over and may need to do beforeCompletion & hope that it works
                TXSynchronizationRunnable sync = txProxy.getSynchronizationRunnable();
                if (sync != null) {
                    sync.runSecondRunnable(afterCompletion);
                } else {
                    if (statusPart.getInt() == Status.STATUS_COMMITTED) {
                        TXStateProxy txState = txMgr.masqueradeAs(clientMessage, member, false);
                        try {
                            if (isDebugEnabled) {
                                logger.debug("Executing beforeCompletion() notification for transaction {} after failover", clientMessage.getTransactionId());
                            }
                            txState.setIsJTA(true);
                            txState.beforeCompletion();
                        } finally {
                            txMgr.unmasquerade(txState);
                        }
                    }
                    afterCompletion.run();
                }
            }
        } catch (Exception e) {
            writeException(clientMessage, MessageType.EXCEPTION, e, false, serverConnection);
            serverConnection.setAsTrue(RESPONDED);
        }
        if (isDebugEnabled) {
            logger.debug("Sent tx synchronization response");
        }
    }
}
Also used : TXManagerImpl(org.apache.geode.internal.cache.TXManagerImpl) CompletionType(org.apache.geode.cache.client.internal.TXSynchronizationOp.CompletionType) IOException(java.io.IOException) ReplyException(org.apache.geode.distributed.internal.ReplyException) IOException(java.io.IOException) MessageTooLargeException(org.apache.geode.internal.cache.tier.sockets.MessageTooLargeException) ReplyException(org.apache.geode.distributed.internal.ReplyException) TXSynchronizationRunnable(org.apache.geode.internal.cache.TXSynchronizationRunnable) Executor(java.util.concurrent.Executor) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) TXStateProxy(org.apache.geode.internal.cache.TXStateProxy) Part(org.apache.geode.internal.cache.tier.sockets.Part) MessageTooLargeException(org.apache.geode.internal.cache.tier.sockets.MessageTooLargeException) TXSynchronizationRunnable(org.apache.geode.internal.cache.TXSynchronizationRunnable) TXCommitMessage(org.apache.geode.internal.cache.TXCommitMessage)

Example 4 with TXCommitMessage

use of org.apache.geode.internal.cache.TXCommitMessage in project geode by apache.

the class ClientTXStateStub method commit.

@Override
public void commit() throws CommitConflictException {
    obtainLocalLocks();
    try {
        TXCommitMessage txcm = firstProxy.commit(proxy.getTxId().getUniqId());
        afterServerCommit(txcm);
    } catch (TransactionDataNodeHasDepartedException e) {
        throw new TransactionInDoubtException(e);
    } finally {
        lockReq.releaseLocal();
        this.firstProxy.getPool().releaseServerAffinity();
    }
}
Also used : TXCommitMessage(org.apache.geode.internal.cache.TXCommitMessage) TransactionInDoubtException(org.apache.geode.cache.TransactionInDoubtException) TransactionDataNodeHasDepartedException(org.apache.geode.cache.TransactionDataNodeHasDepartedException)

Aggregations

TXCommitMessage (org.apache.geode.internal.cache.TXCommitMessage)4 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)3 IOException (java.io.IOException)2 TXId (org.apache.geode.internal.cache.TXId)2 TXManagerImpl (org.apache.geode.internal.cache.TXManagerImpl)2 TXStateProxy (org.apache.geode.internal.cache.TXStateProxy)2 Executor (java.util.concurrent.Executor)1 CommitConflictException (org.apache.geode.cache.CommitConflictException)1 TransactionDataNodeHasDepartedException (org.apache.geode.cache.TransactionDataNodeHasDepartedException)1 TransactionInDoubtException (org.apache.geode.cache.TransactionInDoubtException)1 CompletionType (org.apache.geode.cache.client.internal.TXSynchronizationOp.CompletionType)1 ReplyException (org.apache.geode.distributed.internal.ReplyException)1 TXSynchronizationRunnable (org.apache.geode.internal.cache.TXSynchronizationRunnable)1 Message (org.apache.geode.internal.cache.tier.sockets.Message)1 MessageTooLargeException (org.apache.geode.internal.cache.tier.sockets.MessageTooLargeException)1 Part (org.apache.geode.internal.cache.tier.sockets.Part)1