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