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.assertThrows;
022
023import java.io.IOException;
024import org.apache.hadoop.hbase.HBaseConfiguration;
025import org.apache.hadoop.hbase.master.LoadBalancer;
026import org.apache.hadoop.hbase.testclassification.LargeTests;
027import org.apache.hadoop.hbase.testclassification.MasterTests;
028import org.apache.hadoop.hbase.util.AbstractHBaseTool;
029import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
030import org.junit.jupiter.api.AfterEach;
031import org.junit.jupiter.api.BeforeEach;
032import org.junit.jupiter.api.Tag;
033import org.junit.jupiter.api.Test;
034
035@Tag(MasterTests.TAG)
036@Tag(LargeTests.TAG)
037public class TestLoadBalancerPerformanceEvaluation {
038
039  private static final LoadBalancerPerformanceEvaluation tool =
040    new LoadBalancerPerformanceEvaluation();
041
042  @BeforeEach
043  public void setUpBeforeEach() {
044    tool.setConf(HBaseConfiguration.create());
045  }
046
047  @AfterEach
048  public void tearDownAfterEach() {
049    DefaultMetricsSystem.shutdown();
050  }
051
052  @Test
053  public void testLoadBalancerWithDefaultParams() throws IOException {
054    int ret = tool.run(new String[0]);
055    assertEquals(AbstractHBaseTool.EXIT_SUCCESS, ret);
056  }
057
058  @Test
059  public void testStochasticLoadBalancer() throws Exception {
060    testLoadBalancer(StochasticLoadBalancer.class);
061  }
062
063  @Test
064  public void testSimpleLoadBalancer() throws Exception {
065    testLoadBalancer(SimpleLoadBalancer.class);
066  }
067
068  @Test
069  public void testCacheAwareLoadBalancer() throws Exception {
070    testLoadBalancer(CacheAwareLoadBalancer.class);
071  }
072
073  private void testLoadBalancer(Class<? extends LoadBalancer> loadBalancerClass) throws Exception {
074    String[] args =
075      { "-regions", "1000", "-servers", "100", "-load_balancer", loadBalancerClass.getName() };
076    int ret = tool.run(args);
077    assertEquals(AbstractHBaseTool.EXIT_SUCCESS, ret);
078  }
079
080  @Test
081  public void testInvalidRegions() {
082    String[] args = { "-regions", "-100" };
083    IllegalArgumentException exception =
084      assertThrows(IllegalArgumentException.class, () -> tool.run(args));
085    assertEquals("Invalid number of regions!", exception.getMessage());
086  }
087
088  @Test
089  public void testInvalidServers() {
090    String[] args = { "-servers", "0" };
091    IllegalArgumentException exception =
092      assertThrows(IllegalArgumentException.class, () -> tool.run(args));
093    assertEquals("Invalid number of servers!", exception.getMessage());
094  }
095
096  @Test
097  public void testEmptyLoadBalancer() {
098    String[] args = { "-load_balancer", "" };
099    IllegalArgumentException exception =
100      assertThrows(IllegalArgumentException.class, () -> tool.run(args));
101    assertEquals("Invalid load balancer type!", exception.getMessage());
102  }
103}