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