Search in sources :

Example 1 with TXId

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

the class TXFailoverCommand method cmdExecute.

@Override
public void cmdExecute(Message clientMessage, ServerConnection serverConnection, long start) throws IOException, ClassNotFoundException, InterruptedException {
    serverConnection.setAsTrue(REQUIRES_RESPONSE);
    // Build the TXId for the transaction
    InternalDistributedMember client = (InternalDistributedMember) serverConnection.getProxyID().getDistributedMember();
    int uniqId = clientMessage.getTransactionId();
    if (logger.isDebugEnabled()) {
        logger.debug("TX: Transaction {} from {} is failing over to this server", uniqId, client);
    }
    TXId txId = new TXId(client, uniqId);
    TXManagerImpl mgr = (TXManagerImpl) serverConnection.getCache().getCacheTransactionManager();
    // in case it's already completing here in another
    mgr.waitForCompletingTransaction(txId);
    // thread
    if (mgr.isHostedTxRecentlyCompleted(txId)) {
        writeReply(clientMessage, serverConnection);
        serverConnection.setAsTrue(RESPONDED);
        mgr.removeHostedTXState(txId);
        return;
    }
    // fixes bug 43350
    boolean wasInProgress = mgr.setInProgress(true);
    TXStateProxy tx = mgr.getTXState();
    Assert.assertTrue(tx != null);
    if (!tx.isRealDealLocal()) {
        // send message to all peers to find out who hosts the transaction
        FindRemoteTXMessageReplyProcessor processor = FindRemoteTXMessage.send(serverConnection.getCache(), txId);
        try {
            processor.waitForRepliesUninterruptibly();
        } catch (ReplyException e) {
            e.handleAsUnexpected();
        }
        // if hosting member is not null, bootstrap PeerTXStateStub to that member
        // if hosting member is null, rebuild TXCommitMessage from partial TXCommitMessages
        InternalDistributedMember hostingMember = processor.getHostingMember();
        if (hostingMember != null) {
            if (logger.isDebugEnabled()) {
                logger.debug("TX: txState is not local, bootstrapping PeerTXState stub for targetNode: {}", hostingMember);
            }
            // inject the real deal
            tx.setLocalTXState(new PeerTXStateStub(tx, hostingMember, client));
        } else {
            // bug #42228 and bug #43504 - this cannot return until the current view
            // has been installed by all members, so that dlocks are released and
            // the same keys can be used in a new transaction by the same client thread
            InternalCache cache = serverConnection.getCache();
            try {
                WaitForViewInstallation.send((DistributionManager) cache.getDistributionManager());
            } catch (InterruptedException e) {
                cache.getDistributionManager().getCancelCriterion().checkCancelInProgress(e);
                Thread.currentThread().interrupt();
            }
            // tx host has departed, rebuild the tx
            if (processor.getTxCommitMessage() != null) {
                if (logger.isDebugEnabled()) {
                    logger.debug("TX: for txId: {} rebuilt a recently completed tx", txId);
                }
                mgr.saveTXCommitMessageForClientFailover(txId, processor.getTxCommitMessage());
            } else {
                writeException(clientMessage, new TransactionDataNodeHasDepartedException("Could not find transaction host for " + txId), false, serverConnection);
                serverConnection.setAsTrue(RESPONDED);
                mgr.removeHostedTXState(txId);
                return;
            }
        }
    }
    if (!wasInProgress) {
        mgr.setInProgress(false);
    }
    writeReply(clientMessage, serverConnection);
    serverConnection.setAsTrue(RESPONDED);
}
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) InternalCache(org.apache.geode.internal.cache.InternalCache) FindRemoteTXMessageReplyProcessor(org.apache.geode.internal.cache.FindRemoteTXMessage.FindRemoteTXMessageReplyProcessor) ReplyException(org.apache.geode.distributed.internal.ReplyException) PeerTXStateStub(org.apache.geode.internal.cache.PeerTXStateStub) TransactionDataNodeHasDepartedException(org.apache.geode.cache.TransactionDataNodeHasDepartedException)

Example 2 with TXId

