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.List;
021import java.util.Map;
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.ServerName;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.client.LogEntry;
027import org.apache.hadoop.hbase.client.RegionInfo;
028import org.apache.hadoop.hbase.namequeues.BalancerRejectionDetails;
029import org.apache.hadoop.hbase.namequeues.request.NamedQueueGetRequest;
030import org.apache.hadoop.hbase.namequeues.response.NamedQueueGetResponse;
031import org.apache.hadoop.hbase.testclassification.MasterTests;
032import org.apache.hadoop.hbase.testclassification.MediumTests;
033import org.junit.Assert;
034import org.junit.ClassRule;
035import org.junit.Test;
036import org.junit.experimental.categories.Category;
037
038import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
039import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProtos;
040import org.apache.hadoop.hbase.shaded.protobuf.generated.RecentLogs;
041
042/**
043 * Test BalancerRejection ring buffer using namedQueue interface
044 */
045@Category({ MasterTests.class, MediumTests.class })
046public class TestBalancerRejection extends BalancerTestBase {
047
048  @ClassRule
049  public static final HBaseClassTestRule CLASS_RULE =
050    HBaseClassTestRule.forClass(TestBalancerRejection.class);
051
052  static class MockCostFunction extends CostFunction {
053    public static double mockCost;
054
055    public MockCostFunction(Configuration c) {
056    }
057
058    @Override
059    protected double cost() {
060      return mockCost;
061    }
062
063    @Override
064    float getMultiplier() {
065      return 1;
066    }
067  }
068
069  @Test
070  public void testBalancerRejections() throws Exception {
071    try {
072      // enabled balancer rejection recording
073      conf.setBoolean(BaseLoadBalancer.BALANCER_REJECTION_BUFFER_ENABLED, true);
074      conf.set(StochasticLoadBalancer.COST_FUNCTIONS_COST_FUNCTIONS_KEY,
075        MockCostFunction.class.getName());
076      loadBalancer.onConfigurationChange(conf);
077      // Simulate 2 servers with 5 regions.
078      Map<ServerName, List<RegionInfo>> servers = mockClusterServers(new int[] { 5, 5 });
079      Map<TableName, Map<ServerName, List<RegionInfo>>> LoadOfAllTable =
080        (Map) mockClusterServersWithTables(servers);
081
082      // Reject case 1: Total cost < 0
083      MockCostFunction.mockCost = -Double.MAX_VALUE;
084      // Since the Balancer was rejected, there should not be any plans
085      Assert.assertNull(loadBalancer.balanceCluster(LoadOfAllTable));
086
087      // Reject case 2: Cost < minCostNeedBalance
088      MockCostFunction.mockCost = 1;
089      conf.setFloat("hbase.master.balancer.stochastic.minCostNeedBalance", Float.MAX_VALUE);
090      loadBalancer.onConfigurationChange(conf);
091      Assert.assertNull(loadBalancer.balanceCluster(LoadOfAllTable));
092
093      // NamedQueue is an async Producer-consumer Pattern, waiting here until it completed
094      int maxWaitingCount = 10;
095      while (maxWaitingCount-- > 0 && getBalancerRejectionLogEntries().size() != 2) {
096        Thread.sleep(1000);
097      }
098      // There are two cases, should be 2 logEntries
099      List<LogEntry> logEntries = getBalancerRejectionLogEntries();
100      Assert.assertEquals(2, logEntries.size());
101      Assert.assertTrue(logEntries.get(0).toJsonPrettyPrint().contains("minCostNeedBalance"));
102      Assert.assertTrue(logEntries.get(1).toJsonPrettyPrint().contains("cost1*multiplier1"));
103    } finally {
104      conf.unset(StochasticLoadBalancer.COST_FUNCTIONS_COST_FUNCTIONS_KEY);
105      conf.unset(BaseLoadBalancer.BALANCER_REJECTION_BUFFER_ENABLED);
106      loadBalancer.onConfigurationChange(conf);
107    }
108  }
109
110  private List<LogEntry> getBalancerRejectionLogEntries() {
111    NamedQueueGetRequest namedQueueGetRequest = new NamedQueueGetRequest();
112    namedQueueGetRequest.setNamedQueueEvent(BalancerRejectionDetails.BALANCER_REJECTION_EVENT);
113    namedQueueGetRequest
114      .setBalancerRejectionsRequest(MasterProtos.BalancerRejectionsRequest.getDefaultInstance());
115    NamedQueueGetResponse namedQueueGetResponse =
116      loadBalancer.namedQueueRecorder.getNamedQueueRecords(namedQueueGetRequest);
117    List<RecentLogs.BalancerRejection> balancerRejections =
118      namedQueueGetResponse.getBalancerRejections();
119    MasterProtos.BalancerRejectionsResponse response = MasterProtos.BalancerRejectionsResponse
120      .newBuilder().addAllBalancerRejection(balancerRejections).build();
121    List<LogEntry> balancerRejectionRecords = ProtobufUtil.getBalancerRejectionEntries(response);
122    return balancerRejectionRecords;
123  }
124}