From 75f2d47ff5736087fa8945f2d70527131bb3d957 Mon Sep 17 00:00:00 2001 From: litao Date: Tue, 23 Mar 2021 17:45:24 +0800 Subject: [PATCH] HDFS-15906. Close FSImage and FSNamesystem after formatting is complete (#2800) (cherry picked from commit d05d15620e320d28bfbeeee2a4f5224e69062528) --- .../apache/hadoop/hdfs/server/namenode/NameNode.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/NameNode.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/NameNode.java index c8cd8f7ee53..e09aae624a6 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/NameNode.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/NameNode.java @@ -1200,8 +1200,9 @@ public class NameNode extends ReconfigurableBase implements System.out.println("Formatting using clusterid: " + clusterId); FSImage fsImage = new FSImage(conf, nameDirsToFormat, editDirsToFormat); + FSNamesystem fsn = null; try { - FSNamesystem fsn = new FSNamesystem(conf, fsImage); + fsn = new FSNamesystem(conf, fsImage); fsImage.getEditLog().initJournalsForWrite(); // Abort NameNode format if reformat is disabled and if @@ -1226,8 +1227,14 @@ public class NameNode extends ReconfigurableBase implements fsImage.format(fsn, clusterId); } catch (IOException ioe) { LOG.warn("Encountered exception during format: ", ioe); - fsImage.close(); throw ioe; + } finally { + if (fsImage != null) { + fsImage.close(); + } + if (fsn != null) { + fsn.close(); + } } return false; }