Search in sources :

Example 11 with StateMachine

use of io.seata.saga.statelang.domain.StateMachine in project seata by seata.

the class StateMachineRepositoryImpl method registryByResources.

@Override
public void registryByResources(Resource[] resources, String tenantId) throws IOException {
    if (resources != null) {
        for (Resource resource : resources) {
            String json = IOUtils.toString(resource.getInputStream(), charset);
            StateMachine stateMachine = StateMachineParserFactory.getStateMachineParser(jsonParserName).parse(json);
            if (stateMachine != null) {
                stateMachine.setContent(json);
                if (StringUtils.isBlank(stateMachine.getTenantId())) {
                    stateMachine.setTenantId(tenantId);
                }
                registryStateMachine(stateMachine);
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("===== StateMachine Loaded: \n{}", json);
                }
            }
        }
    }
}
Also used : StateMachine(io.seata.saga.statelang.domain.StateMachine) Resource(org.springframework.core.io.Resource)

Example 12 with StateMachine

use of io.seata.saga.statelang.domain.StateMachine in project seata by seata.

the class DbAndReportTcStateLogStore method isUpdateMode.

private boolean isUpdateMode(StateInstance stateInstance, ProcessContext context) {
    DefaultStateMachineConfig stateMachineConfig = (DefaultStateMachineConfig) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONFIG);
    StateInstruction instruction = context.getInstruction(StateInstruction.class);
    ServiceTaskStateImpl state = (ServiceTaskStateImpl) instruction.getState(context);
    StateMachine stateMachine = stateInstance.getStateMachineInstance().getStateMachine();
    if (StringUtils.hasLength(stateInstance.getStateIdRetriedFor())) {
        if (null != state.isRetryPersistModeUpdate()) {
            return state.isRetryPersistModeUpdate();
        } else if (null != stateMachine.isRetryPersistModeUpdate()) {
            return stateMachine.isRetryPersistModeUpdate();
        }
        return stateMachineConfig.isSagaRetryPersistModeUpdate();
    } else if (StringUtils.hasLength(stateInstance.getStateIdCompensatedFor())) {
        // find if this compensate has been executed
        for (int i = 0; i < stateInstance.getStateMachineInstance().getStateList().size(); i++) {
            StateInstance aStateInstance = stateInstance.getStateMachineInstance().getStateList().get(i);
            if (aStateInstance.isForCompensation() && aStateInstance.getName().equals(stateInstance.getName())) {
                if (null != state.isCompensatePersistModeUpdate()) {
                    return state.isCompensatePersistModeUpdate();
                } else if (null != stateMachine.isCompensatePersistModeUpdate()) {
                    return stateMachine.isCompensatePersistModeUpdate();
                }
                return stateMachineConfig.isSagaCompensatePersistModeUpdate();
            }
        }
        return false;
    }
    return false;
}
Also used : StateInstruction(io.seata.saga.engine.pcext.StateInstruction) StateMachine(io.seata.saga.statelang.domain.StateMachine) DefaultStateMachineConfig(io.seata.saga.engine.impl.DefaultStateMachineConfig) ServiceTaskStateImpl(io.seata.saga.statelang.domain.impl.ServiceTaskStateImpl) StateInstance(io.seata.saga.statelang.domain.StateInstance)

Example 13 with StateMachine

use of io.seata.saga.statelang.domain.StateMachine in project seata by seata.

the class StateMachineProcessRouter method route.

@Override
public Instruction route(ProcessContext context) throws FrameworkException {
    StateInstruction stateInstruction = context.getInstruction(StateInstruction.class);
    State state;
    if (stateInstruction.getTemporaryState() != null) {
        state = stateInstruction.getTemporaryState();
        stateInstruction.setTemporaryState(null);
    } else {
        StateMachineConfig stateMachineConfig = (StateMachineConfig) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONFIG);
        StateMachine stateMachine = stateMachineConfig.getStateMachineRepository().getStateMachine(stateInstruction.getStateMachineName(), stateInstruction.getTenantId());
        state = stateMachine.getStates().get(stateInstruction.getStateName());
    }
    String stateType = state.getType();
    StateRouter router = stateRouters.get(stateType);
    Instruction instruction = null;
    List<StateRouterInterceptor> interceptors = null;
    if (router instanceof InterceptableStateRouter) {
        interceptors = ((InterceptableStateRouter) router).getInterceptors();
    }
    List<StateRouterInterceptor> executedInterceptors = null;
    Exception exception = null;
    try {
        if (CollectionUtils.isNotEmpty(interceptors)) {
            executedInterceptors = new ArrayList<>(interceptors.size());
            for (StateRouterInterceptor interceptor : interceptors) {
                executedInterceptors.add(interceptor);
                interceptor.preRoute(context, state);
            }
        }
        instruction = router.route(context, state);
    } catch (Exception e) {
        exception = e;
        throw e;
    } finally {
        if (CollectionUtils.isNotEmpty(executedInterceptors)) {
            for (int i = executedInterceptors.size() - 1; i >= 0; i--) {
                StateRouterInterceptor interceptor = executedInterceptors.get(i);
                interceptor.postRoute(context, state, instruction, exception);
            }
        }
        // if 'Succeed' or 'Fail' State did not configured, we must end the state machine
        if (instruction == null && !stateInstruction.isEnd()) {
            EngineUtils.endStateMachine(context);
        }
    }
    return instruction;
}
Also used : TaskStateRouter(io.seata.saga.engine.pcext.routers.TaskStateRouter) EndStateRouter(io.seata.saga.engine.pcext.routers.EndStateRouter) StateMachine(io.seata.saga.statelang.domain.StateMachine) Instruction(io.seata.saga.proctrl.Instruction) FrameworkException(io.seata.common.exception.FrameworkException) State(io.seata.saga.statelang.domain.State) StateMachineConfig(io.seata.saga.engine.StateMachineConfig)

Aggregations

StateMachine (io.seata.saga.statelang.domain.StateMachine)13 EngineExecutionException (io.seata.saga.engine.exception.EngineExecutionException)6 State (io.seata.saga.statelang.domain.State)5 StateInstance (io.seata.saga.statelang.domain.StateInstance)4 StateInstruction (io.seata.saga.engine.pcext.StateInstruction)3 AbstractTaskState (io.seata.saga.statelang.domain.impl.AbstractTaskState)3 Date (java.util.Date)3 StateMachineConfig (io.seata.saga.engine.StateMachineConfig)2 CompensateSubStateMachineState (io.seata.saga.statelang.domain.CompensateSubStateMachineState)2 StateMachineInstance (io.seata.saga.statelang.domain.StateMachineInstance)2 SubStateMachine (io.seata.saga.statelang.domain.SubStateMachine)2 FrameworkException (io.seata.common.exception.FrameworkException)1 DefaultStateMachineConfig (io.seata.saga.engine.impl.DefaultStateMachineConfig)1 EndStateRouter (io.seata.saga.engine.pcext.routers.EndStateRouter)1 TaskStateRouter (io.seata.saga.engine.pcext.routers.TaskStateRouter)1 HierarchicalProcessContext (io.seata.saga.proctrl.HierarchicalProcessContext)1 Instruction (io.seata.saga.proctrl.Instruction)1 LoopStartStateImpl (io.seata.saga.statelang.domain.impl.LoopStartStateImpl)1 ServiceTaskStateImpl (io.seata.saga.statelang.domain.impl.ServiceTaskStateImpl)1 StateMachineInstanceImpl (io.seata.saga.statelang.domain.impl.StateMachineInstanceImpl)1