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 BalancerTestBase {
035
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038    HBaseClassTestRule.forClass(TestStochasticLoadBalancerRegionReplicaWithRacks.class);
039
040  private static class ForTestRackManager extends RackManager {
041    int numRacks;
042    Map<String, Integer> serverIndexes = new HashMap<String, Integer>();
043    int numServers = 0;
044
045    public ForTestRackManager(int numRacks) {
046      this.numRacks = numRacks;
047    }
048
049    @Override
050    public String getRack(ServerName server) {
051      String key = server.getServerName();
052      if (!serverIndexes.containsKey(key)) {
053        serverIndexes.put(key, numServers++);
054      }
055      return "rack_" + serverIndexes.get(key) % numRacks;
056    }
057  }
058
059  @Test
060  public void testRegionReplicationOnMidClusterWithRacks() {
061    conf.setLong(StochasticLoadBalancer.MAX_STEPS_KEY, 100000000L);
062    conf.setBoolean("hbase.master.balancer.stochastic.runMaxSteps", true);
063    conf.setLong("hbase.master.balancer.stochastic.maxRunningTime", 120 * 1000); // 120 sec
064    loadBalancer.onConfigurationChange(conf);
065    int numNodes = 4;
066    int numRegions = numNodes * 1;
067    int replication = 3; // 3 replicas per region
068    int numRegionsPerServer = 1;
069    int numTables = 1;
070    int numRacks = 3; // all replicas should be on a different rack
071    Map<ServerName, List<RegionInfo>> serverMap =
072      createServerMap(numNodes, numRegions, numRegionsPerServer, replication, numTables);
073    RackManager rm = new ForTestRackManager(numRacks);
074
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}