From 7cb77a3b1bf9e41384a1f74a60d34214199755d8 Mon Sep 17 00:00:00 2001 From: Jitendra Nath Pandey Date: Mon, 31 Oct 2011 19:03:55 +0000 Subject: [PATCH] HDFS-2334. Add Closeable to JournalManager. Contributed by Ivan Kelly. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1195620 13f79535-47bb-0310-9956-ffa450edef68 --- hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt | 2 ++ .../server/namenode/BackupJournalManager.java | 3 +++ .../hdfs/server/namenode/FSEditLog.java | 6 +++++ .../server/namenode/FileJournalManager.java | 3 +++ .../hdfs/server/namenode/JournalManager.java | 8 +++++- .../hdfs/server/namenode/JournalSet.java | 27 ++++++++++++++++--- 6 files changed, 44 insertions(+), 5 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt index 0b879913b6c..a0151638cd9 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt @@ -50,6 +50,8 @@ Trunk (unreleased changes) HDFS-2479 HDFS Client Data Types in Protocol Buffers (sanjay) + HDFS-2334. Add Closeable to JournalManager. (Ivan Kelly via jitendra) + BUG FIXES HDFS-2287. TestParallelRead has a small off-by-one bug. (todd) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/BackupJournalManager.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/BackupJournalManager.java index 69766203411..2bd585e236b 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/BackupJournalManager.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/BackupJournalManager.java @@ -77,6 +77,9 @@ class BackupJournalManager implements JournalManager { public void recoverUnfinalizedSegments() throws IOException { } + @Override + public void close() throws IOException {} + public boolean matchesRegistration(NamenodeRegistration bnReg) { return bnReg.getAddress().equals(this.bnReg.getAddress()); } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSEditLog.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSEditLog.java index aac2a35592e..f80f863346f 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSEditLog.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSEditLog.java @@ -215,6 +215,12 @@ public class FSEditLog { waitForSyncToFinish(); endCurrentLogSegment(true); } + + try { + journalSet.close(); + } catch (IOException ioe) { + LOG.warn("Error closing journalSet", ioe); + } state = State.CLOSED; } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FileJournalManager.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FileJournalManager.java index 8cfc9758239..eeb40c2f572 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FileJournalManager.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FileJournalManager.java @@ -70,6 +70,9 @@ class FileJournalManager implements JournalManager { this.sd = sd; } + @Override + public void close() throws IOException {} + @Override synchronized public EditLogOutputStream startLogSegment(long txid) throws IOException { diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/JournalManager.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/JournalManager.java index 0bb7b0f8aaf..348e3ef9819 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/JournalManager.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/JournalManager.java @@ -17,6 +17,7 @@ */ package org.apache.hadoop.hdfs.server.namenode; +import java.io.Closeable; import java.io.IOException; @@ -27,7 +28,7 @@ import java.io.IOException; * each conceptual place of storage corresponds to exactly one instance of * this class, which is created when the EditLog is first opened. */ -interface JournalManager { +interface JournalManager extends Closeable { /** * Begin writing to a new segment of the log stream, which starts at * the given transaction ID. @@ -81,6 +82,11 @@ interface JournalManager { */ void recoverUnfinalizedSegments() throws IOException; + /** + * Close the journal manager, freeing any resources it may hold. + */ + void close() throws IOException; + /** * Indicate that a journal is cannot be used to load a certain range of * edits. diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/JournalSet.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/JournalSet.java index 0d6bc743daf..45b5714082d 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/JournalSet.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/JournalSet.java @@ -72,11 +72,20 @@ public class JournalSet implements JournalManager { /** * Closes the stream, also sets it to null. */ - public void close() throws IOException { + public void closeStream() throws IOException { if (stream == null) return; stream.close(); stream = null; } + + /** + * Close the Journal and Stream + */ + public void close() throws IOException { + closeStream(); + + journal.close(); + } /** * Aborts the stream, also sets it to null. @@ -145,13 +154,23 @@ public class JournalSet implements JournalManager { @Override public void apply(JournalAndStream jas) throws IOException { if (jas.isActive()) { - jas.close(); + jas.closeStream(); jas.getManager().finalizeLogSegment(firstTxId, lastTxId); } } }, "finalize log segment " + firstTxId + ", " + lastTxId); } - + + @Override + public void close() throws IOException { + mapJournalsAndReportErrors(new JournalClosure() { + @Override + public void apply(JournalAndStream jas) throws IOException { + jas.close(); + } + }, "close journal"); + } + /** * Find the best editlog input stream to read from txid. @@ -332,7 +351,7 @@ public class JournalSet implements JournalManager { mapJournalsAndReportErrors(new JournalClosure() { @Override public void apply(JournalAndStream jas) throws IOException { - jas.close(); + jas.closeStream(); } }, "close"); }