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.assertNotNull;
022import static org.junit.Assert.assertNull;
023import static org.junit.Assert.assertTrue;
024import static org.mockito.Mockito.mock;
025import static org.mockito.Mockito.when;
026
027import java.util.ArrayList;
028import java.util.Arrays;
029import java.util.HashMap;
030import java.util.HashSet;
031import java.util.List;
032import java.util.Map;
033import java.util.Queue;
034import java.util.Set;
035import java.util.TreeMap;
036import org.apache.hadoop.conf.Configuration;
037import org.apache.hadoop.hbase.ClusterMetrics;
038import org.apache.hadoop.hbase.HBaseClassTestRule;
039import org.apache.hadoop.hbase.HBaseConfiguration;
040import org.apache.hadoop.hbase.HConstants;
041import org.apache.hadoop.hbase.RegionMetrics;
042import org.apache.hadoop.hbase.ServerMetrics;
043import org.apache.hadoop.hbase.ServerName;
044import org.apache.hadoop.hbase.Size;
045import org.apache.hadoop.hbase.TableName;
046import org.apache.hadoop.hbase.client.RegionInfo;
047import org.apache.hadoop.hbase.master.RegionPlan;
048import org.apache.hadoop.hbase.testclassification.MasterTests;
049import org.apache.hadoop.hbase.testclassification.MediumTests;
050import org.apache.hadoop.hbase.util.Bytes;
051import org.junit.ClassRule;
052import org.junit.Test;
053import org.junit.experimental.categories.Category;
054
055import org.apache.hbase.thirdparty.org.apache.commons.collections4.CollectionUtils;
056
057@Category({ MasterTests.class, MediumTests.class })
058public class TestStochasticLoadBalancer extends StochasticBalancerTestBase {
059
060  @ClassRule
061  public static final HBaseClassTestRule CLASS_RULE =
062      HBaseClassTestRule.forClass(TestStochasticLoadBalancer.class);
063
064  private static final String REGION_KEY = "testRegion";
065
066  // Mapping of locality test -> expected locality
067  private float[] expectedLocalities = {1.0f, 0.0f, 0.50f, 0.25f, 1.0f};
068
069  /**
070   * Data set for testLocalityCost:
071   * [test][0][0] = mapping of server to number of regions it hosts
072   * [test][region + 1][0] = server that region is hosted on
073   * [test][region + 1][server + 1] = locality for region on server
074   */
075  private int[][][] clusterRegionLocationMocks = new int[][][]{
076    // Test 1: each region is entirely on server that hosts it
077    new int[][]{
078      new int[]{2, 1, 1},
079      new int[]{2, 0, 0, 100},   // region 0 is hosted and entirely local on server 2
080      new int[]{0, 100, 0, 0},   // region 1 is hosted and entirely on server 0
081      new int[]{0, 100, 0, 0},   // region 2 is hosted and entirely on server 0
082      new int[]{1, 0, 100, 0},   // region 3 is hosted and entirely on server 1
083    },
084
085    // Test 2: each region is 0% local on the server that hosts it
086    new int[][]{
087      new int[]{1, 2, 1},
088      new int[]{0, 0, 0, 100},   // region 0 is hosted and entirely local on server 2
089      new int[]{1, 100, 0, 0},   // region 1 is hosted and entirely on server 0
090      new int[]{1, 100, 0, 0},   // region 2 is hosted and entirely on server 0
091      new int[]{2, 0, 100, 0},   // region 3 is hosted and entirely on server 1
092    },
093
094    // Test 3: each region is 25% local on the server that hosts it (and 50% locality is possible)
095    new int[][]{
096      new int[]{1, 2, 1},
097      new int[]{0, 25, 0, 50},   // region 0 is hosted and entirely local on server 2
098      new int[]{1, 50, 25, 0},   // region 1 is hosted and entirely on server 0
099      new int[]{1, 50, 25, 0},   // region 2 is hosted and entirely on server 0
100      new int[]{2, 0, 50, 25},   // region 3 is hosted and entirely on server 1
101    },
102
103    // Test 4: each region is 25% local on the server that hosts it (and 100% locality is possible)
104    new int[][]{
105      new int[]{1, 2, 1},
106      new int[]{0, 25, 0, 100},   // region 0 is hosted and entirely local on server 2
107      new int[]{1, 100, 25, 0},   // region 1 is hosted and entirely on server 0
108      new int[]{1, 100, 25, 0},   // region 2 is hosted and entirely on server 0
109      new int[]{2, 0, 100, 25},   // region 3 is hosted and entirely on server 1
110    },
111
112    // Test 5: each region is 75% local on the server that hosts it (and 75% locality is possible
113    // everywhere)
114    new int[][]{
115      new int[]{1, 2, 1},
116      new int[]{0, 75, 75, 75},   // region 0 is hosted and entirely local on server 2
117      new int[]{1, 75, 75, 75},   // region 1 is hosted and entirely on server 0
118      new int[]{1, 75, 75, 75},   // region 2 is hosted and entirely on server 0
119      new int[]{2, 75, 75, 75},   // region 3 is hosted and entirely on server 1
120    },
121  };
122
123  private ServerMetrics mockServerMetricsWithCpRequests(List<RegionInfo> regionsOnServer,
124    long cpRequestCount) {
125    ServerMetrics serverMetrics = mock(ServerMetrics.class);
126    Map<byte[], RegionMetrics> regionLoadMap = new TreeMap<>(Bytes.BYTES_COMPARATOR);
127    for (RegionInfo info : regionsOnServer) {
128      RegionMetrics rl = mock(RegionMetrics.class);
129      when(rl.getReadRequestCount()).thenReturn(0L);
130      when(rl.getCpRequestCount()).thenReturn(cpRequestCount);
131      when(rl.getWriteRequestCount()).thenReturn(0L);
132      when(rl.getMemStoreSize()).thenReturn(Size.ZERO);
133      when(rl.getStoreFileSize()).thenReturn(Size.ZERO);
134      regionLoadMap.put(info.getRegionName(), rl);
135    }
136    when(serverMetrics.getRegionMetrics()).thenReturn(regionLoadMap);
137    return serverMetrics;
138  }
139
140  @Test
141  public void testCPRequestCost() {
142    // in order to pass needsBalance judgement
143    conf.setFloat("hbase.master.balancer.stochastic.cpRequestCost", 10000f);
144    loadBalancer.onConfigurationChange(conf);
145    // mock cluster State
146    Map<ServerName, List<RegionInfo>> clusterState = new HashMap<ServerName, List<RegionInfo>>();
147    ServerName serverA = randomServer(3).getServerName();
148    ServerName serverB = randomServer(3).getServerName();
149    ServerName serverC = randomServer(3).getServerName();
150    List<RegionInfo> regionsOnServerA = randomRegions(3);
151    List<RegionInfo> regionsOnServerB = randomRegions(3);
152    List<RegionInfo> regionsOnServerC = randomRegions(3);
153    clusterState.put(serverA, regionsOnServerA);
154    clusterState.put(serverB, regionsOnServerB);
155    clusterState.put(serverC, regionsOnServerC);
156    // mock ClusterMetrics
157    Map<ServerName, ServerMetrics> serverMetricsMap = new TreeMap<>();
158    serverMetricsMap.put(serverA, mockServerMetricsWithCpRequests(regionsOnServerA, 0));
159    serverMetricsMap.put(serverB, mockServerMetricsWithCpRequests(regionsOnServerB, 0));
160    serverMetricsMap.put(serverC, mockServerMetricsWithCpRequests(regionsOnServerC, 0));
161    ClusterMetrics clusterStatus = mock(ClusterMetrics.class);
162    when(clusterStatus.getLiveServerMetrics()).thenReturn(serverMetricsMap);
163    loadBalancer.updateClusterMetrics(clusterStatus);
164
165    // CPRequestCostFunction are Rate based, So doing setClusterMetrics again
166    // this time, regions on serverA with more cpRequestCount load
167    // serverA : 1000,1000,1000
168    // serverB : 0,0,0
169    // serverC : 0,0,0
170    // so should move two regions from serverA to serverB & serverC
171    serverMetricsMap = new TreeMap<>();
172    serverMetricsMap.put(serverA, mockServerMetricsWithCpRequests(regionsOnServerA, 1000));
173    serverMetricsMap.put(serverB, mockServerMetricsWithCpRequests(regionsOnServerB, 0));
174    serverMetricsMap.put(serverC, mockServerMetricsWithCpRequests(regionsOnServerC, 0));
175    clusterStatus = mock(ClusterMetrics.class);
176    when(clusterStatus.getLiveServerMetrics()).thenReturn(serverMetricsMap);
177    loadBalancer.updateClusterMetrics(clusterStatus);
178
179    List<RegionPlan> plans =
180        loadBalancer.balanceTable(HConstants.ENSEMBLE_TABLE_NAME, clusterState);
181    Set<RegionInfo> regionsMoveFromServerA = new HashSet<>();
182    Set<ServerName> targetServers = new HashSet<>();
183    for (RegionPlan plan : plans) {
184      if (plan.getSource().equals(serverA)) {
185        regionsMoveFromServerA.add(plan.getRegionInfo());
186        targetServers.add(plan.getDestination());
187      }
188    }
189    // should move 2 regions from serverA, one moves to serverB, the other moves to serverC
190    assertEquals(2, regionsMoveFromServerA.size());
191    assertEquals(2, targetServers.size());
192    assertTrue(regionsOnServerA.containsAll(regionsMoveFromServerA));
193    // reset config
194    conf.setFloat("hbase.master.balancer.stochastic.cpRequestCost", 5f);
195    loadBalancer.onConfigurationChange(conf);
196  }
197
198  @Test
199  public void testKeepRegionLoad() throws Exception {
200    ServerName sn = ServerName.valueOf("test:8080", 100);
201    int numClusterStatusToAdd = 20000;
202    for (int i = 0; i < numClusterStatusToAdd; i++) {
203      ServerMetrics sl = mock(ServerMetrics.class);
204
205      RegionMetrics rl = mock(RegionMetrics.class);
206      when(rl.getReadRequestCount()).thenReturn(0L);
207      when(rl.getCpRequestCount()).thenReturn(0L);
208      when(rl.getWriteRequestCount()).thenReturn(0L);
209      when(rl.getMemStoreSize()).thenReturn(Size.ZERO);
210      when(rl.getStoreFileSize()).thenReturn(new Size(i, Size.Unit.MEGABYTE));
211
212      Map<byte[], RegionMetrics> regionLoadMap = new TreeMap<>(Bytes.BYTES_COMPARATOR);
213      regionLoadMap.put(Bytes.toBytes(REGION_KEY), rl);
214      when(sl.getRegionMetrics()).thenReturn(regionLoadMap);
215
216      ClusterMetrics clusterStatus = mock(ClusterMetrics.class);
217      Map<ServerName, ServerMetrics> serverMetricsMap = new TreeMap<>();
218      serverMetricsMap.put(sn, sl);
219      when(clusterStatus.getLiveServerMetrics()).thenReturn(serverMetricsMap);
220
221      loadBalancer.updateClusterMetrics(clusterStatus);
222    }
223
224    String regionNameAsString = RegionInfo.getRegionNameAsString(Bytes.toBytes(REGION_KEY));
225    assertTrue(loadBalancer.loads.get(regionNameAsString) != null);
226    assertTrue(loadBalancer.loads.get(regionNameAsString).size() == 15);
227
228    Queue<BalancerRegionLoad> loads = loadBalancer.loads.get(regionNameAsString);
229    int i = 0;
230    while(loads.size() > 0) {
231      BalancerRegionLoad rl = loads.remove();
232      assertEquals(i + (numClusterStatusToAdd - 15), rl.getStorefileSizeMB());
233      i ++;
234    }
235  }
236
237  @Test
238  public void testUpdateBalancerLoadInfo(){
239    int[] cluster = new int[] { 10, 0 };
240    Map<ServerName, List<RegionInfo>> servers = mockClusterServers(cluster);
241    BalancerClusterState clusterState = mockCluster(cluster);
242    Map<TableName, Map<ServerName, List<RegionInfo>>> LoadOfAllTable =
243      (Map) mockClusterServersWithTables(servers);
244    try {
245      boolean[] perTableBalancerConfigs = { true, false };
246      for (boolean isByTable : perTableBalancerConfigs) {
247        conf.setBoolean(HConstants.HBASE_MASTER_LOADBALANCE_BYTABLE, isByTable);
248        loadBalancer.onConfigurationChange(conf);
249        dummyMetricsStochasticBalancer.clearDummyMetrics();
250        loadBalancer.updateBalancerLoadInfo(LoadOfAllTable);
251        assertTrue("Metrics should be recorded!",
252          dummyMetricsStochasticBalancer.getDummyCostsMap() != null && !dummyMetricsStochasticBalancer.getDummyCostsMap().isEmpty());
253
254        String metricRecordKey;
255        if (isByTable) {
256          metricRecordKey = "table1#" + StochasticLoadBalancer.OVERALL_COST_FUNCTION_NAME;
257        } else {
258          metricRecordKey = HConstants.ENSEMBLE_TABLE_NAME + "#" + StochasticLoadBalancer.OVERALL_COST_FUNCTION_NAME;
259        }
260        double curOverallCost = loadBalancer.computeCost(clusterState, Double.MAX_VALUE);
261        double curOverallCostInMetrics =
262          dummyMetricsStochasticBalancer.getDummyCostsMap().get(metricRecordKey);
263        assertEquals(curOverallCost, curOverallCostInMetrics, 0.001);
264      }
265    }finally {
266      conf.unset(HConstants.HBASE_MASTER_LOADBALANCE_BYTABLE);
267      loadBalancer.onConfigurationChange(conf);
268    }
269  }
270
271  @Test
272  public void testUpdateStochasticCosts() {
273    float minCost = conf.getFloat("hbase.master.balancer.stochastic.minCostNeedBalance", 0.05f);
274    try {
275      int[] cluster = new int[] { 10, 0 };
276      Map<ServerName, List<RegionInfo>> servers = mockClusterServers(cluster);
277      BalancerClusterState clusterState = mockCluster(cluster);
278      conf.setFloat("hbase.master.balancer.stochastic.minCostNeedBalance", 1.0f);
279      conf.setBoolean(HConstants.HBASE_MASTER_LOADBALANCE_BYTABLE, false);
280      loadBalancer.onConfigurationChange(conf);
281      dummyMetricsStochasticBalancer.clearDummyMetrics();
282      List<RegionPlan> plans = loadBalancer.balanceCluster((Map)mockClusterServersWithTables(servers));
283
284      assertTrue("Balance plan should not be empty!", plans != null && !plans.isEmpty());
285      assertTrue("There should be metrics record in MetricsStochasticBalancer",
286        !dummyMetricsStochasticBalancer.getDummyCostsMap().isEmpty());
287
288      double overallCostOfCluster = loadBalancer.computeCost(clusterState, Double.MAX_VALUE);
289      double overallCostInMetrics = dummyMetricsStochasticBalancer.getDummyCostsMap().get(
290        HConstants.ENSEMBLE_TABLE_NAME + "#" + StochasticLoadBalancer.OVERALL_COST_FUNCTION_NAME);
291      assertEquals(overallCostOfCluster, overallCostInMetrics, 0.001);
292    } finally {
293      //reset config
294      conf.setFloat("hbase.master.balancer.stochastic.minCostNeedBalance", minCost);
295      conf.unset(HConstants.HBASE_MASTER_LOADBALANCE_BYTABLE);
296      loadBalancer.onConfigurationChange(conf);
297    }
298  }
299
300  @Test
301  public void testUpdateStochasticCostsIfBalanceNotRan() {
302    float minCost = conf.getFloat("hbase.master.balancer.stochastic.minCostNeedBalance", 0.05f);
303    try {
304      int[] cluster = new int[] { 10, 10 };
305      Map<ServerName, List<RegionInfo>> servers = mockClusterServers(cluster);
306      BalancerClusterState clusterState = mockCluster(cluster);
307      conf.setFloat("hbase.master.balancer.stochastic.minCostNeedBalance", Float.MAX_VALUE);
308      conf.setBoolean(HConstants.HBASE_MASTER_LOADBALANCE_BYTABLE, false);
309      loadBalancer.onConfigurationChange(conf);
310      dummyMetricsStochasticBalancer.clearDummyMetrics();
311      List<RegionPlan> plans = loadBalancer.balanceCluster( (Map) mockClusterServersWithTables(servers));
312
313      assertTrue("Balance plan should be empty!", plans == null || plans.isEmpty());
314      assertTrue("There should be metrics record in MetricsStochasticBalancer!",
315        !dummyMetricsStochasticBalancer.getDummyCostsMap().isEmpty());
316
317      double overallCostOfCluster = loadBalancer.computeCost(clusterState, Double.MAX_VALUE);
318      double overallCostInMetrics = dummyMetricsStochasticBalancer.getDummyCostsMap().get(
319        HConstants.ENSEMBLE_TABLE_NAME + "#" + StochasticLoadBalancer.OVERALL_COST_FUNCTION_NAME);
320      assertEquals(overallCostOfCluster, overallCostInMetrics, 0.001);
321    } finally {
322      //reset config
323      conf.setFloat("hbase.master.balancer.stochastic.minCostNeedBalance", minCost);
324      conf.unset(HConstants.HBASE_MASTER_LOADBALANCE_BYTABLE);
325      loadBalancer.onConfigurationChange(conf);
326    }
327  }
328
329  @Test
330  public void testNeedBalance() {
331    float minCost = conf.getFloat("hbase.master.balancer.stochastic.minCostNeedBalance", 0.05f);
332    conf.setFloat("hbase.master.balancer.stochastic.minCostNeedBalance", 1.0f);
333    try {
334      // Test with/without per table balancer.
335      boolean[] perTableBalancerConfigs = {true, false};
336      for (boolean isByTable : perTableBalancerConfigs) {
337        conf.setBoolean(HConstants.HBASE_MASTER_LOADBALANCE_BYTABLE, isByTable);
338        loadBalancer.onConfigurationChange(conf);
339        for (int[] mockCluster : clusterStateMocks) {
340          Map<ServerName, List<RegionInfo>> servers = mockClusterServers(mockCluster);
341          Map<TableName, Map<ServerName, List<RegionInfo>>> LoadOfAllTable =
342              (Map) mockClusterServersWithTables(servers);
343          List<RegionPlan> plans = loadBalancer.balanceCluster(LoadOfAllTable);
344          boolean emptyPlans = plans == null || plans.isEmpty();
345          assertTrue(emptyPlans || needsBalanceIdleRegion(mockCluster));
346        }
347      }
348    } finally {
349      // reset config
350      conf.unset(HConstants.HBASE_MASTER_LOADBALANCE_BYTABLE);
351      conf.setFloat("hbase.master.balancer.stochastic.minCostNeedBalance", minCost);
352      loadBalancer.onConfigurationChange(conf);
353    }
354  }
355
356  @Test
357  public void testLocalityCost() throws Exception {
358    Configuration conf = HBaseConfiguration.create();
359    CostFunction
360        costFunction = new ServerLocalityCostFunction(conf);
361
362    for (int test = 0; test < clusterRegionLocationMocks.length; test++) {
363      int[][] clusterRegionLocations = clusterRegionLocationMocks[test];
364      MockCluster cluster = new MockCluster(clusterRegionLocations);
365      costFunction.prepare(cluster);
366      double cost = costFunction.cost();
367      double expected = 1 - expectedLocalities[test];
368      assertEquals(expected, cost, 0.001);
369    }
370  }
371
372  @Test
373  public void testMoveCostMultiplier() throws Exception {
374    Configuration conf = HBaseConfiguration.create();
375    ClusterInfoProvider provider = mock(ClusterInfoProvider.class);
376    CostFunction costFunction =
377      new MoveCostFunction(conf, provider);
378    when(provider.isOffPeakHour()).thenReturn(false);
379    BalancerClusterState cluster = mockCluster(clusterStateMocks[0]);
380    costFunction.prepare(cluster);
381    costFunction.cost();
382    assertEquals(MoveCostFunction.DEFAULT_MOVE_COST,
383      costFunction.getMultiplier(), 0.01);
384
385    // In offpeak hours, the multiplier of move cost should be lower
386    when(provider.isOffPeakHour()).thenReturn(true);
387    costFunction.prepare(cluster);
388    costFunction.cost();
389    assertEquals(MoveCostFunction.DEFAULT_MOVE_COST_OFFPEAK,
390      costFunction.getMultiplier(), 0.01);
391  }
392
393  @Test
394  public void testMoveCost() throws Exception {
395    Configuration conf = HBaseConfiguration.create();
396    CostFunction costFunction = new MoveCostFunction(conf, new DummyClusterInfoProvider(conf));
397    for (int[] mockCluster : clusterStateMocks) {
398      BalancerClusterState cluster = mockCluster(mockCluster);
399      costFunction.prepare(cluster);
400      double cost = costFunction.cost();
401      assertEquals(0.0f, cost, 0.001);
402
403      // cluster region number is smaller than maxMoves=600
404      cluster.setNumRegions(200);
405      cluster.setNumMovedRegions(10);
406      cost = costFunction.cost();
407      assertEquals(0.05f, cost, 0.001);
408      cluster.setNumMovedRegions(100);
409      cost = costFunction.cost();
410      assertEquals(0.5f, cost, 0.001);
411      cluster.setNumMovedRegions(200);
412      cost = costFunction.cost();
413      assertEquals(1.0f, cost, 0.001);
414
415
416      // cluster region number is bigger than maxMoves=2500
417      cluster.setNumRegions(10000);
418      cluster.setNumMovedRegions(250);
419      cost = costFunction.cost();
420      assertEquals(0.025f, cost, 0.001);
421      cluster.setNumMovedRegions(1250);
422      cost = costFunction.cost();
423      assertEquals(0.125f, cost, 0.001);
424      cluster.setNumMovedRegions(2500);
425      cost = costFunction.cost();
426      assertEquals(0.25f, cost, 0.01);
427    }
428  }
429
430  @Test
431  public void testSkewCost() {
432    Configuration conf = HBaseConfiguration.create();
433    CostFunction
434        costFunction = new RegionCountSkewCostFunction(conf);
435    for (int[] mockCluster : clusterStateMocks) {
436      costFunction.prepare(mockCluster(mockCluster));
437      double cost = costFunction.cost();
438      assertTrue(cost >= 0);
439      assertTrue(cost <= 1.01);
440    }
441
442    costFunction.prepare(mockCluster(new int[]{0, 0, 0, 0, 1}));
443    assertEquals(0,costFunction.cost(), 0.01);
444    costFunction.prepare(mockCluster(new int[]{0, 0, 0, 1, 1}));
445    assertEquals(0, costFunction.cost(), 0.01);
446    costFunction.prepare(mockCluster(new int[]{0, 0, 1, 1, 1}));
447    assertEquals(0, costFunction.cost(), 0.01);
448    costFunction.prepare(mockCluster(new int[]{0, 1, 1, 1, 1}));
449    assertEquals(0, costFunction.cost(), 0.01);
450    costFunction.prepare(mockCluster(new int[]{1, 1, 1, 1, 1}));
451    assertEquals(0, costFunction.cost(), 0.01);
452    costFunction.prepare(mockCluster(new int[]{10000, 0, 0, 0, 0}));
453    assertEquals(1, costFunction.cost(), 0.01);
454  }
455
456  @Test
457  public void testCostAfterUndoAction() {
458    final int runs = 10;
459    loadBalancer.onConfigurationChange(conf);
460    for (int[] mockCluster : clusterStateMocks) {
461      BalancerClusterState cluster = mockCluster(mockCluster);
462      loadBalancer.initCosts(cluster);
463      for (int i = 0; i != runs; ++i) {
464        final double expectedCost = loadBalancer.computeCost(cluster, Double.MAX_VALUE);
465        BalanceAction action = loadBalancer.nextAction(cluster);
466        cluster.doAction(action);
467        loadBalancer.updateCostsAndWeightsWithAction(cluster, action);
468        BalanceAction undoAction = action.undoAction();
469        cluster.doAction(undoAction);
470        loadBalancer.updateCostsAndWeightsWithAction(cluster, undoAction);
471        final double actualCost = loadBalancer.computeCost(cluster, Double.MAX_VALUE);
472        assertEquals(expectedCost, actualCost, 0);
473      }
474    }
475  }
476
477  @Test
478  public void testTableSkewCost() {
479    Configuration conf = HBaseConfiguration.create();
480    CostFunction
481        costFunction = new TableSkewCostFunction(conf);
482    for (int[] mockCluster : clusterStateMocks) {
483      BalancerClusterState cluster = mockCluster(mockCluster);
484      costFunction.prepare(cluster);
485      double cost = costFunction.cost();
486      assertTrue(cost >= 0);
487      assertTrue(cost <= 1.01);
488    }
489  }
490
491  @Test
492  public void testRegionLoadCost() {
493    List<BalancerRegionLoad> regionLoads = new ArrayList<>();
494    for (int i = 1; i < 5; i++) {
495      BalancerRegionLoad regionLoad = mock(BalancerRegionLoad.class);
496      when(regionLoad.getReadRequestsCount()).thenReturn((long) i);
497      when(regionLoad.getCpRequestsCount()).thenReturn((long) i);
498      when(regionLoad.getStorefileSizeMB()).thenReturn(i);
499      regionLoads.add(regionLoad);
500    }
501
502    Configuration conf = HBaseConfiguration.create();
503    ReadRequestCostFunction readCostFunction =
504        new ReadRequestCostFunction(conf);
505    double rateResult = readCostFunction.getRegionLoadCost(regionLoads);
506    // read requests are treated as a rate so the average rate here is simply 1
507    assertEquals(1, rateResult, 0.01);
508
509    CPRequestCostFunction cpCostFunction =
510        new CPRequestCostFunction(conf);
511    rateResult = cpCostFunction.getRegionLoadCost(regionLoads);
512    // coprocessor requests are treated as a rate so the average rate here is simply 1
513    assertEquals(1, rateResult, 0.01);
514
515    StoreFileCostFunction storeFileCostFunction =
516        new StoreFileCostFunction(conf);
517    double result = storeFileCostFunction.getRegionLoadCost(regionLoads);
518    // storefile size cost is simply an average of it's value over time
519    assertEquals(2.5, result, 0.01);
520  }
521
522  @Test
523  public void testLosingRs() throws Exception {
524    int numNodes = 3;
525    int numRegions = 20;
526    int numRegionsPerServer = 3; //all servers except one
527    int replication = 1;
528    int numTables = 2;
529
530    Map<ServerName, List<RegionInfo>> serverMap =
531        createServerMap(numNodes, numRegions, numRegionsPerServer, replication, numTables);
532    List<ServerAndLoad> list = convertToList(serverMap);
533
534
535    List<RegionPlan> plans = loadBalancer.balanceTable(HConstants.ENSEMBLE_TABLE_NAME, serverMap);
536    assertNotNull(plans);
537
538    // Apply the plan to the mock cluster.
539    List<ServerAndLoad> balancedCluster = reconcile(list, plans, serverMap);
540
541    assertClusterAsBalanced(balancedCluster);
542
543    ServerName sn = serverMap.keySet().toArray(new ServerName[serverMap.size()])[0];
544
545    ServerName deadSn = ServerName.valueOf(sn.getHostname(), sn.getPort(), sn.getStartcode() - 100);
546
547    serverMap.put(deadSn, new ArrayList<>(0));
548
549    plans = loadBalancer.balanceTable(HConstants.ENSEMBLE_TABLE_NAME, serverMap);
550    assertNull(plans);
551  }
552
553  @Test
554  public void testAdditionalCostFunction() {
555    try {
556      conf.set(StochasticLoadBalancer.COST_FUNCTIONS_COST_FUNCTIONS_KEY,
557        DummyCostFunction.class.getName());
558
559      loadBalancer.onConfigurationChange(conf);
560      assertTrue(Arrays.
561        asList(loadBalancer.getCostFunctionNames()).
562        contains(DummyCostFunction.class.getSimpleName()));
563    } finally {
564      conf.unset(StochasticLoadBalancer.COST_FUNCTIONS_COST_FUNCTIONS_KEY);
565      loadBalancer.onConfigurationChange(conf);
566    }
567  }
568
569  @Test
570  public void testDefaultCostFunctionList() {
571    List<String> expected = Arrays.asList(
572      RegionCountSkewCostFunction.class.getSimpleName(),
573      PrimaryRegionCountSkewCostFunction.class.getSimpleName(),
574      MoveCostFunction.class.getSimpleName(),
575      RackLocalityCostFunction.class.getSimpleName(),
576      TableSkewCostFunction.class.getSimpleName(),
577      RegionReplicaHostCostFunction.class.getSimpleName(),
578      RegionReplicaRackCostFunction.class.getSimpleName(),
579      ReadRequestCostFunction.class.getSimpleName(),
580      CPRequestCostFunction.class.getSimpleName(),
581      WriteRequestCostFunction.class.getSimpleName(),
582      MemStoreSizeCostFunction.class.getSimpleName(),
583      StoreFileCostFunction.class.getSimpleName()
584    );
585
586    List<String> actual = Arrays.asList(loadBalancer.getCostFunctionNames());
587    assertTrue("ExpectedCostFunctions: " + expected + " ActualCostFunctions: " + actual,
588      CollectionUtils.isEqualCollection(expected, actual));
589  }
590
591  private boolean needsBalanceIdleRegion(int[] cluster) {
592    return Arrays.stream(cluster).anyMatch(x -> x > 1) &&
593      Arrays.stream(cluster).anyMatch(x -> x < 1);
594  }
595
596  // This mock allows us to test the LocalityCostFunction
597  private class MockCluster extends BalancerClusterState {
598
599    private int[][] localities = null;   // [region][server] = percent of blocks
600
601    public MockCluster(int[][] regions) {
602
603      // regions[0] is an array where index = serverIndex an value = number of regions
604      super(mockClusterServers(regions[0], 1), null, null, null);
605
606      localities = new int[regions.length - 1][];
607      for (int i = 1; i < regions.length; i++) {
608        int regionIndex = i - 1;
609        localities[regionIndex] = new int[regions[i].length - 1];
610        regionIndexToServerIndex[regionIndex] = regions[i][0];
611        for (int j = 1; j < regions[i].length; j++) {
612          int serverIndex = j - 1;
613          localities[regionIndex][serverIndex] =
614            regions[i][j] > 100 ? regions[i][j] % 100 : regions[i][j];
615        }
616      }
617    }
618
619    @Override
620    float getLocalityOfRegion(int region, int server) {
621      // convert the locality percentage to a fraction
622      return localities[region][server] / 100.0f;
623    }
624
625    @Override
626    public int getRegionSizeMB(int region) {
627      return 1;
628    }
629  }
630}