use of org.apache.geode.internal.cache.TXId 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 3 with TXId

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

the class NestedTransactionFunction method execute.

public void execute(FunctionContext context) {
    Cache cache = CacheFactory.getAnyInstance();
    ArrayList args = (ArrayList) context.getArguments();
    TXId txId = null;
    int action = 0;
    try {
        txId = (TXId) args.get(0);
        action = (Integer) args.get(1);
    } catch (ClassCastException e) {
        logger.info("CommitFunction should be invoked with a TransactionId as an argument i.e. setArguments(txId).execute(function)");
        throw e;
    }
    CacheTransactionManager txMgr = cache.getCacheTransactionManager();
    Boolean result = false;
    final boolean isDebugEnabled = logger.isDebugEnabled();
    if (txMgr.tryResume(txId)) {
        if (isDebugEnabled) {
            logger.debug("CommitFunction: resumed transaction: {}", txId);
        }
        if (action == COMMIT) {
            if (isDebugEnabled) {
                logger.debug("CommitFunction: committing transaction: {}", txId);
            }
            txMgr.commit();
        } else if (action == ROLLBACK) {
            if (isDebugEnabled) {
                logger.debug("CommitFunction: rolling back transaction: {}", txId);
            }
            txMgr.rollback();
        } else {
            throw new IllegalStateException("unknown transaction termination action");
        }
        result = true;
    }
    if (isDebugEnabled) {
        logger.debug("CommitFunction: for transaction: {} sending result: {}", txId, result);
    }
    context.getResultSender().lastResult(result);
}
Also used : ArrayList(java.util.ArrayList) TXId(org.apache.geode.internal.cache.TXId) Cache(org.apache.geode.cache.Cache) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager)

Example 4 with TXId

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

the class CommitFunction method execute.

public void execute(FunctionContext context) {
    Cache cache = CacheFactory.getAnyInstance();
    TXId txId = null;
    try {
        txId = (TXId) context.getArguments();
    } catch (ClassCastException e) {
        logger.info("CommitFunction should be invoked with a TransactionId as an argument i.e. setArguments(txId).execute(function)");
        throw e;
    }
    DistributedMember member = txId.getMemberId();
    Boolean result = false;
    final boolean isDebugEnabled = logger.isDebugEnabled();
    if (cache.getDistributedSystem().getDistributedMember().equals(member)) {
        if (isDebugEnabled) {
            logger.debug("CommitFunction: for transaction: {} committing locally", txId);
        }
        CacheTransactionManager txMgr = cache.getCacheTransactionManager();
        if (txMgr.tryResume(txId)) {
            if (isDebugEnabled) {
                logger.debug("CommitFunction: resumed transaction: {}", txId);
            }
            txMgr.commit();
            result = true;
        }
    } else {
        ArrayList args = new ArrayList();
        args.add(txId);
        args.add(NestedTransactionFunction.COMMIT);
        Execution ex = FunctionService.onMember(member).setArguments(args);
        if (isDebugEnabled) {
            logger.debug("CommitFunction: for transaction: {} executing NestedTransactionFunction on member: {}", txId, member);
        }
        try {
            List list = (List) ex.execute(new NestedTransactionFunction()).getResult();
            result = (Boolean) list.get(0);
        } catch (FunctionException fe) {
            if (fe.getCause() instanceof FunctionInvocationTargetException) {
                throw new TransactionDataNodeHasDepartedException("Could not commit on member:" + member);
            } else {
                throw fe;
            }
        }
    }
    if (isDebugEnabled) {
        logger.debug("CommitFunction: for transaction: {} returning result: {}", txId, result);
    }
    context.getResultSender().lastResult(result);
}
Also used : ArrayList(java.util.ArrayList) FunctionException(org.apache.geode.cache.execute.FunctionException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) TransactionDataNodeHasDepartedException(org.apache.geode.cache.TransactionDataNodeHasDepartedException) Execution(org.apache.geode.cache.execute.Execution) TXId(org.apache.geode.internal.cache.TXId) DistributedMember(org.apache.geode.distributed.DistributedMember) FunctionInvocationTargetException(org.apache.geode.cache.execute.FunctionInvocationTargetException) ArrayList(java.util.ArrayList) List(java.util.List) Cache(org.apache.geode.cache.Cache)

