Search in sources :

Example 21 with EngineExecutionException

use of io.seata.saga.engine.exception.EngineExecutionException in project seata by seata.

the class ExceptionUtils method createEngineExecutionException.

public static EngineExecutionException createEngineExecutionException(Exception e, FrameworkErrorCode code, String message, StateMachineInstance stateMachineInstance, String stateName) {
    EngineExecutionException exception = new EngineExecutionException(e, message, code);
    if (stateMachineInstance != null) {
        exception.setStateMachineName(stateMachineInstance.getStateMachine().getAppName());
        exception.setStateMachineInstanceId(stateMachineInstance.getId());
        exception.setStateName(stateName);
    }
    return exception;
}
Also used : EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException)

Example 22 with EngineExecutionException

use of io.seata.saga.engine.exception.EngineExecutionException in project seata by seata.

the class ExceptionUtils method createEngineExecutionException.

public static EngineExecutionException createEngineExecutionException(Exception e, FrameworkErrorCode code, String message, StateMachineInstance stateMachineInstance, StateInstance stateInstance) {
    EngineExecutionException exception = new EngineExecutionException(e, message, code);
    if (stateMachineInstance != null) {
        exception.setStateMachineName(stateMachineInstance.getStateMachine().getAppName());
        exception.setStateMachineInstanceId(stateMachineInstance.getId());
        if (stateInstance != null) {
            exception.setStateName(stateInstance.getName());
            exception.setStateInstanceId(stateInstance.getId());
        }
    }
    return exception;
}
Also used : EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException)

Example 23 with EngineExecutionException

use of io.seata.saga.engine.exception.EngineExecutionException in project seata by seata.

the class DbAndReportTcStateLogStore method branchRegister.

protected void branchRegister(StateInstance stateInstance, ProcessContext context) {
    if (sagaTransactionalTemplate != null) {
        StateMachineConfig stateMachineConfig = (StateMachineConfig) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONFIG);
        if (stateMachineConfig instanceof DbStateMachineConfig && !((DbStateMachineConfig) stateMachineConfig).isSagaBranchRegisterEnable()) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("sagaBranchRegisterEnable = false, skip register branch. state[" + stateInstance.getName() + "]");
            }
            return;
        }
        // Register branch
        try {
            StateMachineInstance machineInstance = stateInstance.getStateMachineInstance();
            GlobalTransaction globalTransaction = getGlobalTransaction(machineInstance, context);
            if (globalTransaction == null) {
                throw new EngineExecutionException("Global transaction is not exists", FrameworkErrorCode.ObjectNotExists);
            }
            String resourceId = stateInstance.getStateMachineInstance().getStateMachine().getName() + "#" + stateInstance.getName();
            long branchId = sagaTransactionalTemplate.branchRegister(resourceId, null, globalTransaction.getXid(), null, null);
            stateInstance.setId(String.valueOf(branchId));
        } catch (TransactionException e) {
            throw new EngineExecutionException(e, "Branch transaction error: " + e.getCode() + ", StateMachine:" + stateInstance.getStateMachineInstance().getStateMachine().getName() + ", XID: " + stateInstance.getStateMachineInstance().getId() + ", State:" + stateInstance.getName() + ", stateId: " + stateInstance.getId() + ", Reason: " + e.getMessage(), FrameworkErrorCode.TransactionManagerError);
        } catch (ExecutionException e) {
            throw new EngineExecutionException(e, "Branch transaction error: " + e.getCode() + ", StateMachine:" + stateInstance.getStateMachineInstance().getStateMachine().getName() + ", XID: " + stateInstance.getStateMachineInstance().getId() + ", State:" + stateInstance.getName() + ", stateId: " + stateInstance.getId() + ", Reason: " + e.getMessage(), FrameworkErrorCode.TransactionManagerError);
        }
    }
}
Also used : TransactionException(io.seata.core.exception.TransactionException) GlobalTransaction(io.seata.tm.api.GlobalTransaction) DbStateMachineConfig(io.seata.saga.engine.config.DbStateMachineConfig) DefaultStateMachineConfig(io.seata.saga.engine.impl.DefaultStateMachineConfig) StateMachineConfig(io.seata.saga.engine.StateMachineConfig) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) ExecutionException(io.seata.tm.api.TransactionalExecutor.ExecutionException) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) DbStateMachineConfig(io.seata.saga.engine.config.DbStateMachineConfig) StateMachineInstance(io.seata.saga.statelang.domain.StateMachineInstance)

Example 24 with EngineExecutionException

use of io.seata.saga.engine.exception.EngineExecutionException in project seata by seata.

the class DbAndReportTcStateLogStore method branchReport.

