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.server.common; |
---|
19 | |
---|
20 | import java.io.IOException; |
---|
21 | import junit.framework.TestCase; |
---|
22 | import org.apache.commons.logging.Log; |
---|
23 | import org.apache.commons.logging.LogFactory; |
---|
24 | import org.apache.hadoop.conf.Configuration; |
---|
25 | |
---|
26 | import static org.apache.hadoop.hdfs.protocol.FSConstants.LAYOUT_VERSION; |
---|
27 | |
---|
28 | import org.apache.hadoop.hdfs.MiniDFSCluster; |
---|
29 | import org.apache.hadoop.hdfs.TestDFSUpgradeFromImage; |
---|
30 | import org.apache.hadoop.hdfs.server.common.HdfsConstants.StartupOption; |
---|
31 | import org.apache.hadoop.hdfs.server.datanode.UpgradeObjectDatanode; |
---|
32 | import org.apache.hadoop.hdfs.server.namenode.UpgradeObjectNamenode; |
---|
33 | import org.apache.hadoop.hdfs.server.protocol.UpgradeCommand; |
---|
34 | import org.apache.hadoop.hdfs.tools.DFSAdmin; |
---|
35 | |
---|
36 | /** |
---|
37 | */ |
---|
38 | public class TestDistributedUpgrade extends TestCase { |
---|
39 | private static final Log LOG = LogFactory.getLog(TestDistributedUpgrade.class); |
---|
40 | private Configuration conf; |
---|
41 | private int testCounter = 0; |
---|
42 | private MiniDFSCluster cluster = null; |
---|
43 | |
---|
44 | /** |
---|
45 | * Writes an INFO log message containing the parameters. |
---|
46 | */ |
---|
47 | void log(String label, int numDirs) { |
---|
48 | LOG.info("============================================================"); |
---|
49 | LOG.info("***TEST " + (testCounter++) + "*** " |
---|
50 | + label + ":" |
---|
51 | + " numDirs="+numDirs); |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * Attempts to start a NameNode with the given operation. Starting |
---|
56 | * the NameNode should throw an exception. |
---|
57 | */ |
---|
58 | void startNameNodeShouldFail(StartupOption operation) { |
---|
59 | try { |
---|
60 | //cluster = new MiniDFSCluster(conf, 0, operation); // should fail |
---|
61 | // we set manage dirs to true as NN has to start from untar'ed image with |
---|
62 | // nn dirs set to name1 and name2 |
---|
63 | cluster = new MiniDFSCluster(0, conf, 0, false, true, |
---|
64 | operation, null); // Should fail |
---|
65 | throw new AssertionError("NameNode should have failed to start"); |
---|
66 | } catch (Exception expected) { |
---|
67 | expected = null; |
---|
68 | // expected |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * Attempts to start a DataNode with the given operation. Starting |
---|
74 | * the DataNode should throw an exception. |
---|
75 | */ |
---|
76 | void startDataNodeShouldFail(StartupOption operation) { |
---|
77 | try { |
---|
78 | cluster.startDataNodes(conf, 1, false, operation, null); // should fail |
---|
79 | throw new AssertionError("DataNode should have failed to start"); |
---|
80 | } catch (Exception expected) { |
---|
81 | // expected |
---|
82 | assertFalse(cluster.isDataNodeUp()); |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | */ |
---|
88 | public void testDistributedUpgrade() throws Exception { |
---|
89 | int numDirs = 1; |
---|
90 | TestDFSUpgradeFromImage testImg = new TestDFSUpgradeFromImage(); |
---|
91 | testImg.unpackStorage(); |
---|
92 | int numDNs = testImg.numDataNodes; |
---|
93 | |
---|
94 | // register new upgrade objects (ignore all existing) |
---|
95 | UpgradeObjectCollection.initialize(); |
---|
96 | UpgradeObjectCollection.registerUpgrade(new UO_Datanode1()); |
---|
97 | UpgradeObjectCollection.registerUpgrade(new UO_Namenode1()); |
---|
98 | UpgradeObjectCollection.registerUpgrade(new UO_Datanode2()); |
---|
99 | UpgradeObjectCollection.registerUpgrade(new UO_Namenode2()); |
---|
100 | UpgradeObjectCollection.registerUpgrade(new UO_Datanode3()); |
---|
101 | UpgradeObjectCollection.registerUpgrade(new UO_Namenode3()); |
---|
102 | |
---|
103 | conf = new Configuration(); |
---|
104 | if (System.getProperty("test.build.data") == null) { // to test to be run outside of ant |
---|
105 | System.setProperty("test.build.data", "build/test/data"); |
---|
106 | } |
---|
107 | conf.setInt("dfs.datanode.scan.period.hours", -1); // block scanning off |
---|
108 | |
---|
109 | log("NameNode start in regular mode when dustributed upgrade is required", numDirs); |
---|
110 | startNameNodeShouldFail(StartupOption.REGULAR); |
---|
111 | |
---|
112 | log("Start NameNode only distributed upgrade", numDirs); |
---|
113 | // cluster = new MiniDFSCluster(conf, 0, StartupOption.UPGRADE); |
---|
114 | cluster = new MiniDFSCluster(0, conf, 0, false, true, |
---|
115 | StartupOption.UPGRADE, null); |
---|
116 | cluster.shutdown(); |
---|
117 | |
---|
118 | log("NameNode start in regular mode when dustributed upgrade has been started", numDirs); |
---|
119 | startNameNodeShouldFail(StartupOption.REGULAR); |
---|
120 | |
---|
121 | log("NameNode rollback to the old version that require a dustributed upgrade", numDirs); |
---|
122 | startNameNodeShouldFail(StartupOption.ROLLBACK); |
---|
123 | |
---|
124 | log("Normal distributed upgrade for the cluster", numDirs); |
---|
125 | cluster = new MiniDFSCluster(0, conf, numDNs, false, true, |
---|
126 | StartupOption.UPGRADE, null); |
---|
127 | DFSAdmin dfsAdmin = new DFSAdmin(); |
---|
128 | dfsAdmin.setConf(conf); |
---|
129 | dfsAdmin.run(new String[] {"-safemode", "wait"}); |
---|
130 | cluster.shutdown(); |
---|
131 | |
---|
132 | // it should be ok to start in regular mode |
---|
133 | log("NameCluster regular startup after the upgrade", numDirs); |
---|
134 | cluster = new MiniDFSCluster(0, conf, numDNs, false, true, |
---|
135 | StartupOption.REGULAR, null); |
---|
136 | cluster.waitActive(); |
---|
137 | cluster.shutdown(); |
---|
138 | } |
---|
139 | |
---|
140 | public static void main(String[] args) throws Exception { |
---|
141 | new TestDistributedUpgrade().testDistributedUpgrade(); |
---|
142 | LOG.info("=== DONE ==="); |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | * Upgrade object for data-node |
---|
148 | */ |
---|
149 | class UO_Datanode extends UpgradeObjectDatanode { |
---|
150 | int version; |
---|
151 | |
---|
152 | UO_Datanode(int v) { |
---|
153 | this.status = (short)0; |
---|
154 | version = v; |
---|
155 | } |
---|
156 | |
---|
157 | public int getVersion() { |
---|
158 | return version; |
---|
159 | } |
---|
160 | |
---|
161 | public void doUpgrade() throws IOException { |
---|
162 | this.status = (short)100; |
---|
163 | getDatanode().namenode.processUpgradeCommand( |
---|
164 | new UpgradeCommand(UpgradeCommand.UC_ACTION_REPORT_STATUS, |
---|
165 | getVersion(), getUpgradeStatus())); |
---|
166 | } |
---|
167 | |
---|
168 | public UpgradeCommand startUpgrade() throws IOException { |
---|
169 | return null; |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | /** |
---|
174 | * Upgrade object for name-node |
---|
175 | */ |
---|
176 | class UO_Namenode extends UpgradeObjectNamenode { |
---|
177 | int version; |
---|
178 | |
---|
179 | UO_Namenode(int v) { |
---|
180 | status = (short)0; |
---|
181 | version = v; |
---|
182 | } |
---|
183 | |
---|
184 | public int getVersion() { |
---|
185 | return version; |
---|
186 | } |
---|
187 | |
---|
188 | synchronized public UpgradeCommand processUpgradeCommand( |
---|
189 | UpgradeCommand command) throws IOException { |
---|
190 | switch(command.getAction()) { |
---|
191 | case UpgradeCommand.UC_ACTION_REPORT_STATUS: |
---|
192 | this.status += command.getCurrentStatus()/8; // 4 reports needed |
---|
193 | break; |
---|
194 | default: |
---|
195 | this.status++; |
---|
196 | } |
---|
197 | return null; |
---|
198 | } |
---|
199 | |
---|
200 | public UpgradeCommand completeUpgrade() throws IOException { |
---|
201 | return null; |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | class UO_Datanode1 extends UO_Datanode { |
---|
206 | UO_Datanode1() { |
---|
207 | super(LAYOUT_VERSION+1); |
---|
208 | } |
---|
209 | } |
---|
210 | |
---|
211 | class UO_Namenode1 extends UO_Namenode { |
---|
212 | UO_Namenode1() { |
---|
213 | super(LAYOUT_VERSION+1); |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | class UO_Datanode2 extends UO_Datanode { |
---|
218 | UO_Datanode2() { |
---|
219 | super(LAYOUT_VERSION+2); |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | class UO_Namenode2 extends UO_Namenode { |
---|
224 | UO_Namenode2() { |
---|
225 | super(LAYOUT_VERSION+2); |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | class UO_Datanode3 extends UO_Datanode { |
---|
230 | UO_Datanode3() { |
---|
231 | super(LAYOUT_VERSION+3); |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | class UO_Namenode3 extends UO_Namenode { |
---|
236 | UO_Namenode3() { |
---|
237 | super(LAYOUT_VERSION+3); |
---|
238 | } |
---|
239 | } |
---|