Example 5 with TXId

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

the class RollbackFunction method execute.

public void execute(FunctionContext context) {
    Cache cache = CacheFactory.getAnyInstance();
    TXId txId = null;
    try {
        txId = (TXId) context.getArguments();
    } catch (ClassCastException e) {
        logger.info("RollbackFunction should be invoked with a TransactionId as an argument i.e. setArguments(txId).execute(function)");
        throw e;
    }
    DistributedMember member = txId.getMemberId();
    Boolean result = false;
    final boolean isDebugEnabled = logger.isDebugEnabled();
    if (cache.getDistributedSystem().getDistributedMember().equals(member)) {
        if (isDebugEnabled) {
            logger.debug("RollbackFunction: for transaction: {} rolling back locally", txId);
        }
        CacheTransactionManager txMgr = cache.getCacheTransactionManager();
        if (txMgr.tryResume(txId)) {
            if (isDebugEnabled) {
                logger.debug("RollbackFunction: resumed transaction: {}", txId);
            }
            txMgr.rollback();
            result = true;
        }
    } else {
        ArrayList args = new ArrayList();
        args.add(txId);
        args.add(NestedTransactionFunction.ROLLBACK);
        Execution ex = FunctionService.onMember(member).setArguments(args);
        if (isDebugEnabled) {
            logger.debug("RollbackFunction: for transaction: {} executing NestedTransactionFunction on member: {}", txId, member);
        }
        try {
            List list = (List) ex.execute(new NestedTransactionFunction()).getResult();
            result = (Boolean) list.get(0);
        } catch (FunctionException fe) {
            throw new TransactionDataNodeHasDepartedException("Could not Rollback on member:" + member);
        }
    }
    if (isDebugEnabled) {
        logger.debug("RollbackFunction: for transaction: {} returning result: {}", txId, result);
    }
    context.getResultSender().lastResult(result);
}
Also used : ArrayList(java.util.ArrayList) FunctionException(org.apache.geode.cache.execute.FunctionException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) TransactionDataNodeHasDepartedException(org.apache.geode.cache.TransactionDataNodeHasDepartedException) Execution(org.apache.geode.cache.execute.Execution) TXId(org.apache.geode.internal.cache.TXId) DistributedMember(org.apache.geode.distributed.DistributedMember) ArrayList(java.util.ArrayList) List(java.util.List) Cache(org.apache.geode.cache.Cache)

Aggregations

TXId (org.apache.geode.internal.cache.TXId)8 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)4 TXManagerImpl (org.apache.geode.internal.cache.TXManagerImpl)4 ArrayList (java.util.ArrayList)3 Cache (org.apache.geode.cache.Cache)3 CacheTransactionManager (org.apache.geode.cache.CacheTransactionManager)3 TransactionDataNodeHasDepartedException (org.apache.geode.cache.TransactionDataNodeHasDepartedException)3 TXStateProxy (org.apache.geode.internal.cache.TXStateProxy)3 IOException (java.io.IOException)2 List (java.util.List)2 Execution (org.apache.geode.cache.execute.Execution)2 FunctionException (org.apache.geode.cache.execute.FunctionException)2 DistributedMember (org.apache.geode.distributed.DistributedMember)2 TXCommitMessage (org.apache.geode.internal.cache.TXCommitMessage)2 CommitConflictException (org.apache.geode.cache.CommitConflictException)1 FunctionInvocationTargetException (org.apache.geode.cache.execute.FunctionInvocationTargetException)1 ReplyException (org.apache.geode.distributed.internal.ReplyException)1 SystemTimerTask (org.apache.geode.internal.SystemTimer.SystemTimerTask)1 FindRemoteTXMessageReplyProcessor (org.apache.geode.internal.cache.FindRemoteTXMessage.FindRemoteTXMessageReplyProcessor)1 InternalCache (org.apache.geode.internal.cache.InternalCache)1