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 java.util.ArrayList;
021import java.util.Collection;
022import java.util.HashSet;
023import java.util.List;
024import org.apache.hadoop.fs.Path;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseTestingUtil;
027import org.apache.hadoop.hbase.ServerName;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.Admin;
030import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
031import org.apache.hadoop.hbase.client.TableDescriptor;
032import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
033import org.apache.hadoop.hbase.net.Address;
034import org.apache.hadoop.hbase.regionserver.HRegionServer;
035import org.apache.hadoop.hbase.rsgroup.RSGroupInfo;
036import org.apache.hadoop.hbase.rsgroup.RSGroupUtil;
037import org.apache.hadoop.hbase.testclassification.MediumTests;
038import org.apache.hadoop.hbase.testclassification.MiscTests;
039import org.apache.hadoop.hbase.util.RegionMover.RegionMoverBuilder;
040import org.junit.AfterClass;
041import org.junit.Before;
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;
048import static org.junit.Assert.assertEquals;
049
050/**
051 * Test for rsgroup enable, unloaded regions from decommissoned host of a rsgroup
052 * should be assigned to those regionservers belonging to the same rsgroup.
053 */
054@Category({ MiscTests.class, MediumTests.class })
055public class TestRegionMoverWithRSGroupEnable {
056
057  @ClassRule
058  public static final HBaseClassTestRule CLASS_RULE =
059    HBaseClassTestRule.forClass(TestRegionMoverWithRSGroupEnable.class);
060
061  private static final Logger LOG = LoggerFactory.getLogger(TestRegionMoverWithRSGroupEnable.class);
062  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
063  private static final String TEST_RSGROUP = "test";
064
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"))
112      .setRegionServerGroup(TEST_RSGROUP)
113      .build();
114    String startKey = "a";
115    String endKey = "z";
116    admin.createTable(tableDesc, Bytes.toBytes(startKey), Bytes.toBytes(endKey), 9);
117  }
118
119  @Test
120  public void testUnloadRegions() throws Exception {
121    Address decommission = rsservers.get(0);
122    Address online = rsservers.get(1);
123    String filename = new Path(TEST_UTIL.getDataTestDir(), "testRSGroupUnload").toString();
124    RegionMoverBuilder builder =
125      new RegionMoverBuilder(decommission.toString(), TEST_UTIL.getConfiguration());
126    try (RegionMover rm = builder.filename(filename).ack(true).build()) {
127      LOG.info("Unloading " + decommission.getHostname());
128      rm.unload();
129    }
130    HRegionServer onlineRS = TEST_UTIL.getMiniHBaseCluster().getRegionServerThreads().stream()
131      .map(JVMClusterUtil.RegionServerThread::getRegionServer)
132      .filter(rs -> rs.getServerName().getAddress().equals(online)).findFirst().get();
133    assertEquals(9, onlineRS.getNumberOfOnlineRegions());
134  }
135
136}