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.List;
021import java.util.Map;
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.ServerName;
024import org.apache.hadoop.hbase.client.RegionInfo;
025import org.apache.hadoop.hbase.master.RackManager;
026import org.apache.hadoop.hbase.testclassification.LargeTests;
027import org.apache.hadoop.hbase.testclassification.MasterTests;
028import org.junit.ClassRule;
029import org.junit.Test;
030import org.junit.experimental.categories.Category;
031
032@Category({ MasterTests.class, LargeTests.class })
033public class TestStochasticLoadBalancerRegionReplicaWithRacks extends BalancerTestBase {
034
035  @ClassRule
036  public static final HBaseClassTestRule CLASS_RULE =
037      HBaseClassTestRule.forClass(TestStochasticLoadBalancerRegionReplicaWithRacks.class);
038
039  private static class ForTestRackManager extends RackManager {
040    int numRacks;
041
042    public ForTestRackManager(int numRacks) {
043      this.numRacks = numRacks;
044    }
045
046    @Override
047    public String getRack(ServerName server) {
048      return "rack_" + (server.hashCode() % numRacks);
049    }
050  }
051
052  @Test
053  public void testRegionReplicationOnMidClusterWithRacks() {
054    conf.setLong(StochasticLoadBalancer.MAX_STEPS_KEY, 10000000L);
055    conf.setFloat("hbase.master.balancer.stochastic.maxMovePercent", 1.0f);
056    conf.setLong("hbase.master.balancer.stochastic.maxRunningTime", 120 * 1000); // 120 sec
057    loadBalancer.setConf(conf);
058    int numNodes = 30;
059    int numRegions = numNodes * 30;
060    int replication = 3; // 3 replicas per region
061    int numRegionsPerServer = 28;
062    int numTables = 10;
063    int numRacks = 4; // all replicas should be on a different rack
064    Map<ServerName, List<RegionInfo>> serverMap =
065        createServerMap(numNodes, numRegions, numRegionsPerServer, replication, numTables);
066    RackManager rm = new ForTestRackManager(numRacks);
067
068    testWithCluster(serverMap, rm, false, true);
069  }
070}