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 TestEnableRSGroups {
044
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE =
047      HBaseClassTestRule.forClass(TestEnableRSGroups.class);
048
049  protected static final Logger LOG = LoggerFactory.getLogger(TestEnableRSGroups.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,
075      RSGroupBasedLoadBalancer.class.getName());
076
077    TEST_UTIL.getMiniHBaseCluster().startMaster();
078    TEST_UTIL.getMiniHBaseCluster().waitForActiveAndReadyMaster(60000);
079    LOG.info("started master...");
080
081    // check if master started successfully
082    assertTrue(TEST_UTIL.getMiniHBaseCluster().getMaster() != null);
083
084    // wait RSGroupBasedLoadBalancer online
085    RSGroupBasedLoadBalancer loadBalancer =
086        (RSGroupBasedLoadBalancer) TEST_UTIL.getMiniHBaseCluster().getMaster().getLoadBalancer();
087    long start = System.currentTimeMillis();
088    while (System.currentTimeMillis() - start <= 60000 && !loadBalancer.isOnline()) {
089      LOG.info("waiting for rsgroup load balancer onLine...");
090      sleep(200);
091    }
092
093    assertTrue(loadBalancer.isOnline());
094  }
095
096}