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.mapred; |
---|
19 | |
---|
20 | import java.io.IOException; |
---|
21 | import junit.framework.TestCase; |
---|
22 | import org.apache.hadoop.hdfs.TestHDFSServerPorts; |
---|
23 | import org.apache.hadoop.hdfs.server.datanode.DataNode; |
---|
24 | import org.apache.hadoop.hdfs.server.namenode.NameNode; |
---|
25 | import org.apache.hadoop.conf.Configuration; |
---|
26 | import org.apache.hadoop.fs.FileSystem; |
---|
27 | |
---|
28 | /** |
---|
29 | * This test checks correctness of port usage by mapreduce components: |
---|
30 | * JobTracker, and TaskTracker. |
---|
31 | * |
---|
32 | * The correct behavior is:<br> |
---|
33 | * - when a specific port is provided the server must either start on that port |
---|
34 | * or fail by throwing {@link java.net.BindException}.<br> |
---|
35 | * - if the port = 0 (ephemeral) then the server should choose |
---|
36 | * a free port and start on it. |
---|
37 | */ |
---|
38 | public class TestMRServerPorts extends TestCase { |
---|
39 | TestHDFSServerPorts hdfs = new TestHDFSServerPorts(); |
---|
40 | |
---|
41 | // Runs the JT in a separate thread |
---|
42 | private static class JTRunner extends Thread { |
---|
43 | JobTracker jt; |
---|
44 | void setJobTracker(JobTracker jt) { |
---|
45 | this.jt = jt; |
---|
46 | } |
---|
47 | |
---|
48 | public void run() { |
---|
49 | if (jt != null) { |
---|
50 | try { |
---|
51 | jt.offerService(); |
---|
52 | } catch (Exception ioe) {} |
---|
53 | } |
---|
54 | } |
---|
55 | } |
---|
56 | /** |
---|
57 | * Check whether the JobTracker can be started. |
---|
58 | */ |
---|
59 | private JobTracker startJobTracker(JobConf conf, JTRunner runner) |
---|
60 | throws IOException { |
---|
61 | conf.set("mapred.job.tracker", "localhost:0"); |
---|
62 | conf.set("mapred.job.tracker.http.address", "0.0.0.0:0"); |
---|
63 | JobTracker jt = null; |
---|
64 | try { |
---|
65 | jt = JobTracker.startTracker(conf); |
---|
66 | runner.setJobTracker(jt); |
---|
67 | runner.start(); |
---|
68 | conf.set("mapred.job.tracker", "localhost:" + jt.getTrackerPort()); |
---|
69 | conf.set("mapred.job.tracker.http.address", |
---|
70 | "0.0.0.0:" + jt.getInfoPort()); |
---|
71 | } catch(InterruptedException e) { |
---|
72 | throw new IOException(e.getLocalizedMessage()); |
---|
73 | } |
---|
74 | return jt; |
---|
75 | } |
---|
76 | |
---|
77 | private void setDataNodePorts(Configuration conf) { |
---|
78 | conf.set("dfs.datanode.address", |
---|
79 | TestHDFSServerPorts.NAME_NODE_HOST + "0"); |
---|
80 | conf.set("dfs.datanode.http.address", |
---|
81 | TestHDFSServerPorts.NAME_NODE_HTTP_HOST + "0"); |
---|
82 | conf.set("dfs.datanode.ipc.address", |
---|
83 | TestHDFSServerPorts.NAME_NODE_HOST + "0"); |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * Check whether the JobTracker can be started. |
---|
88 | */ |
---|
89 | private boolean canStartJobTracker(JobConf conf) |
---|
90 | throws IOException, InterruptedException { |
---|
91 | JobTracker jt = null; |
---|
92 | try { |
---|
93 | jt = JobTracker.startTracker(conf); |
---|
94 | } catch(IOException e) { |
---|
95 | if (e instanceof java.net.BindException) |
---|
96 | return false; |
---|
97 | throw e; |
---|
98 | } |
---|
99 | jt.fs.close(); |
---|
100 | jt.stopTracker(); |
---|
101 | return true; |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * Check whether the TaskTracker can be started. |
---|
106 | */ |
---|
107 | private boolean canStartTaskTracker(JobConf conf) |
---|
108 | throws IOException, InterruptedException { |
---|
109 | TaskTracker tt = null; |
---|
110 | try { |
---|
111 | tt = new TaskTracker(conf); |
---|
112 | } catch(IOException e) { |
---|
113 | if (e instanceof java.net.BindException) |
---|
114 | return false; |
---|
115 | throw e; |
---|
116 | } |
---|
117 | tt.shutdown(); |
---|
118 | return true; |
---|
119 | } |
---|
120 | |
---|
121 | /** |
---|
122 | * Verify JobTracker port usage. |
---|
123 | */ |
---|
124 | public void testJobTrackerPorts() throws Exception { |
---|
125 | NameNode nn = null; |
---|
126 | DataNode dn = null; |
---|
127 | try { |
---|
128 | nn = hdfs.startNameNode(); |
---|
129 | setDataNodePorts(hdfs.getConfig()); |
---|
130 | dn = hdfs.startDataNode(1, hdfs.getConfig()); |
---|
131 | |
---|
132 | // start job tracker on the same port as name-node |
---|
133 | JobConf conf2 = new JobConf(hdfs.getConfig()); |
---|
134 | conf2.set("mapred.job.tracker", |
---|
135 | FileSystem.getDefaultUri(hdfs.getConfig()).toString()); |
---|
136 | conf2.set("mapred.job.tracker.http.address", |
---|
137 | TestHDFSServerPorts.NAME_NODE_HTTP_HOST + 0); |
---|
138 | boolean started = canStartJobTracker(conf2); |
---|
139 | assertFalse(started); // should fail |
---|
140 | |
---|
141 | // bind http server to the same port as name-node |
---|
142 | conf2.set("mapred.job.tracker", TestHDFSServerPorts.NAME_NODE_HOST + 0); |
---|
143 | conf2.set("mapred.job.tracker.http.address", |
---|
144 | hdfs.getConfig().get("dfs.http.address")); |
---|
145 | started = canStartJobTracker(conf2); |
---|
146 | assertFalse(started); // should fail again |
---|
147 | |
---|
148 | // both ports are different from the name-node ones |
---|
149 | conf2.set("mapred.job.tracker", TestHDFSServerPorts.NAME_NODE_HOST + 0); |
---|
150 | conf2.set("mapred.job.tracker.http.address", |
---|
151 | TestHDFSServerPorts.NAME_NODE_HTTP_HOST + 0); |
---|
152 | started = canStartJobTracker(conf2); |
---|
153 | assertTrue(started); // should start now |
---|
154 | |
---|
155 | } finally { |
---|
156 | hdfs.stopDataNode(dn); |
---|
157 | hdfs.stopNameNode(nn); |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | /** |
---|
162 | * Verify JobTracker port usage. |
---|
163 | */ |
---|
164 | public void testTaskTrackerPorts() throws Exception { |
---|
165 | NameNode nn = null; |
---|
166 | DataNode dn = null; |
---|
167 | JobTracker jt = null; |
---|
168 | JTRunner runner = null; |
---|
169 | try { |
---|
170 | nn = hdfs.startNameNode(); |
---|
171 | setDataNodePorts(hdfs.getConfig()); |
---|
172 | dn = hdfs.startDataNode(2, hdfs.getConfig()); |
---|
173 | |
---|
174 | JobConf conf2 = new JobConf(hdfs.getConfig()); |
---|
175 | runner = new JTRunner(); |
---|
176 | jt = startJobTracker(conf2, runner); |
---|
177 | |
---|
178 | // start job tracker on the same port as name-node |
---|
179 | conf2.set("mapred.task.tracker.report.address", |
---|
180 | FileSystem.getDefaultUri(hdfs.getConfig()).toString()); |
---|
181 | conf2.set("mapred.task.tracker.http.address", |
---|
182 | TestHDFSServerPorts.NAME_NODE_HTTP_HOST + 0); |
---|
183 | boolean started = canStartTaskTracker(conf2); |
---|
184 | assertFalse(started); // should fail |
---|
185 | |
---|
186 | // bind http server to the same port as name-node |
---|
187 | conf2.set("mapred.task.tracker.report.address", |
---|
188 | TestHDFSServerPorts.NAME_NODE_HOST + 0); |
---|
189 | conf2.set("mapred.task.tracker.http.address", |
---|
190 | hdfs.getConfig().get("dfs.http.address")); |
---|
191 | started = canStartTaskTracker(conf2); |
---|
192 | assertFalse(started); // should fail again |
---|
193 | |
---|
194 | // both ports are different from the name-node ones |
---|
195 | conf2.set("mapred.task.tracker.report.address", |
---|
196 | TestHDFSServerPorts.NAME_NODE_HOST + 0); |
---|
197 | conf2.set("mapred.task.tracker.http.address", |
---|
198 | TestHDFSServerPorts.NAME_NODE_HTTP_HOST + 0); |
---|
199 | started = canStartTaskTracker(conf2); |
---|
200 | assertTrue(started); // should start now |
---|
201 | } finally { |
---|
202 | if (jt != null) { |
---|
203 | jt.fs.close(); |
---|
204 | jt.stopTracker(); |
---|
205 | runner.interrupt(); |
---|
206 | runner.join(); |
---|
207 | } |
---|
208 | hdfs.stopDataNode(dn); |
---|
209 | hdfs.stopNameNode(nn); |
---|
210 | } |
---|
211 | } |
---|
212 | } |
---|