Search in sources :

Example 31 with EngineExecutionException

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

the class LoopStartStateHandler method process.

@Override
public void process(ProcessContext context) throws EngineExecutionException {
    StateInstruction instruction = context.getInstruction(StateInstruction.class);
    StateMachineInstance stateMachineInstance = (StateMachineInstance) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_INST);
    StateMachineConfig stateMachineConfig = (StateMachineConfig) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONFIG);
    instruction.setTemporaryState(null);
    Loop loop = LoopTaskUtils.getLoopConfig(context, instruction.getState(context));
    LoopContextHolder loopContextHolder = LoopContextHolder.getCurrent(context, true);
    Semaphore semaphore = null;
    int maxInstances = 0;
    List<ProcessContext> loopContextList = new ArrayList<>();
    if (null != loop) {
        if (!stateMachineConfig.isEnableAsync() || null == stateMachineConfig.getAsyncProcessCtrlEventPublisher()) {
            throw new EngineExecutionException("Asynchronous start is disabled. Loop execution will run asynchronous, please set " + "StateMachineConfig.enableAsync=true first.", FrameworkErrorCode.AsynchronousStartDisabled);
        }
        int totalInstances;
        if (DomainConstants.OPERATION_NAME_FORWARD.equals(context.getVariable(DomainConstants.VAR_NAME_OPERATION_NAME))) {
            LoopTaskUtils.reloadLoopContext(context, instruction.getState(context).getName());
            totalInstances = loopContextHolder.getNrOfInstances().get() - loopContextHolder.getNrOfCompletedInstances().get();
        } else {
            LoopTaskUtils.createLoopCounterContext(context);
            totalInstances = loopContextHolder.getNrOfInstances().get();
        }
        maxInstances = Math.min(loop.getParallel(), totalInstances);
        semaphore = new Semaphore(maxInstances);
        context.setVariable(DomainConstants.LOOP_SEMAPHORE, semaphore);
        context.setVariable(DomainConstants.VAR_NAME_IS_LOOP_STATE, true);
        // publish loop tasks
        for (int i = 0; i < totalInstances; i++) {
            try {
                semaphore.acquire();
                ProcessContextImpl tempContext;
                // fail end inst should be forward without completion condition check
                if (!loopContextHolder.getForwardCounterStack().isEmpty()) {
                    int failEndLoopCounter = loopContextHolder.getForwardCounterStack().pop();
                    tempContext = (ProcessContextImpl) LoopTaskUtils.createLoopEventContext(context, failEndLoopCounter);
                } else if (loopContextHolder.isFailEnd() || LoopTaskUtils.isCompletionConditionSatisfied(context)) {
                    semaphore.release();
                    break;
                } else {
                    tempContext = (ProcessContextImpl) LoopTaskUtils.createLoopEventContext(context, -1);
                }
                if (DomainConstants.OPERATION_NAME_FORWARD.equals(context.getVariable(DomainConstants.VAR_NAME_OPERATION_NAME))) {
                    ((HierarchicalProcessContext) context).setVariableLocally(DomainConstants.VAR_NAME_IS_FOR_SUB_STATMACHINE_FORWARD, LoopTaskUtils.isForSubStateMachineForward(tempContext));
                }
                stateMachineConfig.getAsyncProcessCtrlEventPublisher().publish(tempContext);
                loopContextHolder.getNrOfActiveInstances().incrementAndGet();
                loopContextList.add(tempContext);
            } catch (InterruptedException e) {
                LOGGER.error("try execute loop task for State: [{}] is interrupted, message: [{}]", instruction.getStateName(), e.getMessage());
                throw new EngineExecutionException(e);
            }
        }
    } else {
        LOGGER.warn("Loop config of State [{}] is illegal, will execute as normal", instruction.getStateName());
        instruction.setTemporaryState(instruction.getState(context));
    }
    try {
        if (null != semaphore) {
            boolean isFinished = false;
            while (!isFinished) {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("wait {}ms for loop state [{}] finish", AWAIT_TIMEOUT, instruction.getStateName());
                }
                isFinished = semaphore.tryAcquire(maxInstances, AWAIT_TIMEOUT, TimeUnit.MILLISECONDS);
            }
            if (loopContextList.size() > 0) {
                LoopTaskUtils.putContextToParent(context, loopContextList, instruction.getState(context));
            }
        }
    } catch (InterruptedException e) {
        LOGGER.error("State: [{}] wait loop execution complete is interrupted, message: [{}]", instruction.getStateName(), e.getMessage());
        throw new EngineExecutionException(e);
    } finally {
        context.removeVariable(DomainConstants.LOOP_SEMAPHORE);
        context.removeVariable(DomainConstants.VAR_NAME_IS_LOOP_STATE);
        LoopContextHolder.clearCurrent(context);
    }
    if (loopContextHolder.isFailEnd()) {
        String currentExceptionRoute = LoopTaskUtils.decideCurrentExceptionRoute(loopContextList, stateMachineInstance.getStateMachine());
        if (StringUtils.isNotBlank(currentExceptionRoute)) {
            ((HierarchicalProcessContext) context).setVariableLocally(DomainConstants.VAR_NAME_CURRENT_EXCEPTION_ROUTE, currentExceptionRoute);
        } else {
            for (ProcessContext processContext : loopContextList) {
                if (processContext.hasVariable(DomainConstants.VAR_NAME_CURRENT_EXCEPTION)) {
                    Exception exception = (Exception) processContext.getVariable(DomainConstants.VAR_NAME_CURRENT_EXCEPTION);
                    EngineUtils.failStateMachine(context, exception);
                    break;
                }
            }
        }
    }
}
Also used : Loop(io.seata.saga.statelang.domain.TaskState.Loop) ProcessContextImpl(io.seata.saga.proctrl.impl.ProcessContextImpl) StateInstruction(io.seata.saga.engine.pcext.StateInstruction) ArrayList(java.util.ArrayList) HierarchicalProcessContext(io.seata.saga.proctrl.HierarchicalProcessContext) Semaphore(java.util.concurrent.Semaphore) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) HierarchicalProcessContext(io.seata.saga.proctrl.HierarchicalProcessContext) ProcessContext(io.seata.saga.proctrl.ProcessContext) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) StateMachineInstance(io.seata.saga.statelang.domain.StateMachineInstance) LoopContextHolder(io.seata.saga.engine.pcext.utils.LoopContextHolder) StateMachineConfig(io.seata.saga.engine.StateMachineConfig)

