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.List; 021import java.util.stream.Collectors; 022import java.util.stream.IntStream; 023import org.apache.hadoop.conf.Configuration; 024import org.apache.hadoop.hbase.HBaseClassTestRule; 025import org.apache.hadoop.hbase.HBaseConfiguration; 026import org.apache.hadoop.hbase.HBaseTestingUtility; 027import org.apache.hadoop.hbase.HConstants; 028import org.apache.hadoop.hbase.MiniHBaseCluster; 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.Put; 034import org.apache.hadoop.hbase.client.Table; 035import org.apache.hadoop.hbase.client.TableDescriptor; 036import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 037import org.apache.hadoop.hbase.regionserver.HRegionServer; 038import org.apache.hadoop.hbase.testclassification.LargeTests; 039import org.apache.hadoop.hbase.testclassification.MiscTests; 040import org.junit.AfterClass; 041import org.junit.Assert; 042import org.junit.Before; 043import org.junit.BeforeClass; 044import org.junit.ClassRule; 045import org.junit.Rule; 046import org.junit.Test; 047import org.junit.experimental.categories.Category; 048import org.junit.rules.TestName; 049import org.slf4j.Logger; 050import org.slf4j.LoggerFactory; 051 052@Category({ MiscTests.class, LargeTests.class }) 053public class TestRegionMoverUseIp { 054 055 @ClassRule 056 public static final HBaseClassTestRule CLASS_RULE = 057 HBaseClassTestRule.forClass(TestRegionMoverUseIp.class); 058 private static final Logger LOG = LoggerFactory.getLogger(TestRegionMoverUseIp.class); 059 060 @Rule 061 public TestName name = new TestName(); 062 063 private static HBaseTestingUtility TEST_UTIL; 064 private static ServerName rs0; 065 private static ServerName rs1; 066 private static ServerName rs2; 067 068 @BeforeClass 069 public static void setUpBeforeClass() throws Exception { 070 Configuration conf = HBaseConfiguration.create(); 071 conf.setBoolean(HConstants.HBASE_SERVER_USEIP_ENABLED_KEY, true); 072 TEST_UTIL = new HBaseTestingUtility(conf); 073 TEST_UTIL.startMiniCluster(3); 074 075 MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster(); 076 rs0 = cluster.getRegionServer(0).getServerName(); 077 rs1 = cluster.getRegionServer(1).getServerName(); 078 rs2 = cluster.getRegionServer(2).getServerName(); 079 LOG.info("rs0 hostname=" + rs0.getHostname()); 080 LOG.info("rs1 hostname=" + rs1.getHostname()); 081 LOG.info("rs2 hostname=" + rs2.getHostname()); 082 TEST_UTIL.getAdmin().balancerSwitch(false, true); 083 } 084 085 @AfterClass 086 public static void tearDownAfterClass() throws Exception { 087 TEST_UTIL.shutdownMiniCluster(); 088 } 089 090 @Before 091 public void setUp() throws Exception { 092 final TableName tableName = TableName.valueOf(name.getMethodName()); 093 TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(tableName) 094 .setColumnFamily(ColumnFamilyDescriptorBuilder.of("fam1")).build(); 095 int startKey = 0; 096 int endKey = 80000; 097 TEST_UTIL.getAdmin().createTable(tableDesc, Bytes.toBytes(startKey), Bytes.toBytes(endKey), 9); 098 } 099 100 @Test 101 public void testRegionUnloadUesIp() throws Exception { 102 final TableName tableName = TableName.valueOf(name.getMethodName()); 103 MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster(); 104 Admin admin = TEST_UTIL.getAdmin(); 105 Table table = TEST_UTIL.getConnection().getTable(tableName); 106 List<Put> puts = IntStream.range(10, 50000).mapToObj(i -> new Put(Bytes.toBytes(i)) 107 .addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("q1"), Bytes.toBytes("val_" + i))) 108 .collect(Collectors.toList()); 109 table.put(puts); 110 admin.flush(tableName); 111 admin.compact(tableName); 112 Thread.sleep(3000); 113 HRegionServer hRegionServer0 = cluster.getRegionServer(0); 114 HRegionServer hRegionServer1 = cluster.getRegionServer(1); 115 HRegionServer hRegionServer2 = cluster.getRegionServer(2); 116 int numRegions0 = hRegionServer0.getNumberOfOnlineRegions(); 117 int numRegions1 = hRegionServer1.getNumberOfOnlineRegions(); 118 int numRegions2 = hRegionServer2.getNumberOfOnlineRegions(); 119 120 Assert.assertTrue(numRegions0 >= 3); 121 Assert.assertTrue(numRegions1 >= 3); 122 Assert.assertTrue(numRegions2 >= 3); 123 int totalRegions = numRegions0 + numRegions1 + numRegions2; 124 125 // source RS: rs0 126 String sourceRSName = rs0.getAddress().toString(); 127 RegionMover.RegionMoverBuilder rmBuilder = 128 new RegionMover.RegionMoverBuilder(sourceRSName, TEST_UTIL.getConfiguration()).ack(true) 129 .maxthreads(8); 130 try (RegionMover regionMover = rmBuilder.build()) { 131 regionMover.unload(); 132 int newNumRegions0 = hRegionServer0.getNumberOfOnlineRegions(); 133 int newNumRegions1 = hRegionServer1.getNumberOfOnlineRegions(); 134 int newNumRegions2 = hRegionServer2.getNumberOfOnlineRegions(); 135 Assert.assertEquals(0, newNumRegions0); 136 Assert.assertEquals(totalRegions, newNumRegions1 + newNumRegions2); 137 } 138 } 139}