Browse Source

HDFS-15594. Lazy calculate live datanodes in safe mode tip (#2332)

Ye Ni 4 years ago
parent
commit
00c4de63cf

+ 40 - 31
hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/blockmanagement/BlockManagerSafeMode.java

@@ -294,65 +294,74 @@ class BlockManagerSafeMode {
   }
 
   String getSafeModeTip() {
-    String msg = "";
+    StringBuilder msg = new StringBuilder();
+    boolean isBlockThresholdMet = false;
 
     synchronized (this) {
-      if (blockSafe < blockThreshold) {
-        msg += String.format(
+      isBlockThresholdMet = (blockSafe >= blockThreshold);
+      if (!isBlockThresholdMet) {
+        msg.append(String.format(
             "The reported blocks %d needs additional %d"
                 + " blocks to reach the threshold %.4f of total blocks %d.%n",
-            blockSafe, (blockThreshold - blockSafe), threshold, blockTotal);
+            blockSafe, (blockThreshold - blockSafe), threshold, blockTotal));
       } else {
-        msg += String.format("The reported blocks %d has reached the threshold"
-            + " %.4f of total blocks %d. ", blockSafe, threshold, blockTotal);
+        msg.append(String.format(
+            "The reported blocks %d has reached the threshold %.4f of total"
+                + " blocks %d. ", blockSafe, threshold, blockTotal));
       }
     }
 
     if (datanodeThreshold > 0) {
-      int numLive = blockManager.getDatanodeManager().getNumLiveDataNodes();
-      if (numLive < datanodeThreshold) {
-        msg += String.format(
-            "The number of live datanodes %d needs an additional %d live "
-                + "datanodes to reach the minimum number %d.%n",
-            numLive, (datanodeThreshold - numLive), datanodeThreshold);
+      if (isBlockThresholdMet) {
+        int numLive = blockManager.getDatanodeManager().getNumLiveDataNodes();
+        if (numLive < datanodeThreshold) {
+          msg.append(String.format(
+              "The number of live datanodes %d needs an additional %d live "
+                  + "datanodes to reach the minimum number %d.%n",
+              numLive, (datanodeThreshold - numLive), datanodeThreshold));
+        } else {
+          msg.append(String.format(
+              "The number of live datanodes %d has reached the minimum number"
+                  + " %d. ", numLive, datanodeThreshold));
+        }
       } else {
-        msg += String.format("The number of live datanodes %d has reached "
-                + "the minimum number %d. ",
-            numLive, datanodeThreshold);
+        msg.append("The number of live datanodes is not calculated ")
+            .append("since reported blocks hasn't reached the threshold. ");
       }
     } else {
-      msg += "The minimum number of live datanodes is not required. ";
+      msg.append("The minimum number of live datanodes is not required. ");
     }
 
     if (getBytesInFuture() > 0) {
-      msg += "Name node detected blocks with generation stamps " +
-          "in future. This means that Name node metadata is inconsistent. " +
-          "This can happen if Name node metadata files have been manually " +
-          "replaced. Exiting safe mode will cause loss of " +
-          getBytesInFuture() + " byte(s). Please restart name node with " +
-          "right metadata or use \"hdfs dfsadmin -safemode forceExit\" " +
-          "if you are certain that the NameNode was started with the " +
-          "correct FsImage and edit logs. If you encountered this during " +
-          "a rollback, it is safe to exit with -safemode forceExit.";
-      return msg;
+      msg.append("Name node detected blocks with generation stamps in future. ")
+          .append("This means that Name node metadata is inconsistent. This ")
+          .append("can happen if Name node metadata files have been manually ")
+          .append("replaced. Exiting safe mode will cause loss of ")
+          .append(getBytesInFuture())
+          .append(" byte(s). Please restart name node with right metadata ")
+          .append("or use \"hdfs dfsadmin -safemode forceExit\" if you ")
+          .append("are certain that the NameNode was started with the correct ")
+          .append("FsImage and edit logs. If you encountered this during ")
+          .append("a rollback, it is safe to exit with -safemode forceExit.");
+      return msg.toString();
     }
 
     final String turnOffTip = "Safe mode will be turned off automatically ";
     switch(status) {
     case PENDING_THRESHOLD:
-      msg += turnOffTip + "once the thresholds have been reached.";
+      msg.append(turnOffTip).append("once the thresholds have been reached.");
       break;
     case EXTENSION:
-      msg += "In safe mode extension. "+ turnOffTip + "in " +
-          timeToLeaveExtension() / 1000 + " seconds.";
+      msg.append("In safe mode extension. ").append(turnOffTip).append("in ")
+          .append(timeToLeaveExtension() / 1000).append(" seconds.");
       break;
     case OFF:
-      msg += turnOffTip + "soon.";
+      msg.append(turnOffTip).append("soon.");
       break;
     default:
       assert false : "Non-recognized block manager safe mode status: " + status;
     }
-    return msg;
+    return msg.toString();
   }
 
   /**

+ 2 - 2
hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/blockmanagement/TestBlockManagerSafeMode.java

@@ -527,8 +527,8 @@ public class TestBlockManagerSafeMode {
                 "threshold %.4f of total blocks %d.%n",
             0, BLOCK_THRESHOLD, THRESHOLD, BLOCK_TOTAL)));
     assertTrue(tip.contains(
-        String.format("The number of live datanodes %d has reached the " +
-            "minimum number %d. ", dn.getNumLiveDataNodes(), DATANODE_NUM)));
+        "The number of live datanodes is not calculated " +
+            "since reported blocks hasn't reached the threshold."));
     assertTrue(tip.contains("Safe mode will be turned off automatically once " +
         "the thresholds have been reached."));