From 6f1ef980ded061dd75d3368b0fc2fbbed14eea9f Mon Sep 17 00:00:00 2001 From: Konstantin Shvachko Date: Wed, 27 Jul 2011 23:28:16 +0000 Subject: [PATCH] HDFS-1981. NameNode does not saveNamespace() when editsNew is empty. Contributed by Uma Maheswara Rao G. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@1151666 13f79535-47bb-0310-9956-ffa450edef68 --- hdfs/CHANGES.txt | 3 + .../hadoop/hdfs/server/namenode/FSImage.java | 4 +- .../hdfs/server/namenode/TestFSImage.java | 91 +++++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 hdfs/src/test/hdfs/org/apache/hadoop/hdfs/server/namenode/TestFSImage.java diff --git a/hdfs/CHANGES.txt b/hdfs/CHANGES.txt index a44224779e3..e7beb78b811 100644 --- a/hdfs/CHANGES.txt +++ b/hdfs/CHANGES.txt @@ -1382,6 +1382,9 @@ Release 0.22.0 - Unreleased HDFS-2071. Use of isConnected() in DataXceiver is invalid. (Kihwal Lee via todd) + HDFS-1981. NameNode does not saveNamespace() when editsNew is empty. + (Uma Maheswara Rao G via shv) + Release 0.21.1 - Unreleased HDFS-1466. TestFcHdfsSymlink relies on /tmp/test not existing. (eli) diff --git a/hdfs/src/java/org/apache/hadoop/hdfs/server/namenode/FSImage.java b/hdfs/src/java/org/apache/hadoop/hdfs/server/namenode/FSImage.java index f81ee730f59..3883f832d33 100644 --- a/hdfs/src/java/org/apache/hadoop/hdfs/server/namenode/FSImage.java +++ b/hdfs/src/java/org/apache/hadoop/hdfs/server/namenode/FSImage.java @@ -763,7 +763,9 @@ public class FSImage implements NNStorageListener, Closeable { numEdits += loader.loadFSEdits(edits); edits.close(); } - + if (numEdits == 0 && editsNew.exists()) { + numEdits++; + } // update the counts. getFSNamesystem().dir.updateCountForINodeWithQuota(); diff --git a/hdfs/src/test/hdfs/org/apache/hadoop/hdfs/server/namenode/TestFSImage.java b/hdfs/src/test/hdfs/org/apache/hadoop/hdfs/server/namenode/TestFSImage.java new file mode 100644 index 00000000000..e92134e366c --- /dev/null +++ b/hdfs/src/test/hdfs/org/apache/hadoop/hdfs/server/namenode/TestFSImage.java @@ -0,0 +1,91 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hdfs.server.namenode; + +import static org.junit.Assert.assertEquals; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.URI; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileUtil; +import org.apache.hadoop.hdfs.DFSConfigKeys; +import org.apache.hadoop.hdfs.MiniDFSCluster; +import org.apache.hadoop.io.IOUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TestFSImage { + + private static final String OUT_DIR = System.getProperty("test.build.data", + "build/test/fsimage"); + + private MiniDFSCluster miniDFSCluster = null; + + private static Configuration nnConf = new Configuration(); + + private File current = new File(OUT_DIR); + + @Before + public void setUpCluster() throws Exception { + clearDirs(); + } + + @After + public void clusterShutdown() throws Exception { + if (null != miniDFSCluster) { + miniDFSCluster.shutdown(); + } + } + + @Test + public void testLoadFsEditsShouldReturnTrueWhenEditsNewExists() + throws Exception { + nnConf.set(DFSConfigKeys.DFS_NAMENODE_NAME_DIR_KEY, OUT_DIR + "/BNN1"); + NameNode.format(nnConf); + miniDFSCluster = new MiniDFSCluster.Builder(nnConf).numDataNodes(1).build(); + FSImage image = miniDFSCluster.getNameNode().getFSImage(); + URI next = FSNamesystem + .getNamespaceDirs(miniDFSCluster.getConfiguration(0)).iterator().next(); + File editsNew = new File(next.getRawPath() , "/current/edits.new"); + createEditsNew(editsNew, image); + int loadFSEdits = image.loadFSEdits(image.getStorage().getStorageDir(0)); + assertEquals("The numEdits should not be zero.", 1, loadFSEdits); + } + + private void createEditsNew(File editsNew, FSImage image) throws Exception { + FileOutputStream fileOutputStream = null; + if (!editsNew.exists()) { + try { + editsNew.createNewFile(); + image.editLog.createEditLogFile(editsNew); + } finally { + IOUtils.closeStream(fileOutputStream); + } + } + } + + private void clearDirs() throws IOException { + if (current.exists()) { + FileUtil.fullyDelete(current); + } + } +}