/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.hadoop.net; import java.util.HashMap; import java.util.Map; import junit.framework.TestCase; import org.apache.hadoop.hdfs.server.namenode.DatanodeDescriptor; import org.apache.hadoop.hdfs.protocol.DatanodeID; public class TestNetworkTopology extends TestCase { private final static NetworkTopology cluster = new NetworkTopology(); private final static DatanodeDescriptor dataNodes[] = new DatanodeDescriptor[] { new DatanodeDescriptor(new DatanodeID("h1:5020"), "/d1/r1"), new DatanodeDescriptor(new DatanodeID("h2:5020"), "/d1/r1"), new DatanodeDescriptor(new DatanodeID("h3:5020"), "/d1/r2"), new DatanodeDescriptor(new DatanodeID("h4:5020"), "/d1/r2"), new DatanodeDescriptor(new DatanodeID("h5:5020"), "/d1/r2"), new DatanodeDescriptor(new DatanodeID("h6:5020"), "/d2/r3"), new DatanodeDescriptor(new DatanodeID("h7:5020"), "/d2/r3") }; private final static DatanodeDescriptor NODE = new DatanodeDescriptor(new DatanodeID("h8:5020"), "/d2/r4"); static { for(int i=0; i pickNodesAtRandom(int numNodes, String excludedScope) { Map frequency = new HashMap(); for (DatanodeDescriptor dnd : dataNodes) { frequency.put(dnd, 0); } for (int j = 0; j < numNodes; j++) { Node random = cluster.chooseRandom(excludedScope); frequency.put(random, frequency.get(random) + 1); } return frequency; } /** * This test checks that chooseRandom works for an excluded node. */ public void testChooseRandomExcludedNode() { String scope = "~" + NodeBase.getPath(dataNodes[0]); Map frequency = pickNodesAtRandom(100, scope); for (Node key : dataNodes) { // all nodes except the first should be more than zero assertTrue(frequency.get(key) > 0 || key == dataNodes[0]); } } /** * This test checks that chooseRandom works for an excluded rack. */ public void testChooseRandomExcludedRack() { Map frequency = pickNodesAtRandom(100, "~" + "/d2"); // all the nodes on the second rack should be zero for (int j = 0; j < dataNodes.length; j++) { int freq = frequency.get(dataNodes[j]); if (dataNodes[j].getNetworkLocation().startsWith("/d2")) { assertEquals(0, freq); } else { assertTrue(freq > 0); } } } }