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 883c352f977..0e9dc01098a 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 @@ -211,7 +211,7 @@ class BlockManagerSafeMode { switch (status) { case PENDING_THRESHOLD: if (areThresholdsMet()) { - if (extension > 0) { + if (blockTotal > 0 && extension > 0) { // PENDING_THRESHOLD -> EXTENSION status = BMSafeModeStatus.EXTENSION; reachedTime.set(monotonicNow()); @@ -527,11 +527,13 @@ class BlockManagerSafeMode { /** * Get time (counting in milliseconds) left to leave extension period. + * It should leave safemode at once if blockTotal = 0 rather than wait + * extension time (30s by default). * * Negative value indicates the extension period has passed. */ private long timeToLeaveExtension() { - return reachedTime.get() + extension - monotonicNow(); + return blockTotal > 0 ? reachedTime.get() + extension - monotonicNow() : 0; } /** 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 af5342a3fb0..243c789e58f 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 @@ -222,6 +222,15 @@ public class TestBlockManagerSafeMode { }, 100, 10000); } + @Test + public void testCheckSafeMode8() throws Exception { + bmSafeMode.activate(0); + setBlockSafe(0); + setSafeModeStatus(BMSafeModeStatus.PENDING_THRESHOLD); + bmSafeMode.checkSafeMode(); + assertEquals(BMSafeModeStatus.OFF, getSafeModeStatus()); + } + /** * Test that the block safe increases up to block threshold. * diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestHASafeMode.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestHASafeMode.java index 6e7bd15a88a..0fc0a5551dd 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestHASafeMode.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/ha/TestHASafeMode.java @@ -492,7 +492,15 @@ public class TestHASafeMode { private static void assertSafeMode(NameNode nn, int safe, int total, int numNodes, int nodeThresh) { String status = nn.getNamesystem().getSafemode(); - if (safe == total) { + if (total == 0 && nodeThresh == 0) { + assertTrue("Bad safemode status: '" + status + "'", + status.isEmpty() + || status.startsWith("Safe mode is ON. The reported blocks 0 " + + "has reached the threshold 0.9990 of total blocks 0. The " + + "minimum number of live datanodes is not required. In safe " + + "mode extension. Safe mode will be turned off automatically " + + "in 0 seconds.")); + } else if (safe == total) { if (nodeThresh == 0) { assertTrue("Bad safemode status: '" + status + "'", status.startsWith("Safe mode is ON. The reported blocks " + safe