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;
}
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;
}
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;
}
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;
}
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();
}
Aggregations