Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize: add StateType Enum #6891

Open
wants to merge 9 commits into
base: 2.x
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@
*/
package org.apache.seata.common.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

import java.util.function.Function;

/**
Expand Down Expand Up @@ -271,11 +267,17 @@ public static Map<String, String> decodeMap(String data) {
* @return the value
*/
public static <K, V> V computeIfAbsent(Map<K, V> map, K key, Function<? super K, ? extends V> mappingFunction) {
V value = map.get(key);
if (value != null) {
return value;
V value;
if ((value = map.get(key)) == null) {
Objects.requireNonNull(mappingFunction);
V newValue;
if ((newValue = mappingFunction.apply(key)) != null) {
map.put(key, newValue);
funky-eyes marked this conversation as resolved.
Show resolved Hide resolved
return newValue;
}
}
return map.computeIfAbsent(key, mappingFunction);

return value;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,20 +214,8 @@ public void init() throws Exception {
}

public ProcessControllerImpl createProcessorController(ProcessCtrlEventPublisher eventPublisher) throws Exception {
StateMachineProcessRouter stateMachineProcessRouter = new StateMachineProcessRouter();
stateMachineProcessRouter.initDefaultStateRouters();
loadStateRouterInterceptors(stateMachineProcessRouter.getStateRouters());

StateMachineProcessHandler stateMachineProcessHandler = new StateMachineProcessHandler();
stateMachineProcessHandler.initDefaultHandlers();
loadStateHandlerInterceptors(stateMachineProcessHandler.getStateHandlers());

DefaultRouterHandler defaultRouterHandler = new DefaultRouterHandler();
defaultRouterHandler.setEventPublisher(eventPublisher);

Map<String, ProcessRouter> processRouterMap = new HashMap<>(1);
processRouterMap.put(ProcessType.STATE_LANG.getCode(), stateMachineProcessRouter);
defaultRouterHandler.setProcessRouters(processRouterMap);
StateMachineProcessHandler stateMachineProcessHandler = buildStateMachineProcessHandler();
DefaultRouterHandler defaultRouterHandler = buildDefaultRouterHandler(eventPublisher);

CustomizeBusinessProcessor customizeBusinessProcessor = new CustomizeBusinessProcessor();

Expand All @@ -245,6 +233,27 @@ public ProcessControllerImpl createProcessorController(ProcessCtrlEventPublisher
return processorController;
}

private StateMachineProcessHandler buildStateMachineProcessHandler() {
StateMachineProcessHandler stateMachineProcessHandler = new StateMachineProcessHandler();
stateMachineProcessHandler.initDefaultHandlers();
loadStateHandlerInterceptors(stateMachineProcessHandler.getStateHandlers());
return stateMachineProcessHandler;
}

private DefaultRouterHandler buildDefaultRouterHandler(ProcessCtrlEventPublisher eventPublisher) {
DefaultRouterHandler defaultRouterHandler = new DefaultRouterHandler();
defaultRouterHandler.setEventPublisher(eventPublisher);

StateMachineProcessRouter stateMachineProcessRouter = new StateMachineProcessRouter();
stateMachineProcessRouter.initDefaultStateRouters();
loadStateRouterInterceptors(stateMachineProcessRouter.getStateRouters());

Map<String, ProcessRouter> processRouterMap = new HashMap<>(1);
processRouterMap.put(ProcessType.STATE_LANG.getCode(), stateMachineProcessRouter);
defaultRouterHandler.setProcessRouters(processRouterMap);
return defaultRouterHandler;
}

public void loadStateHandlerInterceptors(Map<String, StateHandler> stateHandlerMap) {
for (StateHandler stateHandler : stateHandlerMap.values()) {
if (stateHandler instanceof InterceptableStateHandler) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.apache.seata.saga.proctrl.ProcessContext;
import org.apache.seata.saga.proctrl.ProcessType;
import org.apache.seata.saga.statelang.domain.DomainConstants;
import org.apache.seata.saga.statelang.domain.StateType;
import org.apache.seata.saga.statelang.domain.ExecutionStatus;
import org.apache.seata.saga.statelang.domain.State;
import org.apache.seata.saga.statelang.domain.StateInstance;
Expand Down Expand Up @@ -280,7 +281,7 @@ protected StateMachineInstance forwardInternal(String stateMachineInstId, Map<St

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
if (StateType.SUB_STATE_MACHINE.getValue().equals(lastForwardState.getType()) && !ExecutionStatus.SU
.equals(lastForwardState.getCompensationStatus())) {

context.setVariable(DomainConstants.VAR_NAME_IS_FOR_SUB_STATMACHINE_FORWARD, true);
Expand Down Expand Up @@ -418,7 +419,7 @@ public StateInstance findOutLastForwardStateInstance(List<StateInstance> stateIn
continue;
}

if (DomainConstants.STATE_TYPE_SUB_STATE_MACHINE.equals(stateInstance.getType())) {
if (StateType.SUB_STATE_MACHINE.getValue().equals(stateInstance.getType())) {

StateInstance finalState = stateInstance;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import org.apache.seata.saga.engine.pcext.handlers.SucceedEndStateHandler;
import org.apache.seata.saga.proctrl.ProcessContext;
import org.apache.seata.saga.proctrl.handler.ProcessHandler;
import org.apache.seata.saga.statelang.domain.DomainConstants;
import org.apache.seata.saga.statelang.domain.StateType;
import org.apache.seata.saga.statelang.domain.State;

/**
Expand Down Expand Up @@ -84,21 +84,20 @@ public void process(ProcessContext context) throws FrameworkException {
}

public void initDefaultHandlers() {
if (stateHandlers.isEmpty()) {
stateHandlers.put(DomainConstants.STATE_TYPE_SERVICE_TASK, new ServiceTaskStateHandler());

stateHandlers.put(DomainConstants.STATE_TYPE_SCRIPT_TASK, new ScriptTaskStateHandler());

stateHandlers.put(DomainConstants.STATE_TYPE_SUB_MACHINE_COMPENSATION, new ServiceTaskStateHandler());
if (!stateHandlers.isEmpty()) {
return;
}

stateHandlers.put(DomainConstants.STATE_TYPE_SUB_STATE_MACHINE, new SubStateMachineHandler());
stateHandlers.put(StateType.SERVICE_TASK.getValue(), new ServiceTaskStateHandler());
stateHandlers.put(StateType.SCRIPT_TASK.getValue(), new ScriptTaskStateHandler());
stateHandlers.put(StateType.SUB_MACHINE_COMPENSATION.getValue(), new ServiceTaskStateHandler());
stateHandlers.put(StateType.SUB_STATE_MACHINE.getValue(), new SubStateMachineHandler());
stateHandlers.put(StateType.CHOICE.getValue(), new ChoiceStateHandler());
stateHandlers.put(StateType.SUCCEED.getValue(), new SucceedEndStateHandler());
stateHandlers.put(StateType.FAIL.getValue(), new FailEndStateHandler());
stateHandlers.put(StateType.COMPENSATION_TRIGGER.getValue(), new CompensationTriggerStateHandler());
stateHandlers.put(StateType.LOOP_START.getValue(), new LoopStartStateHandler());

stateHandlers.put(DomainConstants.STATE_TYPE_CHOICE, new ChoiceStateHandler());
stateHandlers.put(DomainConstants.STATE_TYPE_SUCCEED, new SucceedEndStateHandler());
stateHandlers.put(DomainConstants.STATE_TYPE_FAIL, new FailEndStateHandler());
stateHandlers.put(DomainConstants.STATE_TYPE_COMPENSATION_TRIGGER, new CompensationTriggerStateHandler());
stateHandlers.put(DomainConstants.STATE_TYPE_LOOP_START, new LoopStartStateHandler());
}
}

public Map<String, StateHandler> getStateHandlers() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.seata.saga.statelang.domain.DomainConstants;
import org.apache.seata.saga.statelang.domain.State;
import org.apache.seata.saga.statelang.domain.StateMachine;
import org.apache.seata.saga.statelang.domain.StateType;

/**
* StateMachine ProcessRouter
Expand Down Expand Up @@ -105,19 +106,21 @@ public Instruction route(ProcessContext context) throws FrameworkException {
}

public void initDefaultStateRouters() {
if (this.stateRouters.isEmpty()) {
TaskStateRouter taskStateRouter = new TaskStateRouter();
this.stateRouters.put(DomainConstants.STATE_TYPE_SERVICE_TASK, taskStateRouter);
this.stateRouters.put(DomainConstants.STATE_TYPE_SCRIPT_TASK, taskStateRouter);
this.stateRouters.put(DomainConstants.STATE_TYPE_CHOICE, taskStateRouter);
this.stateRouters.put(DomainConstants.STATE_TYPE_COMPENSATION_TRIGGER, taskStateRouter);
this.stateRouters.put(DomainConstants.STATE_TYPE_SUB_STATE_MACHINE, taskStateRouter);
this.stateRouters.put(DomainConstants.STATE_TYPE_SUB_MACHINE_COMPENSATION, taskStateRouter);
this.stateRouters.put(DomainConstants.STATE_TYPE_LOOP_START, taskStateRouter);

this.stateRouters.put(DomainConstants.STATE_TYPE_SUCCEED, new EndStateRouter());
this.stateRouters.put(DomainConstants.STATE_TYPE_FAIL, new EndStateRouter());
if (!stateRouters.isEmpty()) {
return;
}

TaskStateRouter taskStateRouter = new TaskStateRouter();
stateRouters.put(StateType.SERVICE_TASK.getValue(), taskStateRouter);
stateRouters.put(StateType.SCRIPT_TASK.getValue(), taskStateRouter);
stateRouters.put(StateType.CHOICE.getValue(), taskStateRouter);
stateRouters.put(StateType.COMPENSATION_TRIGGER.getValue(), taskStateRouter);
stateRouters.put(StateType.SUB_STATE_MACHINE.getValue(), taskStateRouter);
stateRouters.put(StateType.SUB_MACHINE_COMPENSATION.getValue(), taskStateRouter);
stateRouters.put(StateType.LOOP_START.getValue(), taskStateRouter);

stateRouters.put(StateType.SUCCEED.getValue(), new EndStateRouter());
stateRouters.put(StateType.FAIL.getValue(), new EndStateRouter());
}

public Map<String, StateRouter> getStateRouters() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.seata.common.util.CollectionUtils;
import org.apache.seata.common.util.StringUtils;
import org.apache.seata.saga.engine.exception.EngineExecutionException;
import org.apache.seata.saga.statelang.domain.StateType;
import org.apache.seata.saga.engine.utils.ExceptionUtils;
import org.apache.seata.saga.proctrl.ProcessContext;
import org.apache.seata.saga.statelang.domain.DomainConstants;
Expand Down Expand Up @@ -125,13 +126,13 @@ private static boolean stateNeedToCompensate(StateInstance stateInstance) {
if (stateInstance.isIgnoreStatus()) {
return false;
}
if (DomainConstants.STATE_TYPE_SUB_STATE_MACHINE.equals(stateInstance.getType())) {
if (StateType.SUB_STATE_MACHINE.getValue().equals(stateInstance.getType())) {

return (!ExecutionStatus.FA.equals(stateInstance.getStatus())) && (!ExecutionStatus.SU.equals(
stateInstance.getCompensationStatus()));
} else {

return DomainConstants.STATE_TYPE_SERVICE_TASK.equals(stateInstance.getType()) && !stateInstance
return StateType.SERVICE_TASK.getValue().equals(stateInstance.getType()) && !stateInstance
.isForCompensation() && (!ExecutionStatus.FA.equals(stateInstance.getStatus())) && (!ExecutionStatus.SU
.equals(stateInstance.getCompensationStatus()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.seata.saga.engine.exception.ForwardInvalidException;
import org.apache.seata.saga.engine.expression.ExpressionResolver;
import org.apache.seata.saga.engine.pcext.StateInstruction;
import org.apache.seata.saga.statelang.domain.StateType;
import org.apache.seata.saga.proctrl.ProcessContext;
import org.apache.seata.saga.proctrl.impl.ProcessContextImpl;
import org.apache.seata.saga.statelang.domain.DomainConstants;
Expand Down Expand Up @@ -97,9 +98,9 @@ public static Loop getLoopConfig(ProcessContext context, State currentState) {
* @return the boolean
*/
public static boolean matchLoop(State state) {
return state != null && (DomainConstants.STATE_TYPE_SERVICE_TASK.equals(state.getType())
|| DomainConstants.STATE_TYPE_SCRIPT_TASK.equals(state.getType())
|| DomainConstants.STATE_TYPE_SUB_STATE_MACHINE.equals(state.getType()));
return state != null && (StateType.SERVICE_TASK.getValue().equals(state.getType())
|| StateType.SCRIPT_TASK.getValue().equals(state.getType())
|| StateType.SUB_STATE_MACHINE.getValue().equals(state.getType()));
}

/**
Expand Down Expand Up @@ -334,7 +335,7 @@ public static boolean isForSubStateMachineForward(ProcessContext context) {
StateInstance lastRetriedStateInstance = LoopTaskUtils.findOutLastRetriedStateInstance(
stateMachineInstance, LoopTaskUtils.generateLoopStateName(context, instruction.getStateName()));

if (null != lastRetriedStateInstance && DomainConstants.STATE_TYPE_SUB_STATE_MACHINE.equals(
if (null != lastRetriedStateInstance && StateType.SUB_STATE_MACHINE.getValue().equals(
lastRetriedStateInstance.getType()) && !ExecutionStatus.SU.equals(
lastRetriedStateInstance.getCompensationStatus())) {

Expand Down Expand Up @@ -381,7 +382,7 @@ public static String decideCurrentExceptionRoute(List<ProcessContext> subContext

// compensate must be execute
State state = stateMachine.getState(next);
if (DomainConstants.STATE_TYPE_COMPENSATION_TRIGGER.equals(state.getType())) {
if (StateType.COMPENSATION_TRIGGER.getValue().equals(state.getType())) {
route = next;
break;
} else if (null == route) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.seata.common.exception.FrameworkErrorCode;
import org.apache.seata.common.util.CollectionUtils;
import org.apache.seata.saga.engine.exception.EngineExecutionException;
import org.apache.seata.saga.statelang.domain.StateType;
import org.apache.seata.saga.engine.pcext.utils.CompensationHolder;
import org.apache.seata.saga.engine.strategy.StatusDecisionStrategy;
import org.apache.seata.saga.engine.utils.ExceptionUtils;
Expand Down Expand Up @@ -110,7 +111,7 @@ public static void setMachineStatusBasedOnStateListAndException(StateMachineInst
stateMachineInstance.setStatus(ExecutionStatus.UN);
hasSetStatus = true;
} else if (ExecutionStatus.SU.equals(stateInstance.getStatus())) {
if (DomainConstants.STATE_TYPE_SERVICE_TASK.equals(stateInstance.getType())) {
if (StateType.SERVICE_TASK.getValue().equals(stateInstance.getType())) {
if (stateInstance.isForUpdate() && !stateInstance.isForCompensation()) {
hasSuccessUpdateService = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,6 @@
*
*/
public interface DomainConstants {

//region State Types
String STATE_TYPE_SERVICE_TASK = "ServiceTask";
String STATE_TYPE_CHOICE = "Choice";
String STATE_TYPE_FAIL = "Fail";
String STATE_TYPE_SUCCEED = "Succeed";
String STATE_TYPE_COMPENSATION_TRIGGER = "CompensationTrigger";
String STATE_TYPE_SUB_STATE_MACHINE = "SubStateMachine";
String STATE_TYPE_SUB_MACHINE_COMPENSATION = "CompensateSubMachine";
String STATE_TYPE_SCRIPT_TASK = "ScriptTask";
String STATE_TYPE_LOOP_START = "LoopStart";
//endregion

String COMPENSATE_SUB_MACHINE_STATE_NAME_PREFIX = "_compensate_sub_machine_state_";

//region Service Types
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.apache.seata.saga.statelang.domain;

/**
* StateType
*/
public enum StateType {

/**
* ServiceTask State
*/
SERVICE_TASK("ServiceTask"),

/**
* Choice State
*/
CHOICE("Choice"),

/**
* Fail State
*/
FAIL("Fail"),

/**
* Succeed State
*/
SUCCEED("Succeed"),

/**
* CompensationTrigger State
*/
COMPENSATION_TRIGGER("CompensationTrigger"),

/**
* SubStateMachine State
*/
SUB_STATE_MACHINE("SubStateMachine"),

/**
* CompensateSubMachine State
*/
SUB_MACHINE_COMPENSATION("CompensateSubMachine"),

/**
* ScriptTask State
*/
SCRIPT_TASK("ScriptTask"),

/**
* LoopStart State
*/
LOOP_START("LoopStart");


private String value;

StateType(String value) {
this.value = value;
}

public String getValue() {
return value;
}

public static StateType getStateType(String value) {
for (StateType stateType : values()) {
if (stateType.getValue().equalsIgnoreCase(value)) {
return stateType;
}
}

throw new IllegalArgumentException("Unknown StateType[" + value + "]");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.Map;

import org.apache.seata.saga.statelang.domain.ChoiceState;
import org.apache.seata.saga.statelang.domain.DomainConstants;
import org.apache.seata.saga.statelang.domain.StateType;

/**
* Single selection status
Expand All @@ -36,7 +36,7 @@ public class ChoiceStateImpl extends BaseState implements ChoiceState {
private Map<Object, String> choiceEvaluators;

public ChoiceStateImpl() {
setType(DomainConstants.STATE_TYPE_CHOICE);
setType(StateType.CHOICE.getValue());
}

@Override
Expand Down
Loading