From f6b7f067c34e1fc3c050453f00f2d81274cd32b4 Mon Sep 17 00:00:00 2001 From: Todd Lipcon Date: Wed, 5 Sep 2012 04:27:49 +0000 Subject: [PATCH] HDFS-3884. Journal format() should reset cached values. Contributed by Todd Lipcon. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/HDFS-3077@1380979 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop-hdfs/CHANGES.HDFS-3077.txt | 2 ++ .../hadoop/hdfs/qjournal/server/Journal.java | 21 +++++++++++++++++-- .../hdfs/qjournal/server/TestJournal.java | 13 ++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.HDFS-3077.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.HDFS-3077.txt index de41bfe35e..16c9bec1e9 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.HDFS-3077.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.HDFS-3077.txt @@ -40,3 +40,5 @@ HDFS-3877. QJM: Provide defaults for dfs.journalnode.*address (eli) HDFS-3863. Track last "committed" txid in QJM (todd) HDFS-3869. Expose non-file journal manager details in web UI (todd) + +HDFS-3884. Journal format() should reset cached values (todd) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/qjournal/server/Journal.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/qjournal/server/Journal.java index 0c38b84cb4..5740b36f78 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/qjournal/server/Journal.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/qjournal/server/Journal.java @@ -105,6 +105,19 @@ class Journal implements Closeable { Journal(File logDir, StorageErrorReporter errorReporter) throws IOException { storage = new JNStorage(logDir, errorReporter); + refreshCachedData(); + + this.fjm = storage.getJournalManager(); + } + + /** + * Reload any data that may have been cached. This is necessary + * when we first load the Journal, but also after any formatting + * operation, since the cached data is no longer relevant. + */ + private synchronized void refreshCachedData() { + IOUtils.closeStream(committedTxnId); + File currentDir = storage.getSingularStorageDir().getCurrentDir(); this.lastPromisedEpoch = new PersistentLongFile( new File(currentDir, LAST_PROMISED_FILENAME), 0); @@ -113,8 +126,6 @@ class Journal implements Closeable { this.committedTxnId = new BestEffortLongFile( new File(currentDir, COMMITTED_TXID_FILENAME), HdfsConstants.INVALID_TXID); - - this.fjm = storage.getJournalManager(); } /** @@ -156,6 +167,7 @@ void format(NamespaceInfo nsInfo) throws IOException { LOG.info("Formatting " + this + " with namespace info: " + nsInfo); storage.format(nsInfo); + refreshCachedData(); } /** @@ -180,6 +192,11 @@ synchronized long getLastPromisedEpoch() throws IOException { checkFormatted(); return lastPromisedEpoch.get(); } + + synchronized public long getLastWriterEpoch() throws IOException { + checkFormatted(); + return lastWriterEpoch.get(); + } synchronized long getCommittedTxnIdForTests() throws IOException { return committedTxnId.get(); 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 8f555e6b0c..f0d481d7e6 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 @@ -148,6 +148,19 @@ public void testRestartJournal() throws Exception { assertEquals(1, newEpoch.getLastSegmentTxId()); } + @Test + public void testFormatResetsCachedValues() throws Exception { + journal.newEpoch(FAKE_NSINFO, 12345L); + journal.startLogSegment(new RequestInfo(JID, 12345L, 1L, 0L), 1L); + + assertEquals(12345L, journal.getLastPromisedEpoch()); + assertEquals(12345L, journal.getLastWriterEpoch()); + + journal.format(FAKE_NSINFO_2); + assertEquals(0, journal.getLastPromisedEpoch()); + assertEquals(0, journal.getLastWriterEpoch()); + } + /** * Test that, if the writer crashes at the very beginning of a segment, * before any transactions are written, that the next newEpoch() call