From 00c4de63cf5cacec3463eafe5c8d654318461ca2 Mon Sep 17 00:00:00 2001 From: Ye Ni <141253+NickyYe@users.noreply.github.com> Date: Fri, 25 Sep 2020 09:47:54 -0700 Subject: [PATCH] HDFS-15594. Lazy calculate live datanodes in safe mode tip (#2332) --- .../blockmanagement/BlockManagerSafeMode.java | 71 +++++++++++-------- .../TestBlockManagerSafeMode.java | 4 +- 2 files changed, 42 insertions(+), 33 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/blockmanagement/BlockManagerSafeMode.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/blockmanagement/BlockManagerSafeMode.java index aecdb59df0..eec2aa55c5 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/blockmanagement/BlockManagerSafeMode.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/blockmanagement/BlockManagerSafeMode.java @@ -294,65 +294,74 @@ void setBlockTotal(long total) { } 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(); } /** diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/blockmanagement/TestBlockManagerSafeMode.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/blockmanagement/TestBlockManagerSafeMode.java index cf4ec6438a..5d2a07ed5d 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/blockmanagement/TestBlockManagerSafeMode.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/blockmanagement/TestBlockManagerSafeMode.java @@ -527,8 +527,8 @@ public void testGetSafeModeTip() throws Exception { "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."));