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