1 | /** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
3 | * or more contributor license agreements. See the NOTICE file |
---|
4 | * distributed with this work for additional information |
---|
5 | * regarding copyright ownership. The ASF licenses this file |
---|
6 | * to you under the Apache License, Version 2.0 (the |
---|
7 | * "License"); you may not use this file except in compliance |
---|
8 | * with the License. You may obtain a copy of the License at |
---|
9 | * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
11 | * |
---|
12 | * Unless required by applicable law or agreed to in writing, software |
---|
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
15 | * See the License for the specific language governing permissions and |
---|
16 | * limitations under the License. |
---|
17 | */ |
---|
18 | package org.apache.hadoop.hdfs; |
---|
19 | |
---|
20 | import java.io.File; |
---|
21 | import java.io.IOException; |
---|
22 | import junit.framework.TestCase; |
---|
23 | import org.apache.commons.logging.Log; |
---|
24 | import org.apache.commons.logging.LogFactory; |
---|
25 | import org.apache.hadoop.conf.Configuration; |
---|
26 | import static org.apache.hadoop.hdfs.server.common.HdfsConstants.NodeType.NAME_NODE; |
---|
27 | import static org.apache.hadoop.hdfs.server.common.HdfsConstants.NodeType.DATA_NODE; |
---|
28 | import org.apache.hadoop.hdfs.server.common.HdfsConstants.StartupOption; |
---|
29 | |
---|
30 | /** |
---|
31 | * This test ensures the appropriate response from the system when |
---|
32 | * the system is finalized. |
---|
33 | */ |
---|
34 | public class TestDFSFinalize extends TestCase { |
---|
35 | |
---|
36 | private static final Log LOG = LogFactory.getLog( |
---|
37 | "org.apache.hadoop.hdfs.TestDFSFinalize"); |
---|
38 | private Configuration conf; |
---|
39 | private int testCounter = 0; |
---|
40 | private MiniDFSCluster cluster = null; |
---|
41 | |
---|
42 | /** |
---|
43 | * Writes an INFO log message containing the parameters. |
---|
44 | */ |
---|
45 | void log(String label, int numDirs) { |
---|
46 | LOG.info("============================================================"); |
---|
47 | LOG.info("***TEST " + (testCounter++) + "*** " |
---|
48 | + label + ":" |
---|
49 | + " numDirs="+numDirs); |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * Verify that the current directory exists and that the previous directory |
---|
54 | * does not exist. Verify that current hasn't been modified by comparing |
---|
55 | * the checksum of all it's containing files with their original checksum. |
---|
56 | * Note that we do not check that previous is removed on the DataNode |
---|
57 | * because its removal is asynchronous therefore we have no reliable |
---|
58 | * way to know when it will happen. |
---|
59 | */ |
---|
60 | void checkResult(String[] nameNodeDirs, String[] dataNodeDirs) throws IOException { |
---|
61 | for (int i = 0; i < nameNodeDirs.length; i++) { |
---|
62 | assertTrue(new File(nameNodeDirs[i],"current").isDirectory()); |
---|
63 | assertTrue(new File(nameNodeDirs[i],"current/VERSION").isFile()); |
---|
64 | assertTrue(new File(nameNodeDirs[i],"current/edits").isFile()); |
---|
65 | assertTrue(new File(nameNodeDirs[i],"current/fsimage").isFile()); |
---|
66 | assertTrue(new File(nameNodeDirs[i],"current/fstime").isFile()); |
---|
67 | } |
---|
68 | for (int i = 0; i < dataNodeDirs.length; i++) { |
---|
69 | assertEquals( |
---|
70 | UpgradeUtilities.checksumContents( |
---|
71 | DATA_NODE, new File(dataNodeDirs[i],"current")), |
---|
72 | UpgradeUtilities.checksumMasterContents(DATA_NODE)); |
---|
73 | } |
---|
74 | for (int i = 0; i < nameNodeDirs.length; i++) { |
---|
75 | assertFalse(new File(nameNodeDirs[i],"previous").isDirectory()); |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * This test attempts to finalize the NameNode and DataNode. |
---|
81 | */ |
---|
82 | public void testFinalize() throws Exception { |
---|
83 | UpgradeUtilities.initialize(); |
---|
84 | |
---|
85 | for (int numDirs = 1; numDirs <= 2; numDirs++) { |
---|
86 | /* This test requires that "current" directory not change after |
---|
87 | * the upgrade. Actually it is ok for those contents to change. |
---|
88 | * For now disabling block verification so that the contents are |
---|
89 | * not changed. |
---|
90 | */ |
---|
91 | conf = new Configuration(); |
---|
92 | conf.setInt("dfs.datanode.scan.period.hours", -1); |
---|
93 | conf = UpgradeUtilities.initializeStorageStateConf(numDirs, conf); |
---|
94 | String[] nameNodeDirs = conf.getStrings("dfs.name.dir"); |
---|
95 | String[] dataNodeDirs = conf.getStrings("dfs.data.dir"); |
---|
96 | |
---|
97 | log("Finalize with existing previous dir", numDirs); |
---|
98 | UpgradeUtilities.createStorageDirs(NAME_NODE, nameNodeDirs, "current"); |
---|
99 | UpgradeUtilities.createStorageDirs(NAME_NODE, nameNodeDirs, "previous"); |
---|
100 | UpgradeUtilities.createStorageDirs(DATA_NODE, dataNodeDirs, "current"); |
---|
101 | UpgradeUtilities.createStorageDirs(DATA_NODE, dataNodeDirs, "previous"); |
---|
102 | cluster = new MiniDFSCluster(conf, 1, StartupOption.REGULAR); |
---|
103 | cluster.finalizeCluster(conf); |
---|
104 | checkResult(nameNodeDirs, dataNodeDirs); |
---|
105 | |
---|
106 | log("Finalize without existing previous dir", numDirs); |
---|
107 | cluster.finalizeCluster(conf); |
---|
108 | checkResult(nameNodeDirs, dataNodeDirs); |
---|
109 | |
---|
110 | cluster.shutdown(); |
---|
111 | UpgradeUtilities.createEmptyDirs(nameNodeDirs); |
---|
112 | UpgradeUtilities.createEmptyDirs(dataNodeDirs); |
---|
113 | } // end numDir loop |
---|
114 | } |
---|
115 | |
---|
116 | protected void tearDown() throws Exception { |
---|
117 | LOG.info("Shutting down MiniDFSCluster"); |
---|
118 | if (cluster != null) cluster.shutdown(); |
---|
119 | } |
---|
120 | |
---|
121 | public static void main(String[] args) throws Exception { |
---|
122 | new TestDFSFinalize().testFinalize(); |
---|
123 | } |
---|
124 | |
---|
125 | } |
---|
126 | |
---|
127 | |
---|