From 14d49bc584685a33e2952573a0cb6e88377ab9f9 Mon Sep 17 00:00:00 2001 From: zt-1997 <41983395+zt-1997@users.noreply.github.com> Date: Tue, 2 Feb 2021 16:07:37 +0800 Subject: [PATCH] [FIX-2101]It solves the problem that the name of the branch will not change after the name of the branch is changed in the workflow page and instance page. (#4555) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: zt-1997 <“18841012545@163.com”> --- .../api/service/ProcessInstanceService.java | 10 +- .../impl/ProcessDefinitionServiceImpl.java | 4 +- .../task/conditions/ConditionsParameters.java | 10 +- .../service/process/ProcessService.java | 58 ++++++++++ .../service/process/ProcessServiceTest.java | 107 ++++++++++++++++++ 5 files changed, 184 insertions(+), 5 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProcessInstanceService.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProcessInstanceService.java index 1d6169b06e..b41ff71f5d 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProcessInstanceService.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/ProcessInstanceService.java @@ -428,10 +428,9 @@ public class ProcessInstanceService extends BaseService { return result; } Date schedule = null; + schedule = processInstance.getScheduleTime(); if (scheduleTime != null) { schedule = DateUtils.getScheduleDate(scheduleTime); - } else { - schedule = processInstance.getScheduleTime(); } processInstance.setScheduleTime(schedule); processInstance.setLocations(locations); @@ -460,13 +459,18 @@ public class ProcessInstanceService extends BaseService { if (tenant != null) { processInstance.setTenantCode(tenant.getTenantCode()); } + // get the processinstancejson before saving,and then save the name and taskid + String oldJson = processInstance.getProcessInstanceJson(); + if (StringUtils.isNotEmpty(oldJson)) { + processInstanceJson = processService.changeJson(processData,oldJson); + } processInstance.setProcessInstanceJson(processInstanceJson); processInstance.setGlobalParams(globalParams); } int update = processService.updateProcessInstance(processInstance); int updateDefine = 1; - if (Boolean.TRUE.equals(syncDefine) && StringUtils.isNotEmpty(processInstanceJson)) { + if (Boolean.TRUE.equals(syncDefine)) { processDefinition.setProcessDefinitionJson(processInstanceJson); processDefinition.setGlobalParams(originDefParams); processDefinition.setLocations(locations); diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProcessDefinitionServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProcessDefinitionServiceImpl.java index 9810674863..123e2a3a91 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProcessDefinitionServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProcessDefinitionServiceImpl.java @@ -433,7 +433,9 @@ public class ProcessDefinitionServiceImpl extends BaseService implements return result; } } - + // get the processdefinitionjson before saving,and then save the name and taskid + String oldJson = processDefine.getProcessDefinitionJson(); + processDefinitionJson = processService.changeJson(processData,oldJson); Date now = new Date(); processDefine.setId(id); diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/task/conditions/ConditionsParameters.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/task/conditions/ConditionsParameters.java index 094620aee5..b136ae295d 100644 --- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/task/conditions/ConditionsParameters.java +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/task/conditions/ConditionsParameters.java @@ -14,6 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package org.apache.dolphinscheduler.common.task.conditions; import org.apache.dolphinscheduler.common.enums.DependentRelation; @@ -36,7 +37,6 @@ public class ConditionsParameters extends AbstractParameters { // node list to run when failed private List failedNode; - @Override public boolean checkParameters() { return true; @@ -78,4 +78,12 @@ public class ConditionsParameters extends AbstractParameters { public void setFailedNode(List failedNode) { this.failedNode = failedNode; } + + public String getConditionResult() { + return "{" + + "\"successNode\": [\"" + successNode.get(0) + + "\"],\"failedNode\": [\"" + failedNode.get(0) + + "\"]}"; + } + } diff --git a/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/process/ProcessService.java b/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/process/ProcessService.java index 7c305097ff..b9065eca3e 100644 --- a/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/process/ProcessService.java +++ b/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/process/ProcessService.java @@ -37,10 +37,12 @@ import org.apache.dolphinscheduler.common.enums.FailureStrategy; import org.apache.dolphinscheduler.common.enums.Flag; import org.apache.dolphinscheduler.common.enums.ResourceType; import org.apache.dolphinscheduler.common.enums.TaskDependType; +import org.apache.dolphinscheduler.common.enums.TaskType; import org.apache.dolphinscheduler.common.enums.WarningType; import org.apache.dolphinscheduler.common.model.DateInterval; import org.apache.dolphinscheduler.common.model.TaskNode; import org.apache.dolphinscheduler.common.process.Property; +import org.apache.dolphinscheduler.common.task.conditions.ConditionsParameters; import org.apache.dolphinscheduler.common.task.subprocess.SubProcessParameters; import org.apache.dolphinscheduler.common.utils.CollectionUtils; import org.apache.dolphinscheduler.common.utils.DateUtils; @@ -1997,4 +1999,60 @@ public class ProcessService { taskInstance.getId()); } + /** + * solve the branch rename bug + * + * @param processData + * @param oldJson + * @return String + */ + public String changeJson(ProcessData processData, String oldJson) { + ProcessData oldProcessData = JSONUtils.parseObject(oldJson, ProcessData.class); + HashMap oldNameTaskId = new HashMap<>(); + List oldTasks = oldProcessData.getTasks(); + for (int i = 0; i < oldTasks.size(); i++) { + TaskNode taskNode = oldTasks.get(i); + String oldName = taskNode.getName(); + String oldId = taskNode.getId(); + oldNameTaskId.put(oldName, oldId); + } + + // take the processdefinitionjson saved this time, and then save the taskid and name + HashMap newNameTaskId = new HashMap<>(); + List newTasks = processData.getTasks(); + for (int i = 0; i < newTasks.size(); i++) { + TaskNode taskNode = newTasks.get(i); + String newId = taskNode.getId(); + String newName = taskNode.getName(); + newNameTaskId.put(newId, newName); + } + + // replace the previous conditionresult with a new one + List tasks = processData.getTasks(); + for (int i = 0; i < tasks.size(); i++) { + TaskNode taskNode = newTasks.get(i); + String type = taskNode.getType(); + if (TaskType.CONDITIONS.getDescp().equalsIgnoreCase(type)) { + ConditionsParameters conditionsParameters = JSONUtils.parseObject(taskNode.getConditionResult(), ConditionsParameters.class); + String oldSuccessNodeName = conditionsParameters.getSuccessNode().get(0); + String oldFailedNodeName = conditionsParameters.getFailedNode().get(0); + String newSuccessNodeName = newNameTaskId.get(oldNameTaskId.get(oldSuccessNodeName)); + String newFailedNodeName = newNameTaskId.get(oldNameTaskId.get(oldFailedNodeName)); + if (newSuccessNodeName != null) { + ArrayList successNode = new ArrayList<>(); + successNode.add(newSuccessNodeName); + conditionsParameters.setSuccessNode(successNode); + } + if (newFailedNodeName != null) { + ArrayList failedNode = new ArrayList<>(); + failedNode.add(newFailedNodeName); + conditionsParameters.setFailedNode(failedNode); + } + String conditionResultStr = conditionsParameters.getConditionResult(); + taskNode.setConditionResult(conditionResultStr); + tasks.set(i, taskNode); + } + } + return JSONUtils.toJsonString(processData); + } } diff --git a/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/process/ProcessServiceTest.java b/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/process/ProcessServiceTest.java index db83e71725..551c9bb09c 100644 --- a/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/process/ProcessServiceTest.java +++ b/dolphinscheduler-service/src/test/java/org/apache/dolphinscheduler/service/process/ProcessServiceTest.java @@ -25,9 +25,12 @@ import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.enums.CommandType; import org.apache.dolphinscheduler.common.enums.Flag; import org.apache.dolphinscheduler.common.enums.WarningType; +import org.apache.dolphinscheduler.common.model.TaskNode; +import org.apache.dolphinscheduler.common.task.conditions.ConditionsParameters; import org.apache.dolphinscheduler.common.utils.DateUtils; import org.apache.dolphinscheduler.common.utils.JSONUtils; import org.apache.dolphinscheduler.dao.entity.Command; +import org.apache.dolphinscheduler.dao.entity.ProcessData; import org.apache.dolphinscheduler.dao.entity.ProcessDefinition; import org.apache.dolphinscheduler.dao.entity.ProcessInstance; import org.apache.dolphinscheduler.dao.entity.ProcessInstanceMap; @@ -335,4 +338,108 @@ public class ProcessServiceTest { processService.recurseFindSubProcessId(parentId, ids); } + + @Test + public void testChangeJson() { + + ProcessData oldProcessData = new ProcessData(); + ConditionsParameters conditionsParameters = new ConditionsParameters(); + ArrayList tasks = new ArrayList<>(); + TaskNode taskNode = new TaskNode(); + TaskNode taskNode11 = new TaskNode(); + TaskNode taskNode111 = new TaskNode(); + ArrayList successNode = new ArrayList<>(); + ArrayList faildNode = new ArrayList<>(); + + taskNode.setName("bbb"); + taskNode.setType("SHELL"); + taskNode.setId("222"); + + taskNode11.setName("vvv"); + taskNode11.setType("CONDITIONS"); + taskNode11.setId("444"); + successNode.add("bbb"); + faildNode.add("ccc"); + + taskNode111.setName("ccc"); + taskNode111.setType("SHELL"); + taskNode111.setId("333"); + + conditionsParameters.setSuccessNode(successNode); + conditionsParameters.setFailedNode(faildNode); + taskNode11.setConditionResult(conditionsParameters.getConditionResult()); + tasks.add(taskNode); + tasks.add(taskNode11); + tasks.add(taskNode111); + oldProcessData.setTasks(tasks); + + ProcessData newProcessData = new ProcessData(); + ConditionsParameters conditionsParameters2 = new ConditionsParameters(); + TaskNode taskNode2 = new TaskNode(); + TaskNode taskNode22 = new TaskNode(); + TaskNode taskNode222 = new TaskNode(); + ArrayList tasks2 = new ArrayList<>(); + ArrayList successNode2 = new ArrayList<>(); + ArrayList faildNode2 = new ArrayList<>(); + + taskNode2.setName("bbbchange"); + taskNode2.setType("SHELL"); + taskNode2.setId("222"); + + taskNode22.setName("vv"); + taskNode22.setType("CONDITIONS"); + taskNode22.setId("444"); + successNode2.add("bbb"); + faildNode2.add("ccc"); + + taskNode222.setName("ccc"); + taskNode222.setType("SHELL"); + taskNode222.setId("333"); + + conditionsParameters2.setSuccessNode(successNode2); + conditionsParameters2.setFailedNode(faildNode2); + taskNode22.setConditionResult(conditionsParameters2.getConditionResult()); + tasks2.add(taskNode2); + tasks2.add(taskNode22); + tasks2.add(taskNode222); + + newProcessData.setTasks(tasks2); + + ProcessData exceptProcessData = new ProcessData(); + ConditionsParameters conditionsParameters3 = new ConditionsParameters(); + TaskNode taskNode3 = new TaskNode(); + TaskNode taskNode33 = new TaskNode(); + TaskNode taskNode333 = new TaskNode(); + ArrayList tasks3 = new ArrayList<>(); + ArrayList successNode3 = new ArrayList<>(); + ArrayList faildNode3 = new ArrayList<>(); + + taskNode3.setName("bbbchange"); + taskNode3.setType("SHELL"); + taskNode3.setId("222"); + + taskNode33.setName("vv"); + taskNode33.setType("CONDITIONS"); + taskNode33.setId("444"); + successNode3.add("bbbchange"); + faildNode3.add("ccc"); + + taskNode333.setName("ccc"); + taskNode333.setType("SHELL"); + taskNode333.setId("333"); + + conditionsParameters3.setSuccessNode(successNode3); + conditionsParameters3.setFailedNode(faildNode3); + taskNode33.setConditionResult(conditionsParameters3.getConditionResult()); + tasks3.add(taskNode3); + tasks3.add(taskNode33); + tasks3.add(taskNode333); + exceptProcessData.setTasks(tasks3); + + String expect = JSONUtils.toJsonString(exceptProcessData); + String oldJson = JSONUtils.toJsonString(oldProcessData); + + Assert.assertEquals(expect, processService.changeJson(newProcessData,oldJson)); + + } }