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 */
018
019package org.apache.hadoop.hbase.master.balancer;
020
021import java.util.Arrays;
022import java.util.List;
023import java.util.Map;
024
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HConstants;
027import org.apache.hadoop.hbase.ServerName;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.LogEntry;
030import org.apache.hadoop.hbase.client.RegionInfo;
031import org.apache.hadoop.hbase.master.RegionPlan;
032import org.apache.hadoop.hbase.namequeues.BalancerDecisionDetails;
033import org.apache.hadoop.hbase.namequeues.request.NamedQueueGetRequest;
034import org.apache.hadoop.hbase.namequeues.response.NamedQueueGetResponse;
035import org.apache.hadoop.hbase.testclassification.MasterTests;
036import org.apache.hadoop.hbase.testclassification.MediumTests;
037import org.junit.Assert;
038import org.junit.ClassRule;
039import org.junit.Test;
040import org.junit.experimental.categories.Category;
041
042import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
043import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos;
044import org.apache.hadoop.hbase.shaded.protobuf.generated.RecentLogs;
045
046/**
047 * Test BalancerDecision ring buffer using namedQueue interface
048 */
049@Category({ MasterTests.class, MediumTests.class })
050public class TestBalancerDecision extends BalancerTestBase {
051
052  @ClassRule
053  public static final HBaseClassTestRule CLASS_RULE =
054    HBaseClassTestRule.forClass(TestBalancerDecision.class);
055
056  @Test
057  public void testBalancerDecisions() {
058    conf.setBoolean("hbase.master.balancer.decision.buffer.enabled", true);
059    loadBalancer.setConf(conf);
060    float minCost = conf.getFloat("hbase.master.balancer.stochastic.minCostNeedBalance", 0.05f);
061    conf.setFloat("hbase.master.balancer.stochastic.minCostNeedBalance", 1.0f);
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.setConf(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 =
086        MasterProtos.BalancerDecisionsResponse.newBuilder()
087          .addAllBalancerDecision(balancerDecisions)
088          .build();
089      List<LogEntry> balancerDecisionRecords =
090        ProtobufUtil.getBalancerDecisionEntries(response);
091      Assert.assertTrue(balancerDecisionRecords.size() > 160);
092    } finally {
093      // reset config
094      conf.unset(HConstants.HBASE_MASTER_LOADBALANCE_BYTABLE);
095      conf.setFloat("hbase.master.balancer.stochastic.minCostNeedBalance", minCost);
096      loadBalancer.setConf(conf);
097    }
098  }
099
100  private static boolean needsBalanceIdleRegion(int[] cluster) {
101    return (Arrays.stream(cluster).anyMatch(x -> x > 1)) && (Arrays.stream(cluster)
102      .anyMatch(x -> x < 1));
103  }
104
105}