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.rsgroup;
019
020import static java.lang.Thread.sleep;
021import static org.junit.Assert.assertTrue;
022
023import java.io.IOException;
024
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.HBaseTestingUtility;
028import org.apache.hadoop.hbase.HConstants;
029import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
030import org.apache.hadoop.hbase.testclassification.MediumTests;
031import org.junit.AfterClass;
032import org.junit.BeforeClass;
033import org.junit.ClassRule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039/**
040 * Test enable RSGroup
041 */
042@Category({ MediumTests.class })
043public class TestEnableRSGroup {
044
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE =
047      HBaseClassTestRule.forClass(TestEnableRSGroup.class);
048
049  protected static final Logger LOG = LoggerFactory.getLogger(TestEnableRSGroup.class);
050
051  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
052
053  @BeforeClass
054  public static void setUp() throws Exception {
055    final Configuration conf = TEST_UTIL.getConfiguration();
056    conf.setBoolean(CoprocessorHost.COPROCESSORS_ENABLED_CONF_KEY, true);
057    TEST_UTIL.startMiniCluster(5);
058  }
059
060  @AfterClass
061  public static void tearDown() throws Exception {
062    LOG.info("to stop miniCluster");
063    TEST_UTIL.shutdownMiniCluster();
064  }
065
066  @Test
067  public void testEnableRSGroup() throws IOException, InterruptedException {
068    TEST_UTIL.getMiniHBaseCluster().stopMaster(0);
069    TEST_UTIL.getMiniHBaseCluster().waitOnMaster(0);
070
071    LOG.info("stopped master...");
072    final Configuration conf = TEST_UTIL.getMiniHBaseCluster().getConfiguration();
073    conf.set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY, RSGroupAdminEndpoint.class.getName());
074    conf.set(HConstants.HBASE_MASTER_LOADBALANCER_CLASS, RSGroupBasedLoadBalancer.class.getName());
075
076    TEST_UTIL.getMiniHBaseCluster().startMaster();
077    TEST_UTIL.getMiniHBaseCluster().waitForActiveAndReadyMaster(60000);
078    LOG.info("started master...");
079
080    // check if master started successfully
081    assertTrue(TEST_UTIL.getMiniHBaseCluster().getMaster() != null);
082
083    // wait RSGroupBasedLoadBalancer online
084    RSGroupBasedLoadBalancer loadBalancer =
085        (RSGroupBasedLoadBalancer) TEST_UTIL.getMiniHBaseCluster().getMaster().getLoadBalancer();
086    long start = System.currentTimeMillis();
087    while (System.currentTimeMillis() - start <= 60000 && !loadBalancer.isOnline()) {
088      LOG.info("waiting for rsgroup load balancer onLine...");
089      sleep(200);
090    }
091
092    assertTrue(loadBalancer.isOnline());
093  }
094
095}