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.net;
19 
20 import java.util.ArrayList;
21 import java.util.List;
22 
23 import org.junit.Assert;
24 import org.junit.Test;
25 
26 public class TestClusterTopology extends Assert {
27 
28   public static class NodeElement implements Node {
29     private String location;
30     private String name;
31     private Node parent;
32     private int level;
33 
NodeElement(String name)34     public NodeElement(String name) {
35       this.name = name;
36     }
37 
38     @Override
getNetworkLocation()39     public String getNetworkLocation() {
40       return location;
41     }
42 
43     @Override
setNetworkLocation(String location)44     public void setNetworkLocation(String location) {
45       this.location = location;
46     }
47 
48     @Override
getName()49     public String getName() {
50       return name;
51     }
52 
53     @Override
getParent()54     public Node getParent() {
55       return parent;
56     }
57 
58     @Override
setParent(Node parent)59     public void setParent(Node parent) {
60       this.parent = parent;
61     }
62 
63     @Override
getLevel()64     public int getLevel() {
65       return level;
66     }
67 
68     @Override
setLevel(int i)69     public void setLevel(int i) {
70       this.level = i;
71     }
72 
73   }
74 
75   /**
76    * Test the count of nodes with exclude list
77    */
78   @Test
testCountNumNodes()79   public void testCountNumNodes() throws Exception {
80     // create the topology
81     NetworkTopology cluster = new NetworkTopology();
82     cluster.add(getNewNode("node1", "/d1/r1"));
83     NodeElement node2 = getNewNode("node2", "/d1/r2");
84     cluster.add(node2);
85     cluster.add(getNewNode("node3", "/d1/r3"));
86     NodeElement node3 = getNewNode("node4", "/d1/r4");
87     cluster.add(node3);
88     // create exclude list
89     List<Node> excludedNodes = new ArrayList<Node>();
90 
91     assertEquals("4 nodes should be available", 4,
92         cluster.countNumOfAvailableNodes(NodeBase.ROOT, excludedNodes));
93     NodeElement deadNode = getNewNode("node5", "/d1/r2");
94     excludedNodes.add(deadNode);
95     assertEquals("4 nodes should be available with extra excluded Node", 4,
96         cluster.countNumOfAvailableNodes(NodeBase.ROOT, excludedNodes));
97     // add one existing node to exclude list
98     excludedNodes.add(node3);
99     assertEquals("excluded nodes with ROOT scope should be considered", 3,
100         cluster.countNumOfAvailableNodes(NodeBase.ROOT, excludedNodes));
101     assertEquals("excluded nodes without ~ scope should be considered", 2,
102         cluster.countNumOfAvailableNodes("~" + deadNode.getNetworkLocation(),
103             excludedNodes));
104     assertEquals("excluded nodes with rack scope should be considered", 1,
105         cluster.countNumOfAvailableNodes(deadNode.getNetworkLocation(),
106             excludedNodes));
107     // adding the node in excluded scope to excluded list
108     excludedNodes.add(node2);
109     assertEquals("excluded nodes with ~ scope should be considered", 2,
110         cluster.countNumOfAvailableNodes("~" + deadNode.getNetworkLocation(),
111             excludedNodes));
112     // getting count with non-exist scope.
113     assertEquals("No nodes should be considered for non-exist scope", 0,
114         cluster.countNumOfAvailableNodes("/non-exist", excludedNodes));
115   }
116 
getNewNode(String name, String rackLocation)117   private NodeElement getNewNode(String name, String rackLocation) {
118     NodeElement node = new NodeElement(name);
119     node.setNetworkLocation(rackLocation);
120     return node;
121   }
122 }
123