Search in sources :

Example 1 with StateMachine

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

the class StateInstruction method getState.

public State getState(ProcessContext context) {
    if (getTemporaryState() != null) {
        return temporaryState;
    }
    String stateName = getStateName();
    String stateMachineName = getStateMachineName();
    String tenantId = getTenantId();
    if (StringUtils.isEmpty(stateMachineName)) {
        throw new EngineExecutionException("StateMachineName is required", FrameworkErrorCode.ParameterRequired);
    }
    StateMachineConfig stateMachineConfig = (StateMachineConfig) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_CONFIG);
    StateMachine stateMachine = stateMachineConfig.getStateMachineRepository().getStateMachine(stateMachineName, tenantId);
    if (stateMachine == null) {
        throw new EngineExecutionException("StateMachine[" + stateMachineName + "] is not exist", FrameworkErrorCode.ObjectNotExists);
    }
    if (StringUtils.isEmpty(stateName)) {
        stateName = stateMachine.getStartState();
        setStateName(stateName);
    }
    State state = stateMachine.getStates().get(stateName);
    if (state == null) {
        throw new EngineExecutionException("State[" + stateName + "] is not exist", FrameworkErrorCode.ObjectNotExists);
    }
    return state;
}
Also used : StateMachine(io.seata.saga.statelang.domain.StateMachine) State(io.seata.saga.statelang.domain.State) StateMachineConfig(io.seata.saga.engine.StateMachineConfig) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException)

Example 2 with StateMachine

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

the class ProcessCtrlStateMachineEngine method reloadStateMachineInstance.

/**
 * override state machine instance
 *
 * @param instId
 * @return
 */
@Override
public StateMachineInstance reloadStateMachineInstance(String instId) {
    StateMachineInstance inst = stateMachineConfig.getStateLogStore().getStateMachineInstance(instId);
    if (inst != null) {
        StateMachine stateMachine = inst.getStateMachine();
        if (stateMachine == null) {
            stateMachine = stateMachineConfig.getStateMachineRepository().getStateMachineById(inst.getMachineId());
            inst.setStateMachine(stateMachine);
        }
        if (stateMachine == null) {
            throw new EngineExecutionException("StateMachine[id:" + inst.getMachineId() + "] not exist.", FrameworkErrorCode.ObjectNotExists);
        }
        List<StateInstance> stateList = inst.getStateList();
        if (CollectionUtils.isEmpty(stateList)) {
            stateList = stateMachineConfig.getStateLogStore().queryStateInstanceListByMachineInstanceId(instId);
            if (CollectionUtils.isNotEmpty(stateList)) {
                for (StateInstance tmpStateInstance : stateList) {
                    inst.putStateInstance(tmpStateInstance.getId(), tmpStateInstance);
                }
            }
        }
        if (CollectionUtils.isEmpty(inst.getEndParams())) {
            inst.setEndParams(replayContextVariables(inst));
        }
    }
    return inst;
}
Also used : StateMachine(io.seata.saga.statelang.domain.StateMachine) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) StateMachineInstance(io.seata.saga.statelang.domain.StateMachineInstance) StateInstance(io.seata.saga.statelang.domain.StateInstance)

Example 3 with StateMachine

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

the class ProcessCtrlStateMachineEngine method createMachineInstance.

private StateMachineInstance createMachineInstance(String stateMachineName, String tenantId, String businessKey, Map<String, Object> startParams) {
    StateMachine stateMachine = stateMachineConfig.getStateMachineRepository().getStateMachine(stateMachineName, tenantId);
    if (stateMachine == null) {
        throw new EngineExecutionException("StateMachine[" + stateMachineName + "] is not exists", FrameworkErrorCode.ObjectNotExists);
    }
    StateMachineInstanceImpl inst = new StateMachineInstanceImpl();
    inst.setStateMachine(stateMachine);
    inst.setMachineId(stateMachine.getId());
    inst.setTenantId(tenantId);
    inst.setBusinessKey(businessKey);
    inst.setStartParams(startParams);
    if (startParams != null) {
        if (StringUtils.hasText(businessKey)) {
            startParams.put(DomainConstants.VAR_NAME_BUSINESSKEY, businessKey);
        }
        String parentId = (String) startParams.get(DomainConstants.VAR_NAME_PARENT_ID);
        if (StringUtils.hasText(parentId)) {
            inst.setParentId(parentId);
            startParams.remove(DomainConstants.VAR_NAME_PARENT_ID);
        }
    }
    inst.setStatus(ExecutionStatus.RU);
    inst.setRunning(true);
    inst.setGmtStarted(new Date());
    inst.setGmtUpdated(inst.getGmtStarted());
    return inst;
}
Also used : StateMachine(io.seata.saga.statelang.domain.StateMachine) StateMachineInstanceImpl(io.seata.saga.statelang.domain.impl.StateMachineInstanceImpl) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) Date(java.util.Date)

