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.master.balancer;
019
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.ServerName;
025import org.apache.hadoop.hbase.client.RegionInfo;
026import org.apache.hadoop.hbase.master.RackManager;
027import org.apache.hadoop.hbase.testclassification.LargeTests;
028import org.apache.hadoop.hbase.testclassification.MasterTests;
029import org.junit.ClassRule;
030import org.junit.Test;
031import org.junit.experimental.categories.Category;
032
033@Category({ MasterTests.class, LargeTests.class })
034public class TestStochasticLoadBalancerRegionReplicaWithRacks extends StochasticBalancerTestBase {
035
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038      HBaseClassTestRule.forClass(TestStochasticLoadBalancerRegionReplicaWithRacks.class);
039
040  private static class ForTestRackManager extends RackManager {
041
042    int numRacks;
043    Map<String, Integer> serverIndexes = new HashMap<String, Integer>();
044    int numServers = 0;
045
046    public ForTestRackManager(int numRacks) {
047      this.numRacks = numRacks;
048    }
049
050    @Override
051    public String getRack(ServerName server) {
052      String key = server.getServerName();
053      if (!serverIndexes.containsKey(key)) {
054        serverIndexes.put(key, numServers++);
055      }
056      return "rack_" + serverIndexes.get(key) % numRacks;
057    }
058  }
059
060  @Test
061  public void testRegionReplicationOnMidClusterWithRacks() {
062    conf.setLong(StochasticLoadBalancer.MAX_STEPS_KEY, 100000000L);
063    conf.setBoolean("hbase.master.balancer.stochastic.runMaxSteps", true);
064    conf.setLong("hbase.master.balancer.stochastic.maxRunningTime", 120 * 1000); // 120 sec
065    loadBalancer.onConfigurationChange(conf);
066    int numNodes = 5;
067    int numRegions = numNodes * 1;
068    int replication = 3; // 3 replicas per region
069    int numRegionsPerServer = 1;
070    int numTables = 1;
071    int numRacks = 3; // all replicas should be on a different rack
072    Map<ServerName, List<RegionInfo>> serverMap =
073        createServerMap(numNodes, numRegions, numRegionsPerServer, replication, numTables);
074    RackManager rm = new ForTestRackManager(numRacks);
075    testWithClusterWithIteration(serverMap, rm, true, true);
076  }
077
078  @Test
079  public void testRegionReplicationOnLargeClusterWithRacks() {
080    conf.setBoolean("hbase.master.balancer.stochastic.runMaxSteps", true);
081    conf.setLong(StochasticLoadBalancer.MAX_STEPS_KEY, 100000000L);
082    conf.setLong("hbase.master.balancer.stochastic.maxRunningTime", 120 * 1000); // 10 sec
083    loadBalancer.onConfigurationChange(conf);
084    int numNodes = 100;
085    int numRegions = numNodes * 30;
086    int replication = 3; // 3 replicas per region
087    int numRegionsPerServer = 28;
088    int numTables = 1;
089    int numRacks = 4; // all replicas should be on a different rack
090    Map<ServerName, List<RegionInfo>> serverMap =
091      createServerMap(numNodes, numRegions, numRegionsPerServer, replication, numTables);
092    RackManager rm = new ForTestRackManager(numRacks);
093
094    testWithClusterWithIteration(serverMap, rm, true, true);
095  }
096}