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.Arrays;
021import java.util.List;
022import java.util.Map;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HConstants;
025import org.apache.hadoop.hbase.ServerName;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.LogEntry;
028import org.apache.hadoop.hbase.client.RegionInfo;
029import org.apache.hadoop.hbase.master.RegionPlan;
030import org.apache.hadoop.hbase.namequeues.BalancerDecisionDetails;
031import org.apache.hadoop.hbase.namequeues.request.NamedQueueGetRequest;
032import org.apache.hadoop.hbase.namequeues.response.NamedQueueGetResponse;
033import org.apache.hadoop.hbase.testclassification.MasterTests;
034import org.apache.hadoop.hbase.testclassification.MediumTests;
035import org.junit.Assert;
036import org.junit.ClassRule;
037import org.junit.Test;
038import org.junit.experimental.categories.Category;
039
040import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
041import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos;
042import org.apache.hadoop.hbase.shaded.protobuf.generated.RecentLogs;
043
044/**
045 * Test BalancerDecision ring buffer using namedQueue interface
046 */
047@Category({ MasterTests.class, MediumTests.class })
048public class TestBalancerDecision extends BalancerTestBase {
049
050  @ClassRule
051  public static final HBaseClassTestRule CLASS_RULE =
052    HBaseClassTestRule.forClass(TestBalancerDecision.class);
053
054  @Test
055  public void testBalancerDecisions() {
056    conf.setBoolean("hbase.master.balancer.decision.buffer.enabled", true);
057    loadBalancer.onConfigurationChange(conf);
058    float minCost = conf.getFloat("hbase.master.balancer.stochastic.minCostNeedBalance", 0.05f);
059    float slop = conf.getFloat(HConstants.LOAD_BALANCER_SLOP_KEY, 0.2f);
060    conf.setFloat("hbase.master.balancer.stochastic.minCostNeedBalance", 1.0f);
061    conf.setFloat(HConstants.LOAD_BALANCER_SLOP_KEY, -1f);
062    try {
063      // Test with/without per table balancer.
064      boolean[] perTableBalancerConfigs = { true, false };
065      for (boolean isByTable : perTableBalancerConfigs) {
066        conf.setBoolean(HConstants.HBASE_MASTER_LOADBALANCE_BYTABLE, isByTable);
067        loadBalancer.onConfigurationChange(conf);
068        for (int[] mockCluster : clusterStateMocks) {
069          Map<ServerName, List<RegionInfo>> servers = mockClusterServers(mockCluster);
070          Map<TableName, Map<ServerName, List<RegionInfo>>> LoadOfAllTable =
071            (Map) mockClusterServersWithTables(servers);
072          List<RegionPlan> plans = loadBalancer.balanceCluster(LoadOfAllTable);
073          boolean emptyPlans = plans == null || plans.isEmpty();
074          Assert.assertTrue(emptyPlans || needsBalanceIdleRegion(mockCluster));
075        }
076      }
077      final NamedQueueGetRequest namedQueueGetRequest = new NamedQueueGetRequest();
078      namedQueueGetRequest.setNamedQueueEvent(BalancerDecisionDetails.BALANCER_DECISION_EVENT);
079      namedQueueGetRequest
080        .setBalancerDecisionsRequest(MasterProtos.BalancerDecisionsRequest.getDefaultInstance());
081      NamedQueueGetResponse namedQueueGetResponse =
082        loadBalancer.namedQueueRecorder.getNamedQueueRecords(namedQueueGetRequest);
083      List<RecentLogs.BalancerDecision> balancerDecisions =
084        namedQueueGetResponse.getBalancerDecisions();
085      MasterProtos.BalancerDecisionsResponse response = MasterProtos.BalancerDecisionsResponse
086        .newBuilder().addAllBalancerDecision(balancerDecisions).build();
087      List<LogEntry> balancerDecisionRecords = ProtobufUtil.getBalancerDecisionEntries(response);
088      Assert.assertTrue(balancerDecisionRecords.size() > 160);
089    } finally {
090      // reset config
091      conf.unset(HConstants.HBASE_MASTER_LOADBALANCE_BYTABLE);
092      conf.setFloat("hbase.master.balancer.stochastic.minCostNeedBalance", minCost);
093      conf.setFloat(HConstants.LOAD_BALANCER_SLOP_KEY, slop);
094      loadBalancer.onConfigurationChange(conf);
095    }
096  }
097
098  private static boolean needsBalanceIdleRegion(int[] cluster) {
099    return (Arrays.stream(cluster).anyMatch(x -> x > 1))
100      && (Arrays.stream(cluster).anyMatch(x -> x < 1));
101  }
102}