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.util;
019
020import static org.junit.Assert.assertEquals;
021
022import java.util.ArrayList;
023import java.util.Collection;
024import java.util.HashSet;
025import java.util.List;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseTestingUtil;
029import org.apache.hadoop.hbase.ServerName;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.client.Admin;
032import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
033import org.apache.hadoop.hbase.client.TableDescriptor;
034import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
035import org.apache.hadoop.hbase.net.Address;
036import org.apache.hadoop.hbase.regionserver.HRegionServer;
037import org.apache.hadoop.hbase.rsgroup.RSGroupInfo;
038import org.apache.hadoop.hbase.rsgroup.RSGroupUtil;
039import org.apache.hadoop.hbase.testclassification.MediumTests;
040import org.apache.hadoop.hbase.testclassification.MiscTests;
041import org.apache.hadoop.hbase.util.RegionMover.RegionMoverBuilder;
042import org.junit.AfterClass;
043import org.junit.Before;
044import org.junit.BeforeClass;
045import org.junit.ClassRule;
046import org.junit.Test;
047import org.junit.experimental.categories.Category;
048import org.slf4j.Logger;
049import org.slf4j.LoggerFactory;
050
051/**
052 * Test for rsgroup enable, unloaded regions from decommissoned host of a rsgroup should be assigned
053 * to those regionservers belonging to the same rsgroup.
054 */
055@Category({ MiscTests.class, MediumTests.class })
056public class TestRegionMoverWithRSGroupEnable {
057
058  @ClassRule
059  public static final HBaseClassTestRule CLASS_RULE =
060    HBaseClassTestRule.forClass(TestRegionMoverWithRSGroupEnable.class);
061
062  private static final Logger LOG = LoggerFactory.getLogger(TestRegionMoverWithRSGroupEnable.class);
063  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
064  private static final String TEST_RSGROUP = "test";
065
066  @BeforeClass
067  public static void setUpBeforeClass() throws Exception {
068    RSGroupUtil.enableRSGroup(TEST_UTIL.getConfiguration());
069    TEST_UTIL.startMiniCluster(5);
070  }
071
072  @AfterClass
073  public static void tearDownAfterClass() throws Exception {
074    TEST_UTIL.shutdownMiniCluster();
075  }
076
077  private final List<Address> rsservers = new ArrayList<>(2);
078
079  @Before
080  public void setUp() throws Exception {
081    Admin admin = TEST_UTIL.getAdmin();
082
083    // Add a new rsgroup and assign two servers to it.
084    admin.addRSGroup(TEST_RSGROUP);
085    Collection<ServerName> allServers = admin.getRegionServers();
086    // Remove rs contains hbase:meta, otherwise test looks unstable and buggy in test env.
087    ServerName rsContainMeta = TEST_UTIL.getMiniHBaseCluster().getRegionServerThreads().stream()
088      .map(t -> t.getRegionServer())
089      .filter(rs -> rs.getRegions(TableName.META_TABLE_NAME).size() > 0).findFirst().get()
090      .getServerName();
091    LOG.info("{} contains hbase:meta", rsContainMeta);
092    List<ServerName> modifiable = new ArrayList<>(allServers);
093    modifiable.remove(rsContainMeta);
094    int i = 0;
095    for (ServerName server : modifiable) {
096      if (i == 2) break;
097      rsservers.add(Address.fromParts(server.getHostname(), server.getPort()));
098      i++;
099    }
100    admin.moveServersToRSGroup(new HashSet<>(rsservers), TEST_RSGROUP);
101    LOG.info("Servers in {} are {}", TEST_RSGROUP, rsservers);
102    assertEquals(3, admin.getRSGroup(RSGroupInfo.DEFAULT_GROUP).getServers().size());
103    assertEquals(2, admin.getRSGroup(TEST_RSGROUP).getServers().size());
104
105    // Create a pre-split table in test rsgroup
106    TableName tableName = TableName.valueOf("testRegionMoverWithRSGroupEnable");
107    if (admin.tableExists(tableName)) {
108      TEST_UTIL.deleteTable(tableName);
109    }
110    TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(tableName)
111      .setColumnFamily(ColumnFamilyDescriptorBuilder.of("f")).setRegionServerGroup(TEST_RSGROUP)
112      .build();
113    String startKey = "a";
114    String endKey = "z";
115    admin.createTable(tableDesc, Bytes.toBytes(startKey), Bytes.toBytes(endKey), 9);
116  }
117
118  @Test
119  public void testUnloadRegions() throws Exception {
120    Address decommission = rsservers.get(0);
121    Address online = rsservers.get(1);
122    String filename = new Path(TEST_UTIL.getDataTestDir(), "testRSGroupUnload").toString();
123    RegionMoverBuilder builder =
124      new RegionMoverBuilder(decommission.toString(), TEST_UTIL.getConfiguration());
125    try (RegionMover rm = builder.filename(filename).ack(true).build()) {
126      LOG.info("Unloading " + decommission.getHostname());
127      rm.unload();
128    }
129    HRegionServer onlineRS = TEST_UTIL.getMiniHBaseCluster().getRegionServerThreads().stream()
130      .map(JVMClusterUtil.RegionServerThread::getRegionServer)
131      .filter(rs -> rs.getServerName().getAddress().equals(online)).findFirst().get();
132    assertEquals(9, onlineRS.getNumberOfOnlineRegions());
133  }
134
135}