|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hbase.util; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Collection; |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.List; |
| 24 | +import org.apache.hadoop.fs.Path; |
| 25 | +import org.apache.hadoop.hbase.HBaseClassTestRule; |
| 26 | +import org.apache.hadoop.hbase.HBaseTestingUtility; |
| 27 | +import org.apache.hadoop.hbase.ServerName; |
| 28 | +import org.apache.hadoop.hbase.TableName; |
| 29 | +import org.apache.hadoop.hbase.client.Admin; |
| 30 | +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; |
| 31 | +import org.apache.hadoop.hbase.client.TableDescriptor; |
| 32 | +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; |
| 33 | +import org.apache.hadoop.hbase.net.Address; |
| 34 | +import org.apache.hadoop.hbase.regionserver.HRegionServer; |
| 35 | +import org.apache.hadoop.hbase.rsgroup.RSGroupInfo; |
| 36 | +import org.apache.hadoop.hbase.rsgroup.RSGroupUtil; |
| 37 | +import org.apache.hadoop.hbase.testclassification.MediumTests; |
| 38 | +import org.apache.hadoop.hbase.testclassification.MiscTests; |
| 39 | +import org.apache.hadoop.hbase.util.RegionMover.RegionMoverBuilder; |
| 40 | +import org.junit.AfterClass; |
| 41 | +import org.junit.Before; |
| 42 | +import org.junit.BeforeClass; |
| 43 | +import org.junit.ClassRule; |
| 44 | +import org.junit.Test; |
| 45 | +import org.junit.experimental.categories.Category; |
| 46 | +import org.slf4j.Logger; |
| 47 | +import org.slf4j.LoggerFactory; |
| 48 | +import static org.junit.Assert.assertEquals; |
| 49 | + |
| 50 | +/** |
| 51 | + * Test for rsgroup enable, unloaded regions from decommissoned host of a rsgroup |
| 52 | + * should be assigned to those regionservers belonging to the same rsgroup. |
| 53 | + */ |
| 54 | +@Category({ MiscTests.class, MediumTests.class }) |
| 55 | +public class TestRegionMoverWithRSGroupEnable { |
| 56 | + |
| 57 | + @ClassRule |
| 58 | + public static final HBaseClassTestRule CLASS_RULE = |
| 59 | + HBaseClassTestRule.forClass(TestRegionMoverWithRSGroupEnable.class); |
| 60 | + |
| 61 | + private static final Logger LOG = LoggerFactory.getLogger(TestRegionMoverWithRSGroupEnable.class); |
| 62 | + private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); |
| 63 | + private static final String TEST_RSGROUP = "test"; |
| 64 | + |
| 65 | + |
| 66 | + @BeforeClass |
| 67 | + public static void setUpBeforeClass() throws Exception { |
| 68 | + RSGroupUtil.enableRSGroup(TEST_UTIL.getConfiguration()); |
| 69 | + TEST_UTIL.startMiniCluster(5); |
| 70 | + } |
| 71 | + |
| 72 | + @AfterClass |
| 73 | + public static void tearDownAfterClass() throws Exception { |
| 74 | + TEST_UTIL.shutdownMiniCluster(); |
| 75 | + } |
| 76 | + |
| 77 | + private final List<Address> rsservers = new ArrayList<>(2); |
| 78 | + |
| 79 | + @Before |
| 80 | + public void setUp() throws Exception { |
| 81 | + Admin admin = TEST_UTIL.getAdmin(); |
| 82 | + |
| 83 | + // Add a new rsgroup and assign two servers to it. |
| 84 | + admin.addRSGroup(TEST_RSGROUP); |
| 85 | + Collection<ServerName> allServers = admin.getRegionServers(); |
| 86 | + // Remove rs contains hbase:meta, otherwise test looks unstable and buggy in test env. |
| 87 | + ServerName rsContainMeta = TEST_UTIL.getMiniHBaseCluster().getRegionServerThreads().stream() |
| 88 | + .map(t -> t.getRegionServer()) |
| 89 | + .filter(rs -> rs.getRegions(TableName.META_TABLE_NAME).size() > 0).findFirst().get() |
| 90 | + .getServerName(); |
| 91 | + LOG.info("{} contains hbase:meta", rsContainMeta); |
| 92 | + List<ServerName> modifiable = new ArrayList<>(allServers); |
| 93 | + modifiable.remove(rsContainMeta); |
| 94 | + int i = 0; |
| 95 | + for (ServerName server : modifiable) { |
| 96 | + if (i == 2) break; |
| 97 | + rsservers.add(Address.fromParts(server.getHostname(), server.getPort())); |
| 98 | + i++; |
| 99 | + } |
| 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 | +} |
0 commit comments