001/**
002 * Copyright The Apache Software Foundation
003 *
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *     http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020package org.apache.hadoop.hbase.rsgroup;
021
022import org.apache.hadoop.hbase.HConstants;
023import org.apache.hadoop.hbase.IntegrationTestingUtility;
024import org.apache.hadoop.hbase.Waiter;
025import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
026import org.apache.hadoop.hbase.testclassification.IntegrationTests;
027import org.junit.After;
028import org.junit.Before;
029import org.junit.experimental.categories.Category;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033/**
034 * Runs all of the units tests defined in TestGroupBase as an integration test.
035 * Requires TestRSGroupBase.NUM_SLAVE_BASE servers to run.
036 */
037@Category(IntegrationTests.class)
038public class IntegrationTestRSGroup extends TestRSGroupsBase {
039  private final static Logger LOG = LoggerFactory.getLogger(IntegrationTestRSGroup.class);
040  private static boolean initialized = false;
041
042  @Before
043  public void beforeMethod() throws Exception {
044    if(!initialized) {
045      LOG.info("Setting up IntegrationTestRSGroup");
046      LOG.info("Initializing cluster with " + NUM_SLAVES_BASE + " servers");
047      TEST_UTIL = new IntegrationTestingUtility();
048      TEST_UTIL.getConfiguration().set(HConstants.HBASE_MASTER_LOADBALANCER_CLASS,
049        RSGroupBasedLoadBalancer.class.getName());
050      TEST_UTIL.getConfiguration().set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
051        RSGroupAdminEndpoint.class.getName());
052      ((IntegrationTestingUtility)TEST_UTIL).initializeCluster(NUM_SLAVES_BASE);
053      //set shared configs
054      admin = TEST_UTIL.getAdmin();
055      cluster = TEST_UTIL.getHBaseClusterInterface();
056      rsGroupAdmin = new VerifyingRSGroupAdminClient(new RSGroupAdminClient(TEST_UTIL.getConnection()),
057          TEST_UTIL.getConfiguration());
058      LOG.info("Done initializing cluster");
059      initialized = true;
060      //cluster may not be clean
061      //cleanup when initializing
062      afterMethod();
063    }
064  }
065
066  @After
067  public void afterMethod() throws Exception {
068    LOG.info("Cleaning up previous test run");
069    //cleanup previous artifacts
070    deleteTableIfNecessary();
071    deleteNamespaceIfNecessary();
072    deleteGroups();
073    admin.setBalancerRunning(true, true);
074
075    LOG.info("Restoring the cluster");
076    ((IntegrationTestingUtility)TEST_UTIL).restoreCluster();
077    LOG.info("Done restoring the cluster");
078
079    TEST_UTIL.waitFor(WAIT_TIMEOUT, new Waiter.Predicate<Exception>() {
080      @Override
081      public boolean evaluate() throws Exception {
082        LOG.info("Waiting for cleanup to finish "+ rsGroupAdmin.listRSGroups());
083        //Might be greater since moving servers back to default
084        //is after starting a server
085        return rsGroupAdmin.getRSGroupInfo(RSGroupInfo.DEFAULT_GROUP).getServers().size()
086            >= NUM_SLAVES_BASE;
087      }
088    });
089
090    TEST_UTIL.waitFor(WAIT_TIMEOUT, new Waiter.Predicate<Exception>() {
091      @Override
092      public boolean evaluate() throws Exception {
093        LOG.info("Waiting for regionservers to be registered "+ rsGroupAdmin.listRSGroups());
094        //Might be greater since moving servers back to default
095        //is after starting a server
096        return rsGroupAdmin.getRSGroupInfo(RSGroupInfo.DEFAULT_GROUP).getServers().size()
097            == getNumServers();
098      }
099    });
100
101    LOG.info("Done cleaning up previous test run");
102  }
103}