Search in sources :

Example 6 with AbstractTaskState

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

the class ProcessCtrlStateMachineEngine method forwardInternal.

protected StateMachineInstance forwardInternal(String stateMachineInstId, Map<String, Object> replaceParams, boolean skip, boolean async, AsyncCallback callback) throws EngineExecutionException {
    StateMachineInstance stateMachineInstance = reloadStateMachineInstance(stateMachineInstId);
    if (stateMachineInstance == null) {
        throw new ForwardInvalidException("StateMachineInstance is not exits", FrameworkErrorCode.StateMachineInstanceNotExists);
    }
    if (ExecutionStatus.SU.equals(stateMachineInstance.getStatus()) && stateMachineInstance.getCompensationStatus() == null) {
        return stateMachineInstance;
    }
    ExecutionStatus[] acceptStatus = new ExecutionStatus[] { ExecutionStatus.FA, ExecutionStatus.UN, ExecutionStatus.RU };
    checkStatus(stateMachineInstance, acceptStatus, null, stateMachineInstance.getStatus(), null, "forward");
    List<StateInstance> actList = stateMachineInstance.getStateList();
    if (CollectionUtils.isEmpty(actList)) {
        throw new ForwardInvalidException("StateMachineInstance[id:" + stateMachineInstId + "] has no stateInstance, pls start a new StateMachine execution instead", FrameworkErrorCode.OperationDenied);
    }
    StateInstance lastForwardState = findOutLastForwardStateInstance(actList);
    if (lastForwardState == null) {
        throw new ForwardInvalidException("StateMachineInstance[id:" + stateMachineInstId + "] Cannot find last forward execution stateInstance", FrameworkErrorCode.OperationDenied);
    }
    ProcessContextBuilder contextBuilder = ProcessContextBuilder.create().withProcessType(ProcessType.STATE_LANG).withOperationName(DomainConstants.OPERATION_NAME_FORWARD).withAsyncCallback(callback).withStateMachineInstance(stateMachineInstance).withStateInstance(lastForwardState).withStateMachineConfig(getStateMachineConfig()).withStateMachineEngine(this);
    contextBuilder.withIsAsyncExecution(async);
    ProcessContext context = contextBuilder.build();
    Map<String, Object> contextVariables = getStateMachineContextVariables(stateMachineInstance);
    if (replaceParams != null) {
        contextVariables.putAll(replaceParams);
    }
    putBusinesskeyToContextariables(stateMachineInstance, contextVariables);
    ConcurrentHashMap<String, Object> concurrentContextVariables = new ConcurrentHashMap<>(contextVariables.size());
    nullSafeCopy(contextVariables, concurrentContextVariables);
    context.setVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONTEXT, concurrentContextVariables);
    stateMachineInstance.setContext(concurrentContextVariables);
    String originStateName = EngineUtils.getOriginStateName(lastForwardState);
    State lastState = stateMachineInstance.getStateMachine().getState(originStateName);
    Loop loop = LoopTaskUtils.getLoopConfig(context, lastState);
    if (null != loop && ExecutionStatus.SU.equals(lastForwardState.getStatus())) {
        lastForwardState = LoopTaskUtils.findOutLastNeedForwardStateInstance(context);
    }
    context.setVariable(lastForwardState.getName() + DomainConstants.VAR_NAME_RETRIED_STATE_INST_ID, lastForwardState.getId());
    if (DomainConstants.STATE_TYPE_SUB_STATE_MACHINE.equals(lastForwardState.getType()) && !ExecutionStatus.SU.equals(lastForwardState.getCompensationStatus())) {
        context.setVariable(DomainConstants.VAR_NAME_IS_FOR_SUB_STATMACHINE_FORWARD, true);
    }
    if (!ExecutionStatus.SU.equals(lastForwardState.getStatus())) {
        lastForwardState.setIgnoreStatus(true);
    }
    try {
        StateInstruction inst = new StateInstruction();
        inst.setTenantId(stateMachineInstance.getTenantId());
        inst.setStateMachineName(stateMachineInstance.getStateMachine().getName());
        if (skip || ExecutionStatus.SU.equals(lastForwardState.getStatus())) {
            String next = null;
            State state = stateMachineInstance.getStateMachine().getState(EngineUtils.getOriginStateName(lastForwardState));
            if (state != null && state instanceof AbstractTaskState) {
                next = ((AbstractTaskState) state).getNext();
            }
            if (StringUtils.isEmpty(next)) {
                LOGGER.warn("Last Forward execution StateInstance was succeed, and it has not Next State , skip forward " + "operation");
                return stateMachineInstance;
            }
            inst.setStateName(next);
        } else {
            if (ExecutionStatus.RU.equals(lastForwardState.getStatus()) && !EngineUtils.isTimeout(lastForwardState.getGmtStarted(), stateMachineConfig.getServiceInvokeTimeout())) {
                throw new EngineExecutionException("State [" + lastForwardState.getName() + "] is running, operation[forward] denied", FrameworkErrorCode.OperationDenied);
            }
            inst.setStateName(EngineUtils.getOriginStateName(lastForwardState));
        }
        context.setInstruction(inst);
        stateMachineInstance.setStatus(ExecutionStatus.RU);
        stateMachineInstance.setRunning(true);
        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("Operation [forward] started  stateMachineInstance[id:" + stateMachineInstance.getId() + "]");
        }
        if (stateMachineInstance.getStateMachine().isPersist()) {
            stateMachineConfig.getStateLogStore().recordStateMachineRestarted(stateMachineInstance, context);
        }
        loop = LoopTaskUtils.getLoopConfig(context, inst.getState(context));
        if (null != loop) {
            inst.setTemporaryState(new LoopStartStateImpl());
        }
        if (async) {
            stateMachineConfig.getAsyncProcessCtrlEventPublisher().publish(context);
        } else {
            stateMachineConfig.getProcessCtrlEventPublisher().publish(context);
        }
    } catch (EngineExecutionException e) {
        LOGGER.error("Operation [forward] failed", e);
        throw e;
    }
    return stateMachineInstance;
}
Also used : Loop(io.seata.saga.statelang.domain.TaskState.Loop) StateInstruction(io.seata.saga.engine.pcext.StateInstruction) ForwardInvalidException(io.seata.saga.engine.exception.ForwardInvalidException) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) ProcessContext(io.seata.saga.proctrl.ProcessContext) StateMachineInstance(io.seata.saga.statelang.domain.StateMachineInstance) AbstractTaskState(io.seata.saga.statelang.domain.impl.AbstractTaskState) LoopStartStateImpl(io.seata.saga.statelang.domain.impl.LoopStartStateImpl) ProcessContextBuilder(io.seata.saga.engine.utils.ProcessContextBuilder) ExecutionStatus(io.seata.saga.statelang.domain.ExecutionStatus) State(io.seata.saga.statelang.domain.State) AbstractTaskState(io.seata.saga.statelang.domain.impl.AbstractTaskState) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StateInstance(io.seata.saga.statelang.domain.StateInstance)