Example 32 with EngineExecutionException

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

the class ChoiceStateHandler method process.

@Override
public void process(ProcessContext context) throws EngineExecutionException {
    StateInstruction instruction = context.getInstruction(StateInstruction.class);
    ChoiceStateImpl choiceState = (ChoiceStateImpl) instruction.getState(context);
    Map<Object, String> choiceEvaluators = choiceState.getChoiceEvaluators();
    if (choiceEvaluators == null) {
        synchronized (choiceState) {
            choiceEvaluators = choiceState.getChoiceEvaluators();
            if (choiceEvaluators == null) {
                List<ChoiceState.Choice> choices = choiceState.getChoices();
                if (choices == null) {
                    choiceEvaluators = new LinkedHashMap<>(0);
                } else {
                    choiceEvaluators = new LinkedHashMap<>(choices.size());
                    for (ChoiceState.Choice choice : choices) {
                        Evaluator evaluator = getEvaluatorFactory(context).createEvaluator(choice.getExpression());
                        choiceEvaluators.put(evaluator, choice.getNext());
                    }
                }
                choiceState.setChoiceEvaluators(choiceEvaluators);
            }
        }
    }
    Evaluator evaluator;
    for (Map.Entry<Object, String> entry : choiceEvaluators.entrySet()) {
        evaluator = (Evaluator) entry.getKey();
        if (evaluator.evaluate(context.getVariables())) {
            context.setVariable(DomainConstants.VAR_NAME_CURRENT_CHOICE, entry.getValue());
            return;
        }
    }
    if (StringUtils.isEmpty(choiceState.getDefault())) {
        StateMachineInstance stateMachineInstance = (StateMachineInstance) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_INST);
        EngineExecutionException exception = ExceptionUtils.createEngineExecutionException(FrameworkErrorCode.StateMachineNoChoiceMatched, "No choice matched, maybe it is a bug. Choice state name: " + choiceState.getName(), stateMachineInstance, null);
        EngineUtils.failStateMachine(context, exception);
        throw exception;
    }
    context.setVariable(DomainConstants.VAR_NAME_CURRENT_CHOICE, choiceState.getDefault());
}
Also used : StateInstruction(io.seata.saga.engine.pcext.StateInstruction) ChoiceStateImpl(io.seata.saga.statelang.domain.impl.ChoiceStateImpl) Evaluator(io.seata.saga.engine.evaluation.Evaluator) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) StateMachineInstance(io.seata.saga.statelang.domain.StateMachineInstance) ChoiceState(io.seata.saga.statelang.domain.ChoiceState) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 33 with EngineExecutionException

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

