source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/hdfs/TestHDFSServerPorts.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 7.7 KB
Line 
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 */
18package org.apache.hadoop.hdfs;
19
20import java.io.File;
21import java.io.IOException;
22
23import junit.framework.TestCase;
24import org.apache.hadoop.conf.Configuration;
25import org.apache.hadoop.fs.FileSystem;
26import org.apache.hadoop.fs.FileUtil;
27import org.apache.hadoop.hdfs.server.datanode.DataNode;
28import org.apache.hadoop.hdfs.server.namenode.NameNode;
29import org.apache.hadoop.hdfs.server.namenode.SecondaryNameNode;
30import org.apache.hadoop.ipc.RPC;
31
32/**
33 * This test checks correctness of port usage by hdfs components:
34 * NameNode, DataNode, and SecondaryNamenode.
35 *
36 * The correct behavior is:<br>
37 * - when a specific port is provided the server must either start on that port
38 * or fail by throwing {@link java.net.BindException}.<br>
39 * - if the port = 0 (ephemeral) then the server should choose
40 * a free port and start on it.
41 */
42public class TestHDFSServerPorts extends TestCase {
43  public static final String NAME_NODE_HOST = "localhost:";
44  public static final String NAME_NODE_HTTP_HOST = "0.0.0.0:";
45
46  Configuration config;
47  File hdfsDir;
48
49  /**
50   * Start the name-node.
51   */
52  public NameNode startNameNode() throws IOException {
53    String dataDir = System.getProperty("test.build.data");
54    hdfsDir = new File(dataDir, "dfs");
55    if ( hdfsDir.exists() && !FileUtil.fullyDelete(hdfsDir) ) {
56      throw new IOException("Could not delete hdfs directory '" + hdfsDir + "'");
57    }
58    config = new Configuration();
59    config.set("dfs.name.dir", new File(hdfsDir, "name1").getPath());
60    FileSystem.setDefaultUri(config, "hdfs://"+NAME_NODE_HOST + "0");
61    config.set("dfs.http.address", NAME_NODE_HTTP_HOST + "0");
62    NameNode.format(config);
63
64    String[] args = new String[] {};
65    // NameNode will modify config with the ports it bound to
66    return NameNode.createNameNode(args, config);
67  }
68
69  /**
70   * Start the data-node.
71   */
72  public DataNode startDataNode(int index, Configuration config) 
73  throws IOException {
74    String dataDir = System.getProperty("test.build.data");
75    File dataNodeDir = new File(dataDir, "data-" + index);
76    config.set("dfs.data.dir", dataNodeDir.getPath());
77
78    String[] args = new String[] {};
79    // NameNode will modify config with the ports it bound to
80    return DataNode.createDataNode(args, config);
81  }
82
83  /**
84   * Stop the datanode.
85   */
86  public void stopDataNode(DataNode dn) {
87    if (dn != null) {
88      dn.shutdown();
89    }
90  }
91
92  public void stopNameNode(NameNode nn) {
93    if (nn != null) {
94      nn.stop();
95    }
96  }
97
98  public Configuration getConfig() {
99    return this.config;
100  }
101
102  /**
103   * Check whether the name-node can be started.
104   */
105  private boolean canStartNameNode(Configuration conf) throws IOException {
106    NameNode nn2 = null;
107    try {
108      nn2 = NameNode.createNameNode(new String[]{}, conf);
109    } catch(IOException e) {
110      if (e instanceof java.net.BindException)
111        return false;
112      throw e;
113    }
114    stopNameNode(nn2);
115    return true;
116  }
117
118  /**
119   * Check whether the data-node can be started.
120   */
121  private boolean canStartDataNode(Configuration conf) throws IOException {
122    DataNode dn = null;
123    try {
124      dn = DataNode.createDataNode(new String[]{}, conf);
125    } catch(IOException e) {
126      if (e instanceof java.net.BindException)
127        return false;
128      throw e;
129    }
130    dn.shutdown();
131    return true;
132  }
133
134  /**
135   * Check whether the secondary name-node can be started.
136   */
137  private boolean canStartSecondaryNode(Configuration conf) throws IOException {
138    SecondaryNameNode sn = null;
139    try {
140      sn = new SecondaryNameNode(conf);
141    } catch(IOException e) {
142      if (e instanceof java.net.BindException)
143        return false;
144      throw e;
145    }
146    sn.shutdown();
147    return true;
148  }
149
150  /**
151   * Verify name-node port usage.
152   */
153  public void testNameNodePorts() throws Exception {
154    NameNode nn = null;
155    try {
156      nn = startNameNode();
157
158      // start another namenode on the same port
159      Configuration conf2 = new Configuration(config);
160      conf2.set("dfs.name.dir", new File(hdfsDir, "name2").getPath());
161      NameNode.format(conf2);
162      boolean started = canStartNameNode(conf2);
163      assertFalse(started); // should fail
164
165      // start on a different main port
166      FileSystem.setDefaultUri(conf2, "hdfs://"+NAME_NODE_HOST + "0");
167      started = canStartNameNode(conf2);
168      assertFalse(started); // should fail again
169
170      // reset conf2 since NameNode modifies it
171      FileSystem.setDefaultUri(conf2, "hdfs://"+NAME_NODE_HOST + "0");
172      // different http port
173      conf2.set("dfs.http.address", NAME_NODE_HTTP_HOST + "0");
174      started = canStartNameNode(conf2);
175      assertTrue(started); // should start now
176    } finally {
177      stopNameNode(nn);
178    }
179  }
180
181  /**
182   * Verify data-node port usage.
183   */
184  public void testDataNodePorts() throws Exception {
185    NameNode nn = null;
186    try {
187      nn = startNameNode();
188
189      // start data-node on the same port as name-node
190      Configuration conf2 = new Configuration(config);
191      conf2.set("dfs.data.dir", new File(hdfsDir, "data").getPath());
192      conf2.set("dfs.datanode.address",
193                FileSystem.getDefaultUri(config).getAuthority());
194      conf2.set("dfs.datanode.http.address", NAME_NODE_HTTP_HOST + "0");
195      boolean started = canStartDataNode(conf2);
196      assertFalse(started); // should fail
197
198      // bind http server to the same port as name-node
199      conf2.set("dfs.datanode.address", NAME_NODE_HOST + "0");
200      conf2.set("dfs.datanode.http.address", 
201                config.get("dfs.http.address"));
202      started = canStartDataNode(conf2);
203      assertFalse(started); // should fail
204   
205      // both ports are different from the name-node ones
206      conf2.set("dfs.datanode.address", NAME_NODE_HOST + "0");
207      conf2.set("dfs.datanode.http.address", NAME_NODE_HTTP_HOST + "0");
208      conf2.set("dfs.datanode.ipc.address", NAME_NODE_HOST + "0");
209      started = canStartDataNode(conf2);
210      assertTrue(started); // should start now
211    } finally {
212      stopNameNode(nn);
213    }
214  }
215
216  /**
217   * Verify secondary name-node port usage.
218   */
219  public void testSecondaryNodePorts() throws Exception {
220    NameNode nn = null;
221    try {
222      nn = startNameNode();
223
224      // bind http server to the same port as name-node
225      Configuration conf2 = new Configuration(config);
226      conf2.set("dfs.secondary.http.address", 
227                config.get("dfs.http.address"));
228      SecondaryNameNode.LOG.info("= Starting 1 on: " + 
229                                 conf2.get("dfs.secondary.http.address"));
230      boolean started = canStartSecondaryNode(conf2);
231      assertFalse(started); // should fail
232
233      // bind http server to a different port
234      conf2.set("dfs.secondary.http.address", NAME_NODE_HTTP_HOST + "0");
235      SecondaryNameNode.LOG.info("= Starting 2 on: " + 
236                                 conf2.get("dfs.secondary.http.address"));
237      started = canStartSecondaryNode(conf2);
238      assertTrue(started); // should start now
239    } finally {
240      stopNameNode(nn);
241    }
242  }
243}
Note: See TracBrowser for help on using the repository browser.