View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertNotEquals;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  import org.apache.hadoop.hbase.testclassification.SmallTests;
27  import org.apache.hadoop.hbase.util.DNS;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  @Category(SmallTests.class)
32  public class TestHDFSBlocksDistribution {
33    @Test
34    public void testAddHostsAndBlockWeight() throws Exception {
35      HDFSBlocksDistribution distribution = new HDFSBlocksDistribution();
36      distribution.addHostsAndBlockWeight(null, 100);
37      assertEquals("Expecting no hosts weights", 0, distribution.getHostAndWeights().size());
38      distribution.addHostsAndBlockWeight(new String[0], 100);
39      assertEquals("Expecting no hosts weights", 0, distribution.getHostAndWeights().size());
40      distribution.addHostsAndBlockWeight(new String[] {"test"}, 101);
41      assertEquals("Should be one host", 1, distribution.getHostAndWeights().size());
42      distribution.addHostsAndBlockWeight(new String[] {"test"}, 202);
43      assertEquals("Should be one host", 1, distribution.getHostAndWeights().size());
44      assertEquals("test host should have weight 303", 303,
45          distribution.getHostAndWeights().get("test").getWeight());
46      distribution.addHostsAndBlockWeight(new String[] {"testTwo"}, 222);
47      assertEquals("Should be two hosts", 2, distribution.getHostAndWeights().size());
48      assertEquals("Total weight should be 525", 525, distribution.getUniqueBlocksTotalWeight());
49    }
50  
51    public class MockHDFSBlocksDistribution extends HDFSBlocksDistribution {
52      public Map<String,HostAndWeight> getHostAndWeights() {
53        HashMap<String, HostAndWeight> map = new HashMap<String, HostAndWeight>();
54        map.put("test", new HostAndWeight(null, 100));
55        return map;
56      }
57  
58    }
59  
60    @Test
61    public void testAdd() throws Exception {
62      HDFSBlocksDistribution distribution = new HDFSBlocksDistribution();
63      distribution.add(new MockHDFSBlocksDistribution());
64      assertEquals("Expecting no hosts weights", 0, distribution.getHostAndWeights().size());
65      distribution.addHostsAndBlockWeight(new String[]{"test"}, 10);
66      assertEquals("Should be one host", 1, distribution.getHostAndWeights().size());
67      distribution.add(new MockHDFSBlocksDistribution());
68      assertEquals("Should be one host", 1, distribution.getHostAndWeights().size());
69      assertEquals("Total weight should be 10", 10, distribution.getUniqueBlocksTotalWeight());
70    }
71  
72    @Test
73    public void testLocalHostCompatibility() throws Exception {
74      String currentHost = DNS.getDefaultHost("default", "default");
75      HDFSBlocksDistribution distribution = new HDFSBlocksDistribution();
76      assertEquals("Locality should be 0.0", 0.0,
77        distribution.getBlockLocalityIndex(currentHost), 0.01);
78      distribution.addHostsAndBlockWeight(new String[] { "localhost" }, 10);
79      assertEquals("Should be one host", 1, distribution.getHostAndWeights().size());
80      assertEquals("Locality should be 0.0", 0.0,
81        distribution.getBlockLocalityIndex("test"), 0.01);
82      assertNotEquals("Locality should be 0.0", 0.0,
83        distribution.getBlockLocalityIndex(currentHost), 0.01);
84    }
85  
86  }