Browse Source

[Fix][API] Fix KeeperException (#4961)

fix #4705
Shiwen Cheng 4 years ago
parent
commit
8112f5bcb5

+ 9 - 12
dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/WorkerGroupServiceImpl.java

@@ -40,6 +40,8 @@ import java.util.Map;
 import java.util.Set;
 import java.util.stream.Collectors;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -50,7 +52,7 @@ import org.springframework.stereotype.Service;
 @Service
 public class WorkerGroupServiceImpl extends BaseServiceImpl implements WorkerGroupService {
 
-    private static final String NO_NODE_EXCEPTION_REGEX = "KeeperException$NoNodeException";
+    private static final Logger logger = LoggerFactory.getLogger(WorkerGroupServiceImpl.class);
 
     @Autowired
     protected ZookeeperCachedOperator zookeeperCachedOperator;
@@ -137,27 +139,22 @@ public class WorkerGroupServiceImpl extends BaseServiceImpl implements WorkerGro
      * @return WorkerGroup list
      */
     private List<WorkerGroup> getWorkerGroups(boolean isPaging) {
-
         String workerPath = zookeeperCachedOperator.getZookeeperConfig().getDsRoot() + Constants.ZOOKEEPER_DOLPHINSCHEDULER_WORKERS;
         List<WorkerGroup> workerGroups = new ArrayList<>();
-        List<String> workerGroupList;
+        List<String> workerGroupList = null;
         try {
             workerGroupList = zookeeperCachedOperator.getChildrenKeys(workerPath);
         } catch (Exception e) {
-            if (e.getMessage().contains(NO_NODE_EXCEPTION_REGEX)) {
-                if (isPaging) {
-                    return workerGroups;
-                }
+            logger.error("getWorkerGroups exception: {}, workerPath: {}, isPaging: {}", e.getMessage(), workerPath, isPaging);
+        }
 
-                //ignore noNodeException return Default
+        if (workerGroupList == null || workerGroupList.isEmpty()) {
+            if (!isPaging) {
                 WorkerGroup wg = new WorkerGroup();
                 wg.setName(DEFAULT_WORKER_GROUP);
                 workerGroups.add(wg);
-                return workerGroups;
-
-            } else {
-                throw e;
             }
+            return workerGroups;
         }
 
         for (String workerGroup : workerGroupList) {

+ 1 - 3
dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/service/WorkerGroupServiceTest.java

@@ -110,9 +110,7 @@ public class WorkerGroupServiceTest {
     }
 
     @Test
-    public void testQueryAllGroupWithNoNodeException() {
-        String workerPath = zookeeperCachedOperator.getZookeeperConfig().getDsRoot() + Constants.ZOOKEEPER_DOLPHINSCHEDULER_WORKERS;
-        Mockito.when(zookeeperCachedOperator.getChildrenKeys(workerPath)).thenThrow(new RuntimeException("KeeperException$NoNodeException"));
+    public void testQueryAllGroupWithDefault() {
         Map<String, Object> result = workerGroupService.queryAllGroup();
         Set<String> workerGroups = (Set<String>) result.get(Constants.DATA_LIST);
         Assert.assertEquals(1, workerGroups.size());