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.assertTrue;
022
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.testclassification.MasterTests;
025import org.apache.hadoop.hbase.testclassification.SmallTests;
026import org.junit.jupiter.api.BeforeEach;
027import org.junit.jupiter.api.Tag;
028import org.junit.jupiter.api.Test;
029
030@Tag(MasterTests.TAG)
031@Tag(SmallTests.TAG)
032public class TestBalancerConditionals extends BalancerTestBase {
033
034  private BalancerConditionals balancerConditionals;
035  private BalancerClusterState mockCluster;
036
037  @BeforeEach
038  public void setUp() {
039    balancerConditionals = BalancerConditionals.create();
040    mockCluster = mockCluster(new int[] { 0, 1, 2 });
041  }
042
043  @Test
044  public void testDefaultConfiguration() {
045    Configuration conf = new Configuration();
046    balancerConditionals.setConf(conf);
047    balancerConditionals.loadClusterState(mockCluster);
048
049    assertEquals(0, balancerConditionals.getConditionalClasses().size(),
050      "No conditionals should be loaded by default");
051  }
052
053  @Test
054  public void testCustomConditionalsViaConfiguration() {
055    Configuration conf = new Configuration();
056    conf.set(BalancerConditionals.ADDITIONAL_CONDITIONALS_KEY,
057      DistributeReplicasConditional.class.getName());
058
059    balancerConditionals.setConf(conf);
060    balancerConditionals.loadClusterState(mockCluster);
061
062    assertTrue(balancerConditionals.isConditionalBalancingEnabled(),
063      "Custom conditionals should be loaded");
064  }
065
066  @Test
067  public void testInvalidCustomConditionalClass() {
068    Configuration conf = new Configuration();
069    conf.set(BalancerConditionals.ADDITIONAL_CONDITIONALS_KEY, "java.lang.String");
070
071    balancerConditionals.setConf(conf);
072    balancerConditionals.loadClusterState(mockCluster);
073
074    assertEquals(0, balancerConditionals.getConditionalClasses().size(),
075      "Invalid classes should not be loaded as conditionals");
076  }
077
078  @Test
079  public void testMetaTableIsolationConditionalEnabled() {
080    Configuration conf = new Configuration();
081    conf.setBoolean(BalancerConditionals.ISOLATE_META_TABLE_KEY, true);
082
083    balancerConditionals.setConf(conf);
084    balancerConditionals.loadClusterState(mockCluster);
085
086    assertTrue(balancerConditionals.isTableIsolationEnabled(),
087      "MetaTableIsolationConditional should be active");
088  }
089}