From 6a4bced87256f3ba5078e235fe822490ec7346c2 Mon Sep 17 00:00:00 2001 From: MaoMaoandSnail <1969945265@qq.com> Date: Sat, 28 Sep 2024 22:54:19 +0800 Subject: [PATCH 1/7] optimize: computeIfAbsent get twice --- .../seata/common/util/CollectionUtils.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/common/src/main/java/org/apache/seata/common/util/CollectionUtils.java b/common/src/main/java/org/apache/seata/common/util/CollectionUtils.java index 244b490006e..c0325c4a4ac 100644 --- a/common/src/main/java/org/apache/seata/common/util/CollectionUtils.java +++ b/common/src/main/java/org/apache/seata/common/util/CollectionUtils.java @@ -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.*; import java.util.function.Function; /** @@ -271,11 +267,17 @@ public static Map decodeMap(String data) { * @return the value */ public static V computeIfAbsent(Map map, K key, Function 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); + return newValue; + } } - return map.computeIfAbsent(key, mappingFunction); + + return value; } /** From 57526debc8bec713dc70b3182c43326d932e6904 Mon Sep 17 00:00:00 2001 From: MaoMaoandSnail <1969945265@qq.com> Date: Sat, 28 Sep 2024 23:52:23 +0800 Subject: [PATCH 2/7] optimize: createProcessorController code order --- .../config/AbstractStateMachineConfig.java | 37 ++++++++++++------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/config/AbstractStateMachineConfig.java b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/config/AbstractStateMachineConfig.java index f61c3777554..e00b3861b41 100644 --- a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/config/AbstractStateMachineConfig.java +++ b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/config/AbstractStateMachineConfig.java @@ -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 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(); @@ -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 processRouterMap = new HashMap<>(1); + processRouterMap.put(ProcessType.STATE_LANG.getCode(), stateMachineProcessRouter); + defaultRouterHandler.setProcessRouters(processRouterMap); + return defaultRouterHandler; + } + public void loadStateHandlerInterceptors(Map stateHandlerMap) { for (StateHandler stateHandler : stateHandlerMap.values()) { if (stateHandler instanceof InterceptableStateHandler) { From 631bfd11a4cc30f311ee6f46315719bad568bf26 Mon Sep 17 00:00:00 2001 From: MaoMaoandSnail <1969945265@qq.com> Date: Sun, 29 Sep 2024 00:05:05 +0800 Subject: [PATCH 3/7] optimize: add StateType Enum --- .../impl/ProcessCtrlStateMachineEngine.java | 5 +- .../pcext/StateMachineProcessHandler.java | 27 ++++--- .../pcext/StateMachineProcessRouter.java | 27 ++++--- .../pcext/utils/CompensationHolder.java | 5 +- .../engine/pcext/utils/LoopTaskUtils.java | 11 +-- .../impl/DefaultStatusDecisionStrategy.java | 3 +- .../statelang/domain/DomainConstants.java | 13 ---- .../saga/statelang/domain/StateType.java | 74 +++++++++++++++++++ .../domain/impl/ChoiceStateImpl.java | 4 +- .../CompensateSubStateMachineStateImpl.java | 4 +- .../impl/CompensationTriggerStateImpl.java | 4 +- .../domain/impl/FailEndStateImpl.java | 4 +- .../domain/impl/LoopStartStateImpl.java | 4 +- .../domain/impl/ScriptTaskStateImpl.java | 4 +- .../domain/impl/ServiceTaskStateImpl.java | 4 +- .../domain/impl/SubStateMachineImpl.java | 4 +- .../domain/impl/SucceedEndStateImpl.java | 4 +- .../statelang/parser/StateParserFactory.java | 18 ++--- .../parser/utils/StateMachineUtils.java | 15 ++-- .../impl/NoRecursiveSubStateMachineRule.java | 4 +- 20 files changed, 153 insertions(+), 85 deletions(-) create mode 100644 saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/StateType.java diff --git a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/impl/ProcessCtrlStateMachineEngine.java b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/impl/ProcessCtrlStateMachineEngine.java index 9d05f5e2749..a4bd6b5fd75 100644 --- a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/impl/ProcessCtrlStateMachineEngine.java +++ b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/impl/ProcessCtrlStateMachineEngine.java @@ -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; @@ -280,7 +281,7 @@ protected StateMachineInstance forwardInternal(String stateMachineInstId, Map stateIn continue; } - if (DomainConstants.STATE_TYPE_SUB_STATE_MACHINE.equals(stateInstance.getType())) { + if (StateType.SUB_STATE_MACHINE.getValue().equals(stateInstance.getType())) { StateInstance finalState = stateInstance; diff --git a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/StateMachineProcessHandler.java b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/StateMachineProcessHandler.java index 30cdbf22c9d..aeb5096376e 100644 --- a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/StateMachineProcessHandler.java +++ b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/StateMachineProcessHandler.java @@ -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; /** @@ -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 getStateHandlers() { diff --git a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/StateMachineProcessRouter.java b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/StateMachineProcessRouter.java index e3a4569fa9e..fdde747de14 100644 --- a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/StateMachineProcessRouter.java +++ b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/StateMachineProcessRouter.java @@ -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 @@ -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 getStateRouters() { diff --git a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/CompensationHolder.java b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/CompensationHolder.java index d7d19d42a4f..080f04407a8 100644 --- a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/CompensationHolder.java +++ b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/CompensationHolder.java @@ -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; @@ -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())); } diff --git a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/LoopTaskUtils.java b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/LoopTaskUtils.java index e71818f1f1a..434446f7a96 100644 --- a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/LoopTaskUtils.java +++ b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/LoopTaskUtils.java @@ -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; @@ -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())); } /** @@ -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())) { @@ -381,7 +382,7 @@ public static String decideCurrentExceptionRoute(List 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) { diff --git a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/strategy/impl/DefaultStatusDecisionStrategy.java b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/strategy/impl/DefaultStatusDecisionStrategy.java index 54b75a3815c..875c3e8f603 100644 --- a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/strategy/impl/DefaultStatusDecisionStrategy.java +++ b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/strategy/impl/DefaultStatusDecisionStrategy.java @@ -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; @@ -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; } diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/DomainConstants.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/DomainConstants.java index 09ce9cbb017..50267e00e45 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/DomainConstants.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/DomainConstants.java @@ -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 diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/StateType.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/StateType.java new file mode 100644 index 00000000000..e9270abb343 --- /dev/null +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/StateType.java @@ -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 + "]"); + } + +} diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ChoiceStateImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ChoiceStateImpl.java index c20b6be2935..45ecea20062 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ChoiceStateImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ChoiceStateImpl.java @@ -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 @@ -36,7 +36,7 @@ public class ChoiceStateImpl extends BaseState implements ChoiceState { private Map choiceEvaluators; public ChoiceStateImpl() { - setType(DomainConstants.STATE_TYPE_CHOICE); + setType(StateType.CHOICE.getValue()); } @Override diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/CompensateSubStateMachineStateImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/CompensateSubStateMachineStateImpl.java index c68fe4e47b2..4ffb7ec0a03 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/CompensateSubStateMachineStateImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/CompensateSubStateMachineStateImpl.java @@ -17,7 +17,7 @@ package org.apache.seata.saga.statelang.domain.impl; import org.apache.seata.saga.statelang.domain.CompensateSubStateMachineState; -import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; /** * Used to compensate the state of the sub state machine, inherited from ServiceTaskState @@ -25,6 +25,6 @@ */ public class CompensateSubStateMachineStateImpl extends ServiceTaskStateImpl implements CompensateSubStateMachineState { public CompensateSubStateMachineStateImpl() { - setType(DomainConstants.STATE_TYPE_SUB_MACHINE_COMPENSATION); + setType(StateType.SUB_MACHINE_COMPENSATION.getValue()); } } diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/CompensationTriggerStateImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/CompensationTriggerStateImpl.java index 8e638e48905..543284b7179 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/CompensationTriggerStateImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/CompensationTriggerStateImpl.java @@ -17,7 +17,7 @@ package org.apache.seata.saga.statelang.domain.impl; import org.apache.seata.saga.statelang.domain.CompensationTriggerState; -import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; /** * Triggering the "compensation" process for the state machine @@ -26,6 +26,6 @@ public class CompensationTriggerStateImpl extends BaseState implements CompensationTriggerState { public CompensationTriggerStateImpl() { - setType(DomainConstants.STATE_TYPE_COMPENSATION_TRIGGER); + setType(StateType.COMPENSATION_TRIGGER.getValue()); } } diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/FailEndStateImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/FailEndStateImpl.java index 4893c4daadd..be9e6d6eaae 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/FailEndStateImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/FailEndStateImpl.java @@ -16,7 +16,7 @@ */ package org.apache.seata.saga.statelang.domain.impl; -import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; import org.apache.seata.saga.statelang.domain.FailEndState; /** @@ -29,7 +29,7 @@ public class FailEndStateImpl extends BaseState implements FailEndState { private String message; public FailEndStateImpl() { - setType(DomainConstants.STATE_TYPE_FAIL); + setType(StateType.FAIL.getValue()); } @Override diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/LoopStartStateImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/LoopStartStateImpl.java index 433ce91a2c1..5ace05f1681 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/LoopStartStateImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/LoopStartStateImpl.java @@ -16,7 +16,7 @@ */ package org.apache.seata.saga.statelang.domain.impl; -import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; import org.apache.seata.saga.statelang.domain.LoopStartState; /** @@ -26,7 +26,7 @@ public class LoopStartStateImpl extends BaseState implements LoopStartState { public LoopStartStateImpl() { - setType(DomainConstants.STATE_TYPE_LOOP_START); + setType(StateType.LOOP_START.getValue()); } } diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ScriptTaskStateImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ScriptTaskStateImpl.java index ed291b06b00..afd75beb53f 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ScriptTaskStateImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ScriptTaskStateImpl.java @@ -16,7 +16,7 @@ */ package org.apache.seata.saga.statelang.domain.impl; -import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; import org.apache.seata.saga.statelang.domain.ScriptTaskState; /** @@ -32,7 +32,7 @@ public class ScriptTaskStateImpl extends AbstractTaskState implements ScriptTask private String scriptContent; public ScriptTaskStateImpl() { - setType(DomainConstants.STATE_TYPE_SCRIPT_TASK); + setType(StateType.SCRIPT_TASK.getValue()); } @Override diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ServiceTaskStateImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ServiceTaskStateImpl.java index fdb1a075e36..a2afbaa4097 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ServiceTaskStateImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ServiceTaskStateImpl.java @@ -20,7 +20,7 @@ import java.util.List; import java.util.Map; -import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; import org.apache.seata.saga.statelang.domain.ServiceTaskState; /** @@ -38,7 +38,7 @@ public class ServiceTaskStateImpl extends AbstractTaskState implements ServiceTa private boolean isAsync; public ServiceTaskStateImpl() { - setType(DomainConstants.STATE_TYPE_SERVICE_TASK); + setType(StateType.SERVICE_TASK.getValue()); } @Override diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/SubStateMachineImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/SubStateMachineImpl.java index bf6d1e78b1c..0ac020fa7d6 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/SubStateMachineImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/SubStateMachineImpl.java @@ -16,7 +16,7 @@ */ package org.apache.seata.saga.statelang.domain.impl; -import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; import org.apache.seata.saga.statelang.domain.SubStateMachine; import org.apache.seata.saga.statelang.domain.TaskState; @@ -31,7 +31,7 @@ public class SubStateMachineImpl extends ServiceTaskStateImpl implements SubStat private TaskState compensateStateObject; public SubStateMachineImpl() { - setType(DomainConstants.STATE_TYPE_SUB_STATE_MACHINE); + setType(StateType.SUB_STATE_MACHINE.getValue()); } @Override diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/SucceedEndStateImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/SucceedEndStateImpl.java index eb5ab712d37..564a8e2788f 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/SucceedEndStateImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/SucceedEndStateImpl.java @@ -16,7 +16,7 @@ */ package org.apache.seata.saga.statelang.domain.impl; -import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; import org.apache.seata.saga.statelang.domain.SucceedEndState; /** @@ -26,6 +26,6 @@ public class SucceedEndStateImpl extends BaseState implements SucceedEndState { public SucceedEndStateImpl() { - setType(DomainConstants.STATE_TYPE_SUCCEED); + setType(StateType.SUCCEED.getValue()); } } diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/StateParserFactory.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/StateParserFactory.java index 3ef0d1e7a86..4ecbc15f537 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/StateParserFactory.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/StateParserFactory.java @@ -19,7 +19,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; import org.apache.seata.saga.statelang.parser.impl.ChoiceStateParser; import org.apache.seata.saga.statelang.parser.impl.CompensateSubStateMachineStateParser; import org.apache.seata.saga.statelang.parser.impl.CompensationTriggerStateParser; @@ -38,15 +38,15 @@ public class StateParserFactory { protected static Map stateParserMap = new ConcurrentHashMap<>(); static { - stateParserMap.put(DomainConstants.STATE_TYPE_SERVICE_TASK, new ServiceTaskStateParser()); - stateParserMap.put(DomainConstants.STATE_TYPE_CHOICE, new ChoiceStateParser()); - stateParserMap.put(DomainConstants.STATE_TYPE_COMPENSATION_TRIGGER, new CompensationTriggerStateParser()); - stateParserMap.put(DomainConstants.STATE_TYPE_FAIL, new FailEndStateParser()); - stateParserMap.put(DomainConstants.STATE_TYPE_SUCCEED, new SucceedEndStateParser()); - stateParserMap.put(DomainConstants.STATE_TYPE_SUB_STATE_MACHINE, new SubStateMachineParser()); - stateParserMap.put(DomainConstants.STATE_TYPE_SUB_MACHINE_COMPENSATION, + stateParserMap.put(StateType.SERVICE_TASK.getValue(), new ServiceTaskStateParser()); + stateParserMap.put(StateType.CHOICE.getValue(), new ChoiceStateParser()); + stateParserMap.put(StateType.COMPENSATION_TRIGGER.getValue(), new CompensationTriggerStateParser()); + stateParserMap.put(StateType.FAIL.getValue(), new FailEndStateParser()); + stateParserMap.put(StateType.SUCCEED.getValue(), new SucceedEndStateParser()); + stateParserMap.put(StateType.SUB_STATE_MACHINE.getValue(), new SubStateMachineParser()); + stateParserMap.put(StateType.SUB_MACHINE_COMPENSATION.getValue(), new CompensateSubStateMachineStateParser()); - stateParserMap.put(DomainConstants.STATE_TYPE_SCRIPT_TASK, new ScriptTaskStateParser()); + stateParserMap.put(StateType.SCRIPT_TASK.getValue(), new ScriptTaskStateParser()); } public static StateParser getStateParser(String stateType) { diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/utils/StateMachineUtils.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/utils/StateMachineUtils.java index eb4e89f25b2..be15f66bb23 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/utils/StateMachineUtils.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/utils/StateMachineUtils.java @@ -18,7 +18,7 @@ import org.apache.seata.common.util.StringUtils; import org.apache.seata.saga.statelang.domain.ChoiceState; -import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; import org.apache.seata.saga.statelang.domain.State; import org.apache.seata.saga.statelang.domain.TaskState; @@ -36,17 +36,18 @@ public static Set getAllPossibleSubsequentStates(State state) { Set subsequentStates = new HashSet<>(); // Next state subsequentStates.add(state.getNext()); - switch (state.getType()) { - case DomainConstants.STATE_TYPE_SCRIPT_TASK: - case DomainConstants.STATE_TYPE_SERVICE_TASK: - case DomainConstants.STATE_TYPE_SUB_STATE_MACHINE: - case DomainConstants.STATE_TYPE_SUB_MACHINE_COMPENSATION: + StateType stateType = StateType.getStateType(state.getType()); + switch (stateType) { + case SCRIPT_TASK: + case SERVICE_TASK: + case SUB_STATE_MACHINE: + case SUB_MACHINE_COMPENSATION: // Next state in catches Optional.ofNullable(((TaskState) state).getCatches()) .ifPresent(c -> c.forEach(e -> subsequentStates.add(e.getNext()))); break; - case DomainConstants.STATE_TYPE_CHOICE: + case CHOICE: // Choice state Optional.ofNullable(((ChoiceState) state).getChoices()) .ifPresent(c -> c.forEach(e -> subsequentStates.add(e.getNext()))); diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/validator/impl/NoRecursiveSubStateMachineRule.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/validator/impl/NoRecursiveSubStateMachineRule.java index e8266f7d453..d02694f0ca6 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/validator/impl/NoRecursiveSubStateMachineRule.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/validator/impl/NoRecursiveSubStateMachineRule.java @@ -16,7 +16,7 @@ */ package org.apache.seata.saga.statelang.validator.impl; -import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; import org.apache.seata.saga.statelang.domain.State; import org.apache.seata.saga.statelang.domain.StateMachine; import org.apache.seata.saga.statelang.domain.SubStateMachine; @@ -30,7 +30,7 @@ public class NoRecursiveSubStateMachineRule extends AbstractRule { @Override public boolean validate(StateMachine stateMachine) { for (State state: stateMachine.getStates().values()) { - if (!DomainConstants.STATE_TYPE_SUB_STATE_MACHINE.equals(state.getType())) { + if (!StateType.SUB_STATE_MACHINE.getValue().equals(state.getType())) { continue; } if (stateMachine.getName().equals(((SubStateMachine) state).getStateMachineName())) { From c007bc43ac079b7bfea6e4144e68827dce859100 Mon Sep 17 00:00:00 2001 From: MaoMaoandSnail <1969945265@qq.com> Date: Sun, 29 Sep 2024 14:39:52 +0800 Subject: [PATCH 4/7] optimize: Rollback CollectionUtils.computeIfAbsent commit --- .../apache/seata/common/util/CollectionUtils.java | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/common/src/main/java/org/apache/seata/common/util/CollectionUtils.java b/common/src/main/java/org/apache/seata/common/util/CollectionUtils.java index c0325c4a4ac..b6846512301 100644 --- a/common/src/main/java/org/apache/seata/common/util/CollectionUtils.java +++ b/common/src/main/java/org/apache/seata/common/util/CollectionUtils.java @@ -267,17 +267,11 @@ public static Map decodeMap(String data) { * @return the value */ public static V computeIfAbsent(Map map, K key, Function mappingFunction) { - V value; - if ((value = map.get(key)) == null) { - Objects.requireNonNull(mappingFunction); - V newValue; - if ((newValue = mappingFunction.apply(key)) != null) { - map.put(key, newValue); - return newValue; - } + V value = map.get(key); + if (value != null) { + return value; } - - return value; + return map.computeIfAbsent(key, mappingFunction); } /** From a614b730b05e655aebeef1d1377f7a6530538a79 Mon Sep 17 00:00:00 2001 From: MaoMaoandSnail <1969945265@qq.com> Date: Sun, 29 Sep 2024 17:51:16 +0800 Subject: [PATCH 5/7] optimize: add licenses --- .../seata/saga/statelang/domain/StateType.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/StateType.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/StateType.java index e9270abb343..38cd69e9881 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/StateType.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/StateType.java @@ -1,7 +1,24 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.apache.seata.saga.statelang.domain; /** * StateType + * */ public enum StateType { From 26fe493ae4730ab080780226e4b7b0e3ec67acf1 Mon Sep 17 00:00:00 2001 From: MaoMaoandSnail <1969945265@qq.com> Date: Tue, 8 Oct 2024 16:29:04 +0800 Subject: [PATCH 6/7] optimize: clearly specify the imported classes --- .../java/org/apache/seata/common/util/CollectionUtils.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/org/apache/seata/common/util/CollectionUtils.java b/common/src/main/java/org/apache/seata/common/util/CollectionUtils.java index b6846512301..244b490006e 100644 --- a/common/src/main/java/org/apache/seata/common/util/CollectionUtils.java +++ b/common/src/main/java/org/apache/seata/common/util/CollectionUtils.java @@ -16,7 +16,11 @@ */ package org.apache.seata.common.util; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.function.Function; /** From c10a67dae6b44181cd36d57afaf2d1634b3b2c1a Mon Sep 17 00:00:00 2001 From: MaoMaoandSnail <1969945265@qq.com> Date: Fri, 15 Nov 2024 19:09:32 +0800 Subject: [PATCH 7/7] optimize: delete many getValues --- .../io/seata/saga/statelang/domain/State.java | 2 +- .../saga/statelang/domain/StateInstance.java | 4 +- .../saga/statelang/domain/StateType.java | 145 ++++++++++++++++++ .../saga/statelang/domain/impl/StateImpl.java | 5 +- .../domain/impl/StateInstanceImpl.java | 13 +- .../config/AbstractStateMachineConfig.java | 7 +- .../impl/ProcessCtrlStateMachineEngine.java | 4 +- .../pcext/StateMachineProcessHandler.java | 26 ++-- .../pcext/StateMachineProcessRouter.java | 28 ++-- .../pcext/utils/CompensationHolder.java | 4 +- .../engine/pcext/utils/LoopTaskUtils.java | 8 +- .../impl/DefaultStatusDecisionStrategy.java | 2 +- .../store/db/DbAndReportTcStateLogStore.java | 10 +- .../seata/saga/statelang/domain/State.java | 2 +- .../saga/statelang/domain/StateInstance.java | 4 +- .../saga/statelang/domain/impl/BaseState.java | 7 +- .../domain/impl/ChoiceStateImpl.java | 2 +- .../CompensateSubStateMachineStateImpl.java | 2 +- .../impl/CompensationTriggerStateImpl.java | 2 +- .../domain/impl/FailEndStateImpl.java | 2 +- .../domain/impl/LoopStartStateImpl.java | 2 +- .../domain/impl/ScriptTaskStateImpl.java | 2 +- .../domain/impl/ServiceTaskStateImpl.java | 2 +- .../domain/impl/StateInstanceImpl.java | 7 +- .../domain/impl/SubStateMachineImpl.java | 2 +- .../domain/impl/SucceedEndStateImpl.java | 2 +- .../statelang/parser/StateParserFactory.java | 20 +-- .../parser/impl/StateMachineParserImpl.java | 3 +- .../parser/utils/StateMachineUtils.java | 2 +- .../impl/NoRecursiveSubStateMachineRule.java | 2 +- 30 files changed, 237 insertions(+), 86 deletions(-) create mode 100644 compatible/src/main/java/io/seata/saga/statelang/domain/StateType.java diff --git a/compatible/src/main/java/io/seata/saga/statelang/domain/State.java b/compatible/src/main/java/io/seata/saga/statelang/domain/State.java index 348445389a5..ff1a7b5e960 100644 --- a/compatible/src/main/java/io/seata/saga/statelang/domain/State.java +++ b/compatible/src/main/java/io/seata/saga/statelang/domain/State.java @@ -43,7 +43,7 @@ public interface State { * * @return the state type */ - String getType(); + StateType getType(); /** * next state name diff --git a/compatible/src/main/java/io/seata/saga/statelang/domain/StateInstance.java b/compatible/src/main/java/io/seata/saga/statelang/domain/StateInstance.java index ad11bdf7877..cf8f8099b42 100644 --- a/compatible/src/main/java/io/seata/saga/statelang/domain/StateInstance.java +++ b/compatible/src/main/java/io/seata/saga/statelang/domain/StateInstance.java @@ -72,14 +72,14 @@ public interface StateInstance { * * @return state instance type */ - String getType(); + StateType getType(); /** * set type * * @param type state instance type */ - void setType(String type); + void setType(StateType type); /** * get service name diff --git a/compatible/src/main/java/io/seata/saga/statelang/domain/StateType.java b/compatible/src/main/java/io/seata/saga/statelang/domain/StateType.java new file mode 100644 index 00000000000..22a9856efb4 --- /dev/null +++ b/compatible/src/main/java/io/seata/saga/statelang/domain/StateType.java @@ -0,0 +1,145 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.seata.saga.statelang.domain; + +/** + * StateType + */ +@Deprecated +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 + "]"); + } + + + public static StateType wrap(org.apache.seata.saga.statelang.domain.StateType target) { + if (target == null) { + return null; + } + switch (target) { + case SERVICE_TASK: + return SERVICE_TASK; + case CHOICE: + return CHOICE; + case FAIL: + return FAIL; + case SUCCEED: + return SUCCEED; + case COMPENSATION_TRIGGER: + return COMPENSATION_TRIGGER; + case SUB_STATE_MACHINE: + return SUB_STATE_MACHINE; + case SUB_MACHINE_COMPENSATION: + return SUB_MACHINE_COMPENSATION; + case SCRIPT_TASK: + return SCRIPT_TASK; + case LOOP_START: + return LOOP_START; + default: + throw new IllegalArgumentException("Cannot convert " + target.name()); + } + } + + public org.apache.seata.saga.statelang.domain.StateType unwrap() { + switch (this) { + case SERVICE_TASK: + return org.apache.seata.saga.statelang.domain.StateType.SERVICE_TASK; + case CHOICE: + return org.apache.seata.saga.statelang.domain.StateType.CHOICE; + case FAIL: + return org.apache.seata.saga.statelang.domain.StateType.FAIL; + case SUCCEED: + return org.apache.seata.saga.statelang.domain.StateType.SUCCEED; + case COMPENSATION_TRIGGER: + return org.apache.seata.saga.statelang.domain.StateType.COMPENSATION_TRIGGER; + case SUB_STATE_MACHINE: + return org.apache.seata.saga.statelang.domain.StateType.SUB_STATE_MACHINE; + case SUB_MACHINE_COMPENSATION: + return org.apache.seata.saga.statelang.domain.StateType.SUB_MACHINE_COMPENSATION; + case SCRIPT_TASK: + return org.apache.seata.saga.statelang.domain.StateType.SCRIPT_TASK; + case LOOP_START: + return org.apache.seata.saga.statelang.domain.StateType.LOOP_START; + default: + throw new IllegalArgumentException("Cannot convert " + this.name()); + } + } + +} diff --git a/compatible/src/main/java/io/seata/saga/statelang/domain/impl/StateImpl.java b/compatible/src/main/java/io/seata/saga/statelang/domain/impl/StateImpl.java index 29f31b9f68b..3e31881f1d8 100644 --- a/compatible/src/main/java/io/seata/saga/statelang/domain/impl/StateImpl.java +++ b/compatible/src/main/java/io/seata/saga/statelang/domain/impl/StateImpl.java @@ -20,6 +20,7 @@ import io.seata.saga.statelang.domain.State; import io.seata.saga.statelang.domain.StateMachine; +import io.seata.saga.statelang.domain.StateType; /** * The type State. @@ -45,8 +46,8 @@ public String getComment() { } @Override - public String getType() { - return actual.getType(); + public StateType getType() { + return StateType.wrap(actual.getType()); } @Override diff --git a/compatible/src/main/java/io/seata/saga/statelang/domain/impl/StateInstanceImpl.java b/compatible/src/main/java/io/seata/saga/statelang/domain/impl/StateInstanceImpl.java index cfc0f49701e..f29e28ed5ea 100644 --- a/compatible/src/main/java/io/seata/saga/statelang/domain/impl/StateInstanceImpl.java +++ b/compatible/src/main/java/io/seata/saga/statelang/domain/impl/StateInstanceImpl.java @@ -21,6 +21,7 @@ import io.seata.saga.statelang.domain.ExecutionStatus; import io.seata.saga.statelang.domain.StateInstance; import io.seata.saga.statelang.domain.StateMachineInstance; +import io.seata.saga.statelang.domain.StateType; /** * state execution instance @@ -67,13 +68,17 @@ public void setName(String name) { } @Override - public String getType() { - return actual.getType(); + public StateType getType() { + return StateType.wrap(actual.getType()); } @Override - public void setType(String type) { - actual.setType(type); + public void setType(StateType type) { + if (type == null) { + actual.setType(null); + } else { + actual.setType(type.unwrap()); + } } @Override diff --git a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/config/AbstractStateMachineConfig.java b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/config/AbstractStateMachineConfig.java index e00b3861b41..96446594775 100644 --- a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/config/AbstractStateMachineConfig.java +++ b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/config/AbstractStateMachineConfig.java @@ -54,6 +54,7 @@ import org.apache.seata.saga.proctrl.impl.ProcessControllerImpl; import org.apache.seata.saga.proctrl.process.impl.CustomizeBusinessProcessor; import org.apache.seata.saga.statelang.domain.DomainConstants; +import org.apache.seata.saga.statelang.domain.StateType; import javax.script.ScriptEngineManager; import java.io.InputStream; @@ -248,13 +249,13 @@ private DefaultRouterHandler buildDefaultRouterHandler(ProcessCtrlEventPublisher stateMachineProcessRouter.initDefaultStateRouters(); loadStateRouterInterceptors(stateMachineProcessRouter.getStateRouters()); - Map processRouterMap = new HashMap<>(1); + Map processRouterMap = new HashMap<>(2); processRouterMap.put(ProcessType.STATE_LANG.getCode(), stateMachineProcessRouter); defaultRouterHandler.setProcessRouters(processRouterMap); return defaultRouterHandler; } - public void loadStateHandlerInterceptors(Map stateHandlerMap) { + public void loadStateHandlerInterceptors(Map stateHandlerMap) { for (StateHandler stateHandler : stateHandlerMap.values()) { if (stateHandler instanceof InterceptableStateHandler) { InterceptableStateHandler interceptableStateHandler = (InterceptableStateHandler) stateHandler; @@ -268,7 +269,7 @@ public void loadStateHandlerInterceptors(Map stateHandlerM } } - public void loadStateRouterInterceptors(Map stateRouterMap) { + public void loadStateRouterInterceptors(Map stateRouterMap) { for (StateRouter stateRouter : stateRouterMap.values()) { if (stateRouter instanceof InterceptableStateRouter) { InterceptableStateRouter interceptableStateRouter = (InterceptableStateRouter) stateRouter; diff --git a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/impl/ProcessCtrlStateMachineEngine.java b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/impl/ProcessCtrlStateMachineEngine.java index cf9b9096ba3..645fc8e656d 100644 --- a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/impl/ProcessCtrlStateMachineEngine.java +++ b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/impl/ProcessCtrlStateMachineEngine.java @@ -281,7 +281,7 @@ protected StateMachineInstance forwardInternal(String stateMachineInstId, Map stateIn continue; } - if (StateType.SUB_STATE_MACHINE.getValue().equals(stateInstance.getType())) { + if (StateType.SUB_STATE_MACHINE.equals(stateInstance.getType())) { StateInstance finalState = stateInstance; diff --git a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/StateMachineProcessHandler.java b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/StateMachineProcessHandler.java index aeb5096376e..462d9b1a055 100644 --- a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/StateMachineProcessHandler.java +++ b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/StateMachineProcessHandler.java @@ -43,13 +43,13 @@ */ public class StateMachineProcessHandler implements ProcessHandler { - private final Map stateHandlers = new ConcurrentHashMap<>(); + private final Map stateHandlers = new ConcurrentHashMap<>(); @Override public void process(ProcessContext context) throws FrameworkException { StateInstruction instruction = context.getInstruction(StateInstruction.class); State state = instruction.getState(context); - String stateType = state.getType(); + StateType stateType = state.getType(); StateHandler stateHandler = stateHandlers.get(stateType); List interceptors = null; @@ -88,23 +88,23 @@ public void initDefaultHandlers() { return; } - 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(StateType.SERVICE_TASK, new ServiceTaskStateHandler()); + stateHandlers.put(StateType.SCRIPT_TASK, new ScriptTaskStateHandler()); + stateHandlers.put(StateType.SUB_MACHINE_COMPENSATION, new ServiceTaskStateHandler()); + stateHandlers.put(StateType.SUB_STATE_MACHINE, new SubStateMachineHandler()); + stateHandlers.put(StateType.CHOICE, new ChoiceStateHandler()); + stateHandlers.put(StateType.SUCCEED, new SucceedEndStateHandler()); + stateHandlers.put(StateType.FAIL, new FailEndStateHandler()); + stateHandlers.put(StateType.COMPENSATION_TRIGGER, new CompensationTriggerStateHandler()); + stateHandlers.put(StateType.LOOP_START, new LoopStartStateHandler()); } - public Map getStateHandlers() { + public Map getStateHandlers() { return stateHandlers; } - public void setStateHandlers(Map stateHandlers) { + public void setStateHandlers(Map stateHandlers) { this.stateHandlers.putAll(stateHandlers); } } diff --git a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/StateMachineProcessRouter.java b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/StateMachineProcessRouter.java index fdde747de14..bcb7c238858 100644 --- a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/StateMachineProcessRouter.java +++ b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/StateMachineProcessRouter.java @@ -42,7 +42,7 @@ */ public class StateMachineProcessRouter implements ProcessRouter { - private final Map stateRouters = new ConcurrentHashMap<>(); + private final Map stateRouters = new ConcurrentHashMap<>(); @Override public Instruction route(ProcessContext context) throws FrameworkException { @@ -61,7 +61,7 @@ public Instruction route(ProcessContext context) throws FrameworkException { state = stateMachine.getStates().get(stateInstruction.getStateName()); } - String stateType = state.getType(); + StateType stateType = state.getType(); StateRouter router = stateRouters.get(stateType); @@ -111,23 +111,23 @@ public void initDefaultStateRouters() { } 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()); + stateRouters.put(StateType.SERVICE_TASK, taskStateRouter); + stateRouters.put(StateType.SCRIPT_TASK, taskStateRouter); + stateRouters.put(StateType.CHOICE, taskStateRouter); + stateRouters.put(StateType.COMPENSATION_TRIGGER, taskStateRouter); + stateRouters.put(StateType.SUB_STATE_MACHINE, taskStateRouter); + stateRouters.put(StateType.SUB_MACHINE_COMPENSATION, taskStateRouter); + stateRouters.put(StateType.LOOP_START, taskStateRouter); + + stateRouters.put(StateType.SUCCEED, new EndStateRouter()); + stateRouters.put(StateType.FAIL, new EndStateRouter()); } - public Map getStateRouters() { + public Map getStateRouters() { return stateRouters; } - public void setStateRouters(Map stateRouters) { + public void setStateRouters(Map stateRouters) { this.stateRouters.putAll(stateRouters); } } diff --git a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/CompensationHolder.java b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/CompensationHolder.java index 080f04407a8..b4d748646ae 100644 --- a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/CompensationHolder.java +++ b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/CompensationHolder.java @@ -126,13 +126,13 @@ private static boolean stateNeedToCompensate(StateInstance stateInstance) { if (stateInstance.isIgnoreStatus()) { return false; } - if (StateType.SUB_STATE_MACHINE.getValue().equals(stateInstance.getType())) { + if (StateType.SUB_STATE_MACHINE.equals(stateInstance.getType())) { return (!ExecutionStatus.FA.equals(stateInstance.getStatus())) && (!ExecutionStatus.SU.equals( stateInstance.getCompensationStatus())); } else { - return StateType.SERVICE_TASK.getValue().equals(stateInstance.getType()) && !stateInstance + return StateType.SERVICE_TASK.equals(stateInstance.getType()) && !stateInstance .isForCompensation() && (!ExecutionStatus.FA.equals(stateInstance.getStatus())) && (!ExecutionStatus.SU .equals(stateInstance.getCompensationStatus())); } diff --git a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/LoopTaskUtils.java b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/LoopTaskUtils.java index 434446f7a96..f9500eb3337 100644 --- a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/LoopTaskUtils.java +++ b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/pcext/utils/LoopTaskUtils.java @@ -98,9 +98,9 @@ public static Loop getLoopConfig(ProcessContext context, State currentState) { * @return the boolean */ public static boolean matchLoop(State state) { - 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())); + return state != null && (StateType.SERVICE_TASK.equals(state.getType()) + || StateType.SCRIPT_TASK.equals(state.getType()) + || StateType.SUB_STATE_MACHINE.equals(state.getType())); } /** @@ -382,7 +382,7 @@ public static String decideCurrentExceptionRoute(List subContext // compensate must be execute State state = stateMachine.getState(next); - if (StateType.COMPENSATION_TRIGGER.getValue().equals(state.getType())) { + if (StateType.COMPENSATION_TRIGGER.equals(state.getType())) { route = next; break; } else if (null == route) { diff --git a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/strategy/impl/DefaultStatusDecisionStrategy.java b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/strategy/impl/DefaultStatusDecisionStrategy.java index 875c3e8f603..5e8ddd23283 100644 --- a/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/strategy/impl/DefaultStatusDecisionStrategy.java +++ b/saga/seata-saga-engine/src/main/java/org/apache/seata/saga/engine/strategy/impl/DefaultStatusDecisionStrategy.java @@ -111,7 +111,7 @@ public static void setMachineStatusBasedOnStateListAndException(StateMachineInst stateMachineInstance.setStatus(ExecutionStatus.UN); hasSetStatus = true; } else if (ExecutionStatus.SU.equals(stateInstance.getStatus())) { - if (StateType.SERVICE_TASK.getValue().equals(stateInstance.getType())) { + if (StateType.SERVICE_TASK.equals(stateInstance.getType())) { if (stateInstance.isForUpdate() && !stateInstance.isForCompensation()) { hasSuccessUpdateService = true; } diff --git a/saga/seata-saga-spring/src/main/java/org/apache/seata/saga/engine/store/db/DbAndReportTcStateLogStore.java b/saga/seata-saga-spring/src/main/java/org/apache/seata/saga/engine/store/db/DbAndReportTcStateLogStore.java index d70515ca288..f3ee1512976 100644 --- a/saga/seata-saga-spring/src/main/java/org/apache/seata/saga/engine/store/db/DbAndReportTcStateLogStore.java +++ b/saga/seata-saga-spring/src/main/java/org/apache/seata/saga/engine/store/db/DbAndReportTcStateLogStore.java @@ -48,11 +48,7 @@ import org.apache.seata.saga.engine.store.StateLogStore; import org.apache.seata.saga.engine.tm.SagaTransactionalTemplate; import org.apache.seata.saga.proctrl.ProcessContext; -import org.apache.seata.saga.statelang.domain.DomainConstants; -import org.apache.seata.saga.statelang.domain.ExecutionStatus; -import org.apache.seata.saga.statelang.domain.StateInstance; -import org.apache.seata.saga.statelang.domain.StateMachine; -import org.apache.seata.saga.statelang.domain.StateMachineInstance; +import org.apache.seata.saga.statelang.domain.*; import org.apache.seata.saga.statelang.domain.impl.ServiceTaskStateImpl; import org.apache.seata.saga.statelang.domain.impl.StateInstanceImpl; import org.apache.seata.saga.statelang.domain.impl.StateMachineInstanceImpl; @@ -843,7 +839,7 @@ public void toStatement(StateInstance stateInstance, PreparedStatement statement statement.setString(1, stateInstance.getId()); statement.setString(2, stateInstance.getMachineInstanceId()); statement.setString(3, stateInstance.getName()); - statement.setString(4, stateInstance.getType()); + statement.setString(4, stateInstance.getType().getValue()); statement.setTimestamp(5, new Timestamp(stateInstance.getGmtStarted().getTime())); statement.setString(6, stateInstance.getServiceName()); statement.setString(7, stateInstance.getServiceMethod()); @@ -908,7 +904,7 @@ public StateInstance toObject(ResultSet resultSet) throws SQLException { stateInstance.setId(resultSet.getString("id")); stateInstance.setMachineInstanceId(resultSet.getString("machine_inst_id")); stateInstance.setName(resultSet.getString("name")); - stateInstance.setType(resultSet.getString("type")); + stateInstance.setType(StateType.getStateType(resultSet.getString("type"))); stateInstance.setBusinessKey(resultSet.getString("business_key")); stateInstance.setStatus(ExecutionStatus.valueOf(resultSet.getString("status"))); stateInstance.setGmtStarted(resultSet.getTimestamp("gmt_started")); diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/State.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/State.java index 1eb02dbe958..985bfb3bfbc 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/State.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/State.java @@ -43,7 +43,7 @@ public interface State { * * @return the state type */ - String getType(); + StateType getType(); /** * next state name diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/StateInstance.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/StateInstance.java index a7b4dc66fd5..fcbcebc9c73 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/StateInstance.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/StateInstance.java @@ -71,14 +71,14 @@ public interface StateInstance { * * @return state instance type */ - String getType(); + StateType getType(); /** * set type * * @param type state instance type */ - void setType(String type); + void setType(StateType type); /** * get service name diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/BaseState.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/BaseState.java index 50ae952f9b9..001083de024 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/BaseState.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/BaseState.java @@ -20,6 +20,7 @@ import org.apache.seata.saga.statelang.domain.State; import org.apache.seata.saga.statelang.domain.StateMachine; +import org.apache.seata.saga.statelang.domain.StateType; /** * BaseState @@ -28,7 +29,7 @@ public abstract class BaseState implements State { private transient String name; - private String type; + private StateType type; private String comment; private String next; private Map extensions; @@ -80,11 +81,11 @@ public void setStateMachine(StateMachine stateMachine) { } @Override - public String getType() { + public StateType getType() { return type; } - protected void setType(String type) { + protected void setType(StateType type) { this.type = type; } } diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ChoiceStateImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ChoiceStateImpl.java index 45ecea20062..da087f545e0 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ChoiceStateImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ChoiceStateImpl.java @@ -36,7 +36,7 @@ public class ChoiceStateImpl extends BaseState implements ChoiceState { private Map choiceEvaluators; public ChoiceStateImpl() { - setType(StateType.CHOICE.getValue()); + setType(StateType.CHOICE); } @Override diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/CompensateSubStateMachineStateImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/CompensateSubStateMachineStateImpl.java index 4ffb7ec0a03..61cddbb6029 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/CompensateSubStateMachineStateImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/CompensateSubStateMachineStateImpl.java @@ -25,6 +25,6 @@ */ public class CompensateSubStateMachineStateImpl extends ServiceTaskStateImpl implements CompensateSubStateMachineState { public CompensateSubStateMachineStateImpl() { - setType(StateType.SUB_MACHINE_COMPENSATION.getValue()); + setType(StateType.SUB_MACHINE_COMPENSATION); } } diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/CompensationTriggerStateImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/CompensationTriggerStateImpl.java index 543284b7179..7aa422cd555 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/CompensationTriggerStateImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/CompensationTriggerStateImpl.java @@ -26,6 +26,6 @@ public class CompensationTriggerStateImpl extends BaseState implements CompensationTriggerState { public CompensationTriggerStateImpl() { - setType(StateType.COMPENSATION_TRIGGER.getValue()); + setType(StateType.COMPENSATION_TRIGGER); } } diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/FailEndStateImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/FailEndStateImpl.java index be9e6d6eaae..8c5b3d7a57c 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/FailEndStateImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/FailEndStateImpl.java @@ -29,7 +29,7 @@ public class FailEndStateImpl extends BaseState implements FailEndState { private String message; public FailEndStateImpl() { - setType(StateType.FAIL.getValue()); + setType(StateType.FAIL); } @Override diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/LoopStartStateImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/LoopStartStateImpl.java index 5ace05f1681..72a3a1f3f04 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/LoopStartStateImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/LoopStartStateImpl.java @@ -26,7 +26,7 @@ public class LoopStartStateImpl extends BaseState implements LoopStartState { public LoopStartStateImpl() { - setType(StateType.LOOP_START.getValue()); + setType(StateType.LOOP_START); } } diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ScriptTaskStateImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ScriptTaskStateImpl.java index afd75beb53f..0634c0fbdeb 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ScriptTaskStateImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ScriptTaskStateImpl.java @@ -32,7 +32,7 @@ public class ScriptTaskStateImpl extends AbstractTaskState implements ScriptTask private String scriptContent; public ScriptTaskStateImpl() { - setType(StateType.SCRIPT_TASK.getValue()); + setType(StateType.SCRIPT_TASK); } @Override diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ServiceTaskStateImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ServiceTaskStateImpl.java index a2afbaa4097..aa16d60b83b 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ServiceTaskStateImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/ServiceTaskStateImpl.java @@ -38,7 +38,7 @@ public class ServiceTaskStateImpl extends AbstractTaskState implements ServiceTa private boolean isAsync; public ServiceTaskStateImpl() { - setType(StateType.SERVICE_TASK.getValue()); + setType(StateType.SERVICE_TASK); } @Override diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/StateInstanceImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/StateInstanceImpl.java index dd6ec21de59..0838f6ea15e 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/StateInstanceImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/StateInstanceImpl.java @@ -22,6 +22,7 @@ import org.apache.seata.saga.statelang.domain.ExecutionStatus; import org.apache.seata.saga.statelang.domain.StateInstance; import org.apache.seata.saga.statelang.domain.StateMachineInstance; +import org.apache.seata.saga.statelang.domain.StateType; /** * state execution instance @@ -32,7 +33,7 @@ public class StateInstanceImpl implements StateInstance { private String id; private String machineInstanceId; private String name; - private String type; + private StateType type; private String serviceName; private String serviceMethod; private String serviceType; @@ -85,12 +86,12 @@ public void setName(String name) { } @Override - public String getType() { + public StateType getType() { return type; } @Override - public void setType(String type) { + public void setType(StateType type) { this.type = type; } diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/SubStateMachineImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/SubStateMachineImpl.java index 0ac020fa7d6..534d95644b0 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/SubStateMachineImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/SubStateMachineImpl.java @@ -31,7 +31,7 @@ public class SubStateMachineImpl extends ServiceTaskStateImpl implements SubStat private TaskState compensateStateObject; public SubStateMachineImpl() { - setType(StateType.SUB_STATE_MACHINE.getValue()); + setType(StateType.SUB_STATE_MACHINE); } @Override diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/SucceedEndStateImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/SucceedEndStateImpl.java index 564a8e2788f..1e6bf1414e2 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/SucceedEndStateImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/domain/impl/SucceedEndStateImpl.java @@ -26,6 +26,6 @@ public class SucceedEndStateImpl extends BaseState implements SucceedEndState { public SucceedEndStateImpl() { - setType(StateType.SUCCEED.getValue()); + setType(StateType.SUCCEED); } } diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/StateParserFactory.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/StateParserFactory.java index 4ecbc15f537..b2e70fa3498 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/StateParserFactory.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/StateParserFactory.java @@ -35,21 +35,21 @@ */ public class StateParserFactory { - protected static Map stateParserMap = new ConcurrentHashMap<>(); + protected static Map stateParserMap = new ConcurrentHashMap<>(); static { - stateParserMap.put(StateType.SERVICE_TASK.getValue(), new ServiceTaskStateParser()); - stateParserMap.put(StateType.CHOICE.getValue(), new ChoiceStateParser()); - stateParserMap.put(StateType.COMPENSATION_TRIGGER.getValue(), new CompensationTriggerStateParser()); - stateParserMap.put(StateType.FAIL.getValue(), new FailEndStateParser()); - stateParserMap.put(StateType.SUCCEED.getValue(), new SucceedEndStateParser()); - stateParserMap.put(StateType.SUB_STATE_MACHINE.getValue(), new SubStateMachineParser()); - stateParserMap.put(StateType.SUB_MACHINE_COMPENSATION.getValue(), + stateParserMap.put(StateType.SERVICE_TASK, new ServiceTaskStateParser()); + stateParserMap.put(StateType.CHOICE, new ChoiceStateParser()); + stateParserMap.put(StateType.COMPENSATION_TRIGGER, new CompensationTriggerStateParser()); + stateParserMap.put(StateType.FAIL, new FailEndStateParser()); + stateParserMap.put(StateType.SUCCEED, new SucceedEndStateParser()); + stateParserMap.put(StateType.SUB_STATE_MACHINE, new SubStateMachineParser()); + stateParserMap.put(StateType.SUB_MACHINE_COMPENSATION, new CompensateSubStateMachineStateParser()); - stateParserMap.put(StateType.SCRIPT_TASK.getValue(), new ScriptTaskStateParser()); + stateParserMap.put(StateType.SCRIPT_TASK, new ScriptTaskStateParser()); } - public static StateParser getStateParser(String stateType) { + public static StateParser getStateParser(StateType stateType) { return stateParserMap.get(stateType); } } diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/impl/StateMachineParserImpl.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/impl/StateMachineParserImpl.java index 46ec2f12df4..da4b5ba1971 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/impl/StateMachineParserImpl.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/impl/StateMachineParserImpl.java @@ -21,6 +21,7 @@ import org.apache.seata.saga.statelang.domain.DomainConstants; import org.apache.seata.saga.statelang.domain.RecoverStrategy; import org.apache.seata.saga.statelang.domain.State; +import org.apache.seata.saga.statelang.domain.StateType; import org.apache.seata.saga.statelang.domain.StateMachine; import org.apache.seata.saga.statelang.domain.impl.AbstractTaskState; import org.apache.seata.saga.statelang.domain.impl.BaseState; @@ -97,7 +98,7 @@ public StateMachine parse(String json) { Map statesNode = (Map) node.get("States"); statesNode.forEach((stateName, value) -> { Map stateNode = (Map) value; - String stateType = (String) stateNode.get("Type"); + StateType stateType = StateType.getStateType((String) stateNode.get("Type")); StateParser stateParser = StateParserFactory.getStateParser(stateType); if (stateParser == null) { throw new IllegalArgumentException("State Type [" + stateType + "] is not support"); diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/utils/StateMachineUtils.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/utils/StateMachineUtils.java index be15f66bb23..69844e58205 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/utils/StateMachineUtils.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/parser/utils/StateMachineUtils.java @@ -36,7 +36,7 @@ public static Set getAllPossibleSubsequentStates(State state) { Set subsequentStates = new HashSet<>(); // Next state subsequentStates.add(state.getNext()); - StateType stateType = StateType.getStateType(state.getType()); + StateType stateType = state.getType(); switch (stateType) { case SCRIPT_TASK: case SERVICE_TASK: diff --git a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/validator/impl/NoRecursiveSubStateMachineRule.java b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/validator/impl/NoRecursiveSubStateMachineRule.java index d02694f0ca6..a355cb3705c 100644 --- a/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/validator/impl/NoRecursiveSubStateMachineRule.java +++ b/saga/seata-saga-statelang/src/main/java/org/apache/seata/saga/statelang/validator/impl/NoRecursiveSubStateMachineRule.java @@ -30,7 +30,7 @@ public class NoRecursiveSubStateMachineRule extends AbstractRule { @Override public boolean validate(StateMachine stateMachine) { for (State state: stateMachine.getStates().values()) { - if (!StateType.SUB_STATE_MACHINE.getValue().equals(state.getType())) { + if (!StateType.SUB_STATE_MACHINE.equals(state.getType())) { continue; } if (stateMachine.getName().equals(((SubStateMachine) state).getStateMachineName())) {