001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertNotEquals;
022
023import java.util.HashMap;
024import java.util.Map;
025import org.apache.hadoop.fs.StorageType;
026import org.apache.hadoop.hbase.testclassification.MiscTests;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.apache.hadoop.hbase.util.DNS;
029import org.junit.jupiter.api.Tag;
030import org.junit.jupiter.api.Test;
031
032@Tag(MiscTests.TAG)
033@Tag(SmallTests.TAG)
034public class TestHDFSBlocksDistribution {
035
036  @Test
037  public void testAddHostsAndBlockWeight() throws Exception {
038    HDFSBlocksDistribution distribution = new HDFSBlocksDistribution();
039    distribution.addHostsAndBlockWeight(null, 100);
040    assertEquals(0, distribution.getHostAndWeights().size(), "Expecting no hosts weights");
041    distribution.addHostsAndBlockWeight(new String[0], 100);
042    assertEquals(0, distribution.getHostAndWeights().size(), "Expecting no hosts weights");
043    distribution.addHostsAndBlockWeight(new String[] { "test" }, 101);
044    assertEquals(1, distribution.getHostAndWeights().size(), "Should be one host");
045    distribution.addHostsAndBlockWeight(new String[] { "test" }, 202);
046    assertEquals(1, distribution.getHostAndWeights().size(), "Should be one host");
047    assertEquals(303, distribution.getHostAndWeights().get("test").getWeight(),
048      "test host should have weight 303");
049    distribution.addHostsAndBlockWeight(new String[] { "testTwo" }, 222);
050    assertEquals(2, distribution.getHostAndWeights().size(), "Should be two hosts");
051    assertEquals(525, distribution.getUniqueBlocksTotalWeight(), "Total weight should be 525");
052    distribution.addHostsAndBlockWeight(new String[] { "test" }, 100,
053      new StorageType[] { StorageType.SSD });
054    assertEquals(403, distribution.getHostAndWeights().get("test").getWeight(),
055      "test host should have weight 403");
056    assertEquals(100, distribution.getHostAndWeights().get("test").getWeightForSsd(),
057      "test host should have weight for ssd 100");
058  }
059
060  private static final class MockHDFSBlocksDistribution extends HDFSBlocksDistribution {
061
062    @Override
063    public Map<String, HostAndWeight> getHostAndWeights() {
064      HashMap<String, HostAndWeight> map = new HashMap<>();
065      map.put("test", new HostAndWeight(null, 100, 0));
066      return map;
067    }
068  }
069
070  @Test
071  public void testAdd() throws Exception {
072    HDFSBlocksDistribution distribution = new HDFSBlocksDistribution();
073    distribution.add(new MockHDFSBlocksDistribution());
074    assertEquals(0, distribution.getHostAndWeights().size(), "Expecting no hosts weights");
075    distribution.addHostsAndBlockWeight(new String[] { "test" }, 10);
076    assertEquals(1, distribution.getHostAndWeights().size(), "Should be one host");
077    distribution.add(new MockHDFSBlocksDistribution());
078    assertEquals(1, distribution.getHostAndWeights().size(), "Should be one host");
079    assertEquals(10, distribution.getUniqueBlocksTotalWeight(), "Total weight should be 10");
080  }
081
082  @Test
083  public void testLocalHostCompatibility() throws Exception {
084    String currentHost = DNS.getDefaultHost("default", "default");
085    HDFSBlocksDistribution distribution = new HDFSBlocksDistribution();
086    assertEquals(0.0, distribution.getBlockLocalityIndex(currentHost), 0.01,
087      "Locality should be 0.0");
088    distribution.addHostsAndBlockWeight(new String[] { "localhost" }, 10);
089    assertEquals(1, distribution.getHostAndWeights().size(), "Should be one host");
090    assertEquals(0.0, distribution.getBlockLocalityIndex("test"), 0.01, "Locality should be 0.0");
091    assertNotEquals(0.0, distribution.getBlockLocalityIndex(currentHost), 0.01,
092      "Locality should be 0.0");
093  }
094
095}