From f88c7c161bb33d769242ac712e9b4ca47c31c5fc Mon Sep 17 00:00:00 2001 From: "lorne.cl" Date: Fri, 29 Nov 2019 11:44:19 +0800 Subject: [PATCH 1/2] transform desinger json to statemachine standard json --- .../parser/impl/StateMachineParserImpl.java | 10 + .../parser/utils/DesignerJsonTransformer.java | 214 +++++++++++++ .../statelang/parser/StateParserTests.java | 15 + .../simple_statemachine_with_layout.json | 288 ++++++++++++++++++ .../saga/engine/StateMachineAsyncTests.java | 53 ++-- .../seata/saga/engine/StateMachineTests.java | 49 ++- .../saga/engine/db/StateMachineDBTests.java | 113 +++++-- .../StateMachineAsyncDBMockServerTests.java | 54 ++-- .../StateMachineDBMockServerTests.java | 178 ++++++++--- ...ang_with_compensation_and_sub_machine.json | 288 ++++++++++++++++++ ...ang_with_compensation_and_sub_machine.json | 1 - 11 files changed, 1134 insertions(+), 129 deletions(-) create mode 100644 saga/seata-saga-statelang/src/main/java/io/seata/saga/statelang/parser/utils/DesignerJsonTransformer.java create mode 100644 saga/seata-saga-statelang/src/test/resources/statelang/simple_statemachine_with_layout.json create mode 100644 test/src/test/resources/saga/statelang/designer_statelang_with_compensation_and_sub_machine.json diff --git a/saga/seata-saga-statelang/src/main/java/io/seata/saga/statelang/parser/impl/StateMachineParserImpl.java b/saga/seata-saga-statelang/src/main/java/io/seata/saga/statelang/parser/impl/StateMachineParserImpl.java index 1b72e3160a7..8798b3123f9 100644 --- a/saga/seata-saga-statelang/src/main/java/io/seata/saga/statelang/parser/impl/StateMachineParserImpl.java +++ b/saga/seata-saga-statelang/src/main/java/io/seata/saga/statelang/parser/impl/StateMachineParserImpl.java @@ -20,6 +20,7 @@ import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.parser.Feature; +import com.alibaba.fastjson.serializer.SerializerFeature; import io.seata.common.util.StringUtils; import io.seata.saga.statelang.domain.State; import io.seata.saga.statelang.domain.StateMachine; @@ -29,6 +30,9 @@ import io.seata.saga.statelang.parser.StateMachineParser; import io.seata.saga.statelang.parser.StateParser; import io.seata.saga.statelang.parser.StateParserFactory; +import io.seata.saga.statelang.parser.utils.DesignerJsonTransformer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * State machine language parser @@ -37,10 +41,16 @@ */ public class StateMachineParserImpl implements StateMachineParser { + private static final Logger LOGGER = LoggerFactory.getLogger(StateMachineParserImpl.class); + @Override public StateMachine parse(String json) { Map node = JSON.parseObject(json, Map.class, Feature.IgnoreAutoType, Feature.OrderedField); + if(DesignerJsonTransformer.isDesignerJson(node)){ + node = DesignerJsonTransformer.toStandardJson(node); + LOGGER.info("===== Transformed standard state language:\n{}", JSON.toJSONString(node, SerializerFeature.PrettyFormat)); + } StateMachineImpl stateMachine = new StateMachineImpl(); stateMachine.setName((String)node.get("Name")); stateMachine.setComment((String)node.get("Comment")); diff --git a/saga/seata-saga-statelang/src/main/java/io/seata/saga/statelang/parser/utils/DesignerJsonTransformer.java b/saga/seata-saga-statelang/src/main/java/io/seata/saga/statelang/parser/utils/DesignerJsonTransformer.java new file mode 100644 index 00000000000..f3573baff7d --- /dev/null +++ b/saga/seata-saga-statelang/src/main/java/io/seata/saga/statelang/parser/utils/DesignerJsonTransformer.java @@ -0,0 +1,214 @@ +/* + * Copyright 1999-2019 Seata.io Group. + * + * Licensed 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.parser.utils; + +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import org.springframework.util.StringUtils; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Transform designer json to standard Saga State language json + * + * @author lorne.cl + */ +public class DesignerJsonTransformer { + + public static Map toStandardJson(Map designerJsonObject) { + + if (!isDesignerJson(designerJsonObject)) { + return designerJsonObject; + } + + JSONObject machineJsonObject = new JSONObject(true); + + List nodes = (List) designerJsonObject.get("nodes"); + if (nodes != null && nodes.size() > 0) { + + Map nodeMap = new HashMap<>(nodes.size()); + + for (Object node : nodes) { + JSONObject nodeObj = (JSONObject) node; + + transformNode(machineJsonObject, nodeMap, nodeObj); + } + + List edges = (List) designerJsonObject.get("edges"); + if (edges != null && edges.size() > 0) { + for (Object edge : edges) { + JSONObject edgeObj = (JSONObject) edge; + transformEdge(machineJsonObject, nodes, nodeMap, edgeObj); + } + } + } + return machineJsonObject; + } + + private static void transformNode(JSONObject machineJsonObject, Map nodeMap, JSONObject nodeObj) { + nodeMap.put(nodeObj.getString("id"), nodeObj); + + String type = nodeObj.getString("stateType"); + JSONObject propsObj = (JSONObject) nodeObj.get("stateProps"); + if ("Start".equals(type)) { + if (propsObj != null && propsObj.containsKey("StateMachine")) { + machineJsonObject.putAll(propsObj.getJSONObject("StateMachine")); + } + } else if (!"Catch".equals(type)) { + + JSONObject states = machineJsonObject.getJSONObject("States"); + if (states == null) { + states = new JSONObject(true); + machineJsonObject.put("States", states); + } + + JSONObject stateJsonObject = new JSONObject(true); + String stateId = nodeObj.getString("stateId"); + if (states.containsKey(stateId)) { + throw new RuntimeException( + "Transform designer json to standard json failed, stateId[" + stateId + "] already exists, pls rename it."); + } + + String comment = nodeObj.getString("label"); + if (StringUtils.hasLength(comment)) { + stateJsonObject.put("Comment", comment); + } + if (propsObj != null) { + stateJsonObject.putAll(propsObj); + } + + states.put(stateId, stateJsonObject); + + String stateType = nodeObj.getString("stateType"); + if ("Compensation".equals(stateType)) { + stateJsonObject.put("Type", "ServiceTask"); + } else { + stateJsonObject.put("Type", stateType); + } + } + } + + private static void transformEdge(JSONObject machineJsonObject, List nodes, Map nodeMap, + JSONObject edgeObj) { + String sourceId = edgeObj.getString("source"); + String targetId = edgeObj.getString("target"); + if (StringUtils.hasLength(sourceId)) { + JSONObject sourceNode = nodeMap.get(sourceId); + JSONObject targetNode = nodeMap.get(targetId); + + if (sourceNode != null) { + + JSONObject states = machineJsonObject.getJSONObject("States"); + JSONObject sourceState = states.getJSONObject(sourceNode.getString("stateId")); + String targetStateId = targetNode.getString("stateId"); + + String sourceType = sourceNode.getString("stateType"); + if ("Start".equals(sourceType)) { + machineJsonObject.put("StartState", targetStateId); + //Make sure 'StartState' is before 'States' + machineJsonObject.put("States", machineJsonObject.remove("States")); + } else if ("ServiceTask".equals(sourceType)) { + if (targetNode != null && "Compensation".equals(targetNode.getString("stateType"))) { + sourceState.put("CompensateState", targetStateId); + } else { + sourceState.put("Next", targetStateId); + } + } else if ("Catch".equals(sourceType)) { + JSONObject catchAttachedNode = getCatchAttachedNode(sourceNode, nodes); + if (catchAttachedNode == null) { + throw new RuntimeException("'Catch' node[" + sourceNode.get("id") + "] is not attached on a 'ServiceTask'"); + } + JSONObject catchAttachedState = (JSONObject) states.get(catchAttachedNode.getString("stateId")); + JSONArray catches = catchAttachedState.getJSONArray("Catch"); + if (catches == null) { + catches = new JSONArray(); + catchAttachedState.put("Catch", catches); + } + + JSONObject edgeProps = (JSONObject) edgeObj.get("stateProps"); + if (edgeProps != null) { + JSONObject catchObj = new JSONObject(true); + catchObj.put("Exceptions", edgeProps.get("Exceptions")); + catchObj.put("Next", targetStateId); + catches.add(catchObj); + } + } else if ("Choice".equals(sourceType)) { + JSONArray choices = sourceState.getJSONArray("Choices"); + if (choices == null) { + choices = new JSONArray(); + sourceState.put("Choices", choices); + } + + JSONObject edgeProps = (JSONObject) edgeObj.get("stateProps"); + if (edgeProps != null) { + + if (Boolean.TRUE.equals(edgeProps.getBoolean("Default"))) { + sourceState.put("Default", targetStateId); + } else { + JSONObject choiceObj = new JSONObject(true); + choiceObj.put("Expression", edgeProps.get("Expression")); + choiceObj.put("Next", targetStateId); + choices.add(choiceObj); + } + } + } else { + sourceState.put("Next", targetStateId); + } + } + } + } + + public static boolean isDesignerJson(Map jsonObject) { + return (jsonObject != null && jsonObject.containsKey("nodes") && jsonObject.containsKey("edges")); + } + + private static JSONObject getCatchAttachedNode(JSONObject catchNode, List nodes) { + int catchNodeX = catchNode.getInteger("x"); + int catchNodeY = catchNode.getInteger("y"); + String catchSize = catchNode.getString("size"); + String[] catchSizes = catchSize.split("\\*"); + int catchWidth = Integer.parseInt(catchSizes[0]); + int catchHeight = Integer.parseInt(catchSizes[1]); + + for (Object node : nodes) { + JSONObject nodeObj = (JSONObject) node; + if (catchNode != nodeObj && "ServiceTask".equals(nodeObj.get("stateType"))) { + + int nodeX = nodeObj.getInteger("x"); + int nodeY = nodeObj.getInteger("y"); + + String nodeSize = nodeObj.getString("size"); + String[] nodeSizes = nodeSize.split("\\*"); + int nodeWidth = Integer.parseInt(nodeSizes[0]); + int nodeHeight = Integer.parseInt(nodeSizes[1]); + + if (isBordersCoincided(catchNodeX, nodeX, catchWidth, nodeWidth) + && isBordersCoincided(catchNodeY, nodeY, catchHeight, nodeHeight)) { + + return nodeObj; + } + } + } + return null; + } + + private static boolean isBordersCoincided(int xyA, int xyB, int lengthA, int lengthB) { + int centerPointLength = xyA > xyB ? xyA - xyB : xyB - xyA; + return ((lengthA + lengthB) / 2) > centerPointLength; + } +} \ No newline at end of file diff --git a/saga/seata-saga-statelang/src/test/java/io/seata/saga/statelang/parser/StateParserTests.java b/saga/seata-saga-statelang/src/test/java/io/seata/saga/statelang/parser/StateParserTests.java index 3d31bad5a2a..785de7f7221 100644 --- a/saga/seata-saga-statelang/src/test/java/io/seata/saga/statelang/parser/StateParserTests.java +++ b/saga/seata-saga-statelang/src/test/java/io/seata/saga/statelang/parser/StateParserTests.java @@ -16,11 +16,14 @@ package io.seata.saga.statelang.parser; import java.io.IOException; +import java.util.Map; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.parser.Feature; import com.alibaba.fastjson.serializer.SerializerFeature; import io.seata.saga.statelang.domain.StateMachine; +import io.seata.saga.statelang.parser.utils.DesignerJsonTransformer; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.core.io.ClassPathResource; @@ -46,4 +49,16 @@ public void testParser() throws IOException { Assertions.assertEquals(stateMachine.getName(), "simpleTestStateMachine"); Assertions.assertTrue(stateMachine.getStates().size() > 0); } + + @Test + public void testDesignerJsonTransformer() throws IOException { + + ClassPathResource resource = new ClassPathResource("statelang/simple_statemachine_with_layout.json"); + String json = io.seata.saga.statelang.parser.utils.IOUtils.toString(resource.getInputStream(), "UTF-8"); + Map parsedObj = DesignerJsonTransformer.toStandardJson(JSON.parseObject(json, Feature.OrderedField)); + Assertions.assertNotNull(parsedObj); + + String outputJson = JSON.toJSONString(parsedObj, SerializerFeature.PrettyFormat); + System.out.println(outputJson); + } } \ No newline at end of file diff --git a/saga/seata-saga-statelang/src/test/resources/statelang/simple_statemachine_with_layout.json b/saga/seata-saga-statelang/src/test/resources/statelang/simple_statemachine_with_layout.json new file mode 100644 index 00000000000..4d886a02c36 --- /dev/null +++ b/saga/seata-saga-statelang/src/test/resources/statelang/simple_statemachine_with_layout.json @@ -0,0 +1,288 @@ +{ + "nodes": [ + { + "type": "node", + "size": "72*72", + "shape": "flow-circle", + "color": "#FA8C16", + "label": "Start", + "stateId": "Start", + "stateType": "Start", + "stateProps": { + "StateMachine": { + "Name": "simpleStateMachineWithCompensationAndSubMachine_layout", + "Comment": "带补偿定义和调用子状态机", + "Version": "0.0.1" + } + }, + "x": 199.875, + "y": 95, + "id": "e2d86441" + }, + { + "type": "node", + "size": "110*48", + "shape": "flow-rect", + "color": "#1890FF", + "label": "FirstState", + "stateId": "FirstState", + "stateType": "ServiceTask", + "stateProps": { + "ServiceName": "demoService", + "ServiceMethod": "foo", + "Input": [ + { + "fooInput": "$.[a]" + } + ], + "Output": { + "fooResult": "$.#root" + } + }, + "x": 199.875, + "y": 213, + "id": "6111bf54" + }, + { + "type": "node", + "size": "80*72", + "shape": "flow-rhombus", + "color": "#13C2C2", + "label": "ChoiceState", + "stateId": "ChoiceState", + "stateType": "Choice", + "x": 199.875, + "y": 341.5, + "id": "5610fa37" + }, + { + "type": "node", + "size": "110*48", + "shape": "flow-rect", + "color": "#1890FF", + "label": "SecondState", + "stateId": "SecondState", + "stateType": "ServiceTask", + "stateProps": { + "ServiceName": "demoService", + "ServiceMethod": "bar", + "Input": [ + { + "barInput": "$.[fooResult]", + "throwException": "$.[barThrowException]" + } + ], + "Output": { + "barResult": "$.#root" + }, + "Status": { + "#root != null": "SU", + "#root == null": "FA", + "$Exception{io.seata.saga.engine.exception.EngineExecutionException}": "UN" + } + }, + "x": 199.375, + "y": 468, + "id": "af5591f9" + }, + { + "type": "node", + "size": "72*72", + "shape": "flow-circle", + "color": "#05A465", + "label": "Succeed", + "stateId": "Succeed", + "stateType": "Succeed", + "x": 199.375, + "y": 609, + "id": "2fd4c8de" + }, + { + "type": "node", + "size": "110*48", + "shape": "flow-rect", + "color": "#FA8C16", + "label": "SubStateMachine", + "stateId": "CallSubStateMachine", + "stateType": "SubStateMachine", + "stateProps": { + "StateMachineName": "simpleCompensationStateMachine", + "Input": [ + { + "a": "$.1", + "barThrowException": "$.[barThrowException]", + "fooThrowException": "$.[fooThrowException]", + "compensateFooThrowException": "$.[compensateFooThrowException]" + } + ], + "Output": { + "fooResult": "$.#root" + } + }, + "x": 55.875, + "y": 467, + "id": "04ea55a5" + }, + { + "type": "node", + "size": "110*48", + "shape": "flow-capsule", + "color": "#722ED1", + "label": "CompenFirstState", + "stateId": "CompensateFirstState", + "stateType": "Compensation", + "stateProps": { + "ServiceName": "demoService", + "ServiceMethod": "compensateFoo", + "Input": [ + { + "compensateFooInput": "$.[fooResult]" + } + ] + }, + "x": 68.875, + "y": 126, + "id": "6a09a5c2" + }, + { + "type": "node", + "size": "39*39", + "shape": "flow-circle", + "color": "red", + "label": "Catch", + "stateId": "Catch", + "stateType": "Catch", + "x": 257.875, + "y": 492, + "id": "e28af1c2" + }, + { + "type": "node", + "size": "110*48", + "shape": "flow-capsule", + "color": "red", + "label": "Compensation\nTrigger", + "stateId": "CompensationTrigger", + "stateType": "CompensationTrigger", + "x": 366.875, + "y": 491.5, + "id": "e32417a0" + }, + { + "type": "node", + "size": "72*72", + "shape": "flow-circle", + "color": "red", + "label": "Fail", + "stateId": "Fail", + "stateType": "Fail", + "stateProps": { + "ErrorCode": "NOT_FOUND", + "Message": "not found" + }, + "x": 513.375, + "y": 491.5, + "id": "d21d24c9" + } + ], + "edges": [ + { + "source": "e2d86441", + "sourceAnchor": 2, + "target": "6111bf54", + "targetAnchor": 0, + "id": "51f30b96" + }, + { + "source": "6111bf54", + "sourceAnchor": 2, + "target": "5610fa37", + "targetAnchor": 0, + "id": "8c3029b1" + }, + { + "source": "5610fa37", + "sourceAnchor": 2, + "target": "af5591f9", + "targetAnchor": 0, + "id": "a9e7d5b4", + "stateProps": { + "Expression": "[a] == 1", + "Default": false + }, + "label": "", + "shape": "flow-smooth" + }, + { + "source": "af5591f9", + "sourceAnchor": 2, + "target": "2fd4c8de", + "targetAnchor": 0, + "id": "61f34a49" + }, + { + "source": "6111bf54", + "sourceAnchor": 3, + "target": "6a09a5c2", + "targetAnchor": 2, + "id": "553384ab", + "style": { + "lineDash": "4" + } + }, + { + "source": "5610fa37", + "sourceAnchor": 3, + "target": "04ea55a5", + "targetAnchor": 0, + "id": "2ee91c33", + "stateProps": { + "Expression": "[a] == 2", + "Default": false + }, + "label": "", + "shape": "flow-smooth" + }, + { + "source": "e28af1c2", + "sourceAnchor": 1, + "target": "e32417a0", + "targetAnchor": 3, + "id": "d854a4d0", + "stateProps": { + "Exceptions": [ + "io.seata.common.exception.FrameworkException" + ] + }, + "label": "", + "shape": "flow-smooth" + }, + { + "source": "04ea55a5", + "sourceAnchor": 2, + "target": "2fd4c8de", + "targetAnchor": 3, + "id": "28734ad2" + }, + { + "source": "5610fa37", + "sourceAnchor": 1, + "target": "d21d24c9", + "targetAnchor": 0, + "id": "7c7595c0", + "stateProps": { + "Expression": "", + "Default": true + }, + "label": "", + "shape": "flow-smooth" + }, + { + "source": "e32417a0", + "sourceAnchor": 1, + "target": "d21d24c9", + "targetAnchor": 3, + "id": "16d809ce" + } + ] +} \ No newline at end of file diff --git a/test/src/test/java/io/seata/saga/engine/StateMachineAsyncTests.java b/test/src/test/java/io/seata/saga/engine/StateMachineAsyncTests.java index 958374d4738..854170981ec 100644 --- a/test/src/test/java/io/seata/saga/engine/StateMachineAsyncTests.java +++ b/test/src/test/java/io/seata/saga/engine/StateMachineAsyncTests.java @@ -37,7 +37,7 @@ public class StateMachineAsyncTests { private static StateMachineEngine stateMachineEngine; @BeforeAll - public static void initApplicationContext(){ + public static void initApplicationContext() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:saga/spring/statemachine_engine_test.xml"); stateMachineEngine = applicationContext.getBean("stateMachineEngine", StateMachineEngine.class); } @@ -45,7 +45,7 @@ public static void initApplicationContext(){ @Test public void testSimpleCatchesStateMachine() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -60,7 +60,6 @@ public void testSimpleCatchesStateMachine() { long cost = System.currentTimeMillis() - start; System.out.println("====== cost :" + cost); - Assertions.assertNotNull(inst.getException()); Assertions.assertTrue(ExecutionStatus.FA.equals(inst.getStatus())); } @@ -68,7 +67,7 @@ public void testSimpleCatchesStateMachine() { @Test public void testStatusMatchingStateMachine() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -83,7 +82,6 @@ public void testStatusMatchingStateMachine() { long cost = System.currentTimeMillis() - start; System.out.println("====== cost :" + cost); - Assertions.assertNotNull(inst.getException()); Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus())); } @@ -91,7 +89,7 @@ public void testStatusMatchingStateMachine() { @Test public void testCompensationStateMachine() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -113,7 +111,7 @@ public void testCompensationStateMachine() { @Test public void testCompensationAndSubStateMachine() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 2); @@ -131,10 +129,31 @@ public void testCompensationAndSubStateMachine() { Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus())); } + @Test + public void testCompensationAndSubStateMachineWithLayout() { + + long start = System.currentTimeMillis(); + + Map paramMap = new HashMap<>(1); + paramMap.put("a", 2); + paramMap.put("barThrowException", "true"); + + String stateMachineName = "simpleStateMachineWithCompensationAndSubMachine_layout"; + + StateMachineInstance inst = stateMachineEngine.startAsync(stateMachineName, null, paramMap, callback); + + waittingForFinish(inst); + + long cost = System.currentTimeMillis() - start; + System.out.println("====== cost :" + cost); + + Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus())); + } + @Test public void testStateMachineWithComplextParams() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); People people = new People(); @@ -150,7 +169,7 @@ public void testStateMachineWithComplextParams() { long cost = System.currentTimeMillis() - start; - People peopleResult = (People)inst.getEndParams().get("complexParameterMethodResult"); + People peopleResult = (People) inst.getEndParams().get("complexParameterMethodResult"); Assertions.assertNotNull(peopleResult); Assertions.assertTrue(people.getName().equals(people.getName())); @@ -162,7 +181,7 @@ public void testStateMachineWithComplextParams() { @Test public void testSimpleStateMachineWithAsyncState() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -185,9 +204,9 @@ public void testSimpleStateMachineWithAsyncState() { } } - private void waittingForFinish(StateMachineInstance inst){ - synchronized (lock){ - if(ExecutionStatus.RU.equals(inst.getStatus())){ + private void waittingForFinish(StateMachineInstance inst) { + synchronized (lock) { + if (ExecutionStatus.RU.equals(inst.getStatus())) { try { lock.wait(); } catch (InterruptedException e) { @@ -197,18 +216,18 @@ private void waittingForFinish(StateMachineInstance inst){ } } - private volatile Object lock = new Object(); - private AsyncCallback callback = new AsyncCallback() { + private volatile Object lock = new Object(); + private AsyncCallback callback = new AsyncCallback() { @Override public void onFinished(ProcessContext context, StateMachineInstance stateMachineInstance) { - synchronized (lock){ + synchronized (lock) { lock.notifyAll(); } } @Override public void onError(ProcessContext context, StateMachineInstance stateMachineInstance, Exception exp) { - synchronized (lock){ + synchronized (lock) { lock.notifyAll(); } } diff --git a/test/src/test/java/io/seata/saga/engine/StateMachineTests.java b/test/src/test/java/io/seata/saga/engine/StateMachineTests.java index 9bf4c0d5cf1..c587384abad 100644 --- a/test/src/test/java/io/seata/saga/engine/StateMachineTests.java +++ b/test/src/test/java/io/seata/saga/engine/StateMachineTests.java @@ -38,7 +38,7 @@ public class StateMachineTests { private static StateMachineEngine stateMachineEngine; @BeforeAll - public static void initApplicationContext(){ + public static void initApplicationContext() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:saga/spring/statemachine_engine_test.xml"); stateMachineEngine = applicationContext.getBean("stateMachineEngine", StateMachineEngine.class); } @@ -52,7 +52,7 @@ public void testSimpleStateMachine() { @Test public void testSimpleStateMachineWithChoice() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(); paramMap.put("a", 1); @@ -64,7 +64,7 @@ public void testSimpleStateMachineWithChoice() { long cost = System.currentTimeMillis() - start; System.out.println("====== cost :" + cost); - start = System.currentTimeMillis(); + start = System.currentTimeMillis(); paramMap.put("a", 2); stateMachineEngine.start(stateMachineName, null, paramMap); @@ -75,7 +75,7 @@ public void testSimpleStateMachineWithChoice() { @Test public void testSimpleStateMachineWithChoiceAndEnd() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -87,7 +87,7 @@ public void testSimpleStateMachineWithChoiceAndEnd() { long cost = System.currentTimeMillis() - start; System.out.println("====== cost :" + cost); - start = System.currentTimeMillis(); + start = System.currentTimeMillis(); paramMap.put("a", 3); stateMachineEngine.start(stateMachineName, null, paramMap); @@ -99,7 +99,7 @@ public void testSimpleStateMachineWithChoiceAndEnd() { @Test public void testSimpleInputAssignmentStateMachine() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -112,7 +112,8 @@ public void testSimpleInputAssignmentStateMachine() { Assertions.assertNotNull(businessKey); System.out.println("====== businessKey :" + businessKey); - String contextBusinessKey = (String)instance.getEndParams().get(instance.getStateList().get(0).getName()+ DomainConstants.VAR_NAME_BUSINESSKEY); + String contextBusinessKey = (String) instance.getEndParams().get( + instance.getStateList().get(0).getName() + DomainConstants.VAR_NAME_BUSINESSKEY); Assertions.assertNotNull(contextBusinessKey); System.out.println("====== context businessKey :" + businessKey); @@ -123,7 +124,7 @@ public void testSimpleInputAssignmentStateMachine() { @Test public void testSimpleCatchesStateMachine() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -143,7 +144,7 @@ public void testSimpleCatchesStateMachine() { @Test public void testStatusMatchingStateMachine() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -160,11 +161,10 @@ public void testStatusMatchingStateMachine() { Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus())); } - @Test public void testCompensationStateMachine() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -184,7 +184,7 @@ public void testCompensationStateMachine() { @Test public void testCompensationAndSubStateMachine() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 2); @@ -200,10 +200,29 @@ public void testCompensationAndSubStateMachine() { Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus())); } + @Test + public void testCompensationAndSubStateMachineWithLayout() { + + long start = System.currentTimeMillis(); + + Map paramMap = new HashMap<>(1); + paramMap.put("a", 2); + paramMap.put("barThrowException", "true"); + + String stateMachineName = "simpleStateMachineWithCompensationAndSubMachine_layout"; + + StateMachineInstance inst = stateMachineEngine.start(stateMachineName, null, paramMap); + + long cost = System.currentTimeMillis() - start; + System.out.println("====== cost :" + cost); + + Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus())); + } + @Test public void testStateMachineWithComplextParams() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); People people = new People(); @@ -219,7 +238,7 @@ public void testStateMachineWithComplextParams() { String stateMachineName = "simpleStateMachineWithComplexParams"; StateMachineInstance instance = stateMachineEngine.start(stateMachineName, null, paramMap); - People peopleResult = (People)instance.getEndParams().get("complexParameterMethodResult"); + People peopleResult = (People) instance.getEndParams().get("complexParameterMethodResult"); Assertions.assertNotNull(peopleResult); Assertions.assertTrue(people.getName().equals(people.getName())); @@ -232,7 +251,7 @@ public void testStateMachineWithComplextParams() { @Test public void testSimpleStateMachineWithAsyncState() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); diff --git a/test/src/test/java/io/seata/saga/engine/db/StateMachineDBTests.java b/test/src/test/java/io/seata/saga/engine/db/StateMachineDBTests.java index f84f56e5b5f..61a22275813 100644 --- a/test/src/test/java/io/seata/saga/engine/db/StateMachineDBTests.java +++ b/test/src/test/java/io/seata/saga/engine/db/StateMachineDBTests.java @@ -49,10 +49,10 @@ public static void initApplicationContext() throws InterruptedException { stateMachineEngine = applicationContext.getBean("stateMachineEngine", StateMachineEngine.class); } - private GlobalTransaction getGlobalTransaction(StateMachineInstance instance){ + private GlobalTransaction getGlobalTransaction(StateMachineInstance instance) { Map params = instance.getContext(); - if(params != null){ - return (GlobalTransaction)params.get(DomainConstants.VAR_NAME_GLOBAL_TX); + if (params != null) { + return (GlobalTransaction) params.get(DomainConstants.VAR_NAME_GLOBAL_TX); } return null; } @@ -66,7 +66,7 @@ public void testSimpleStateMachine() { @Test public void testSimpleStateMachineWithChoice() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(); paramMap.put("a", 1); @@ -78,7 +78,7 @@ public void testSimpleStateMachineWithChoice() { long cost = System.currentTimeMillis() - start; System.out.println("====== cost :" + cost); - start = System.currentTimeMillis(); + start = System.currentTimeMillis(); paramMap.put("a", 2); stateMachineEngine.start(stateMachineName, null, paramMap); @@ -89,7 +89,7 @@ public void testSimpleStateMachineWithChoice() { @Test public void testSimpleStateMachineWithChoiceAndEnd() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -101,7 +101,7 @@ public void testSimpleStateMachineWithChoiceAndEnd() { long cost = System.currentTimeMillis() - start; System.out.println("====== cost :" + cost); - start = System.currentTimeMillis(); + start = System.currentTimeMillis(); paramMap.put("a", 3); stateMachineEngine.start(stateMachineName, null, paramMap); @@ -113,7 +113,7 @@ public void testSimpleStateMachineWithChoiceAndEnd() { @Test public void testSimpleInputAssignmentStateMachine() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -126,7 +126,8 @@ public void testSimpleInputAssignmentStateMachine() { Assertions.assertNotNull(businessKey); System.out.println("====== businessKey :" + businessKey); - String contextBusinessKey = (String)instance.getEndParams().get(instance.getStateList().get(0).getName()+ DomainConstants.VAR_NAME_BUSINESSKEY); + String contextBusinessKey = (String) instance.getEndParams().get( + instance.getStateList().get(0).getName() + DomainConstants.VAR_NAME_BUSINESSKEY); Assertions.assertNotNull(contextBusinessKey); System.out.println("====== context businessKey :" + businessKey); @@ -137,7 +138,7 @@ public void testSimpleInputAssignmentStateMachine() { @Test public void testSimpleCatchesStateMachine() throws Exception { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -161,7 +162,7 @@ public void testSimpleCatchesStateMachine() throws Exception { @Test public void testStatusMatchingStateMachine() throws Exception { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -183,11 +184,10 @@ public void testStatusMatchingStateMachine() throws Exception { Assertions.assertTrue(GlobalStatus.CommitRetrying.equals(globalTransaction.getStatus())); } - @Test public void testCompensationStateMachine() throws Exception { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -212,7 +212,7 @@ public void testCompensationStateMachine() throws Exception { @Test public void testCompensationAndSubStateMachine() throws Exception { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 2); @@ -232,10 +232,33 @@ public void testCompensationAndSubStateMachine() throws Exception { Assertions.assertTrue(GlobalStatus.CommitRetrying.equals(globalTransaction.getStatus())); } + @Test + public void testCompensationAndSubStateMachineLayout() throws Exception { + + long start = System.currentTimeMillis(); + + Map paramMap = new HashMap<>(1); + paramMap.put("a", 2); + paramMap.put("barThrowException", "true"); + + String stateMachineName = "simpleStateMachineWithCompensationAndSubMachine_layout"; + + StateMachineInstance inst = stateMachineEngine.start(stateMachineName, null, paramMap); + + long cost = System.currentTimeMillis() - start; + System.out.println("====== cost :" + cost); + + Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus())); + + GlobalTransaction globalTransaction = getGlobalTransaction(inst); + Assertions.assertNotNull(globalTransaction); + Assertions.assertTrue(GlobalStatus.CommitRetrying.equals(globalTransaction.getStatus())); + } + @Test public void testCompensationStateMachineForRecovery() throws Exception { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -253,26 +276,27 @@ public void testCompensationStateMachineForRecovery() throws Exception { GlobalTransaction globalTransaction = getGlobalTransaction(inst); Assertions.assertNotNull(globalTransaction); - System.out.println("====== GlobalStatus: "+globalTransaction.getStatus()); + System.out.println("====== GlobalStatus: " + globalTransaction.getStatus()); // waiting for global transaction recover - while (!(ExecutionStatus.SU.equals(inst.getStatus()) || ExecutionStatus.SU.equals(inst.getCompensationStatus()))){ - System.out.println("====== GlobalStatus: "+globalTransaction.getStatus()); + while (!(ExecutionStatus.SU.equals(inst.getStatus()) || ExecutionStatus.SU.equals(inst.getCompensationStatus()))) { + System.out.println("====== GlobalStatus: " + globalTransaction.getStatus()); Thread.sleep(2000); inst = stateMachineEngine.getStateMachineConfig().getStateLogStore().getStateMachineInstance(inst.getId()); } } @Test - public void testReloadStateMachineInstance(){ - StateMachineInstance instance = stateMachineEngine.getStateMachineConfig().getStateLogStore().getStateMachineInstance("10.15.232.93:8091:2019567124"); + public void testReloadStateMachineInstance() { + StateMachineInstance instance = stateMachineEngine.getStateMachineConfig().getStateLogStore().getStateMachineInstance( + "10.15.232.93:8091:2019567124"); System.out.println(instance); } @Test public void testSimpleStateMachineWithAsyncState() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -296,7 +320,7 @@ public void testSimpleStateMachineWithAsyncState() { @Test public void testSimpleCatchesStateMachineAsync() throws Exception { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -311,7 +335,6 @@ public void testSimpleCatchesStateMachineAsync() throws Exception { long cost = System.currentTimeMillis() - start; System.out.println("====== cost :" + cost); - Assertions.assertNotNull(inst.getException()); Assertions.assertTrue(ExecutionStatus.FA.equals(inst.getStatus())); @@ -323,7 +346,7 @@ public void testSimpleCatchesStateMachineAsync() throws Exception { @Test public void testStatusMatchingStateMachineAsync() throws Exception { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -338,7 +361,6 @@ public void testStatusMatchingStateMachineAsync() throws Exception { long cost = System.currentTimeMillis() - start; System.out.println("====== cost :" + cost); - Assertions.assertNotNull(inst.getException()); Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus())); @@ -350,7 +372,7 @@ public void testStatusMatchingStateMachineAsync() throws Exception { @Test public void testCompensationStateMachineAsync() throws Exception { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -376,7 +398,7 @@ public void testCompensationStateMachineAsync() throws Exception { @Test public void testCompensationAndSubStateMachineAsync() throws Exception { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 2); @@ -398,10 +420,35 @@ public void testCompensationAndSubStateMachineAsync() throws Exception { Assertions.assertTrue(GlobalStatus.CommitRetrying.equals(globalTransaction.getStatus())); } + @Test + public void testCompensationAndSubStateMachineAsyncWithLayout() throws Exception { + + long start = System.currentTimeMillis(); + + Map paramMap = new HashMap<>(1); + paramMap.put("a", 2); + paramMap.put("barThrowException", "true"); + + String stateMachineName = "simpleStateMachineWithCompensationAndSubMachine_layout"; + + StateMachineInstance inst = stateMachineEngine.startAsync(stateMachineName, null, paramMap, callback); + + waittingForFinish(inst); + + long cost = System.currentTimeMillis() - start; + System.out.println("====== cost :" + cost); + + Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus())); + + GlobalTransaction globalTransaction = getGlobalTransaction(inst); + Assertions.assertNotNull(globalTransaction); + Assertions.assertTrue(GlobalStatus.CommitRetrying.equals(globalTransaction.getStatus())); + } + @Test public void testAsyncStartSimpleStateMachineWithAsyncState() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -423,9 +470,9 @@ public void testAsyncStartSimpleStateMachineWithAsyncState() { } } - private void waittingForFinish(StateMachineInstance inst){ - synchronized (lock){ - if(ExecutionStatus.RU.equals(inst.getStatus())){ + private void waittingForFinish(StateMachineInstance inst) { + synchronized (lock) { + if (ExecutionStatus.RU.equals(inst.getStatus())) { try { lock.wait(); } catch (InterruptedException e) { @@ -439,14 +486,14 @@ private void waittingForFinish(StateMachineInstance inst){ private AsyncCallback callback = new AsyncCallback() { @Override public void onFinished(ProcessContext context, StateMachineInstance stateMachineInstance) { - synchronized (lock){ + synchronized (lock) { lock.notifyAll(); } } @Override public void onError(ProcessContext context, StateMachineInstance stateMachineInstance, Exception exp) { - synchronized (lock){ + synchronized (lock) { lock.notifyAll(); } } diff --git a/test/src/test/java/io/seata/saga/engine/db/mockserver/StateMachineAsyncDBMockServerTests.java b/test/src/test/java/io/seata/saga/engine/db/mockserver/StateMachineAsyncDBMockServerTests.java index 648f64b7b57..6804006d544 100644 --- a/test/src/test/java/io/seata/saga/engine/db/mockserver/StateMachineAsyncDBMockServerTests.java +++ b/test/src/test/java/io/seata/saga/engine/db/mockserver/StateMachineAsyncDBMockServerTests.java @@ -40,14 +40,15 @@ public class StateMachineAsyncDBMockServerTests { @BeforeAll public static void initApplicationContext() throws InterruptedException { - ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:saga/spring/statemachine_engine_db_mockserver_test.xml"); + ApplicationContext applicationContext = new ClassPathXmlApplicationContext( + "classpath:saga/spring/statemachine_engine_db_mockserver_test.xml"); stateMachineEngine = applicationContext.getBean("stateMachineEngine", StateMachineEngine.class); } @Test public void testSimpleCatchesStateMachine() throws Exception { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -62,7 +63,6 @@ public void testSimpleCatchesStateMachine() throws Exception { long cost = System.currentTimeMillis() - start; System.out.println("====== cost :" + cost); - Assertions.assertNotNull(inst.getException()); Assertions.assertTrue(ExecutionStatus.FA.equals(inst.getStatus())); } @@ -70,7 +70,7 @@ public void testSimpleCatchesStateMachine() throws Exception { @Test public void testStatusMatchingStateMachine() throws Exception { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -85,7 +85,6 @@ public void testStatusMatchingStateMachine() throws Exception { long cost = System.currentTimeMillis() - start; System.out.println("====== cost :" + cost); - Assertions.assertNotNull(inst.getException()); Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus())); } @@ -93,7 +92,7 @@ public void testStatusMatchingStateMachine() throws Exception { @Test public void testCompensationStateMachine() throws Exception { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -115,7 +114,7 @@ public void testCompensationStateMachine() throws Exception { @Test public void testCompensationAndSubStateMachine() throws Exception { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 2); @@ -133,10 +132,31 @@ public void testCompensationAndSubStateMachine() throws Exception { Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus())); } + @Test + public void testCompensationAndSubStateMachineWithLayout() throws Exception { + + long start = System.currentTimeMillis(); + + Map paramMap = new HashMap<>(1); + paramMap.put("a", 2); + paramMap.put("barThrowException", "true"); + + String stateMachineName = "simpleStateMachineWithCompensationAndSubMachine_layout"; + + StateMachineInstance inst = stateMachineEngine.startAsync(stateMachineName, null, paramMap, callback); + + waittingForFinish(inst); + + long cost = System.currentTimeMillis() - start; + System.out.println("====== cost :" + cost); + + Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus())); + } + @Test public void testStateMachineWithComplextParams() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); People people = new People(); @@ -152,7 +172,7 @@ public void testStateMachineWithComplextParams() { long cost = System.currentTimeMillis() - start; - People peopleResult = (People)inst.getEndParams().get("complexParameterMethodResult"); + People peopleResult = (People) inst.getEndParams().get("complexParameterMethodResult"); Assertions.assertNotNull(peopleResult); Assertions.assertTrue(people.getName().equals(people.getName())); @@ -164,7 +184,7 @@ public void testStateMachineWithComplextParams() { @Test public void testSimpleStateMachineWithAsyncState() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -187,9 +207,9 @@ public void testSimpleStateMachineWithAsyncState() { } } - private void waittingForFinish(StateMachineInstance inst){ - synchronized (lock){ - if(ExecutionStatus.RU.equals(inst.getStatus())){ + private void waittingForFinish(StateMachineInstance inst) { + synchronized (lock) { + if (ExecutionStatus.RU.equals(inst.getStatus())) { try { lock.wait(); } catch (InterruptedException e) { @@ -199,18 +219,18 @@ private void waittingForFinish(StateMachineInstance inst){ } } - private volatile Object lock = new Object(); - private AsyncCallback callback = new AsyncCallback() { + private volatile Object lock = new Object(); + private AsyncCallback callback = new AsyncCallback() { @Override public void onFinished(ProcessContext context, StateMachineInstance stateMachineInstance) { - synchronized (lock){ + synchronized (lock) { lock.notifyAll(); } } @Override public void onError(ProcessContext context, StateMachineInstance stateMachineInstance, Exception exp) { - synchronized (lock){ + synchronized (lock) { lock.notifyAll(); } } diff --git a/test/src/test/java/io/seata/saga/engine/db/mockserver/StateMachineDBMockServerTests.java b/test/src/test/java/io/seata/saga/engine/db/mockserver/StateMachineDBMockServerTests.java index d57b2380532..eb400a7357d 100644 --- a/test/src/test/java/io/seata/saga/engine/db/mockserver/StateMachineDBMockServerTests.java +++ b/test/src/test/java/io/seata/saga/engine/db/mockserver/StateMachineDBMockServerTests.java @@ -40,7 +40,8 @@ public class StateMachineDBMockServerTests { @BeforeAll public static void initApplicationContext() throws InterruptedException { - ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:saga/spring/statemachine_engine_db_mockserver_test.xml"); + ApplicationContext applicationContext = new ClassPathXmlApplicationContext( + "classpath:saga/spring/statemachine_engine_db_mockserver_test.xml"); stateMachineEngine = applicationContext.getBean("stateMachineEngine", StateMachineEngine.class); } @@ -53,7 +54,7 @@ public void testSimpleStateMachine() { @Test public void testSimpleStateMachineWithChoice() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -71,7 +72,7 @@ public void testSimpleStateMachineWithChoice() { Assertions.assertNotNull(inst); Assertions.assertTrue(ExecutionStatus.SU.equals(inst.getStatus())); - start = System.currentTimeMillis(); + start = System.currentTimeMillis(); paramMap.put("a", 2); inst = stateMachineEngine.start(stateMachineName, null, paramMap); @@ -84,7 +85,7 @@ public void testSimpleStateMachineWithChoice() { @Test public void testSimpleStateMachineWithChoiceAndEnd() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -96,7 +97,7 @@ public void testSimpleStateMachineWithChoiceAndEnd() { long cost = System.currentTimeMillis() - start; System.out.println("====== cost :" + cost); - start = System.currentTimeMillis(); + start = System.currentTimeMillis(); paramMap.put("a", 3); stateMachineEngine.start(stateMachineName, null, paramMap); @@ -108,7 +109,7 @@ public void testSimpleStateMachineWithChoiceAndEnd() { @Test public void testSimpleInputAssignmentStateMachine() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -121,7 +122,8 @@ public void testSimpleInputAssignmentStateMachine() { Assertions.assertNotNull(businessKey); System.out.println("====== businessKey :" + businessKey); - String contextBusinessKey = (String)instance.getEndParams().get(instance.getStateList().get(0).getName()+ DomainConstants.VAR_NAME_BUSINESSKEY); + String contextBusinessKey = (String) instance.getEndParams().get( + instance.getStateList().get(0).getName() + DomainConstants.VAR_NAME_BUSINESSKEY); Assertions.assertNotNull(contextBusinessKey); System.out.println("====== context businessKey :" + businessKey); @@ -132,7 +134,7 @@ public void testSimpleInputAssignmentStateMachine() { @Test public void testSimpleCatchesStateMachine() throws Exception { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -152,7 +154,7 @@ public void testSimpleCatchesStateMachine() throws Exception { @Test public void testStatusMatchingStateMachine() throws Exception { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -169,11 +171,10 @@ public void testStatusMatchingStateMachine() throws Exception { Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus())); } - @Test public void testCompensationStateMachine() throws Exception { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -193,7 +194,7 @@ public void testCompensationStateMachine() throws Exception { @Test public void testSubStateMachine() throws Exception { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 2); @@ -208,7 +209,36 @@ public void testSubStateMachine() throws Exception { Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus())); - start = System.currentTimeMillis(); + start = System.currentTimeMillis(); + + paramMap.put("barThrowException", "false"); + inst = stateMachineEngine.forward(inst.getId(), paramMap); + + cost = System.currentTimeMillis() - start; + System.out.println("====== XID: " + inst.getId() + " cost :" + cost); + + Assertions.assertTrue(ExecutionStatus.SU.equals(inst.getStatus())); + } + + @Test + public void testSubStateMachineWithLayout() throws Exception { + + long start = System.currentTimeMillis(); + + Map paramMap = new HashMap<>(1); + paramMap.put("a", 2); + paramMap.put("barThrowException", "true"); + + String stateMachineName = "simpleStateMachineWithCompensationAndSubMachine_layout"; + + StateMachineInstance inst = stateMachineEngine.start(stateMachineName, null, paramMap); + + long cost = System.currentTimeMillis() - start; + System.out.println("====== XID: " + inst.getId() + " cost :" + cost); + + Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus())); + + start = System.currentTimeMillis(); paramMap.put("barThrowException", "false"); inst = stateMachineEngine.forward(inst.getId(), paramMap); @@ -222,7 +252,7 @@ public void testSubStateMachine() throws Exception { @Test public void testForwardSubStateMachine() throws Exception { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 2); @@ -237,7 +267,36 @@ public void testForwardSubStateMachine() throws Exception { Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus())); - start = System.currentTimeMillis(); + start = System.currentTimeMillis(); + + paramMap.put("fooThrowException", "false"); + inst = stateMachineEngine.forward(inst.getId(), paramMap); + + cost = System.currentTimeMillis() - start; + System.out.println("====== XID: " + inst.getId() + " cost :" + cost); + + Assertions.assertTrue(ExecutionStatus.SU.equals(inst.getStatus())); + } + + @Test + public void testForwardSubStateMachineWithLayout() throws Exception { + + long start = System.currentTimeMillis(); + + Map paramMap = new HashMap<>(1); + paramMap.put("a", 2); + paramMap.put("fooThrowException", "true"); + + String stateMachineName = "simpleStateMachineWithCompensationAndSubMachine_layout"; + + StateMachineInstance inst = stateMachineEngine.start(stateMachineName, null, paramMap); + + long cost = System.currentTimeMillis() - start; + System.out.println("====== XID: " + inst.getId() + " cost :" + cost); + + Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus())); + + start = System.currentTimeMillis(); paramMap.put("fooThrowException", "false"); inst = stateMachineEngine.forward(inst.getId(), paramMap); @@ -251,7 +310,7 @@ public void testForwardSubStateMachine() throws Exception { @Test public void testCompensateSubStateMachine() throws Exception { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 2); @@ -267,7 +326,36 @@ public void testCompensateSubStateMachine() throws Exception { Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus())); - start = System.currentTimeMillis(); + start = System.currentTimeMillis(); + + inst = stateMachineEngine.compensate(inst.getId(), paramMap); + + cost = System.currentTimeMillis() - start; + System.out.println("====== XID: " + inst.getId() + " cost :" + cost); + + Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getCompensationStatus())); + } + + @Test + public void testCompensateSubStateMachineWithLayout() throws Exception { + + long start = System.currentTimeMillis(); + + Map paramMap = new HashMap<>(1); + paramMap.put("a", 2); + paramMap.put("barThrowException", "true"); + paramMap.put("compensateFooThrowException", "true"); + + String stateMachineName = "simpleStateMachineWithCompensationAndSubMachine_layout"; + + StateMachineInstance inst = stateMachineEngine.start(stateMachineName, null, paramMap); + + long cost = System.currentTimeMillis() - start; + System.out.println("====== XID: " + inst.getId() + " cost :" + cost); + + Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus())); + + start = System.currentTimeMillis(); inst = stateMachineEngine.compensate(inst.getId(), paramMap); @@ -280,7 +368,7 @@ public void testCompensateSubStateMachine() throws Exception { @Test public void testUserDefCompensateSubStateMachine() throws Exception { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 2); @@ -296,7 +384,7 @@ public void testUserDefCompensateSubStateMachine() throws Exception { Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus())); - start = System.currentTimeMillis(); + start = System.currentTimeMillis(); paramMap.put("compensateFooThrowException", "false"); inst = stateMachineEngine.compensate(inst.getId(), paramMap); @@ -310,7 +398,7 @@ public void testUserDefCompensateSubStateMachine() throws Exception { @Test public void testCommitRetryingThenRetryCommitted() throws Exception { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -321,19 +409,18 @@ public void testCommitRetryingThenRetryCommitted() throws Exception { StateMachineInstance inst = stateMachineEngine.start(stateMachineName, null, paramMap); long cost = System.currentTimeMillis() - start; - System.out.println("====== XID: "+ inst.getId() +" cost :" + cost); + System.out.println("====== XID: " + inst.getId() + " cost :" + cost); Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus())); - paramMap.put("fooThrowException", "false"); - start = System.currentTimeMillis(); + start = System.currentTimeMillis(); inst = stateMachineEngine.forward(inst.getId(), paramMap); cost = System.currentTimeMillis() - start; - System.out.println("====== XID: "+ inst.getId() +" cost :" + cost); + System.out.println("====== XID: " + inst.getId() + " cost :" + cost); Assertions.assertTrue(ExecutionStatus.SU.equals(inst.getStatus())); } @@ -341,7 +428,7 @@ public void testCommitRetryingThenRetryCommitted() throws Exception { @Test public void testCommitRetryingThenRetryRollbacked() throws Exception { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -352,20 +439,19 @@ public void testCommitRetryingThenRetryRollbacked() throws Exception { StateMachineInstance inst = stateMachineEngine.start(stateMachineName, null, paramMap); long cost = System.currentTimeMillis() - start; - System.out.println("====== XID: "+ inst.getId() +" cost :" + cost); + System.out.println("====== XID: " + inst.getId() + " cost :" + cost); Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus())); - paramMap.put("fooThrowException", "false"); paramMap.put("barThrowException", "true"); - start = System.currentTimeMillis(); + start = System.currentTimeMillis(); inst = stateMachineEngine.forward(inst.getId(), paramMap); cost = System.currentTimeMillis() - start; - System.out.println("====== XID: "+ inst.getId() +" cost :" + cost); + System.out.println("====== XID: " + inst.getId() + " cost :" + cost); Assertions.assertTrue(ExecutionStatus.SU.equals(inst.getCompensationStatus())); } @@ -373,7 +459,7 @@ public void testCommitRetryingThenRetryRollbacked() throws Exception { @Test public void testRollbackRetryingThenRetryRollbacked() throws Exception { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -385,21 +471,20 @@ public void testRollbackRetryingThenRetryRollbacked() throws Exception { StateMachineInstance inst = stateMachineEngine.start(stateMachineName, null, paramMap); long cost = System.currentTimeMillis() - start; - System.out.println("====== XID: "+ inst.getId() +" cost :" + cost); + System.out.println("====== XID: " + inst.getId() + " cost :" + cost); Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus())); Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getCompensationStatus())); - paramMap.put("barThrowException", "false"); paramMap.put("compensateFooThrowException", "false"); - start = System.currentTimeMillis(); + start = System.currentTimeMillis(); inst = stateMachineEngine.compensate(inst.getId(), paramMap); cost = System.currentTimeMillis() - start; - System.out.println("====== XID: "+ inst.getId() +" cost :" + cost); + System.out.println("====== XID: " + inst.getId() + " cost :" + cost); Assertions.assertTrue(ExecutionStatus.SU.equals(inst.getCompensationStatus())); } @@ -407,7 +492,7 @@ public void testRollbackRetryingThenRetryRollbacked() throws Exception { @Test public void testRollbackRetryingTwiceThenRetryRollbacked() throws Exception { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -419,17 +504,17 @@ public void testRollbackRetryingTwiceThenRetryRollbacked() throws Exception { StateMachineInstance inst = stateMachineEngine.start(stateMachineName, null, paramMap); long cost = System.currentTimeMillis() - start; - System.out.println("====== XID: "+ inst.getId() +" cost :" + cost); + System.out.println("====== XID: " + inst.getId() + " cost :" + cost); Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus())); Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getCompensationStatus())); - start = System.currentTimeMillis(); + start = System.currentTimeMillis(); inst = stateMachineEngine.compensate(inst.getId(), paramMap); cost = System.currentTimeMillis() - start; - System.out.println("====== XID: "+ inst.getId() +" cost :" + cost); + System.out.println("====== XID: " + inst.getId() + " cost :" + cost); Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getStatus())); Assertions.assertTrue(ExecutionStatus.UN.equals(inst.getCompensationStatus())); @@ -437,12 +522,12 @@ public void testRollbackRetryingTwiceThenRetryRollbacked() throws Exception { paramMap.put("barThrowException", "false"); paramMap.put("compensateFooThrowException", "false"); - start = System.currentTimeMillis(); + start = System.currentTimeMillis(); inst = stateMachineEngine.compensate(inst.getId(), paramMap); cost = System.currentTimeMillis() - start; - System.out.println("====== XID: "+ inst.getId() +" cost :" + cost); + System.out.println("====== XID: " + inst.getId() + " cost :" + cost); Assertions.assertTrue(ExecutionStatus.SU.equals(inst.getCompensationStatus())); } @@ -450,7 +535,7 @@ public void testRollbackRetryingTwiceThenRetryRollbacked() throws Exception { @Test public void testStateMachineWithComplextParams() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); People people = new People(); @@ -467,12 +552,12 @@ public void testStateMachineWithComplextParams() { StateMachineInstance instance = stateMachineEngine.start(stateMachineName, null, paramMap); - People peopleResult = (People)instance.getEndParams().get("complexParameterMethodResult"); + People peopleResult = (People) instance.getEndParams().get("complexParameterMethodResult"); Assertions.assertNotNull(peopleResult); Assertions.assertTrue(people.getName().equals(people.getName())); long cost = System.currentTimeMillis() - start; - System.out.println("====== XID: "+ instance.getId() +" cost :" + cost); + System.out.println("====== XID: " + instance.getId() + " cost :" + cost); Assertions.assertTrue(ExecutionStatus.SU.equals(instance.getStatus())); } @@ -480,7 +565,7 @@ public void testStateMachineWithComplextParams() { @Test public void testSimpleStateMachineWithAsyncState() { - long start = System.currentTimeMillis(); + long start = System.currentTimeMillis(); Map paramMap = new HashMap<>(1); paramMap.put("a", 1); @@ -502,8 +587,9 @@ public void testSimpleStateMachineWithAsyncState() { } @Test - public void testReloadStateMachineInstance(){ - StateMachineInstance instance = stateMachineEngine.getStateMachineConfig().getStateLogStore().getStateMachineInstance("10.15.232.93:8091:2019567124"); + public void testReloadStateMachineInstance() { + StateMachineInstance instance = stateMachineEngine.getStateMachineConfig().getStateLogStore().getStateMachineInstance( + "10.15.232.93:8091:2019567124"); System.out.println(instance); } } \ No newline at end of file diff --git a/test/src/test/resources/saga/statelang/designer_statelang_with_compensation_and_sub_machine.json b/test/src/test/resources/saga/statelang/designer_statelang_with_compensation_and_sub_machine.json new file mode 100644 index 00000000000..4d886a02c36 --- /dev/null +++ b/test/src/test/resources/saga/statelang/designer_statelang_with_compensation_and_sub_machine.json @@ -0,0 +1,288 @@ +{ + "nodes": [ + { + "type": "node", + "size": "72*72", + "shape": "flow-circle", + "color": "#FA8C16", + "label": "Start", + "stateId": "Start", + "stateType": "Start", + "stateProps": { + "StateMachine": { + "Name": "simpleStateMachineWithCompensationAndSubMachine_layout", + "Comment": "带补偿定义和调用子状态机", + "Version": "0.0.1" + } + }, + "x": 199.875, + "y": 95, + "id": "e2d86441" + }, + { + "type": "node", + "size": "110*48", + "shape": "flow-rect", + "color": "#1890FF", + "label": "FirstState", + "stateId": "FirstState", + "stateType": "ServiceTask", + "stateProps": { + "ServiceName": "demoService", + "ServiceMethod": "foo", + "Input": [ + { + "fooInput": "$.[a]" + } + ], + "Output": { + "fooResult": "$.#root" + } + }, + "x": 199.875, + "y": 213, + "id": "6111bf54" + }, + { + "type": "node", + "size": "80*72", + "shape": "flow-rhombus", + "color": "#13C2C2", + "label": "ChoiceState", + "stateId": "ChoiceState", + "stateType": "Choice", + "x": 199.875, + "y": 341.5, + "id": "5610fa37" + }, + { + "type": "node", + "size": "110*48", + "shape": "flow-rect", + "color": "#1890FF", + "label": "SecondState", + "stateId": "SecondState", + "stateType": "ServiceTask", + "stateProps": { + "ServiceName": "demoService", + "ServiceMethod": "bar", + "Input": [ + { + "barInput": "$.[fooResult]", + "throwException": "$.[barThrowException]" + } + ], + "Output": { + "barResult": "$.#root" + }, + "Status": { + "#root != null": "SU", + "#root == null": "FA", + "$Exception{io.seata.saga.engine.exception.EngineExecutionException}": "UN" + } + }, + "x": 199.375, + "y": 468, + "id": "af5591f9" + }, + { + "type": "node", + "size": "72*72", + "shape": "flow-circle", + "color": "#05A465", + "label": "Succeed", + "stateId": "Succeed", + "stateType": "Succeed", + "x": 199.375, + "y": 609, + "id": "2fd4c8de" + }, + { + "type": "node", + "size": "110*48", + "shape": "flow-rect", + "color": "#FA8C16", + "label": "SubStateMachine", + "stateId": "CallSubStateMachine", + "stateType": "SubStateMachine", + "stateProps": { + "StateMachineName": "simpleCompensationStateMachine", + "Input": [ + { + "a": "$.1", + "barThrowException": "$.[barThrowException]", + "fooThrowException": "$.[fooThrowException]", + "compensateFooThrowException": "$.[compensateFooThrowException]" + } + ], + "Output": { + "fooResult": "$.#root" + } + }, + "x": 55.875, + "y": 467, + "id": "04ea55a5" + }, + { + "type": "node", + "size": "110*48", + "shape": "flow-capsule", + "color": "#722ED1", + "label": "CompenFirstState", + "stateId": "CompensateFirstState", + "stateType": "Compensation", + "stateProps": { + "ServiceName": "demoService", + "ServiceMethod": "compensateFoo", + "Input": [ + { + "compensateFooInput": "$.[fooResult]" + } + ] + }, + "x": 68.875, + "y": 126, + "id": "6a09a5c2" + }, + { + "type": "node", + "size": "39*39", + "shape": "flow-circle", + "color": "red", + "label": "Catch", + "stateId": "Catch", + "stateType": "Catch", + "x": 257.875, + "y": 492, + "id": "e28af1c2" + }, + { + "type": "node", + "size": "110*48", + "shape": "flow-capsule", + "color": "red", + "label": "Compensation\nTrigger", + "stateId": "CompensationTrigger", + "stateType": "CompensationTrigger", + "x": 366.875, + "y": 491.5, + "id": "e32417a0" + }, + { + "type": "node", + "size": "72*72", + "shape": "flow-circle", + "color": "red", + "label": "Fail", + "stateId": "Fail", + "stateType": "Fail", + "stateProps": { + "ErrorCode": "NOT_FOUND", + "Message": "not found" + }, + "x": 513.375, + "y": 491.5, + "id": "d21d24c9" + } + ], + "edges": [ + { + "source": "e2d86441", + "sourceAnchor": 2, + "target": "6111bf54", + "targetAnchor": 0, + "id": "51f30b96" + }, + { + "source": "6111bf54", + "sourceAnchor": 2, + "target": "5610fa37", + "targetAnchor": 0, + "id": "8c3029b1" + }, + { + "source": "5610fa37", + "sourceAnchor": 2, + "target": "af5591f9", + "targetAnchor": 0, + "id": "a9e7d5b4", + "stateProps": { + "Expression": "[a] == 1", + "Default": false + }, + "label": "", + "shape": "flow-smooth" + }, + { + "source": "af5591f9", + "sourceAnchor": 2, + "target": "2fd4c8de", + "targetAnchor": 0, + "id": "61f34a49" + }, + { + "source": "6111bf54", + "sourceAnchor": 3, + "target": "6a09a5c2", + "targetAnchor": 2, + "id": "553384ab", + "style": { + "lineDash": "4" + } + }, + { + "source": "5610fa37", + "sourceAnchor": 3, + "target": "04ea55a5", + "targetAnchor": 0, + "id": "2ee91c33", + "stateProps": { + "Expression": "[a] == 2", + "Default": false + }, + "label": "", + "shape": "flow-smooth" + }, + { + "source": "e28af1c2", + "sourceAnchor": 1, + "target": "e32417a0", + "targetAnchor": 3, + "id": "d854a4d0", + "stateProps": { + "Exceptions": [ + "io.seata.common.exception.FrameworkException" + ] + }, + "label": "", + "shape": "flow-smooth" + }, + { + "source": "04ea55a5", + "sourceAnchor": 2, + "target": "2fd4c8de", + "targetAnchor": 3, + "id": "28734ad2" + }, + { + "source": "5610fa37", + "sourceAnchor": 1, + "target": "d21d24c9", + "targetAnchor": 0, + "id": "7c7595c0", + "stateProps": { + "Expression": "", + "Default": true + }, + "label": "", + "shape": "flow-smooth" + }, + { + "source": "e32417a0", + "sourceAnchor": 1, + "target": "d21d24c9", + "targetAnchor": 3, + "id": "16d809ce" + } + ] +} \ No newline at end of file diff --git a/test/src/test/resources/saga/statelang/simple_statelang_with_compensation_and_sub_machine.json b/test/src/test/resources/saga/statelang/simple_statelang_with_compensation_and_sub_machine.json index 2d3cbdaf72d..24fd959d037 100644 --- a/test/src/test/resources/saga/statelang/simple_statelang_with_compensation_and_sub_machine.json +++ b/test/src/test/resources/saga/statelang/simple_statelang_with_compensation_and_sub_machine.json @@ -37,7 +37,6 @@ "Type": "ServiceTask", "ServiceName": "demoService", "ServiceMethod": "bar", - "CompensateState": "CompensateSecondState", "Input": [ { "barInput": "$.[fooResult]", From e2f02c4ce69ea96b829a8f12c7bf37537d31efbc Mon Sep 17 00:00:00 2001 From: "lorne.cl" Date: Fri, 29 Nov 2019 14:31:46 +0800 Subject: [PATCH 2/2] fix checkstyle promblem --- .../parser/impl/StateMachineParserImpl.java | 22 +++++++++---------- .../parser/utils/DesignerJsonTransformer.java | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/saga/seata-saga-statelang/src/main/java/io/seata/saga/statelang/parser/impl/StateMachineParserImpl.java b/saga/seata-saga-statelang/src/main/java/io/seata/saga/statelang/parser/impl/StateMachineParserImpl.java index 8798b3123f9..4c66a29ea0e 100644 --- a/saga/seata-saga-statelang/src/main/java/io/seata/saga/statelang/parser/impl/StateMachineParserImpl.java +++ b/saga/seata-saga-statelang/src/main/java/io/seata/saga/statelang/parser/impl/StateMachineParserImpl.java @@ -47,31 +47,31 @@ public class StateMachineParserImpl implements StateMachineParser { public StateMachine parse(String json) { Map node = JSON.parseObject(json, Map.class, Feature.IgnoreAutoType, Feature.OrderedField); - if(DesignerJsonTransformer.isDesignerJson(node)){ + if (DesignerJsonTransformer.isDesignerJson(node)) { node = DesignerJsonTransformer.toStandardJson(node); LOGGER.info("===== Transformed standard state language:\n{}", JSON.toJSONString(node, SerializerFeature.PrettyFormat)); } StateMachineImpl stateMachine = new StateMachineImpl(); - stateMachine.setName((String)node.get("Name")); - stateMachine.setComment((String)node.get("Comment")); - stateMachine.setVersion((String)node.get("Version")); - stateMachine.setStartState((String)node.get("StartState")); + stateMachine.setName((String) node.get("Name")); + stateMachine.setComment((String) node.get("Comment")); + stateMachine.setVersion((String) node.get("Version")); + stateMachine.setStartState((String) node.get("StartState")); Object isPersist = node.get("IsPersist"); if (Boolean.FALSE.equals(isPersist)) { stateMachine.setPersist(false); } - Map statesNode = (Map)node.get("States"); + Map statesNode = (Map) node.get("States"); for (String stateName : statesNode.keySet()) { - Map stateNode = (Map)statesNode.get(stateName); - String stateType = (String)stateNode.get("Type"); + Map stateNode = (Map) statesNode.get(stateName); + String stateType = (String) stateNode.get("Type"); StateParser stateParser = StateParserFactory.getStateParser(stateType); if (stateParser == null) { throw new IllegalArgumentException("State Type [" + stateType + "] is not support"); } State state = stateParser.parse(stateNode); if (state instanceof BaseState) { - ((BaseState)state).setName(stateName); + ((BaseState) state).setName(stateName); } if (stateMachine.getState(stateName) != null) { @@ -84,13 +84,13 @@ public StateMachine parse(String json) { for (String name : stateMap.keySet()) { State state = stateMap.get(name); if (state instanceof AbstractTaskState) { - AbstractTaskState taskState = (AbstractTaskState)state; + AbstractTaskState taskState = (AbstractTaskState) state; if (StringUtils.isNotBlank(taskState.getCompensateState())) { taskState.setForUpdate(true); State compState = stateMap.get(taskState.getCompensateState()); if (compState != null && compState instanceof AbstractTaskState) { - ((AbstractTaskState)compState).setForCompensation(true); + ((AbstractTaskState) compState).setForCompensation(true); } } } diff --git a/saga/seata-saga-statelang/src/main/java/io/seata/saga/statelang/parser/utils/DesignerJsonTransformer.java b/saga/seata-saga-statelang/src/main/java/io/seata/saga/statelang/parser/utils/DesignerJsonTransformer.java index f3573baff7d..a1e71066391 100644 --- a/saga/seata-saga-statelang/src/main/java/io/seata/saga/statelang/parser/utils/DesignerJsonTransformer.java +++ b/saga/seata-saga-statelang/src/main/java/io/seata/saga/statelang/parser/utils/DesignerJsonTransformer.java @@ -174,7 +174,7 @@ private static void transformEdge(JSONObject machineJsonObject, List nod } public static boolean isDesignerJson(Map jsonObject) { - return (jsonObject != null && jsonObject.containsKey("nodes") && jsonObject.containsKey("edges")); + return jsonObject != null && jsonObject.containsKey("nodes") && jsonObject.containsKey("edges"); } private static JSONObject getCatchAttachedNode(JSONObject catchNode, List nodes) {