the class ScriptTaskHandlerInterceptor method postProcess.

@Override
public void postProcess(ProcessContext context, Exception exp) throws EngineExecutionException {
    StateInstruction instruction = context.getInstruction(StateInstruction.class);
    ScriptTaskStateImpl state = (ScriptTaskStateImpl) instruction.getState(context);
    StateMachineInstance stateMachineInstance = (StateMachineInstance) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_INST);
    StateMachineConfig stateMachineConfig = (StateMachineConfig) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONFIG);
    if (exp == null) {
        exp = (Exception) context.getVariable(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, state.getName());
            EngineUtils.failStateMachine(context, exception);
            throw exception;
        }
    }
    context.removeVariable(DomainConstants.VAR_NAME_OUTPUT_PARAMS);
    context.removeVariable(DomainConstants.VAR_NAME_INPUT_PARAMS);
    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) ScriptTaskStateImpl(io.seata.saga.statelang.domain.impl.ScriptTaskStateImpl) StateMachineConfig(io.seata.saga.engine.StateMachineConfig) Map(java.util.Map) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) StateMachineInstance(io.seata.saga.statelang.domain.StateMachineInstance)

Example 34 with EngineExecutionException

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

the class ScriptTaskHandlerInterceptor method preProcess.

@Override
public void preProcess(ProcessContext context) throws EngineExecutionException {
    StateInstruction instruction = context.getInstruction(StateInstruction.class);
    StateMachineInstance stateMachineInstance = (StateMachineInstance) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_INST);
    StateMachineConfig stateMachineConfig = (StateMachineConfig) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONFIG);
    Map<String, Object> contextVariables = (Map<String, Object>) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONTEXT);
    ScriptTaskStateImpl state = (ScriptTaskStateImpl) instruction.getState(context);
    List<Object> serviceInputParams = null;
    if (contextVariables != null) {
        try {
            serviceInputParams = ParameterUtils.createInputParams(stateMachineConfig.getExpressionFactoryManager(), null, state, contextVariables);
        } catch (Exception e) {
            String message = "Task [" + state.getName() + "] input parameters assign failed, please check 'Input' expression:" + e.getMessage();
            EngineExecutionException exception = ExceptionUtils.createEngineExecutionException(e, FrameworkErrorCode.VariablesAssignError, message, stateMachineInstance, state.getName());
            EngineUtils.failStateMachine(context, exception);
            throw exception;
        }
    }
    ((HierarchicalProcessContext) context).setVariableLocally(DomainConstants.VAR_NAME_INPUT_PARAMS, serviceInputParams);
}
Also used : StateInstruction(io.seata.saga.engine.pcext.StateInstruction) ScriptTaskStateImpl(io.seata.saga.statelang.domain.impl.ScriptTaskStateImpl) StateMachineConfig(io.seata.saga.engine.StateMachineConfig) HierarchicalProcessContext(io.seata.saga.proctrl.HierarchicalProcessContext) Map(java.util.Map) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) StateMachineInstance(io.seata.saga.statelang.domain.StateMachineInstance)

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