protected void branchReport(StateInstance stateInstance, ProcessContext context) {
    if (sagaTransactionalTemplate != null) {
        StateMachineConfig stateMachineConfig = (StateMachineConfig) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONFIG);
        if (stateMachineConfig instanceof DbStateMachineConfig && !((DbStateMachineConfig) stateMachineConfig).isSagaBranchRegisterEnable()) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("sagaBranchRegisterEnable = false, skip branch report. state[" + stateInstance.getName() + "]");
            }
            return;
        }
        BranchStatus branchStatus = null;
        // find out the original state instance, only the original state instance is registered on the server, and its status should
        // be reported.
        StateInstance originalStateInst = null;
        if (StringUtils.hasLength(stateInstance.getStateIdRetriedFor())) {
            if (isUpdateMode(stateInstance, context)) {
                originalStateInst = stateInstance;
            } else {
                originalStateInst = findOutOriginalStateInstanceOfRetryState(stateInstance);
            }
            if (ExecutionStatus.SU.equals(stateInstance.getStatus())) {
                branchStatus = BranchStatus.PhaseTwo_Committed;
            } else if (ExecutionStatus.FA.equals(stateInstance.getStatus()) || ExecutionStatus.UN.equals(stateInstance.getStatus())) {
                branchStatus = BranchStatus.PhaseOne_Failed;
            } else {
                branchStatus = BranchStatus.Unknown;
            }
        } else if (StringUtils.hasLength(stateInstance.getStateIdCompensatedFor())) {
            if (isUpdateMode(stateInstance, context)) {
                originalStateInst = stateInstance.getStateMachineInstance().getStateMap().get(stateInstance.getStateIdCompensatedFor());
            } else {
                originalStateInst = findOutOriginalStateInstanceOfCompensateState(stateInstance);
            }
        }
        if (originalStateInst == null) {
            originalStateInst = stateInstance;
        }
        if (branchStatus == null) {
            if (ExecutionStatus.SU.equals(originalStateInst.getStatus()) && originalStateInst.getCompensationStatus() == null) {
                branchStatus = BranchStatus.PhaseTwo_Committed;
            } else if (ExecutionStatus.SU.equals(originalStateInst.getCompensationStatus())) {
                branchStatus = BranchStatus.PhaseTwo_Rollbacked;
            } else if (ExecutionStatus.FA.equals(originalStateInst.getCompensationStatus()) || ExecutionStatus.UN.equals(originalStateInst.getCompensationStatus())) {
                branchStatus = BranchStatus.PhaseTwo_RollbackFailed_Retryable;
            } else if ((ExecutionStatus.FA.equals(originalStateInst.getStatus()) || ExecutionStatus.UN.equals(originalStateInst.getStatus())) && originalStateInst.getCompensationStatus() == null) {
                branchStatus = BranchStatus.PhaseOne_Failed;
            } else {
                branchStatus = BranchStatus.Unknown;
            }
        }
        try {
            StateMachineInstance machineInstance = stateInstance.getStateMachineInstance();
            GlobalTransaction globalTransaction = getGlobalTransaction(machineInstance, context);
            if (globalTransaction == null) {
                throw new EngineExecutionException("Global transaction is not exists", FrameworkErrorCode.ObjectNotExists);
            }
            sagaTransactionalTemplate.branchReport(globalTransaction.getXid(), Long.parseLong(originalStateInst.getId()), branchStatus, null);
        } catch (TransactionException e) {
            LOGGER.error("Report branch status to server error: {}, StateMachine:{}, StateName:{}, XID: {}, branchId: {}, branchStatus:{}," + " Reason:{} ", e.getCode(), originalStateInst.getStateMachineInstance().getStateMachine().getName(), originalStateInst.getName(), originalStateInst.getStateMachineInstance().getId(), originalStateInst.getId(), branchStatus, e.getMessage(), e);
        } catch (ExecutionException e) {
            LOGGER.error("Report branch status to server error: {}, StateMachine:{}, StateName:{}, XID: {}, branchId: {}, branchStatus:{}," + " Reason:{} ", e.getCode(), originalStateInst.getStateMachineInstance().getStateMachine().getName(), originalStateInst.getName(), originalStateInst.getStateMachineInstance().getId(), originalStateInst.getId(), branchStatus, e.getMessage(), e);
        }
    }
}
Also used : TransactionException(io.seata.core.exception.TransactionException) GlobalTransaction(io.seata.tm.api.GlobalTransaction) DbStateMachineConfig(io.seata.saga.engine.config.DbStateMachineConfig) DefaultStateMachineConfig(io.seata.saga.engine.impl.DefaultStateMachineConfig) StateMachineConfig(io.seata.saga.engine.StateMachineConfig) BranchStatus(io.seata.core.model.BranchStatus) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) ExecutionException(io.seata.tm.api.TransactionalExecutor.ExecutionException) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) DbStateMachineConfig(io.seata.saga.engine.config.DbStateMachineConfig) StateInstance(io.seata.saga.statelang.domain.StateInstance) StateMachineInstance(io.seata.saga.statelang.domain.StateMachineInstance)

