From 50a0bedead549aa14d91f74bf06feb5401d99ae9 Mon Sep 17 00:00:00 2001 From: Arpit Agarwal Date: Thu, 20 Feb 2014 00:47:15 +0000 Subject: [PATCH] HDFS-5976. Create unit tests for downgrade and finalize rolling upgrade. (Contributed by Haohui Mai) git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/HDFS-5535@1570019 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop-hdfs/CHANGES_HDFS-5535.txt | 3 + .../hdfs/server/namenode/NNStorage.java | 3 +- .../hadoop/hdfs/TestRollingUpgrade.java | 98 +++++++++++++++++++ 3 files changed, 103 insertions(+), 1 deletion(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES_HDFS-5535.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES_HDFS-5535.txt index cd55ac969e..ed3e041ff8 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES_HDFS-5535.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES_HDFS-5535.txt @@ -56,3 +56,6 @@ HDFS-5535 subtasks: HDFS-5963. TestRollingUpgrade#testSecondaryNameNode causes subsequent tests to fail. (szetszwo via Arpit Agarwal) + HDFS-5976. Create unit tests for downgrade and finalize rolling upgrade. + (Haohui Mai via Arpit Agarwal) + diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/NNStorage.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/NNStorage.java index 50e3f27871..3ba2e3da08 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/NNStorage.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/NNStorage.java @@ -80,7 +80,8 @@ public enum NameNodeFile { private String fileName = null; private NameNodeFile(String name) { this.fileName = name; } - String getName() { return fileName; } + @VisibleForTesting + public String getName() { return fileName; } } /** diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestRollingUpgrade.java b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestRollingUpgrade.java index 7010becc1a..018b842117 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestRollingUpgrade.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestRollingUpgrade.java @@ -18,6 +18,7 @@ package org.apache.hadoop.hdfs; import java.io.File; +import java.io.FilenameFilter; import java.io.IOException; import org.apache.commons.logging.Log; @@ -30,8 +31,10 @@ import org.apache.hadoop.hdfs.protocol.HdfsConstants.SafeModeAction; import org.apache.hadoop.hdfs.protocol.RollingUpgradeInfo; import org.apache.hadoop.hdfs.qjournal.MiniJournalCluster; +import org.apache.hadoop.hdfs.qjournal.MiniQJMHACluster; import org.apache.hadoop.hdfs.server.common.HdfsServerConstants.StartupOption; import org.apache.hadoop.hdfs.server.datanode.DataNode; +import org.apache.hadoop.hdfs.server.namenode.NNStorage; import org.apache.hadoop.hdfs.tools.DFSAdmin; import org.junit.Assert; import org.junit.Test; @@ -308,4 +311,99 @@ public void testDFSAdminDatanodeUpgradeControlCommands() throws Exception { if (cluster != null) cluster.shutdown(); } } + + @Test + public void testDowngrade() throws Exception { + final Configuration conf = new HdfsConfiguration(); + MiniQJMHACluster cluster = null; + final Path foo = new Path("/foo"); + final Path bar = new Path("/bar"); + + try { + cluster = new MiniQJMHACluster.Builder(conf).build(); + MiniDFSCluster dfsCluster = cluster.getDfsCluster(); + dfsCluster.waitActive(); + + dfsCluster.transitionToActive(0); + DistributedFileSystem dfs = dfsCluster.getFileSystem(0); + dfs.mkdirs(foo); + + // start rolling upgrade + RollingUpgradeInfo info = dfs.rollingUpgrade(RollingUpgradeAction.START); + Assert.assertTrue(info.isStarted()); + dfs.mkdirs(bar); + dfs.close(); + + dfsCluster.restartNameNode(0, true, "-rollingUpgrade", "downgrade"); + // shutdown NN1 + dfsCluster.shutdownNameNode(1); + dfsCluster.transitionToActive(0); + + dfs = dfsCluster.getFileSystem(0); + Assert.assertTrue(dfs.exists(foo)); + Assert.assertTrue(dfs.exists(bar)); + } finally { + if (cluster != null) { + cluster.shutdown(); + } + } + } + + private static boolean existRollbackFsImage(NNStorage storage) + throws IOException { + final FilenameFilter filter = new FilenameFilter() { + + @Override + public boolean accept(File dir, String name) { + return name.indexOf(NNStorage.NameNodeFile.IMAGE_ROLLBACK.getName()) != -1; + } + }; + for (int i = 0; i < storage.getNumStorageDirs(); i++) { + File dir = storage.getStorageDir(i).getCurrentDir(); + int l = dir.list(filter).length; + if (l > 0) { + return true; + } + } + return false; + } + + @Test + public void testFinalize() throws Exception { + final Configuration conf = new HdfsConfiguration(); + MiniQJMHACluster cluster = null; + final Path foo = new Path("/foo"); + final Path bar = new Path("/bar"); + + try { + cluster = new MiniQJMHACluster.Builder(conf).build(); + MiniDFSCluster dfsCluster = cluster.getDfsCluster(); + dfsCluster.waitActive(); + + dfsCluster.transitionToActive(0); + DistributedFileSystem dfs = dfsCluster.getFileSystem(0); + dfs.mkdirs(foo); + + NNStorage storage = dfsCluster.getNamesystem(0).getFSImage() + .getStorage(); + + // start rolling upgrade + RollingUpgradeInfo info = dfs.rollingUpgrade(RollingUpgradeAction.START); + Assert.assertTrue(info.isStarted()); + dfs.mkdirs(bar); + // The NN should have a copy of the fsimage in case of rollbacks. + Assert.assertTrue(existRollbackFsImage(storage)); + + info = dfs.rollingUpgrade(RollingUpgradeAction.FINALIZE); + Assert.assertTrue(info.isFinalized()); + Assert.assertTrue(dfs.exists(foo)); + + // Once finalized, there should be no more fsimage for rollbacks. + Assert.assertFalse(existRollbackFsImage(storage)); + } finally { + if (cluster != null) { + cluster.shutdown(); + } + } + } }