Radeity commented on code in PR #14366:
URL: 
https://github.com/apache/dolphinscheduler/pull/14366#discussion_r1232018561


##########
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/TaskDefinitionServiceImpl.java:
##########
@@ -814,153 +823,143 @@ private TaskDefinitionLog updateTask(User loginUser, 
long projectCode, long task
      * @return update result code
      */
     @Override
+    @Transactional
     public Map<String, Object> updateTaskWithUpstream(User loginUser, long 
projectCode, long taskCode,
                                                       String 
taskDefinitionJsonObj, String upstreamCodes) {
+        // Update the version of the task definition
         Map<String, Object> result = new HashMap<>();
         TaskDefinitionLog taskDefinitionToUpdate =
                 updateTask(loginUser, projectCode, taskCode, 
taskDefinitionJsonObj, result);
-        List<ProcessTaskRelation> upstreamTaskRelations =
-                processTaskRelationMapper.queryUpstreamByCode(projectCode, 
taskCode);
-        Set<Long> upstreamCodeSet =
-                
upstreamTaskRelations.stream().map(ProcessTaskRelation::getPreTaskCode).collect(Collectors.toSet());
-        Set<Long> upstreamTaskCodes = Collections.emptySet();
-        if (StringUtils.isNotEmpty(upstreamCodes)) {
-            upstreamTaskCodes = 
Arrays.stream(upstreamCodes.split(Constants.COMMA)).map(Long::parseLong)
-                    .collect(Collectors.toSet());
-        }
-        if (CollectionUtils.isEqualCollection(upstreamCodeSet, 
upstreamTaskCodes) && taskDefinitionToUpdate == null) {
-            putMsg(result, Status.SUCCESS);
+        if (taskDefinitionToUpdate == null) {
             return result;
         }
-        Map<Long, TaskDefinition> queryUpStreamTaskCodeMap;
-        if (CollectionUtils.isNotEmpty(upstreamTaskCodes)) {
-            List<TaskDefinition> upstreamTaskDefinitionList = 
taskDefinitionMapper.queryByCodeList(upstreamTaskCodes);
-            queryUpStreamTaskCodeMap = upstreamTaskDefinitionList.stream()
-                    .collect(Collectors.toMap(TaskDefinition::getCode, 
taskDefinition -> taskDefinition));
-            // upstreamTaskCodes - queryUpStreamTaskCodeMap.keySet
-            upstreamTaskCodes.removeAll(queryUpStreamTaskCodeMap.keySet());
+
+        // Update the upstream task used by the task definition And Insert the 
process task relation log data.
+        Map<Long, TaskDefinition> queryUpstreamTaskCodeMap = new HashMap<>();
+        if (StringUtils.isNotEmpty(upstreamCodes)) {
+            Set<Long> upstreamTaskCodes = 
Arrays.stream(upstreamCodes.split(Constants.COMMA)).map(Long::parseLong)
+                    .collect(Collectors.toSet());
             if (CollectionUtils.isNotEmpty(upstreamTaskCodes)) {
-                String notExistTaskCodes = StringUtils.join(upstreamTaskCodes, 
Constants.COMMA);
-                log.error("Some task definitions in parameter 
upstreamTaskCodes do not exist, notExistTaskCodes:{}.",
-                        notExistTaskCodes);
-                putMsg(result, Status.TASK_DEFINE_NOT_EXIST, 
notExistTaskCodes);
-                return result;
-            }
-        } else {
-            queryUpStreamTaskCodeMap = new HashMap<>();
-        }
-        if (MapUtils.isNotEmpty(queryUpStreamTaskCodeMap)) {
-            ProcessTaskRelation taskRelation = upstreamTaskRelations.get(0);
-            List<ProcessTaskRelation> processTaskRelations =
-                    processTaskRelationMapper.queryByProcessCode(projectCode, 
taskRelation.getProcessDefinitionCode());
-
-            // set upstream code list
-            updateUpstreamTask(new 
HashSet<>(queryUpStreamTaskCodeMap.keySet()),
-                    taskCode, projectCode, 
taskRelation.getProcessDefinitionCode(), loginUser);
-
-            List<ProcessTaskRelation> processTaskRelationList = 
Lists.newArrayList(processTaskRelations);
-            List<ProcessTaskRelation> relationList = Lists.newArrayList();
-            for (ProcessTaskRelation processTaskRelation : 
processTaskRelationList) {
-                if (processTaskRelation.getPostTaskCode() == taskCode) {
-                    if 
(queryUpStreamTaskCodeMap.containsKey(processTaskRelation.getPreTaskCode())
-                            && processTaskRelation.getPreTaskCode() != 0L) {
-                        
queryUpStreamTaskCodeMap.remove(processTaskRelation.getPreTaskCode());
-                    } else {
-                        processTaskRelation.setPreTaskCode(0L);
-                        processTaskRelation.setPreTaskVersion(0);
-                        relationList.add(processTaskRelation);
-                    }
+                List<TaskDefinition> upstreamTaskDefinitionList =
+                        
taskDefinitionMapper.queryByCodeList(upstreamTaskCodes);
+                queryUpstreamTaskCodeMap = upstreamTaskDefinitionList.stream()
+                        .collect(Collectors.toMap(TaskDefinition::getCode, 
Function.identity(), (v1, v2) -> v2));
+                // upstreamTaskCodes - queryUpStreamTaskCodeMap.keySet
+                upstreamTaskCodes.removeAll(queryUpstreamTaskCodeMap.keySet());
+                if (CollectionUtils.isNotEmpty(upstreamTaskCodes)) {
+                    String notExistTaskCodes = 
StringUtils.join(upstreamTaskCodes, Constants.COMMA);
+                    log.error(
+                            "Some task definitions in parameter 
upstreamTaskCodes do not exist, notExistTaskCodes:{}.",
+                            notExistTaskCodes);
+                    putMsg(result, Status.TASK_DEFINE_NOT_EXIST, 
notExistTaskCodes);
+                    return result;
                 }
             }
-            processTaskRelationList.removeAll(relationList);
-            for (Map.Entry<Long, TaskDefinition> queryUpStreamTask : 
queryUpStreamTaskCodeMap.entrySet()) {
-                taskRelation.setPreTaskCode(queryUpStreamTask.getKey());
-                
taskRelation.setPreTaskVersion(queryUpStreamTask.getValue().getVersion());
-                processTaskRelationList.add(taskRelation);
-            }
-            if (MapUtils.isEmpty(queryUpStreamTaskCodeMap) && 
CollectionUtils.isNotEmpty(processTaskRelationList)) {
-                processTaskRelationList.add(processTaskRelationList.get(0));
-            }
         }
+
+        updateUpstreamTaskAndDag(queryUpstreamTaskCodeMap, 
taskDefinitionToUpdate, projectCode, loginUser);

Review Comment:
   Can we simply add one line to switch version here?
   ```java
   switchVersion(loginUser, projectCode, taskCode, 
taskDefinitionToUpdate.getVersion());
   ```
   Maybe this fix is more simple, and can also decouple the logic of update 
task and update dag.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to