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