source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/org/apache/hadoop/hdfs/server/namenode/TestReplicationPolicy.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: 16.9 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 */
18
19package org.apache.hadoop.hdfs.server.namenode;
20
21import java.io.IOException;
22import java.util.ArrayList;
23import java.util.List;
24
25import org.apache.hadoop.conf.Configuration;
26import org.apache.hadoop.net.NetworkTopology;
27import org.apache.hadoop.net.Node;
28import org.apache.hadoop.fs.FileSystem;
29import org.apache.hadoop.hdfs.protocol.DatanodeID;
30import org.apache.hadoop.hdfs.protocol.FSConstants;
31
32import junit.framework.TestCase;
33
34public class TestReplicationPolicy extends TestCase {
35  private static final int BLOCK_SIZE = 1024;
36  private static final int NUM_OF_DATANODES = 6;
37  private static final Configuration CONF = new Configuration();
38  private static final NetworkTopology cluster;
39  private static final NameNode namenode;
40  private static final ReplicationTargetChooser replicator;
41  private static final DatanodeDescriptor dataNodes[] = 
42    new DatanodeDescriptor[] {
43      new DatanodeDescriptor(new DatanodeID("h1:5020"), "/d1/r1"),
44      new DatanodeDescriptor(new DatanodeID("h2:5020"), "/d1/r1"),
45      new DatanodeDescriptor(new DatanodeID("h3:5020"), "/d1/r2"),
46      new DatanodeDescriptor(new DatanodeID("h4:5020"), "/d1/r2"),
47      new DatanodeDescriptor(new DatanodeID("h5:5020"), "/d2/r3"),
48      new DatanodeDescriptor(new DatanodeID("h6:5020"), "/d2/r3")
49    };
50   
51  private final static DatanodeDescriptor NODE = 
52    new DatanodeDescriptor(new DatanodeID("h7:5020"), "/d2/r4");
53 
54  static {
55    try {
56      FileSystem.setDefaultUri(CONF, "hdfs://localhost:0");
57      CONF.set("dfs.http.address", "0.0.0.0:0");
58      NameNode.format(CONF);
59      namenode = new NameNode(CONF);
60    } catch (IOException e) {
61      e.printStackTrace();
62      throw (RuntimeException)new RuntimeException().initCause(e);
63    }
64    FSNamesystem fsNamesystem = FSNamesystem.getFSNamesystem();
65    replicator = fsNamesystem.replicator;
66    cluster = fsNamesystem.clusterMap;
67    // construct network topology
68    for(int i=0; i<NUM_OF_DATANODES; i++) {
69      cluster.add(dataNodes[i]);
70    }
71    for(int i=0; i<NUM_OF_DATANODES; i++) {
72      dataNodes[i].updateHeartbeat(
73          2*FSConstants.MIN_BLOCKS_FOR_WRITE*BLOCK_SIZE, 0L,
74          2*FSConstants.MIN_BLOCKS_FOR_WRITE*BLOCK_SIZE, 0);
75    }
76  }
77 
78  /**
79   * In this testcase, client is dataNodes[0]. So the 1st replica should be
80   * placed on dataNodes[0], the 2nd replica should be placed on
81   * different rack and third should be placed on different node
82   * of rack chosen for 2nd node.
83   * The only excpetion is when the <i>numOfReplicas</i> is 2,
84   * the 1st is on dataNodes[0] and the 2nd is on a different rack.
85   * @throws Exception
86   */
87  public void testChooseTarget1() throws Exception {
88    dataNodes[0].updateHeartbeat(
89        2*FSConstants.MIN_BLOCKS_FOR_WRITE*BLOCK_SIZE, 0L, 
90        FSConstants.MIN_BLOCKS_FOR_WRITE*BLOCK_SIZE, 4); // overloaded
91
92    DatanodeDescriptor[] targets;
93    targets = replicator.chooseTarget(
94                                      0, dataNodes[0], null, BLOCK_SIZE);
95    assertEquals(targets.length, 0);
96   
97    targets = replicator.chooseTarget(
98                                      1, dataNodes[0], null, BLOCK_SIZE);
99    assertEquals(targets.length, 1);
100    assertEquals(targets[0], dataNodes[0]);
101   
102    targets = replicator.chooseTarget(
103                                      2, dataNodes[0], null, BLOCK_SIZE);
104    assertEquals(targets.length, 2);
105    assertEquals(targets[0], dataNodes[0]);
106    assertFalse(cluster.isOnSameRack(targets[0], targets[1]));
107   
108    targets = replicator.chooseTarget(
109                                      3, dataNodes[0], null, BLOCK_SIZE);
110    assertEquals(targets.length, 3);
111    assertEquals(targets[0], dataNodes[0]);
112    assertFalse(cluster.isOnSameRack(targets[0], targets[1]));
113    assertTrue(cluster.isOnSameRack(targets[1], targets[2]));
114
115    targets = replicator.chooseTarget(
116                                     4, dataNodes[0], null, BLOCK_SIZE);
117    assertEquals(targets.length, 4);
118    assertEquals(targets[0], dataNodes[0]);
119    assertTrue(cluster.isOnSameRack(targets[1], targets[2]) ||
120               cluster.isOnSameRack(targets[2], targets[3]));
121    assertFalse(cluster.isOnSameRack(targets[0], targets[2]));
122   
123    dataNodes[0].updateHeartbeat(
124        2*FSConstants.MIN_BLOCKS_FOR_WRITE*BLOCK_SIZE, 0L,
125        FSConstants.MIN_BLOCKS_FOR_WRITE*BLOCK_SIZE, 0); 
126  }
127
128  /**
129   * In this testcase, client is dataNodes[0], but the dataNodes[1] is
130   * not allowed to be chosen. So the 1st replica should be
131   * placed on dataNodes[0], the 2nd replica should be placed on a different
132   * rack, the 3rd should be on same rack as the 2nd replica, and the rest
133   * should be placed on a third rack.
134   * @throws Exception
135   */
136  public void testChooseTarget2() throws Exception { 
137    List<Node> excludedNodes;
138    DatanodeDescriptor[] targets;
139   
140    excludedNodes = new ArrayList<Node>();
141    excludedNodes.add(dataNodes[1]); 
142    targets = replicator.chooseTarget(
143                                      0, dataNodes[0], excludedNodes, BLOCK_SIZE);
144    assertEquals(targets.length, 0);
145   
146    excludedNodes.clear();
147    excludedNodes.add(dataNodes[1]); 
148    targets = replicator.chooseTarget(
149                                      1, dataNodes[0], excludedNodes, BLOCK_SIZE);
150    assertEquals(targets.length, 1);
151    assertEquals(targets[0], dataNodes[0]);
152   
153    excludedNodes.clear();
154    excludedNodes.add(dataNodes[1]); 
155    targets = replicator.chooseTarget(
156                                      2, dataNodes[0], excludedNodes, BLOCK_SIZE);
157    assertEquals(targets.length, 2);
158    assertEquals(targets[0], dataNodes[0]);
159    assertFalse(cluster.isOnSameRack(targets[0], targets[1]));
160   
161    excludedNodes.clear();
162    excludedNodes.add(dataNodes[1]); 
163    targets = replicator.chooseTarget(
164                                      3, dataNodes[0], excludedNodes, BLOCK_SIZE);
165    assertEquals(targets.length, 3);
166    assertEquals(targets[0], dataNodes[0]);
167    assertFalse(cluster.isOnSameRack(targets[0], targets[1]));
168    assertTrue(cluster.isOnSameRack(targets[1], targets[2]));
169   
170    excludedNodes.clear();
171    excludedNodes.add(dataNodes[1]); 
172    targets = replicator.chooseTarget(
173                                      4, dataNodes[0], excludedNodes, BLOCK_SIZE);
174    assertEquals(targets.length, 4);
175    assertEquals(targets[0], dataNodes[0]);
176    for(int i=1; i<4; i++) {
177      assertFalse(cluster.isOnSameRack(targets[0], targets[i]));
178    }
179    assertTrue(cluster.isOnSameRack(targets[1], targets[2]) ||
180               cluster.isOnSameRack(targets[2], targets[3]));
181    assertFalse(cluster.isOnSameRack(targets[1], targets[3]));
182  }
183
184  /**
185   * In this testcase, client is dataNodes[0], but dataNodes[0] is not qualified
186   * to be chosen. So the 1st replica should be placed on dataNodes[1],
187   * the 2nd replica should be placed on a different rack,
188   * the 3rd replica should be placed on the same rack as the 2nd replica,
189   * and the rest should be placed on the third rack.
190   * @throws Exception
191   */
192  public void testChooseTarget3() throws Exception {
193    // make data node 0 to be not qualified to choose
194    dataNodes[0].updateHeartbeat(
195        2*FSConstants.MIN_BLOCKS_FOR_WRITE*BLOCK_SIZE, 0L,
196        (FSConstants.MIN_BLOCKS_FOR_WRITE-1)*BLOCK_SIZE, 0); // no space
197       
198    DatanodeDescriptor[] targets;
199    targets = replicator.chooseTarget(
200                                      0, dataNodes[0], null, BLOCK_SIZE);
201    assertEquals(targets.length, 0);
202   
203    targets = replicator.chooseTarget(
204                                      1, dataNodes[0], null, BLOCK_SIZE);
205    assertEquals(targets.length, 1);
206    assertEquals(targets[0], dataNodes[1]);
207   
208    targets = replicator.chooseTarget(
209                                      2, dataNodes[0], null, BLOCK_SIZE);
210    assertEquals(targets.length, 2);
211    assertEquals(targets[0], dataNodes[1]);
212    assertFalse(cluster.isOnSameRack(targets[0], targets[1]));
213   
214    targets = replicator.chooseTarget(
215                                      3, dataNodes[0], null, BLOCK_SIZE);
216    assertEquals(targets.length, 3);
217    assertEquals(targets[0], dataNodes[1]);
218    assertTrue(cluster.isOnSameRack(targets[1], targets[2]));
219    assertFalse(cluster.isOnSameRack(targets[0], targets[1]));
220   
221    targets = replicator.chooseTarget(
222                                      4, dataNodes[0], null, BLOCK_SIZE);
223    assertEquals(targets.length, 4);
224    assertEquals(targets[0], dataNodes[1]);
225    for(int i=1; i<4; i++) {
226      assertFalse(cluster.isOnSameRack(targets[0], targets[i]));
227    }
228    assertTrue(cluster.isOnSameRack(targets[1], targets[2]) ||
229               cluster.isOnSameRack(targets[2], targets[3]));
230    assertFalse(cluster.isOnSameRack(targets[1], targets[3]));
231
232    dataNodes[0].updateHeartbeat(
233        2*FSConstants.MIN_BLOCKS_FOR_WRITE*BLOCK_SIZE, 0L,
234        FSConstants.MIN_BLOCKS_FOR_WRITE*BLOCK_SIZE, 0); 
235  }
236 
237  /**
238   * In this testcase, client is dataNodes[0], but none of the nodes on rack 1
239   * is qualified to be chosen. So the 1st replica should be placed on either
240   * rack 2 or rack 3.
241   * the 2nd replica should be placed on a different rack,
242   * the 3rd replica should be placed on the same rack as the 1st replica,
243   * @throws Exception
244   */
245  public void testChoooseTarget4() throws Exception {
246    // make data node 0 & 1 to be not qualified to choose: not enough disk space
247    for(int i=0; i<2; i++) {
248      dataNodes[i].updateHeartbeat(
249          2*FSConstants.MIN_BLOCKS_FOR_WRITE*BLOCK_SIZE, 0L,
250          (FSConstants.MIN_BLOCKS_FOR_WRITE-1)*BLOCK_SIZE, 0);
251    }
252     
253    DatanodeDescriptor[] targets;
254    targets = replicator.chooseTarget(
255                                      0, dataNodes[0], null, BLOCK_SIZE);
256    assertEquals(targets.length, 0);
257   
258    targets = replicator.chooseTarget(
259                                      1, dataNodes[0], null, BLOCK_SIZE);
260    assertEquals(targets.length, 1);
261    assertFalse(cluster.isOnSameRack(targets[0], dataNodes[0]));
262   
263    targets = replicator.chooseTarget(
264                                      2, dataNodes[0], null, BLOCK_SIZE);
265    assertEquals(targets.length, 2);
266    assertFalse(cluster.isOnSameRack(targets[0], dataNodes[0]));
267    assertFalse(cluster.isOnSameRack(targets[0], targets[1]));
268   
269    targets = replicator.chooseTarget(
270                                      3, dataNodes[0], null, BLOCK_SIZE);
271    assertEquals(targets.length, 3);
272    for(int i=0; i<3; i++) {
273      assertFalse(cluster.isOnSameRack(targets[i], dataNodes[0]));
274    }
275    assertTrue(cluster.isOnSameRack(targets[0], targets[1]) ||
276               cluster.isOnSameRack(targets[1], targets[2]));
277    assertFalse(cluster.isOnSameRack(targets[0], targets[2]));
278   
279    for(int i=0; i<2; i++) {
280      dataNodes[i].updateHeartbeat(
281          2*FSConstants.MIN_BLOCKS_FOR_WRITE*BLOCK_SIZE, 0L,
282          FSConstants.MIN_BLOCKS_FOR_WRITE*BLOCK_SIZE, 0);
283    }
284  }
285  /**
286   * In this testcase, client is is a node outside of file system.
287   * So the 1st replica can be placed on any node.
288   * the 2nd replica should be placed on a different rack,
289   * the 3rd replica should be placed on the same rack as the 2nd replica,
290   * @throws Exception
291   */
292  public void testChooseTarget5() throws Exception {
293    DatanodeDescriptor[] targets;
294    targets = replicator.chooseTarget(
295                                      0, NODE, null, BLOCK_SIZE);
296    assertEquals(targets.length, 0);
297   
298    targets = replicator.chooseTarget(
299                                      1, NODE, null, BLOCK_SIZE);
300    assertEquals(targets.length, 1);
301   
302    targets = replicator.chooseTarget(
303                                      2, NODE, null, BLOCK_SIZE);
304    assertEquals(targets.length, 2);
305    assertFalse(cluster.isOnSameRack(targets[0], targets[1]));
306   
307    targets = replicator.chooseTarget(
308                                      3, NODE, null, BLOCK_SIZE);
309    assertEquals(targets.length, 3);
310    assertTrue(cluster.isOnSameRack(targets[1], targets[2]));
311    assertFalse(cluster.isOnSameRack(targets[0], targets[1]));   
312  }
313 
314  /**
315   * This testcase tests re-replication, when dataNodes[0] is already chosen.
316   * So the 1st replica can be placed on random rack.
317   * the 2nd replica should be placed on different node by same rack as
318   * the 1st replica. The 3rd replica can be placed randomly.
319   * @throws Exception
320   */
321  public void testRereplicate1() throws Exception {
322    List<DatanodeDescriptor> chosenNodes = new ArrayList<DatanodeDescriptor>();
323    chosenNodes.add(dataNodes[0]);   
324    DatanodeDescriptor[] targets;
325   
326    targets = replicator.chooseTarget(
327                                      0, dataNodes[0], chosenNodes, null, BLOCK_SIZE);
328    assertEquals(targets.length, 0);
329   
330    targets = replicator.chooseTarget(
331                                      1, dataNodes[0], chosenNodes, null, BLOCK_SIZE);
332    assertEquals(targets.length, 1);
333    assertFalse(cluster.isOnSameRack(dataNodes[0], targets[0]));
334   
335    targets = replicator.chooseTarget(
336                                      2, dataNodes[0], chosenNodes, null, BLOCK_SIZE);
337    assertEquals(targets.length, 2);
338    assertTrue(cluster.isOnSameRack(dataNodes[0], targets[0]));
339    assertFalse(cluster.isOnSameRack(targets[0], targets[1]));
340   
341    targets = replicator.chooseTarget(
342                                      3, dataNodes[0], chosenNodes, null, BLOCK_SIZE);
343    assertEquals(targets.length, 3);
344    assertTrue(cluster.isOnSameRack(dataNodes[0], targets[0]));
345    assertFalse(cluster.isOnSameRack(targets[0], targets[2]));
346  }
347
348  /**
349   * This testcase tests re-replication,
350   * when dataNodes[0] and dataNodes[1] are already chosen.
351   * So the 1st replica should be placed on a different rack than rack 1.
352   * the rest replicas can be placed randomly,
353   * @throws Exception
354   */
355  public void testRereplicate2() throws Exception {
356    List<DatanodeDescriptor> chosenNodes = new ArrayList<DatanodeDescriptor>();
357    chosenNodes.add(dataNodes[0]);
358    chosenNodes.add(dataNodes[1]);
359
360    DatanodeDescriptor[] targets;
361    targets = replicator.chooseTarget(
362                                      0, dataNodes[0], chosenNodes, null, BLOCK_SIZE);
363    assertEquals(targets.length, 0);
364   
365    targets = replicator.chooseTarget(
366                                      1, dataNodes[0], chosenNodes, null, BLOCK_SIZE);
367    assertEquals(targets.length, 1);
368    assertFalse(cluster.isOnSameRack(dataNodes[0], targets[0]));
369   
370    targets = replicator.chooseTarget(
371                                      2, dataNodes[0], chosenNodes, null, BLOCK_SIZE);
372    assertEquals(targets.length, 2);
373    assertFalse(cluster.isOnSameRack(dataNodes[0], targets[0]));
374    assertFalse(cluster.isOnSameRack(dataNodes[0], targets[1]));
375  }
376
377  /**
378   * This testcase tests re-replication,
379   * when dataNodes[0] and dataNodes[2] are already chosen.
380   * So the 1st replica should be placed on the rack that the writer resides.
381   * the rest replicas can be placed randomly,
382   * @throws Exception
383   */
384  public void testRereplicate3() throws Exception {
385    List<DatanodeDescriptor> chosenNodes = new ArrayList<DatanodeDescriptor>();
386    chosenNodes.add(dataNodes[0]);
387    chosenNodes.add(dataNodes[2]);
388   
389    DatanodeDescriptor[] targets;
390    targets = replicator.chooseTarget(
391                                      0, dataNodes[0], chosenNodes, null, BLOCK_SIZE);
392    assertEquals(targets.length, 0);
393   
394    targets = replicator.chooseTarget(
395                                      1, dataNodes[0], chosenNodes, null, BLOCK_SIZE);
396    assertEquals(targets.length, 1);
397    assertTrue(cluster.isOnSameRack(dataNodes[0], targets[0]));
398    assertFalse(cluster.isOnSameRack(dataNodes[2], targets[0]));
399   
400    targets = replicator.chooseTarget(
401                               1, dataNodes[2], chosenNodes, null, BLOCK_SIZE);
402    assertEquals(targets.length, 1);
403    assertTrue(cluster.isOnSameRack(dataNodes[2], targets[0]));
404    assertFalse(cluster.isOnSameRack(dataNodes[0], targets[0]));
405
406    targets = replicator.chooseTarget(
407                                      2, dataNodes[0], chosenNodes, null, BLOCK_SIZE);
408    assertEquals(targets.length, 2);
409    assertTrue(cluster.isOnSameRack(dataNodes[0], targets[0]));
410   
411    targets = replicator.chooseTarget(
412                               2, dataNodes[2], chosenNodes, null, BLOCK_SIZE);
413    assertEquals(targets.length, 2);
414    assertTrue(cluster.isOnSameRack(dataNodes[2], targets[0]));
415  }
416 
417}
Note: See TracBrowser for help on using the repository browser.