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

This commit is contained in:
Ye Ni 2020-09-25 09:47:54 -07:00 committed by GitHub
parent e3cd627069
commit 00c4de63cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 33 deletions

View File

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

View File

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