Example 4 with StateMachine

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

the class CompensationHolder method findStateInstListToBeCompensated.

public static List<StateInstance> findStateInstListToBeCompensated(ProcessContext context, List<StateInstance> stateInstanceList) {
    List<StateInstance> stateListToBeCompensated = null;
    if (CollectionUtils.isNotEmpty(stateInstanceList)) {
        stateListToBeCompensated = new ArrayList<>(stateInstanceList.size());
        StateMachine stateMachine = (StateMachine) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE);
        StateMachineInstance stateMachineInstance = (StateMachineInstance) context.getVariable(DomainConstants.VAR_NAME_STATEMACHINE_INST);
        for (StateInstance stateInstance : stateInstanceList) {
            if (stateNeedToCompensate(stateInstance)) {
                State state = stateMachine.getState(EngineUtils.getOriginStateName(stateInstance));
                AbstractTaskState taskState = null;
                if (state instanceof AbstractTaskState) {
                    taskState = (AbstractTaskState) state;
                }
                // The state machine needs to exit directly without compensation.
                if (stateInstance.isForUpdate() && taskState != null && StringUtils.isBlank(taskState.getCompensateState())) {
                    String message = "StateMachineInstance[" + stateMachineInstance.getId() + ":" + stateMachine.getName() + "] have a state [" + stateInstance.getName() + "] is a service for update data, but no compensateState found.";
                    EngineExecutionException exception = ExceptionUtils.createEngineExecutionException(FrameworkErrorCode.CompensationStateNotFound, message, stateMachineInstance, stateInstance);
                    EngineUtils.failStateMachine(context, exception);
                    throw exception;
                }
                if (taskState != null && StringUtils.isNotBlank(taskState.getCompensateState())) {
                    stateListToBeCompensated.add(stateInstance);
                }
            }
        }
    }
    return stateListToBeCompensated;
}
Also used : AbstractTaskState(io.seata.saga.statelang.domain.impl.AbstractTaskState) StateMachine(io.seata.saga.statelang.domain.StateMachine) State(io.seata.saga.statelang.domain.State) AbstractTaskState(io.seata.saga.statelang.domain.impl.AbstractTaskState) EngineExecutionException(io.seata.saga.engine.exception.EngineExecutionException) StateInstance(io.seata.saga.statelang.domain.StateInstance) StateMachineInstance(io.seata.saga.statelang.domain.StateMachineInstance)

Example 5 with StateMachine

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

the class StateMachineRepositoryImpl method getStateMachine.

@Override
public StateMachine getStateMachine(String stateMachineName, String tenantId) {
    Item item = CollectionUtils.computeIfAbsent(stateMachineMapByNameAndTenant, stateMachineName + "_" + tenantId, key -> new Item());
    if (item.getValue() == null && stateLangStore != null) {
        synchronized (item) {
            if (item.getValue() == null && stateLangStore != null) {
                StateMachine stateMachine = stateLangStore.getLastVersionStateMachine(stateMachineName, tenantId);
                if (stateMachine != null) {
                    StateMachine parsedStatMachine = StateMachineParserFactory.getStateMachineParser(jsonParserName).parse(stateMachine.getContent());
                    if (parsedStatMachine == null) {
                        throw new RuntimeException("Parse State Language failed, stateMachineId:" + stateMachine.getId() + ", name:" + stateMachine.getName());
                    }
                    stateMachine.setStartState(parsedStatMachine.getStartState());
                    stateMachine.getStates().putAll(parsedStatMachine.getStates());
                    item.setValue(stateMachine);
                    stateMachineMapById.put(stateMachine.getId(), item);
                }
            }
        }
    }
    return item.getValue();
}
Also used : StateMachine(io.seata.saga.statelang.domain.StateMachine)

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