Example 7 with AbstractTaskState

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

the class TaskStateRouter method compensateRoute.

private Instruction compensateRoute(ProcessContext context, State compensationTriggerState) {
    // and the compensation process is interrupted.
    if (Boolean.TRUE.equals(context.getVariable(DomainConstants.VAR_NAME_FIRST_COMPENSATION_STATE_STARTED))) {
        Exception exception = (Exception) context.getVariable(DomainConstants.VAR_NAME_CURRENT_EXCEPTION);
        if (exception != null) {
            EngineUtils.endStateMachine(context);
            return null;
        }
        StateInstance stateInstance = (StateInstance) context.getVariable(DomainConstants.VAR_NAME_STATE_INST);
        if (stateInstance != null && (!ExecutionStatus.SU.equals(stateInstance.getStatus()))) {
            EngineUtils.endStateMachine(context);
            return null;
        }
    }
    Stack<StateInstance> stateStackToBeCompensated = CompensationHolder.getCurrent(context, true).getStateStackNeedCompensation();
    if (!stateStackToBeCompensated.isEmpty()) {
        StateInstance stateToBeCompensated = stateStackToBeCompensated.pop();
        StateMachine stateMachine = (StateMachine) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE);
        State state = stateMachine.getState(EngineUtils.getOriginStateName(stateToBeCompensated));
        if (state != null && state instanceof AbstractTaskState) {
            AbstractTaskState taskState = (AbstractTaskState) state;
            StateInstruction instruction = context.getInstruction(StateInstruction.class);
            State compensateState = null;
            String compensateStateName = taskState.getCompensateState();
            if (StringUtils.hasLength(compensateStateName)) {
                compensateState = stateMachine.getState(compensateStateName);
            }
            if (compensateState == null && (taskState instanceof SubStateMachine)) {
                compensateState = ((SubStateMachine) taskState).getCompensateStateObject();
                instruction.setTemporaryState(compensateState);
            }
            if (compensateState == null) {
                EngineUtils.endStateMachine(context);
                return null;
            }
            instruction.setStateName(compensateState.getName());
            CompensationHolder.getCurrent(context, true).addToBeCompensatedState(compensateState.getName(), stateToBeCompensated);
            ((HierarchicalProcessContext) context).setVariableLocally(DomainConstants.VAR_NAME_FIRST_COMPENSATION_STATE_STARTED, true);
            if (compensateState instanceof CompensateSubStateMachineState) {
                ((HierarchicalProcessContext) context).setVariableLocally(compensateState.getName() + DomainConstants.VAR_NAME_SUB_MACHINE_PARENT_ID, EngineUtils.generateParentId(stateToBeCompensated));
            }
            return instruction;
        }
    }
    context.removeVariable(DomainConstants.VAR_NAME_CURRENT_COMPEN_TRIGGER_STATE);
    String compensationTriggerStateNext = compensationTriggerState.getNext();
    if (StringUtils.isEmpty(compensationTriggerStateNext)) {
        EngineUtils.endStateMachine(context);
        return null;
    }
    StateInstruction instruction = context.getInstruction(StateInstruction.class);
    instruction.setStateName(compensationTriggerStateNext);
    return instruction;
}
Also used : AbstractTaskState(io.seata.saga.statelang.domain.impl.AbstractTaskState) StateInstruction(io.seata.saga.engine.pcext.StateInstruction) StateMachine(io.seata.saga.statelang.domain.StateMachine) SubStateMachine(io.seata.saga.statelang.domain.SubStateMachine) CompensateSubStateMachineState(io.seata.saga.statelang.domain.CompensateSubStateMachineState) State(io.seata.saga.statelang.domain.State) AbstractTaskState(io.seata.saga.statelang.domain.impl.AbstractTaskState) HierarchicalProcessContext(io.seata.saga.proctrl.HierarchicalProcessContext) CompensateSubStateMachineState(io.seata.saga.statelang.domain.CompensateSubStateMachineState) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) StateInstance(io.seata.saga.statelang.domain.StateInstance) SubStateMachine(io.seata.saga.statelang.domain.SubStateMachine)

