Przeglądaj źródła

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
Arpit Agarwal 11 lat temu
rodzic
commit
50a0bedead

+ 3 - 0
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)
+

+ 2 - 1
hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/NNStorage.java

@@ -80,7 +80,8 @@ public class NNStorage extends Storage implements Closeable,
 
     private String fileName = null;
     private NameNodeFile(String name) { this.fileName = name; }
-    String getName() { return fileName; }
+    @VisibleForTesting
+    public String getName() { return fileName; }
   }
 
   /**

+ 98 - 0
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.RollingUpgradeAction;
 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 class TestRollingUpgrade {
       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();
+      }
+    }
+  }
 }