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