Example 8 with AbstractTaskState

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

the class StateMachineParserImpl method parse.

@Override
public StateMachine parse(String json) {
    JsonParser jsonParser = JsonParserFactory.getJsonParser(jsonParserName);
    if (jsonParser == null) {
        throw new RuntimeException("Cannot find JsonParer by name: " + jsonParserName);
    }
    Map<String, Object> node = jsonParser.parse(json, Map.class, true);
    if (DesignerJsonTransformer.isDesignerJson(node)) {
        node = DesignerJsonTransformer.toStandardJson(node);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("===== Transformed standard state language:\n{}", jsonParser.toJsonString(node, true));
        }
    }
    StateMachineImpl stateMachine = new StateMachineImpl();
    stateMachine.setName((String) node.get("Name"));
    stateMachine.setComment((String) node.get("Comment"));
    stateMachine.setVersion((String) node.get("Version"));
    stateMachine.setStartState((String) node.get("StartState"));
    String recoverStrategy = (String) node.get("RecoverStrategy");
    if (StringUtils.isNotBlank(recoverStrategy)) {
        stateMachine.setRecoverStrategy(RecoverStrategy.valueOf(recoverStrategy));
    }
    Object isPersist = node.get("IsPersist");
    if (Boolean.FALSE.equals(isPersist)) {
        stateMachine.setPersist(false);
    }
    // customize if update origin or append new retryStateInstLog
    Object isRetryPersistModeUpdate = node.get("IsRetryPersistModeUpdate");
    if (isRetryPersistModeUpdate instanceof Boolean) {
        stateMachine.setRetryPersistModeUpdate(Boolean.TRUE.equals(isRetryPersistModeUpdate));
    }
    // customize if update last or append new compensateStateInstLog
    Object isCompensatePersistModeUpdate = node.get("IsCompensatePersistModeUpdate");
    if (isCompensatePersistModeUpdate instanceof Boolean) {
        stateMachine.setCompensatePersistModeUpdate(Boolean.TRUE.equals(isCompensatePersistModeUpdate));
    }
    Map<String, Object> statesNode = (Map<String, Object>) node.get("States");
    statesNode.forEach((stateName, value) -> {
        Map<String, Object> stateNode = (Map<String, Object>) value;
        String stateType = (String) stateNode.get("Type");
        StateParser<?> stateParser = StateParserFactory.getStateParser(stateType);
        if (stateParser == null) {
            throw new IllegalArgumentException("State Type [" + stateType + "] is not support");
        }
        State state = stateParser.parse(stateNode);
        if (state instanceof BaseState) {
            ((BaseState) state).setName(stateName);
        }
        if (stateMachine.getState(stateName) != null) {
            throw new IllegalArgumentException("State[name:" + stateName + "] is already exists");
        }
        stateMachine.putState(stateName, state);
    });
    Map<String, State> stateMap = stateMachine.getStates();
    for (State state : stateMap.values()) {
        if (state instanceof AbstractTaskState) {
            AbstractTaskState taskState = (AbstractTaskState) state;
            if (StringUtils.isNotBlank(taskState.getCompensateState())) {
                taskState.setForUpdate(true);
                State compState = stateMap.get(taskState.getCompensateState());
                if (compState instanceof AbstractTaskState) {
                    ((AbstractTaskState) compState).setForCompensation(true);
                }
            }
        }
    }
    return stateMachine;
}
Also used : BaseState(io.seata.saga.statelang.domain.impl.BaseState) StateMachineImpl(io.seata.saga.statelang.domain.impl.StateMachineImpl) AbstractTaskState(io.seata.saga.statelang.domain.impl.AbstractTaskState) State(io.seata.saga.statelang.domain.State) BaseState(io.seata.saga.statelang.domain.impl.BaseState) AbstractTaskState(io.seata.saga.statelang.domain.impl.AbstractTaskState) Map(java.util.Map) JsonParser(io.seata.saga.statelang.parser.JsonParser)

