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