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