Search in sources :

Example 11 with EngineExecutionException

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

the class DbAndReportTcStateLogStore method beginTransaction.

protected void beginTransaction(StateMachineInstance machineInstance, ProcessContext context) {
    if (sagaTransactionalTemplate != null) {
        StateMachineConfig stateMachineConfig = (StateMachineConfig) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONFIG);
        TransactionInfo transactionInfo = new TransactionInfo();
        transactionInfo.setTimeOut(stateMachineConfig.getTransOperationTimeout());
        transactionInfo.setName(Constants.SAGA_TRANS_NAME_PREFIX + machineInstance.getStateMachine().getName());
        try {
            GlobalTransaction globalTransaction = sagaTransactionalTemplate.beginTransaction(transactionInfo);
            machineInstance.setId(globalTransaction.getXid());
            context.setVariable(DomainConstants.VAR_NAME_GLOBAL_TX, globalTransaction);
            Map<String, Object> machineContext = machineInstance.getContext();
            if (machineContext != null) {
                machineContext.put(DomainConstants.VAR_NAME_GLOBAL_TX, globalTransaction);
            }
        } catch (ExecutionException e) {
            String xid = null;
            if (e.getTransaction() != null) {
                xid = e.getTransaction().getXid();
            }
            throw new EngineExecutionException(e, e.getCode() + ", TransName:" + transactionInfo.getName() + ", XID: " + xid + ", Reason: " + e.getMessage(), FrameworkErrorCode.TransactionManagerError);
        } finally {
            if (Boolean.TRUE.equals(context.getVariable(DomainConstants.VAR_NAME_IS_ASYNC_EXECUTION))) {
                RootContext.unbind();
                RootContext.unbindBranchType();
            }
        }
    }
}
Also used : TransactionInfo(io.seata.tm.api.transaction.TransactionInfo) 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)

Example 12 with EngineExecutionException

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

the class StateMachineDBTests method doTestStateMachineCustomRecoverStrategyOnTimeout.

private void doTestStateMachineCustomRecoverStrategyOnTimeout(Map<String, Object> paramMap) throws Exception {
    long start = System.currentTimeMillis();
    String stateMachineName = "simpleStateMachineWithRecoverStrategy";
    StateMachineInstance inst;
    try {
        inst = stateMachineEngine.start(stateMachineName, null, paramMap);
    } catch (EngineExecutionException e) {
        e.printStackTrace();
        inst = stateMachineEngine.getStateMachineConfig().getStateLogStore().getStateMachineInstance(e.getStateMachineInstanceId());
    }
    long cost = System.currentTimeMillis() - start;
    System.out.println("====== cost :" + cost);
    GlobalTransaction globalTransaction = getGlobalTransaction(inst);
    Assertions.assertNotNull(globalTransaction);
    System.out.println("====== GlobalStatus: " + globalTransaction.getStatus());
    // waiting for global transaction recover
    while (!(ExecutionStatus.SU.equals(inst.getStatus()) && GlobalStatus.Finished.equals(globalTransaction.getStatus()))) {
        System.out.println("====== GlobalStatus: " + globalTransaction.getStatus());
        System.out.println("====== StateMachineInstanceStatus: " + inst.getStatus());
        Thread.sleep(2000);
        inst = stateMachineEngine.getStateMachineConfig().getStateLogStore().getStateMachineInstance(inst.getId());
    }
    Assertions.assertTrue(ExecutionStatus.SU.equals(inst.getStatus()));
    Assertions.assertNull(inst.getCompensationStatus());
}
Also used : GlobalTransaction(io.seata.tm.api.GlobalTransaction) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) StateMachineInstance(io.seata.saga.statelang.domain.StateMachineInstance)

Example 13 with EngineExecutionException

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

the class StateMachineDBTests method doTestStateMachineTransTimeout.

private void doTestStateMachineTransTimeout(Map<String, Object> paramMap) throws Exception {
    long start = System.currentTimeMillis();
    String stateMachineName = "simpleCompensationStateMachine";
    StateMachineInstance inst;
    try {
        inst = stateMachineEngine.start(stateMachineName, null, paramMap);
    } catch (EngineExecutionException e) {
        e.printStackTrace();
        inst = stateMachineEngine.getStateMachineConfig().getStateLogStore().getStateMachineInstance(e.getStateMachineInstanceId());
    }
    long cost = System.currentTimeMillis() - start;
    System.out.println("====== cost :" + cost);
    GlobalTransaction globalTransaction = getGlobalTransaction(inst);
    Assertions.assertNotNull(globalTransaction);
    System.out.println("====== GlobalStatus: " + globalTransaction.getStatus());
    // waiting for global transaction recover
    while (!ExecutionStatus.SU.equals(inst.getCompensationStatus())) {
        System.out.println("====== GlobalStatus: " + globalTransaction.getStatus());
        Thread.sleep(2000);
        inst = stateMachineEngine.getStateMachineConfig().getStateLogStore().getStateMachineInstance(inst.getId());
    }
    Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus()) || ExecutionStatus.SU.equals(inst.getStatus()));
    Assertions.assertTrue(ExecutionStatus.SU.equals(inst.getCompensationStatus()));
}
Also used : GlobalTransaction(io.seata.tm.api.GlobalTransaction) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) StateMachineInstance(io.seata.saga.statelang.domain.StateMachineInstance)