Aggregations

AbstractTaskState (io.seata.saga.statelang.domain.impl.AbstractTaskState)8 StateInstruction (io.seata.saga.engine.pcext.StateInstruction)4 State (io.seata.saga.statelang.domain.State)4 StateInstance (io.seata.saga.statelang.domain.StateInstance)4 EngineExecutionException (io.seata.saga.engine.exception.EngineExecutionException)3 HierarchicalProcessContext (io.seata.saga.proctrl.HierarchicalProcessContext)3 StateMachineInstance (io.seata.saga.statelang.domain.StateMachineInstance)3 Loop (io.seata.saga.statelang.domain.TaskState.Loop)3 Map (java.util.Map)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 StateMachine (io.seata.saga.statelang.domain.StateMachine)2 Collection (java.util.Collection)2 StateMachineConfig (io.seata.saga.engine.StateMachineConfig)1 ForwardInvalidException (io.seata.saga.engine.exception.ForwardInvalidException)1 ScriptTaskStateHandler (io.seata.saga.engine.pcext.handlers.ScriptTaskStateHandler)1 CompensationHolder (io.seata.saga.engine.pcext.utils.CompensationHolder)1 ProcessContextBuilder (io.seata.saga.engine.utils.ProcessContextBuilder)1 ProcessContext (io.seata.saga.proctrl.ProcessContext)1 CompensateSubStateMachineState (io.seata.saga.statelang.domain.CompensateSubStateMachineState)1 ExecutionStatus (io.seata.saga.statelang.domain.ExecutionStatus)1