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 static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022import static org.mockito.Mockito.mock;
023import static org.mockito.Mockito.when;
024
025import java.io.IOException;
026import java.util.HashMap;
027import java.util.HashSet;
028import java.util.List;
029import java.util.Map;
030import java.util.Set;
031import java.util.TreeMap;
032import org.apache.hadoop.hbase.ClusterMetrics;
033import org.apache.hadoop.hbase.HBaseClassTestRule;
034import org.apache.hadoop.hbase.RegionMetrics;
035import org.apache.hadoop.hbase.ServerMetrics;
036import org.apache.hadoop.hbase.ServerName;
037import org.apache.hadoop.hbase.Size;
038import org.apache.hadoop.hbase.TableName;
039import org.apache.hadoop.hbase.client.RegionInfo;
040import org.apache.hadoop.hbase.master.RegionPlan;
041import org.apache.hadoop.hbase.rsgroup.RSGroupBasedLoadBalancer;
042import org.apache.hadoop.hbase.rsgroup.RSGroupInfo;
043import org.apache.hadoop.hbase.testclassification.LargeTests;
044import org.apache.hadoop.hbase.util.Bytes;
045import org.apache.hadoop.net.DNSToSwitchMapping;
046import org.junit.BeforeClass;
047import org.junit.ClassRule;
048import org.junit.Test;
049import org.junit.experimental.categories.Category;
050
051/**
052 * Test RSGroupBasedLoadBalancer with StochasticLoadBalancer as internal balancer
053 */
054@Category(LargeTests.class)
055public class TestRSGroupBasedLoadBalancerWithStochasticLoadBalancerAsInternal
056  extends RSGroupableBalancerTestBase {
057  @ClassRule
058  public static final HBaseClassTestRule CLASS_RULE = HBaseClassTestRule
059    .forClass(TestRSGroupBasedLoadBalancerWithStochasticLoadBalancerAsInternal.class);
060  private static RSGroupBasedLoadBalancer loadBalancer;
061
062  @BeforeClass
063  public static void beforeAllTests() throws Exception {
064    groups = new String[] { RSGroupInfo.DEFAULT_GROUP };
065    servers = generateServers(3);
066    groupMap = constructGroupInfo(servers, groups);
067    tableDescs = constructTableDesc(false);
068    conf.set("hbase.regions.slop", "0");
069    conf.setFloat("hbase.master.balancer.stochastic.readRequestCost", 10000f);
070    conf.set("hbase.rsgroup.grouploadbalancer.class",
071      StochasticLoadBalancer.class.getCanonicalName());
072    conf.setClass("hbase.util.ip.to.rack.determiner", MockMapping.class, DNSToSwitchMapping.class);
073    loadBalancer = new RSGroupBasedLoadBalancer();
074    loadBalancer.setMasterServices(getMockedMaster());
075    loadBalancer.initialize();
076  }
077
078  private ServerMetrics mockServerMetricsWithReadRequests(ServerName server,
079    List<RegionInfo> regionsOnServer, long readRequestCount) {
080    ServerMetrics serverMetrics = mock(ServerMetrics.class);
081    Map<byte[], RegionMetrics> regionLoadMap = new TreeMap<>(Bytes.BYTES_COMPARATOR);
082    for (RegionInfo info : regionsOnServer) {
083      RegionMetrics rl = mock(RegionMetrics.class);
084      when(rl.getReadRequestCount()).thenReturn(readRequestCount);
085      when(rl.getCpRequestCount()).thenReturn(0L);
086      when(rl.getWriteRequestCount()).thenReturn(0L);
087      when(rl.getMemStoreSize()).thenReturn(Size.ZERO);
088      when(rl.getStoreFileSize()).thenReturn(Size.ZERO);
089      regionLoadMap.put(info.getRegionName(), rl);
090    }
091    when(serverMetrics.getRegionMetrics()).thenReturn(regionLoadMap);
092    return serverMetrics;
093  }
094
095  /**
096   * Test HBASE-20791
097   */
098  @Test
099  public void testBalanceCluster() throws IOException {
100    // mock cluster State
101    Map<ServerName, List<RegionInfo>> clusterState = new HashMap<ServerName, List<RegionInfo>>();
102    ServerName serverA = servers.get(0);
103    ServerName serverB = servers.get(1);
104    ServerName serverC = servers.get(2);
105    List<RegionInfo> regionsOnServerA = randomRegions(3);
106    List<RegionInfo> regionsOnServerB = randomRegions(3);
107    List<RegionInfo> regionsOnServerC = randomRegions(3);
108    clusterState.put(serverA, regionsOnServerA);
109    clusterState.put(serverB, regionsOnServerB);
110    clusterState.put(serverC, regionsOnServerC);
111    // mock ClusterMetrics
112    Map<ServerName, ServerMetrics> serverMetricsMap = new TreeMap<>();
113    serverMetricsMap.put(serverA, mockServerMetricsWithReadRequests(serverA, regionsOnServerA, 0));
114    serverMetricsMap.put(serverB, mockServerMetricsWithReadRequests(serverB, regionsOnServerB, 0));
115    serverMetricsMap.put(serverC, mockServerMetricsWithReadRequests(serverC, regionsOnServerC, 0));
116    ClusterMetrics clusterStatus = mock(ClusterMetrics.class);
117    when(clusterStatus.getLiveServerMetrics()).thenReturn(serverMetricsMap);
118    loadBalancer.updateClusterMetrics(clusterStatus);
119
120    // ReadRequestCostFunction are Rate based, So doing setClusterMetrics again
121    // this time, regions on serverA with more readRequestCount load
122    // serverA : 1000,1000,1000
123    // serverB : 0,0,0
124    // serverC : 0,0,0
125    // so should move two regions from serverA to serverB & serverC
126    serverMetricsMap = new TreeMap<>();
127    serverMetricsMap.put(serverA,
128      mockServerMetricsWithReadRequests(serverA, regionsOnServerA, 1000));
129    serverMetricsMap.put(serverB, mockServerMetricsWithReadRequests(serverB, regionsOnServerB, 0));
130    serverMetricsMap.put(serverC, mockServerMetricsWithReadRequests(serverC, regionsOnServerC, 0));
131    clusterStatus = mock(ClusterMetrics.class);
132    when(clusterStatus.getLiveServerMetrics()).thenReturn(serverMetricsMap);
133    loadBalancer.updateClusterMetrics(clusterStatus);
134
135    Map<TableName, Map<ServerName, List<RegionInfo>>> LoadOfAllTable =
136      (Map) mockClusterServersWithTables(clusterState);
137    List<RegionPlan> plans = loadBalancer.balanceCluster(LoadOfAllTable);
138    Set<RegionInfo> regionsMoveFromServerA = new HashSet<>();
139    Set<ServerName> targetServers = new HashSet<>();
140    for (RegionPlan plan : plans) {
141      if (plan.getSource().equals(serverA)) {
142        regionsMoveFromServerA.add(plan.getRegionInfo());
143        targetServers.add(plan.getDestination());
144      }
145    }
146    // should move 2 regions from serverA, one moves to serverB, the other moves to serverC
147    assertEquals(2, regionsMoveFromServerA.size());
148    assertEquals(2, targetServers.size());
149    assertTrue(regionsOnServerA.containsAll(regionsMoveFromServerA));
150  }
151}