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.assertEquals;
021import static org.junit.Assert.assertTrue;
022import static org.mockito.Mockito.anyString;
023import static org.mockito.Mockito.mock;
024import static org.mockito.Mockito.when;
025
026import java.io.IOException;
027import java.util.Collections;
028import java.util.List;
029
030import org.apache.hadoop.hbase.HBaseClassTestRule;
031import org.apache.hadoop.hbase.HBaseTestingUtility;
032import org.apache.hadoop.hbase.HConstants;
033import org.apache.hadoop.hbase.NamespaceDescriptor;
034import org.apache.hadoop.hbase.TableName;
035import org.apache.hadoop.hbase.Waiter.Predicate;
036import org.apache.hadoop.hbase.client.Admin;
037import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
038import org.apache.hadoop.hbase.master.HMaster;
039import org.apache.hadoop.hbase.regionserver.HRegionServer;
040import org.apache.hadoop.hbase.rsgroup.RSGroupInfoManagerImpl.RSGroupMappingScript;
041import org.apache.hadoop.hbase.testclassification.MediumTests;
042import org.junit.AfterClass;
043import org.junit.BeforeClass;
044import org.junit.ClassRule;
045import org.junit.Test;
046import org.junit.experimental.categories.Category;
047import org.slf4j.Logger;
048import org.slf4j.LoggerFactory;
049
050/**
051 * Test {@link RSGroupInfoManager#determineRSGroupInfoForTable(TableName)}
052 */
053@Category({ MediumTests.class })
054public class TestDetermineRSGroupInfoForTable {
055
056  private static final Logger LOG = LoggerFactory.getLogger(TestDetermineRSGroupInfoForTable.class);
057
058  @ClassRule
059  public static final HBaseClassTestRule CLASS_RULE =
060    HBaseClassTestRule.forClass(TestDetermineRSGroupInfoForTable.class);
061
062  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
063
064  private static HMaster master;
065
066  private static Admin admin;
067
068  private static RSGroupInfoManager rsGroupInfoManager;
069
070  private static RSGroupAdminClient rsGroupAdminClient;
071
072  private static final String GROUP_NAME = "rsg";
073
074  private static final String NAMESPACE_NAME = "ns";
075  private static final String OTHER_NAMESPACE_NAME = "other";
076
077  private static final TableName TABLE_NAME = TableName.valueOf(NAMESPACE_NAME, "tb");
078
079  @BeforeClass
080  public static void setUp() throws Exception {
081    UTIL.getConfiguration().set(
082      HConstants.HBASE_MASTER_LOADBALANCER_CLASS,
083      RSGroupBasedLoadBalancer.class.getName());
084    UTIL.getConfiguration().set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
085      RSGroupAdminEndpoint.class.getName());
086    UTIL.startMiniCluster(5);
087    master = UTIL.getMiniHBaseCluster().getMaster();
088    admin = UTIL.getAdmin();
089    rsGroupAdminClient = new RSGroupAdminClient(UTIL.getConnection());
090
091    UTIL.waitFor(60000, (Predicate<Exception>) () ->
092        master.isInitialized() && ((RSGroupBasedLoadBalancer) master.getLoadBalancer()).isOnline());
093
094    List<RSGroupAdminEndpoint> cps =
095        master.getMasterCoprocessorHost().findCoprocessors(RSGroupAdminEndpoint.class);
096    assertTrue(cps.size() > 0);
097    rsGroupInfoManager = cps.get(0).getGroupInfoManager();
098
099    HRegionServer rs = UTIL.getHBaseCluster().getRegionServer(0);
100    rsGroupAdminClient.addRSGroup(GROUP_NAME);
101    rsGroupAdminClient.moveServers(
102      Collections.singleton(rs.getServerName().getAddress()), GROUP_NAME);
103    admin.createNamespace(NamespaceDescriptor.create(NAMESPACE_NAME)
104      .addConfiguration(RSGroupInfo.NAMESPACE_DESC_PROP_GROUP, GROUP_NAME)
105      .build());
106    admin.createNamespace(NamespaceDescriptor.create(OTHER_NAMESPACE_NAME).build());
107  }
108
109  @AfterClass
110  public static void tearDown() throws Exception {
111    admin.deleteNamespace(NAMESPACE_NAME);
112
113    UTIL.shutdownMiniCluster();
114  }
115
116  @Test
117  public void testByDefault() throws IOException {
118    RSGroupInfo group =
119      rsGroupInfoManager.determineRSGroupInfoForTable(TableName.valueOf("tb"));
120    assertEquals(group.getName(), RSGroupInfo.DEFAULT_GROUP);
121  }
122
123  @Test
124  public void testDetermineByNamespaceConfig() throws IOException {
125    RSGroupInfo group = rsGroupInfoManager.determineRSGroupInfoForTable(TABLE_NAME);
126    assertEquals(group.getName(), GROUP_NAME);
127
128    group = rsGroupInfoManager.determineRSGroupInfoForTable(
129      TableName.valueOf(OTHER_NAMESPACE_NAME, "tb"));
130    assertEquals(group.getName(), RSGroupInfo.DEFAULT_GROUP);
131  }
132
133  /**
134   * determine by script
135   */
136  @Test
137  public void testDetermineByScript() throws IOException {
138    RSGroupMappingScript script = mock(RSGroupMappingScript.class);
139    when(script.getRSGroup(anyString(), anyString())).thenReturn(GROUP_NAME);
140    RSGroupMappingScript oldScript = ((RSGroupInfoManagerImpl) rsGroupInfoManager).script;
141    ((RSGroupInfoManagerImpl) rsGroupInfoManager).script = script;
142
143    RSGroupInfo group = rsGroupInfoManager.determineRSGroupInfoForTable(TABLE_NAME);
144    assertEquals(group.getName(), GROUP_NAME);
145    // reset script to avoid affecting other tests
146    ((RSGroupInfoManagerImpl) rsGroupInfoManager).script = oldScript;
147  }
148
149}