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;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
027import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
028import org.junit.AfterClass;
029import org.junit.BeforeClass;
030import org.junit.Test;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034public abstract class EnableRSGroupsTestBase {
035
036  private static final Logger LOG = LoggerFactory.getLogger(TestEnableRSGroupsCompatibility.class);
037
038  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
039
040  @BeforeClass
041  public static void setUp() throws Exception {
042    final Configuration conf = TEST_UTIL.getConfiguration();
043    conf.setBoolean(CoprocessorHost.COPROCESSORS_ENABLED_CONF_KEY, true);
044    TEST_UTIL.startMiniCluster(5);
045  }
046
047  @AfterClass
048  public static void tearDown() throws Exception {
049    LOG.info("to stop miniCluster");
050    TEST_UTIL.shutdownMiniCluster();
051  }
052
053  protected abstract void enableRSGroup(Configuration conf);
054
055  @Test
056  public void testEnableRSGroup() throws IOException, InterruptedException {
057    TEST_UTIL.getMiniHBaseCluster().stopMaster(0);
058    TEST_UTIL.getMiniHBaseCluster().waitOnMaster(0);
059
060    LOG.info("stopped master...");
061    enableRSGroup(TEST_UTIL.getMiniHBaseCluster().getConfiguration());
062
063    TEST_UTIL.getMiniHBaseCluster().startMaster();
064    TEST_UTIL.getMiniHBaseCluster().waitForActiveAndReadyMaster(60000);
065    LOG.info("started master...");
066
067    // check if master started successfully
068    assertTrue(TEST_UTIL.getMiniHBaseCluster().getMaster() != null);
069
070    // wait RSGroupBasedLoadBalancer online
071    RSGroupBasedLoadBalancer loadBalancer =
072      (RSGroupBasedLoadBalancer) TEST_UTIL.getMiniHBaseCluster().getMaster().getLoadBalancer();
073    long start = EnvironmentEdgeManager.currentTime();
074    while (EnvironmentEdgeManager.currentTime() - start <= 60000 && !loadBalancer.isOnline()) {
075      LOG.info("waiting for rsgroup load balancer onLine...");
076      sleep(200);
077    }
078
079    assertTrue(loadBalancer.isOnline());
080  }
081}