BlockManagerSafeMode { String getSafeModeTip() { String msg = ""; synchronized (this) { if (blockSafe < blockThreshold) { msg += 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); } else { msg += String.format("The reported blocks %d has reached the threshold" + " %.4f of total blocks %d. ", blockSafe, threshold, blockTotal); } } 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); } else { msg += String.format("The number of live datanodes %d has reached " + "the minimum number %d. ", numLive, datanodeThreshold); } if (blockManager.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 " + blockManager .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; } final String turnOffTip = "Safe mode will be turned off automatically "; switch(status) { case PENDING_THRESHOLD: msg += turnOffTip + "once the thresholds have been reached."; break; case EXTENSION: msg += "In safe mode extension. "+ turnOffTip + "in " + timeToLeaveExtension() / 1000 + " seconds."; break; case OFF: msg += turnOffTip + "soon."; break; default: assert false : "Non-recognized block manager safe mode status: " + status; } return msg; } BlockManagerSafeMode(BlockManager blockManager, Namesystem namesystem,
      Configuration conf);   }
@Test(timeout = 30000) public void testGetSafeModeTip() throws Exception { bmSafeMode.activate(BLOCK_TOTAL); String tip = bmSafeMode.getSafeModeTip(); assertTrue(tip.contains( String.format( "The reported blocks %d needs additional %d blocks to reach the " + "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))); assertTrue(tip.contains("Safe mode will be turned off automatically once " + "the thresholds have been reached.")); setBlockSafe(BLOCK_THRESHOLD); bmSafeMode.checkSafeMode(); tip = bmSafeMode.getSafeModeTip(); assertTrue(tip.contains( String.format("The reported blocks %d has reached the threshold" + " %.4f of total blocks %d. ", getblockSafe(), THRESHOLD, BLOCK_TOTAL))); assertTrue(tip.contains( String.format("The number of live datanodes %d has reached the " + "minimum number %d. ", dn.getNumLiveDataNodes(), DATANODE_NUM))); assertTrue(tip.contains("In safe mode extension. Safe mode will be turned" + " off automatically in")); waitForExtensionPeriod(); tip = bmSafeMode.getSafeModeTip(); System.out.println(tip); assertTrue(tip.contains( String.format("The reported blocks %d has reached the threshold" + " %.4f of total blocks %d. ", getblockSafe(), THRESHOLD, BLOCK_TOTAL))); assertTrue(tip.contains( String.format("The number of live datanodes %d has reached the " + "minimum number %d. ", dn.getNumLiveDataNodes(), DATANODE_NUM))); assertTrue(tip.contains("Safe mode will be turned off automatically soon")); }