JacksonYao287 commented on a change in pull request #2578:
URL: https://github.com/apache/ozone/pull/2578#discussion_r718257878



##########
File path: 
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/balancer/ContainerBalancer.java
##########
@@ -718,8 +747,10 @@ public void stop() {
         return;
       }
       balancerRunning = false;
-      currentBalancingThread.interrupt();
-      currentBalancingThread.join(1000);
+      if (Thread.currentThread().getId() != currentBalancingThread.getId()) {
+        currentBalancingThread.interrupt();
+        currentBalancingThread.join(0);

Review comment:
       NIT: better to use `join()` instead of `join(0)`, they are equal

##########
File path: 
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/balancer/ContainerBalancer.java
##########
@@ -348,17 +361,24 @@ private boolean initializeIteration() {
   }
 
   private IterationResult doIteration() {
+    // note that potential and selected targets are updated in the following
+    // loop
     List<DatanodeDetails> potentialTargets = getPotentialTargets();
     Set<DatanodeDetails> selectedTargets =
         new HashSet<>(potentialTargets.size());
     moveSelectionToFutureMap = new HashMap<>(unBalancedNodes.size());
 
     // match each overUtilized node with a target
     for (DatanodeUsageInfo datanodeUsageInfo : overUtilizedNodes) {
+      if (!balancerRunning || Thread.currentThread().isInterrupted()) {

Review comment:
       when reaching here, is there a case `balancerRunning` is true and 
meanwhile `Thread.currentThread().isInterrupted()` is true? we can only check 
`balancerRunning`, maybe that is enough 

##########
File path: 
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/balancer/ContainerBalancer.java
##########
@@ -718,8 +747,10 @@ public void stop() {
         return;
       }
       balancerRunning = false;
-      currentBalancingThread.interrupt();
-      currentBalancingThread.join(1000);
+      if (Thread.currentThread().getId() != currentBalancingThread.getId()) {
+        currentBalancingThread.interrupt();
+        currentBalancingThread.join(0);
+      }
 
       // allow garbage collector to collect balancing thread
       currentBalancingThread = null;

Review comment:
       maybe we could ignore catching InterruptedException here , `finally` is 
enough.
   no matter `stop` is called by balancing thread itself or command line,  the 
lock will be hold, thus when `join`, no `interrupt()` could be called.

##########
File path: 
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/balancer/ContainerBalancer.java
##########
@@ -203,13 +205,17 @@ private void balance() {
           } catch (InterruptedException e) {
             LOG.info("Container Balancer was interrupted while waiting for" +
                 " next iteration.");
-            stop();
+            if (!isBalancerRunning()) {
+              stop();
+            }

Review comment:
       the only `interrupt()` is called in `stop()` , so if we reach here, 
`isBalancerRunning()` will always return false. 
   we can remove these code , and this also avoids  dead lock

##########
File path: 
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/balancer/ContainerBalancer.java
##########
@@ -203,13 +205,17 @@ private void balance() {
           } catch (InterruptedException e) {
             LOG.info("Container Balancer was interrupted while waiting for" +
                 " next iteration.");
-            stop();
+            if (!isBalancerRunning()) {
+              stop();
+            }
             return;
           }
         }
       }
     }
-    stop();
+    if (!isBalancerRunning()) {

Review comment:
       seems we should remove `!` here , or there may be a dead lock

##########
File path: 
hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/container/balancer/ContainerBalancer.java
##########
@@ -184,7 +184,9 @@ private void balance() {
     for (int i = 0; i < idleIteration && balancerRunning; i++) {
       // stop balancing if iteration is not initialized
       if (!initializeIteration()) {
-        stop();
+        if (!isBalancerRunning()) {
+          stop();
+        }
         return;
       }
       doIteration();

Review comment:
       NIT: may be we can use the following code here,
   ```
   if (doIteration() = IterationResult.ITERATION_INTERRUPTED) {
          return;
   }
   ```
    and remove the following 
   ```
         // return if balancing has been stopped
         if (!isBalancerRunning()) {
           return;
         }
   ```
   




-- 
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]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to