You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dolphinscheduler.apache.org by GitBox <gi...@apache.org> on 2022/08/31 07:18:48 UTC

[GitHub] [dolphinscheduler] caishunfeng commented on a diff in pull request #11702: Refactor heart beat task, use json to serialize/deserialize

caishunfeng commented on code in PR #11702:
URL: https://github.com/apache/dolphinscheduler/pull/11702#discussion_r959236571


##########
dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/registry/ServerNodeManager.java:
##########
@@ -379,19 +343,24 @@ private void syncMasterNodes(Collection<String> nodes, List<Server> masterNodes)
      * @param nodes worker nodes
      */
     private void syncWorkerGroupNodes(String workerGroup, Collection<String> nodes) {
-        workerGroupLock.lock();
+        workerGroupWriteLock.lock();
         try {
             Set<String> workerNodes = workerGroupNodes.getOrDefault(workerGroup, new HashSet<>());
             workerNodes.clear();
             workerNodes.addAll(nodes);
             workerGroupNodes.put(workerGroup, workerNodes);
         } finally {
-            workerGroupLock.unlock();
+            workerGroupWriteLock.unlock();
         }
     }
 
     public Map<String, Set<String>> getWorkerGroupNodes() {
-        return Collections.unmodifiableMap(workerGroupNodes);
+        workerGroupLock.readLock();
+        try {
+            return Collections.unmodifiableMap(workerGroupNodes);
+        } finally {
+            workerGroupLock.writeLock();

Review Comment:
   ```suggestion
               workerGroupReadLock.unlock();
   ```



##########
dolphinscheduler-master/src/main/java/org/apache/dolphinscheduler/server/master/registry/ServerNodeManager.java:
##########
@@ -379,19 +343,24 @@ private void syncMasterNodes(Collection<String> nodes, List<Server> masterNodes)
      * @param nodes worker nodes
      */
     private void syncWorkerGroupNodes(String workerGroup, Collection<String> nodes) {
-        workerGroupLock.lock();
+        workerGroupWriteLock.lock();
         try {
             Set<String> workerNodes = workerGroupNodes.getOrDefault(workerGroup, new HashSet<>());
             workerNodes.clear();
             workerNodes.addAll(nodes);
             workerGroupNodes.put(workerGroup, workerNodes);
         } finally {
-            workerGroupLock.unlock();
+            workerGroupWriteLock.unlock();
         }
     }
 
     public Map<String, Set<String>> getWorkerGroupNodes() {
-        return Collections.unmodifiableMap(workerGroupNodes);
+        workerGroupLock.readLock();

Review Comment:
   ```suggestion
           workerGroupReadLock.lock();
   ```



##########
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/model/BaseHeartBeatTask.java:
##########
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.dolphinscheduler.common.model;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.dolphinscheduler.common.lifecycle.ServerLifeCycleManager;
+import org.apache.dolphinscheduler.common.thread.BaseDaemonThread;
+
+@Slf4j
+public abstract class BaseHeartBeatTask<T> extends BaseDaemonThread {
+
+    private final String threadName;
+    private final long heartBeatInterval;
+
+    protected boolean runningFlag;
+
+    public BaseHeartBeatTask(String threadName, long heartBeatInterval) {
+        super(threadName);
+        this.threadName = threadName;
+        this.heartBeatInterval = heartBeatInterval;
+        this.runningFlag = true;
+    }
+
+    @Override
+    public synchronized void start() {
+        log.info("Starting {}", threadName);
+        super.start();
+        log.info("Started {}, heartBeatInterval: {}", threadName, heartBeatInterval);
+    }
+
+    @Override
+    public void run() {
+        while (runningFlag) {
+            try {
+                if (!ServerLifeCycleManager.isRunning()) {
+                    log.info("The current server status is {}, will not write heartBeatInfo into registry", ServerLifeCycleManager.getServerStatus());
+                    continue;
+                }
+                T heartBeat = getHeartBeat();
+                writeHeartBeat(heartBeat);
+            } catch (Exception ex) {
+                log.error("{} task execute failed", threadName, ex);
+            } finally {
+                try {
+                    Thread.sleep(heartBeatInterval);
+                } catch (InterruptedException e) {
+                    handleInterruptException(e);
+                }
+            }
+        }
+    }
+
+    public void shutdown() {
+        log.warn("{} task finished", threadName);
+        runningFlag = false;
+    }
+
+    private void handleInterruptException(InterruptedException ex) {
+        shutdown();

Review Comment:
   should not shutdown when interrupt.



-- 
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: commits-unsubscribe@dolphinscheduler.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org