Search in sources :

Example 1 with SubStateMachineImpl

use of io.seata.saga.statelang.domain.impl.SubStateMachineImpl in project seata by seata.

the class SubStateMachineHandler method process.

@Override
public void process(ProcessContext context) throws EngineExecutionException {
    StateInstruction instruction = context.getInstruction(StateInstruction.class);
    SubStateMachineImpl subStateMachine = (SubStateMachineImpl) instruction.getState(context);
    StateMachineEngine engine = (StateMachineEngine) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_ENGINE);
    StateMachineInstance stateMachineInstance = (StateMachineInstance) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_INST);
    StateInstance stateInstance = (StateInstance) context.getVariable(DomainConstants.VAR_NAME_STATE_INST);
    Object inputParamsObj = context.getVariable(DomainConstants.VAR_NAME_INPUT_PARAMS);
    Map<String, Object> startParams = new HashMap<>(0);
    if (inputParamsObj instanceof List) {
        List<Object> listInputParams = (List<Object>) inputParamsObj;
        if (listInputParams.size() > 0) {
            startParams = (Map<String, Object>) listInputParams.get(0);
        }
    } else if (inputParamsObj instanceof Map) {
        startParams = (Map<String, Object>) inputParamsObj;
    }
    startParams.put(DomainConstants.VAR_NAME_PARENT_ID, EngineUtils.generateParentId(stateInstance));
    try {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(">>>>>>>>>>>>>>>>>>>>>> Start to execute SubStateMachine [{}] by state[{}]", subStateMachine.getStateMachineName(), subStateMachine.getName());
        }
        StateMachineInstance subStateMachineInstance = callSubStateMachine(startParams, engine, context, stateInstance, subStateMachine);
        Map<String, Object> outputParams = subStateMachineInstance.getEndParams();
        boolean isForward = DomainConstants.OPERATION_NAME_FORWARD.equals(context.getVariable(DomainConstants.VAR_NAME_OPERATION_NAME));
        ExecutionStatus callSubMachineStatus = decideStatus(subStateMachineInstance, isForward);
        stateInstance.setStatus(callSubMachineStatus);
        outputParams.put(DomainConstants.VAR_NAME_SUB_STATEMACHINE_EXEC_STATUE, callSubMachineStatus.toString());
        context.setVariable(DomainConstants.VAR_NAME_OUTPUT_PARAMS, outputParams);
        stateInstance.setOutputParams(outputParams);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("<<<<<<<<<<<<<<<<<<<<<< SubStateMachine[{}] execute finish with status[{}], compensateStatus[{}]", subStateMachine.getStateMachineName(), subStateMachineInstance.getStatus(), subStateMachineInstance.getCompensationStatus());
        }
    } catch (Exception e) {
        LOGGER.error("SubStateMachine[{}] execute failed by state[name:{}]", subStateMachine.getStateMachineName(), subStateMachine.getName(), e);
        if (e instanceof ForwardInvalidException) {
            String retriedId = stateInstance.getStateIdRetriedFor();
            StateInstance stateToBeRetried = null;
            for (StateInstance stateInst : stateMachineInstance.getStateList()) {
                if (retriedId.equals(stateInst.getId())) {
                    stateToBeRetried = stateInst;
                    break;
                }
            }
            if (stateToBeRetried != null) {
                stateInstance.setStatus(stateToBeRetried.getStatus());
            }
        }
        context.setVariable(DomainConstants.VAR_NAME_CURRENT_EXCEPTION, e);
        EngineUtils.handleException(context, subStateMachine, e);
    }
}
Also used : StateMachineEngine(io.seata.saga.engine.StateMachineEngine) StateInstruction(io.seata.saga.engine.pcext.StateInstruction) HashMap(java.util.HashMap) ForwardInvalidException(io.seata.saga.engine.exception.ForwardInvalidException) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) ForwardInvalidException(io.seata.saga.engine.exception.ForwardInvalidException) StateMachineInstance(io.seata.saga.statelang.domain.StateMachineInstance) ExecutionStatus(io.seata.saga.statelang.domain.ExecutionStatus) ArrayList(java.util.ArrayList) List(java.util.List) SubStateMachineImpl(io.seata.saga.statelang.domain.impl.SubStateMachineImpl) HashMap(java.util.HashMap) Map(java.util.Map) StateInstance(io.seata.saga.statelang.domain.StateInstance)

Example 2 with SubStateMachineImpl

use of io.seata.saga.statelang.domain.impl.SubStateMachineImpl in project seata by seata.

the class SubStateMachineParser method parse.

@Override
public SubStateMachine parse(Object node) {
    SubStateMachineImpl subStateMachine = new SubStateMachineImpl();
    parseTaskAttributes(subStateMachine, node);
    Map<String, Object> nodeMap = (Map<String, Object>) node;
    subStateMachine.setStateMachineName((String) nodeMap.get("StateMachineName"));
    if (StringUtils.isEmpty(subStateMachine.getCompensateState())) {
        // build default SubStateMachine compensate state
        CompensateSubStateMachineStateParser compensateSubStateMachineStateParser = new CompensateSubStateMachineStateParser();
        ServiceTaskState subStateMachineCompenState = compensateSubStateMachineStateParser.parse(null);
        subStateMachine.setCompensateStateObject(subStateMachineCompenState);
        subStateMachine.setCompensateState(subStateMachineCompenState.getName());
    }
    return subStateMachine;
}
Also used : ServiceTaskState(io.seata.saga.statelang.domain.ServiceTaskState) SubStateMachineImpl(io.seata.saga.statelang.domain.impl.SubStateMachineImpl) Map(java.util.Map)

Aggregations

SubStateMachineImpl (io.seata.saga.statelang.domain.impl.SubStateMachineImpl)2 Map (java.util.Map)2 StateMachineEngine (io.seata.saga.engine.StateMachineEngine)1 EngineExecutionException (io.seata.saga.engine.exception.EngineExecutionException)1 ForwardInvalidException (io.seata.saga.engine.exception.ForwardInvalidException)1 StateInstruction (io.seata.saga.engine.pcext.StateInstruction)1 ExecutionStatus (io.seata.saga.statelang.domain.ExecutionStatus)1 ServiceTaskState (io.seata.saga.statelang.domain.ServiceTaskState)1 StateInstance (io.seata.saga.statelang.domain.StateInstance)1 StateMachineInstance (io.seata.saga.statelang.domain.StateMachineInstance)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1