Example 14 with EngineExecutionException

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

the class ServiceTaskHandlerInterceptor method postProcess.

@Override
public void postProcess(ProcessContext context, Exception exp) throws EngineExecutionException {
    StateInstruction instruction = context.getInstruction(StateInstruction.class);
    ServiceTaskStateImpl state = (ServiceTaskStateImpl) instruction.getState(context);
    StateMachineInstance stateMachineInstance = (StateMachineInstance) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_INST);
    StateInstance stateInstance = (StateInstance) context.getVariable(DomainConstants.VAR_NAME_STATE_INST);
    if (stateInstance == null || !stateMachineInstance.isRunning()) {
        LOGGER.warn("StateMachineInstance[id:" + stateMachineInstance.getId() + "] is end. stop running");
        return;
    }
    StateMachineConfig stateMachineConfig = (StateMachineConfig) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONFIG);
    if (exp == null) {
        exp = (Exception) context.getVariable(DomainConstants.VAR_NAME_CURRENT_EXCEPTION);
    }
    stateInstance.setException(exp);
    decideExecutionStatus(context, stateInstance, state, exp);
    if (ExecutionStatus.SU.equals(stateInstance.getStatus()) && exp != null) {
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("Although an exception occurs, the execution status map to SU, and the exception is ignored when " + "the execution status decision.");
        }
        context.removeVariable(DomainConstants.VAR_NAME_CURRENT_EXCEPTION);
    }
    Map<String, Object> contextVariables = (Map<String, Object>) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONTEXT);
    Object serviceOutputParams = context.getVariable(DomainConstants.VAR_NAME_OUTPUT_PARAMS);
    if (serviceOutputParams != null) {
        try {
            Map<String, Object> outputVariablesToContext = ParameterUtils.createOutputParams(stateMachineConfig.getExpressionFactoryManager(), state, serviceOutputParams);
            if (CollectionUtils.isNotEmpty(outputVariablesToContext)) {
                contextVariables.putAll(outputVariablesToContext);
            }
        } catch (Exception e) {
            String message = "Task [" + state.getName() + "] output parameters assign failed, please check 'Output' expression:" + e.getMessage();
            EngineExecutionException exception = ExceptionUtils.createEngineExecutionException(e, FrameworkErrorCode.VariablesAssignError, message, stateMachineInstance, stateInstance);
            if (stateMachineInstance.getStateMachine().isPersist() && state.isPersist() && stateMachineConfig.getStateLogStore() != null) {
                stateMachineConfig.getStateLogStore().recordStateFinished(stateInstance, context);
            }
            EngineUtils.failStateMachine(context, exception);
            throw exception;
        }
    }
    context.removeVariable(DomainConstants.VAR_NAME_OUTPUT_PARAMS);
    context.removeVariable(DomainConstants.VAR_NAME_INPUT_PARAMS);
    stateInstance.setGmtEnd(new Date());
    if (stateMachineInstance.getStateMachine().isPersist() && state.isPersist() && stateMachineConfig.getStateLogStore() != null) {
        stateMachineConfig.getStateLogStore().recordStateFinished(stateInstance, context);
    }
    if (exp != null && context.getVariable(DomainConstants.VAR_NAME_IS_EXCEPTION_NOT_CATCH) != null && (Boolean) context.getVariable(DomainConstants.VAR_NAME_IS_EXCEPTION_NOT_CATCH)) {
        // If there is an exception and there is no catch, need to exit the state machine to execute.
        context.removeVariable(DomainConstants.VAR_NAME_IS_EXCEPTION_NOT_CATCH);
        EngineUtils.failStateMachine(context, exp);
    }
}
Also used : StateInstruction(io.seata.saga.engine.pcext.StateInstruction) StateMachineConfig(io.seata.saga.engine.StateMachineConfig) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) Date(java.util.Date) ServiceTaskStateImpl(io.seata.saga.statelang.domain.impl.ServiceTaskStateImpl) StateMachineInstance(io.seata.saga.statelang.domain.StateMachineInstance) StateInstance(io.seata.saga.statelang.domain.StateInstance)

Example 15 with EngineExecutionException

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

