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