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

wenjun 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 e3cf72cbcd [Fix-12832][API] Fix update worker group exception group 
name already exists. (#12874)
e3cf72cbcd is described below

commit e3cf72cbcd022adb0527b5c22199a851b11be6bf
Author: Kerwin <[email protected]>
AuthorDate: Mon Nov 14 11:20:51 2022 +0800

    [Fix-12832][API] Fix update worker group exception group name already 
exists. (#12874)
---
 .../api/service/impl/WorkerGroupServiceImpl.java   | 28 +++++++++-------------
 .../api/service/WorkerGroupServiceTest.java        |  6 -----
 2 files changed, 11 insertions(+), 23 deletions(-)

diff --git 
a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkerGroupServiceImpl.java
 
b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkerGroupServiceImpl.java
index 11b0ef6933..fe1d2114c6 100644
--- 
a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkerGroupServiceImpl.java
+++ 
b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkerGroupServiceImpl.java
@@ -113,15 +113,11 @@ public class WorkerGroupServiceImpl extends 
BaseServiceImpl implements WorkerGro
             return result;
         }
         Date now = new Date();
-        WorkerGroup workerGroup;
+        WorkerGroup workerGroup = null;
         if (id != 0) {
             workerGroup = workerGroupMapper.selectById(id);
-            // check exist
-            if (workerGroup == null) {
-                workerGroup = new WorkerGroup();
-                workerGroup.setCreateTime(now);
-            }
-        } else {
+        }
+        if (workerGroup == null) {
             workerGroup = new WorkerGroup();
             workerGroup.setCreateTime(now);
         }
@@ -165,23 +161,21 @@ public class WorkerGroupServiceImpl extends 
BaseServiceImpl implements WorkerGro
      * @return boolean
      */
     private boolean checkWorkerGroupNameExists(WorkerGroup workerGroup) {
+        // check database
         List<WorkerGroup> workerGroupList = 
workerGroupMapper.queryWorkerGroupByName(workerGroup.getName());
         if (CollectionUtils.isNotEmpty(workerGroupList)) {
-            // new group has same name
+            // create group, the same group name exists in the database
             if (workerGroup.getId() == null) {
                 return true;
             }
-            // check group id
-            for (WorkerGroup group : workerGroupList) {
-                if (Objects.equals(group.getId(), workerGroup.getId())) {
-                    return true;
-                }
+            // update group, the database exists with the same group name 
except itself
+            Optional<WorkerGroup> sameNameWorkGroupOptional = 
workerGroupList.stream()
+                    .filter(group -> !Objects.equals(group.getId(), 
workerGroup.getId())).findFirst();
+            if (sameNameWorkGroupOptional.isPresent()) {
+                return true;
             }
         }
-        // check zookeeper
-        String workerGroupPath =
-                Constants.REGISTRY_DOLPHINSCHEDULER_WORKERS + 
Constants.SINGLE_SLASH + workerGroup.getName();
-        return registryClient.exists(workerGroupPath);
+        return false;
     }
 
     /**
diff --git 
a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkerGroupServiceTest.java
 
b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkerGroupServiceTest.java
index 70135d468b..ea8aaf6dc3 100644
--- 
a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkerGroupServiceTest.java
+++ 
b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkerGroupServiceTest.java
@@ -153,9 +153,6 @@ public class WorkerGroupServiceTest {
                 baseServiceLogger)).thenReturn(true);
         Mockito.when(workerGroupMapper.selectById(1)).thenReturn(null);
         
Mockito.when(workerGroupMapper.queryWorkerGroupByName(GROUP_NAME)).thenReturn(null);
-        String workerGroupPath =
-                Constants.REGISTRY_DOLPHINSCHEDULER_WORKERS + 
Constants.SINGLE_SLASH + GROUP_NAME;
-        Mockito.when(registryClient.exists(workerGroupPath)).thenReturn(false);
         Map<String, String> serverMaps = new HashMap<>();
         serverMaps.put("localhost1:0000", "");
         
Mockito.when(registryClient.getServerMaps(NodeType.WORKER)).thenReturn(serverMaps);
@@ -176,9 +173,6 @@ public class WorkerGroupServiceTest {
 
         Mockito.when(workerGroupMapper.selectById(1)).thenReturn(null);
         
Mockito.when(workerGroupMapper.queryWorkerGroupByName(GROUP_NAME)).thenReturn(null);
-        String workerGroupPath =
-                Constants.REGISTRY_DOLPHINSCHEDULER_WORKERS + 
Constants.SINGLE_SLASH + GROUP_NAME;
-        Mockito.when(registryClient.exists(workerGroupPath)).thenReturn(false);
         Map<String, String> serverMaps = new HashMap<>();
         serverMaps.put("localhost:0000", "");
         
Mockito.when(registryClient.getServerMaps(NodeType.WORKER)).thenReturn(serverMaps);

Reply via email to