the class ServiceTaskHandlerInterceptor method decideExecutionStatus.

private void decideExecutionStatus(ProcessContext context, StateInstance stateInstance, ServiceTaskStateImpl state, Exception exp) {
    Map<String, String> statusMatchList = state.getStatus();
    if (CollectionUtils.isNotEmpty(statusMatchList)) {
        if (state.isAsync()) {
            if (LOGGER.isWarnEnabled()) {
                LOGGER.warn("Service[{}.{}] is execute asynchronously, null return value collected, so user defined " + "Status Matching skipped. stateName: {}, branchId: {}", state.getServiceName(), state.getServiceMethod(), state.getName(), stateInstance.getId());
            }
        } else {
            StateMachineConfig stateMachineConfig = (StateMachineConfig) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONFIG);
            Map<Object, String> statusEvaluators = state.getStatusEvaluators();
            if (statusEvaluators == null) {
                synchronized (state) {
                    statusEvaluators = state.getStatusEvaluators();
                    if (statusEvaluators == null) {
                        statusEvaluators = new LinkedHashMap<>(statusMatchList.size());
                        String expressionStr, statusVal;
                        Evaluator evaluator;
                        for (Map.Entry<String, String> entry : statusMatchList.entrySet()) {
                            expressionStr = entry.getKey();
                            statusVal = entry.getValue();
                            evaluator = createEvaluator(stateMachineConfig.getEvaluatorFactoryManager(), expressionStr);
                            if (evaluator != null) {
                                statusEvaluators.put(evaluator, statusVal);
                            }
                        }
                    }
                    state.setStatusEvaluators(statusEvaluators);
                }
            }
            for (Object evaluatorObj : statusEvaluators.keySet()) {
                Evaluator evaluator = (Evaluator) evaluatorObj;
                String statusVal = statusEvaluators.get(evaluator);
                if (evaluator.evaluate(context.getVariables())) {
                    stateInstance.setStatus(ExecutionStatus.valueOf(statusVal));
                    break;
                }
            }
            if (exp == null && (stateInstance.getStatus() == null || ExecutionStatus.RU.equals(stateInstance.getStatus()))) {
                if (state.isForUpdate()) {
                    stateInstance.setStatus(ExecutionStatus.UN);
                } else {
                    stateInstance.setStatus(ExecutionStatus.FA);
                }
                stateInstance.setGmtEnd(new Date());
                StateMachineInstance stateMachineInstance = stateInstance.getStateMachineInstance();
                if (stateMachineInstance.getStateMachine().isPersist() && state.isPersist() && stateMachineConfig.getStateLogStore() != null) {
                    stateMachineConfig.getStateLogStore().recordStateFinished(stateInstance, context);
                }
                EngineExecutionException exception = new EngineExecutionException("State [" + state.getName() + "] execute finished, but cannot matching status, pls check its status manually", FrameworkErrorCode.NoMatchedStatus);
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("State[{}] execute finish with status[{}]", state.getName(), stateInstance.getStatus());
                }
                EngineUtils.failStateMachine(context, exception);
                throw exception;
            }
        }
    }
    if (stateInstance.getStatus() == null || ExecutionStatus.RU.equals(stateInstance.getStatus())) {
        if (exp == null) {
            stateInstance.setStatus(ExecutionStatus.SU);
        } else {
            if (state.isForUpdate() || state.isForCompensation()) {
                stateInstance.setStatus(ExecutionStatus.UN);
                ExceptionUtils.NetExceptionType t = ExceptionUtils.getNetExceptionType(exp);
                if (t != null) {
                    if (t.equals(ExceptionUtils.NetExceptionType.CONNECT_EXCEPTION)) {
                        stateInstance.setStatus(ExecutionStatus.FA);
                    } else if (t.equals(ExceptionUtils.NetExceptionType.READ_TIMEOUT_EXCEPTION)) {
                        stateInstance.setStatus(ExecutionStatus.UN);
                    }
                } else {
                    stateInstance.setStatus(ExecutionStatus.UN);
                }
            } else {
                stateInstance.setStatus(ExecutionStatus.FA);
            }
        }
    }
    if (LOGGER.isInfoEnabled()) {
        LOGGER.info("State[{}] finish with status[{}]", state.getName(), stateInstance.getStatus());
    }
}
Also used : ExceptionUtils(io.seata.saga.engine.utils.ExceptionUtils) ExpressionEvaluator(io.seata.saga.engine.evaluation.expression.ExpressionEvaluator) Evaluator(io.seata.saga.engine.evaluation.Evaluator) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) Date(java.util.Date) StateMachineInstance(io.seata.saga.statelang.domain.StateMachineInstance) StateMachineConfig(io.seata.saga.engine.StateMachineConfig) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

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