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 org.junit.Assert.assertFalse;
021import static org.junit.Assert.assertTrue;
022
023import java.io.IOException;
024import java.util.Collections;
025import java.util.List;
026
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseTestingUtility;
029import org.apache.hadoop.hbase.HConstants;
030import org.apache.hadoop.hbase.Waiter.Predicate;
031import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
032import org.apache.hadoop.hbase.master.HMaster;
033import org.apache.hadoop.hbase.regionserver.HRegionServer;
034import org.apache.hadoop.hbase.testclassification.MediumTests;
035import org.junit.AfterClass;
036import org.junit.BeforeClass;
037import org.junit.ClassRule;
038import org.junit.Test;
039import org.junit.experimental.categories.Category;
040import org.slf4j.Logger;
041import org.slf4j.LoggerFactory;
042
043@Category({MediumTests.class})
044public class TestRSGroupUtil {
045
046  @ClassRule
047  public static final HBaseClassTestRule CLASS_RULE =
048      HBaseClassTestRule.forClass(TestRSGroupUtil.class);
049
050  private static final Logger LOG = LoggerFactory.getLogger(TestRSGroupUtil.class);
051
052  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
053
054  private static HMaster master;
055
056  private static RSGroupAdminClient rsGroupAdminClient;
057
058  private static final String GROUP_NAME = "rsg";
059
060  private static RSGroupInfoManager rsGroupInfoManager;
061
062  @BeforeClass
063  public static void setUp() throws Exception {
064    UTIL.getConfiguration().set(
065      HConstants.HBASE_MASTER_LOADBALANCER_CLASS,
066      RSGroupBasedLoadBalancer.class.getName());
067    UTIL.getConfiguration().set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
068      RSGroupAdminEndpoint.class.getName());
069    UTIL.startMiniCluster(5);
070    master = UTIL.getMiniHBaseCluster().getMaster();
071
072    UTIL.waitFor(60000, (Predicate<Exception>) () ->
073        master.isInitialized() && ((RSGroupBasedLoadBalancer) master.getLoadBalancer()).isOnline());
074
075    rsGroupAdminClient = new RSGroupAdminClient(UTIL.getConnection());
076
077    List<RSGroupAdminEndpoint> cps =
078        master.getMasterCoprocessorHost().findCoprocessors(RSGroupAdminEndpoint.class);
079    assertTrue(cps.size() > 0);
080    rsGroupInfoManager = cps.get(0).getGroupInfoManager();
081  }
082
083  @AfterClass
084  public static void tearDown() throws Exception {
085    UTIL.shutdownMiniCluster();
086  }
087
088  @Test
089  public void rsGroupHasOnlineServer() throws IOException {
090    rsGroupInfoManager.refresh();
091    RSGroupInfo defaultGroup = rsGroupInfoManager.getRSGroup(RSGroupInfo.DEFAULT_GROUP);
092    assertTrue(RSGroupUtil.rsGroupHasOnlineServer(master, defaultGroup));
093
094    HRegionServer rs = UTIL.getHBaseCluster().getRegionServer(0);
095    rsGroupAdminClient.addRSGroup(GROUP_NAME);
096    rsGroupAdminClient.moveServers(Collections.singleton(rs.getServerName().getAddress()), GROUP_NAME);
097
098    rsGroupInfoManager.refresh();
099    RSGroupInfo rsGroup = rsGroupInfoManager.getRSGroup(GROUP_NAME);
100    assertTrue(RSGroupUtil.rsGroupHasOnlineServer(master, rsGroup));
101
102    rsGroupAdminClient.addRSGroup("empty");
103    rsGroupInfoManager.refresh();
104    RSGroupInfo emptyGroup = rsGroupInfoManager.getRSGroup("empty");
105    assertFalse(RSGroupUtil.rsGroupHasOnlineServer(master, emptyGroup));
106  }
107}