From 83e57e083f2cf6c0de8a46966c5492faeabd8f2a Mon Sep 17 00:00:00 2001 From: Kihwal Lee Date: Mon, 15 Aug 2016 13:01:23 -0500 Subject: [PATCH] HDFS-9696. Garbage snapshot records linger forever. Contributed by Kihwal Lee --- .../namenode/FSImageFormatProtobuf.java | 6 ++- .../server/namenode/TestSaveNamespace.java | 42 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSImageFormatProtobuf.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSImageFormatProtobuf.java index 05087d13f0..7a81f9ecf7 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSImageFormatProtobuf.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSImageFormatProtobuf.java @@ -459,7 +459,11 @@ public final class FSImageFormatProtobuf { this, summary, context, context.getSourceNamesystem()); snapshotSaver.serializeSnapshotSection(sectionOutputStream); - snapshotSaver.serializeSnapshotDiffSection(sectionOutputStream); + // Skip snapshot-related sections when there is no snapshot. + if (context.getSourceNamesystem().getSnapshotManager() + .getNumSnapshots() > 0) { + snapshotSaver.serializeSnapshotDiffSection(sectionOutputStream); + } snapshotSaver.serializeINodeReferenceSection(sectionOutputStream); } diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestSaveNamespace.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestSaveNamespace.java index a374585dc2..0c86ef4c12 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestSaveNamespace.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/server/namenode/TestSaveNamespace.java @@ -647,6 +647,48 @@ public class TestSaveNamespace { } } + + @Test + public void testSkipSnapshotSection() throws Exception { + MiniDFSCluster cluster = new MiniDFSCluster.Builder(new Configuration()) + .numDataNodes(1).build(); + cluster.waitActive(); + DistributedFileSystem fs = cluster.getFileSystem(); + OutputStream out = null; + try { + String path = "/skipSnapshot"; + out = fs.create(new Path(path)); + out.close(); + + // add a bogus filediff + FSDirectory dir = cluster.getNamesystem().getFSDirectory(); + INodeFile file = dir.getINode(path).asFile(); + file.addSnapshotFeature(null).getDiffs() + .saveSelf2Snapshot(-1, file, null, false); + + // make sure it has a diff + assertTrue("Snapshot fileDiff is missing.", + file.getFileWithSnapshotFeature().getDiffs() != null); + + // saveNamespace + fs.setSafeMode(SafeModeAction.SAFEMODE_ENTER); + cluster.getNameNodeRpc().saveNamespace(0, 0); + fs.setSafeMode(SafeModeAction.SAFEMODE_LEAVE); + + // restart namenode + cluster.restartNameNode(true); + dir = cluster.getNamesystem().getFSDirectory(); + file = dir.getINode(path).asFile(); + + // there should be no snapshot feature for the inode, when there is + // no snapshot. + assertTrue("There should be no snapshot feature for this INode.", + file.getFileWithSnapshotFeature() == null); + } finally { + cluster.shutdown(); + } + } + @Test public void testSaveNamespaceBeforeShutdown() throws Exception { Configuration conf = new HdfsConfiguration();