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