You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dolphinscheduler.apache.org by ca...@apache.org on 2022/08/12 01:37:35 UTC

[dolphinscheduler] branch dev updated: [Improvement] Avoid using search in for and start using testSaveTaskDefine (#11383)

This is an automated email from the ASF dual-hosted git repository.

caishunfeng pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/dolphinscheduler.git


The following commit(s) were added to refs/heads/dev by this push:
     new be66035dee [Improvement] Avoid using search in for and start using testSaveTaskDefine (#11383)
be66035dee is described below

commit be66035dee3600e67196805e940419ac4166134e
Author: longtb <67...@users.noreply.github.com>
AuthorDate: Fri Aug 12 09:37:24 2022 +0800

    [Improvement] Avoid using search in for and start using testSaveTaskDefine (#11383)
    
    * [Improvement] Avoid using search in for and start using testSaveTaskDefine()
    
    * [Improvement] not import *
    
    Co-authored-by: zhangshunmin <zh...@kezaihui.com>
---
 .../service/process/ProcessServiceImpl.java        | 29 ++++++++++++++--------
 .../service/process/ProcessServiceTest.java        |  8 +++++-
 2 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/process/ProcessServiceImpl.java b/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/process/ProcessServiceImpl.java
index a48d783f9b..fc77bc30b3 100644
--- a/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/process/ProcessServiceImpl.java
+++ b/dolphinscheduler-service/src/main/java/org/apache/dolphinscheduler/service/process/ProcessServiceImpl.java
@@ -2559,17 +2559,26 @@ public class ProcessServiceImpl implements ProcessService {
         }
         int insertResult = 0;
         int updateResult = 0;
-        for (TaskDefinitionLog taskDefinitionToUpdate : updateTaskDefinitionLogs) {
-            TaskDefinition task = taskDefinitionMapper.queryByCode(taskDefinitionToUpdate.getCode());
-            if (task == null) {
-                newTaskDefinitionLogs.add(taskDefinitionToUpdate);
-            } else {
-                insertResult += taskDefinitionLogMapper.insert(taskDefinitionToUpdate);
-                if (Boolean.TRUE.equals(syncDefine)) {
-                    taskDefinitionToUpdate.setId(task.getId());
-                    updateResult += taskDefinitionMapper.updateById(taskDefinitionToUpdate);
+        if (!updateTaskDefinitionLogs.isEmpty()) {
+            List<TaskDefinition> taskDefinitions = taskDefinitionMapper.queryByCodeList(updateTaskDefinitionLogs.stream().map(TaskDefinition::getCode).distinct().collect(Collectors.toList()));
+            for (TaskDefinitionLog taskDefinitionToUpdate : updateTaskDefinitionLogs) {
+                TaskDefinition task = null;
+                for (TaskDefinition taskDefinition : taskDefinitions) {
+                    if (taskDefinitionToUpdate.getCode() == taskDefinition.getCode()) {
+                        task = taskDefinition;
+                        break;
+                    }
+                }
+                if (task == null) {
+                    newTaskDefinitionLogs.add(taskDefinitionToUpdate);
                 } else {
-                    updateResult++;
+                    insertResult += taskDefinitionLogMapper.insert(taskDefinitionToUpdate);
+                    if (Boolean.TRUE.equals(syncDefine)) {
+                        taskDefinitionToUpdate.setId(task.getId());
+                        updateResult += taskDefinitionMapper.updateById(taskDefinitionToUpdate);
+                    } else {
+                        updateResult++;
+                    }
                 }
             }
         }
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 71fcf75858..584368243e 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
@@ -81,6 +81,7 @@ import org.apache.dolphinscheduler.service.cron.CronUtilsTest;
 import org.apache.dolphinscheduler.service.exceptions.CronParseException;
 import org.apache.dolphinscheduler.service.exceptions.ServiceException;
 import org.apache.dolphinscheduler.service.expand.CuringParamsService;
+import org.apache.dolphinscheduler.service.task.TaskPluginManager;
 import org.apache.dolphinscheduler.spi.params.base.FormType;
 
 import java.util.ArrayList;
@@ -168,6 +169,9 @@ public class ProcessServiceTest {
     @Mock
     CuringParamsService curingGlobalParamsService;
 
+    @Mock
+    TaskPluginManager taskPluginManager;
+
     @Test
     public void testCreateSubCommand() {
         ProcessInstance parentInstance = new ProcessInstance();
@@ -682,6 +686,7 @@ public class ProcessServiceTest {
         return list;
     }
 
+    @Test
     public void testSaveTaskDefine() {
         User operator = new User();
         operator.setId(-1);
@@ -706,9 +711,10 @@ public class ProcessServiceTest {
         taskDefinition.setVersion(1);
         taskDefinition.setCreateTime(new Date());
         taskDefinition.setUpdateTime(new Date());
+        Mockito.when(taskPluginManager.getParameters(any())).thenReturn(null);
         Mockito.when(taskDefinitionLogMapper.queryByDefinitionCodeAndVersion(taskDefinition.getCode(), taskDefinition.getVersion())).thenReturn(taskDefinition);
         Mockito.when(taskDefinitionLogMapper.queryMaxVersionForDefinition(taskDefinition.getCode())).thenReturn(1);
-        Mockito.when(taskDefinitionMapper.queryByCode(taskDefinition.getCode())).thenReturn(taskDefinition);
+        Mockito.when(taskDefinitionMapper.queryByCodeList(Collections.singletonList(taskDefinition.getCode()))).thenReturn(Collections.singletonList(taskDefinition));
         int result = processService.saveTaskDefine(operator, projectCode, taskDefinitionLogs, Boolean.TRUE);
         Assert.assertEquals(0, result);
     }