Example 25 with EngineExecutionException

use of io.seata.saga.engine.exception.EngineExecutionException in project seata by seata.

the class DbAndReportTcStateLogStore method reportTransactionFinished.

protected void reportTransactionFinished(StateMachineInstance machineInstance, ProcessContext context) {
    if (sagaTransactionalTemplate != null) {
        try {
            GlobalTransaction globalTransaction = getGlobalTransaction(machineInstance, context);
            if (globalTransaction == null) {
                throw new EngineExecutionException("Global transaction is not exists", FrameworkErrorCode.ObjectNotExists);
            }
            GlobalStatus globalStatus;
            if (ExecutionStatus.SU.equals(machineInstance.getStatus()) && machineInstance.getCompensationStatus() == null) {
                globalStatus = GlobalStatus.Committed;
            } else if (ExecutionStatus.SU.equals(machineInstance.getCompensationStatus())) {
                globalStatus = GlobalStatus.Rollbacked;
            } else if (ExecutionStatus.FA.equals(machineInstance.getCompensationStatus()) || ExecutionStatus.UN.equals(machineInstance.getCompensationStatus())) {
                globalStatus = GlobalStatus.RollbackRetrying;
            } else if (ExecutionStatus.FA.equals(machineInstance.getStatus()) && machineInstance.getCompensationStatus() == null) {
                globalStatus = GlobalStatus.Finished;
            } else if (ExecutionStatus.UN.equals(machineInstance.getStatus()) && machineInstance.getCompensationStatus() == null) {
                globalStatus = GlobalStatus.CommitRetrying;
            } else {
                globalStatus = GlobalStatus.UnKnown;
            }
            sagaTransactionalTemplate.reportTransaction(globalTransaction, globalStatus);
        } catch (ExecutionException e) {
            LOGGER.error("Report transaction finish to server error: {}, StateMachine: {}, XID: {}, Reason: {}", e.getCode(), machineInstance.getStateMachine().getName(), machineInstance.getId(), e.getMessage(), e);
        } catch (TransactionException e) {
            LOGGER.error("Report transaction finish to server error: {}, StateMachine: {}, XID: {}, Reason: {}", e.getCode(), machineInstance.getStateMachine().getName(), machineInstance.getId(), e.getMessage(), e);
        } finally {
            // clear
            RootContext.unbind();
            RootContext.unbindBranchType();
            sagaTransactionalTemplate.triggerAfterCompletion();
            sagaTransactionalTemplate.cleanUp();
        }
    }
}
Also used : TransactionException(io.seata.core.exception.TransactionException) GlobalStatus(io.seata.core.model.GlobalStatus) GlobalTransaction(io.seata.tm.api.GlobalTransaction) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) ExecutionException(io.seata.tm.api.TransactionalExecutor.ExecutionException) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException)

Aggregations

EngineExecutionException (io.seata.saga.engine.exception.EngineExecutionException)34 StateMachineInstance (io.seata.saga.statelang.domain.StateMachineInstance)20 StateMachineConfig (io.seata.saga.engine.StateMachineConfig)14 StateInstruction (io.seata.saga.engine.pcext.StateInstruction)14 StateInstance (io.seata.saga.statelang.domain.StateInstance)11 Map (java.util.Map)10 GlobalTransaction (io.seata.tm.api.GlobalTransaction)6 ArrayList (java.util.ArrayList)6 HierarchicalProcessContext (io.seata.saga.proctrl.HierarchicalProcessContext)5 ServiceTaskStateImpl (io.seata.saga.statelang.domain.impl.ServiceTaskStateImpl)5 Date (java.util.Date)5 List (java.util.List)5 TransactionException (io.seata.core.exception.TransactionException)4 ForwardInvalidException (io.seata.saga.engine.exception.ForwardInvalidException)4 ProcessContext (io.seata.saga.proctrl.ProcessContext)4 ExecutionStatus (io.seata.saga.statelang.domain.ExecutionStatus)4 State (io.seata.saga.statelang.domain.State)4 StateMachine (io.seata.saga.statelang.domain.StateMachine)4 ExecutionException (io.seata.tm.api.TransactionalExecutor.ExecutionException)4 HashMap (java.util.HashMap)4