From 6aa09dc28adc3e7c81258568ac08996eee0356da Mon Sep 17 00:00:00 2001 From: Arpit Agarwal Date: Wed, 1 Feb 2017 16:51:58 -0800 Subject: [PATCH] HDFS-11112. Journal Nodes should refuse to format non-empty directories. Contributed by Yiqun Lin. --- .../hadoop/hdfs/qjournal/server/JNStorage.java | 7 +++++++ .../hadoop/hdfs/server/common/Storage.java | 2 +- .../hdfs/qjournal/server/TestJournal.java | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/qjournal/server/JNStorage.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/qjournal/server/JNStorage.java index 77171a1013..07c9286161 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/qjournal/server/JNStorage.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/qjournal/server/JNStorage.java @@ -180,7 +180,14 @@ private static void purgeMatching(File dir, List patterns, } void format(NamespaceInfo nsInfo) throws IOException { + unlockAll(); + try { + sd.analyzeStorage(StartupOption.FORMAT, this, true); + } finally { + sd.unlock(); + } setStorageInfo(nsInfo); + LOG.info("Formatting journal " + sd + " with nsid: " + getNamespaceID()); // Unlock the directory before formatting, because we will // re-analyze it after format(). The analyzeStorage() call diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/common/Storage.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/common/Storage.java index 519c28f679..1af78770c7 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/common/Storage.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/common/Storage.java @@ -550,7 +550,7 @@ private void checkEmptyCurrent() throws InconsistentFSStateException, Files.newDirectoryStream(currentDir.toPath())) { if (dirStream.iterator().hasNext()) { throw new InconsistentFSStateException(root, - "Can't format the storage directory because the current/ " + "Can't format the storage directory because the current " + "directory is not empty."); } } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/qjournal/server/TestJournal.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/qjournal/server/TestJournal.java index 5cdc1a303d..4c36bcb77b 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/qjournal/server/TestJournal.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/qjournal/server/TestJournal.java @@ -204,6 +204,9 @@ public void testFormatResetsCachedValues() throws Exception { // Close the journal in preparation for reformatting it. journal.close(); + // Clear the storage directory before reformatting it + journal.getStorage().getJournalManager() + .getStorageDirectory().clearDirectory(); journal.format(FAKE_NSINFO_2); assertEquals(0, journal.getLastPromisedEpoch()); @@ -417,4 +420,18 @@ public void testNamespaceVerification() throws Exception { } } + @Test + public void testFormatNonEmptyStorageDirectories() throws Exception { + try { + // Format again here and to format the non-empty directories in + // journal node. + journal.format(FAKE_NSINFO); + fail("Did not fail to format non-empty directories in journal node."); + } catch (IOException ioe) { + GenericTestUtils.assertExceptionContains( + "Can't format the storage directory because the current " + + "directory is not empty.", ioe); + } + } + }