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