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 */
018
019package org.apache.hadoop.hbase.util;
020
021import org.apache.hadoop.hbase.HBaseClassTestRule;
022import org.apache.hadoop.hbase.HBaseTestingUtil;
023import org.apache.hadoop.hbase.ServerName;
024import org.apache.hadoop.hbase.SingleProcessHBaseCluster;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.client.Admin;
027import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
028import org.apache.hadoop.hbase.client.Put;
029import org.apache.hadoop.hbase.client.Table;
030import org.apache.hadoop.hbase.client.TableDescriptor;
031import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
032import org.apache.hadoop.hbase.master.RackManager;
033import org.apache.hadoop.hbase.regionserver.HRegion;
034import org.apache.hadoop.hbase.regionserver.HRegionServer;
035import org.apache.hadoop.hbase.testclassification.LargeTests;
036import org.apache.hadoop.hbase.testclassification.MiscTests;
037import org.junit.AfterClass;
038import org.junit.Assert;
039import org.junit.Before;
040import org.junit.BeforeClass;
041import org.junit.ClassRule;
042import org.junit.Rule;
043import org.junit.Test;
044import org.junit.experimental.categories.Category;
045import org.junit.rules.TestName;
046import java.util.ArrayList;
047import java.util.List;
048import java.util.stream.Collectors;
049import java.util.stream.IntStream;
050
051
052@Category({ MiscTests.class, LargeTests.class})
053public class TestRegionMover3 {
054
055  @ClassRule
056  public static final HBaseClassTestRule CLASS_RULE =
057    HBaseClassTestRule.forClass(TestRegionMover3.class);
058
059  @Rule
060  public TestName name = new TestName();
061
062  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
063  private static ServerName rs0;
064  private static ServerName rs1;
065  private static ServerName rs2;
066
067  @BeforeClass
068  public static void setUpBeforeClass() throws Exception {
069    TEST_UTIL.startMiniCluster(3);
070    SingleProcessHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
071    rs0 = cluster.getRegionServer(0).getServerName();
072    rs1 = cluster.getRegionServer(1).getServerName();
073    rs2 = cluster.getRegionServer(2).getServerName();
074    TEST_UTIL.getAdmin().balancerSwitch(false, true);
075  }
076
077  @AfterClass
078  public static void tearDownAfterClass() throws Exception {
079    TEST_UTIL.shutdownMiniCluster();
080  }
081
082  @Before
083  public void setUp() throws Exception {
084    final TableName tableName = TableName.valueOf(name.getMethodName());
085    TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(tableName)
086      .setColumnFamily(ColumnFamilyDescriptorBuilder.of("fam1")).build();
087    int startKey = 0;
088    int endKey = 80000;
089    TEST_UTIL.getAdmin().createTable(tableDesc, Bytes.toBytes(startKey), Bytes.toBytes(endKey), 9);
090  }
091
092  @Test
093  public void testRegionUnloadWithRack() throws Exception {
094    final TableName tableName = TableName.valueOf(name.getMethodName());
095    SingleProcessHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
096    Admin admin = TEST_UTIL.getAdmin();
097    Table table = TEST_UTIL.getConnection().getTable(tableName);
098    List<Put> puts = IntStream.range(10, 50000)
099      .mapToObj(i -> new Put(Bytes.toBytes(i))
100      .addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("q1"), Bytes.toBytes("val_" + i)))
101      .collect(Collectors.toList());
102    table.put(puts);
103    admin.flush(tableName);
104    admin.compact(tableName);
105    Thread.sleep(3000);
106    HRegionServer hRegionServer0 = cluster.getRegionServer(0);
107    HRegionServer hRegionServer1 = cluster.getRegionServer(1);
108    HRegionServer hRegionServer2 = cluster.getRegionServer(2);
109    int numRegions0 = hRegionServer0.getNumberOfOnlineRegions();
110    int numRegions1 = hRegionServer1.getNumberOfOnlineRegions();
111    int numRegions2 = hRegionServer2.getNumberOfOnlineRegions();
112
113    Assert.assertTrue(numRegions0 >= 3);
114    Assert.assertTrue(numRegions1 >= 3);
115    Assert.assertTrue(numRegions2 >= 3);
116    int totalRegions = numRegions0 + numRegions1 + numRegions2;
117
118    // source RS: rs0
119    String sourceRSName = rs0.getAddress().toString();
120
121    // move all regions from rs1 to rs0
122    for (HRegion region : hRegionServer1.getRegions()) {
123      TEST_UTIL.getAdmin().move(region.getRegionInfo().getEncodedNameAsBytes(), rs0);
124    }
125    TEST_UTIL.waitFor(5000, () -> {
126      int newNumRegions0 = hRegionServer0.getNumberOfOnlineRegions();
127      int newNumRegions1 = hRegionServer1.getNumberOfOnlineRegions();
128      return newNumRegions1 == 0 && newNumRegions0 == (numRegions0 + numRegions1);
129    });
130
131    // regionMover obj on rs0. While unloading regions from rs0
132    // with default rackManager, which resolves "/default-rack" for each server, no region
133    // is moved while using unloadFromRack() as all rs belong to same rack.
134    RegionMover.RegionMoverBuilder rmBuilder =
135      new RegionMover.RegionMoverBuilder(sourceRSName, TEST_UTIL.getConfiguration())
136        .ack(true)
137        .maxthreads(8);
138    try (RegionMover regionMover = rmBuilder.build()) {
139      regionMover.unloadFromRack();
140      int newNumRegions0 = hRegionServer0.getNumberOfOnlineRegions();
141      int newNumRegions1 = hRegionServer1.getNumberOfOnlineRegions();
142      int newNumRegions2 = hRegionServer2.getNumberOfOnlineRegions();
143      Assert.assertEquals(0, newNumRegions1);
144      Assert.assertEquals(totalRegions, newNumRegions0 + newNumRegions2);
145    }
146
147    // use custom rackManager, which resolves "rack-1" for rs0 and rs1,
148    // while "rack-2" for rs2. Hence, unloadFromRack() from rs0 should move all
149    // regions that belong to rs0 to rs2 only, and nothing should be moved to rs1
150    // as rs0 and rs1 belong to same rack.
151    rmBuilder.rackManager(new MockRackManager());
152    try (RegionMover regionMover = rmBuilder.build()) {
153      regionMover.unloadFromRack();
154      int newNumRegions0 = hRegionServer0.getNumberOfOnlineRegions();
155      int newNumRegions1 = hRegionServer1.getNumberOfOnlineRegions();
156      int newNumRegions2 = hRegionServer2.getNumberOfOnlineRegions();
157      Assert.assertEquals(0, newNumRegions0);
158      Assert.assertEquals(0, newNumRegions1);
159      Assert.assertEquals(totalRegions, newNumRegions2);
160    }
161
162  }
163
164  private static class MockRackManager extends RackManager {
165
166    private static final String RACK_2 = "rack-2";
167    private static final String RACK_1 = "rack-1";
168
169    @Override
170    public String getRack(ServerName server) {
171      return rs2.equals(server) ? RACK_2 : RACK_1;
172    }
173
174    @Override
175    public List<String> getRack(List<ServerName> servers) {
176      List<String> racks = new ArrayList<>();
177      servers.forEach(serverName -> {
178        if (rs2.equals(serverName)) {
179          racks.add(RACK_2);
180        } else {
181          racks.add(RACK_1);
182        }
183      });
184      return racks;
185    }
186  }
187
188}