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.ArrayList;
021import java.util.List;
022import java.util.concurrent.TimeUnit;
023import java.util.stream.Collectors;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.SingleProcessHBaseCluster;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.client.Admin;
029import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
030import org.apache.hadoop.hbase.client.Put;
031import org.apache.hadoop.hbase.client.Table;
032import org.apache.hadoop.hbase.client.TableDescriptor;
033import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
034import org.apache.hadoop.hbase.regionserver.HRegion;
035import org.apache.hadoop.hbase.regionserver.HRegionServer;
036import org.apache.hadoop.hbase.testclassification.LargeTests;
037import org.apache.hadoop.hbase.testclassification.MiscTests;
038import org.junit.After;
039import org.junit.AfterClass;
040import org.junit.Assert;
041import org.junit.Before;
042import org.junit.BeforeClass;
043import org.junit.ClassRule;
044import org.junit.Rule;
045import org.junit.Test;
046import org.junit.experimental.categories.Category;
047import org.junit.rules.TestName;
048import org.slf4j.Logger;
049import org.slf4j.LoggerFactory;
050
051/**
052 * Tests for Region Mover Load/Unload functionality with and without ack mode and also to test
053 * exclude functionality useful for rack decommissioning
054 */
055@Category({ MiscTests.class, LargeTests.class })
056public class TestRegionMover2 {
057
058  @ClassRule
059  public static final HBaseClassTestRule CLASS_RULE =
060    HBaseClassTestRule.forClass(TestRegionMover2.class);
061
062  @Rule
063  public TestName name = new TestName();
064
065  private static final Logger LOG = LoggerFactory.getLogger(TestRegionMover2.class);
066
067  private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
068
069  @BeforeClass
070  public static void setUpBeforeClass() throws Exception {
071    TEST_UTIL.startMiniCluster(3);
072    TEST_UTIL.getAdmin().balancerSwitch(false, true);
073  }
074
075  @AfterClass
076  public static void tearDownAfterClass() throws Exception {
077    TEST_UTIL.shutdownMiniCluster();
078  }
079
080  @Before
081  public void setUp() throws Exception {
082    final TableName tableName = TableName.valueOf(name.getMethodName());
083    TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(tableName)
084      .setColumnFamily(ColumnFamilyDescriptorBuilder.of("fam1")).build();
085    int startKey = 0;
086    int endKey = 80000;
087    TEST_UTIL.getAdmin().createTable(tableDesc, Bytes.toBytes(startKey), Bytes.toBytes(endKey), 9);
088  }
089
090  @After
091  public void tearDown() throws Exception {
092    final TableName tableName = TableName.valueOf(name.getMethodName());
093    TEST_UTIL.getAdmin().disableTable(tableName);
094    TEST_UTIL.getAdmin().deleteTable(tableName);
095  }
096
097  @Test
098  public void testWithMergedRegions() throws Exception {
099    final TableName tableName = TableName.valueOf(name.getMethodName());
100    SingleProcessHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
101    Admin admin = TEST_UTIL.getAdmin();
102    Table table = TEST_UTIL.getConnection().getTable(tableName);
103    List<Put> puts = new ArrayList<>();
104    for (int i = 0; i < 10000; i++) {
105      puts.add(new Put(Bytes.toBytes("rowkey_" + i)).addColumn(Bytes.toBytes("fam1"),
106        Bytes.toBytes("q1"), Bytes.toBytes("val_" + i)));
107    }
108    table.put(puts);
109    admin.flush(tableName);
110    HRegionServer regionServer = cluster.getRegionServer(0);
111    String rsName = regionServer.getServerName().getAddress().toString();
112    int numRegions = regionServer.getNumberOfOnlineRegions();
113    List<HRegion> hRegions = regionServer.getRegions().stream()
114      .filter(hRegion -> hRegion.getRegionInfo().getTable().equals(tableName))
115      .collect(Collectors.toList());
116    RegionMover.RegionMoverBuilder rmBuilder =
117      new RegionMover.RegionMoverBuilder(rsName, TEST_UTIL.getConfiguration()).ack(true)
118        .maxthreads(8);
119    try (RegionMover rm = rmBuilder.build()) {
120      LOG.debug("Unloading {}", regionServer.getServerName());
121      rm.unload();
122      Assert.assertEquals(0, regionServer.getNumberOfOnlineRegions());
123      LOG.debug("Successfully Unloaded, now Loading");
124      admin.mergeRegionsAsync(new byte[][] { hRegions.get(0).getRegionInfo().getRegionName(),
125        hRegions.get(1).getRegionInfo().getRegionName() }, true).get(5, TimeUnit.SECONDS);
126      Assert.assertTrue(rm.load());
127      Assert.assertEquals(numRegions - 2, regionServer.getNumberOfOnlineRegions());
128    }
129  }
130
131  @Test
132  public void testWithSplitRegions() throws Exception {
133    final TableName tableName = TableName.valueOf(name.getMethodName());
134    SingleProcessHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
135    Admin admin = TEST_UTIL.getAdmin();
136    Table table = TEST_UTIL.getConnection().getTable(tableName);
137    List<Put> puts = new ArrayList<>();
138    for (int i = 10; i < 50000; i++) {
139      puts.add(new Put(Bytes.toBytes(i)).addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("q1"),
140        Bytes.toBytes("val_" + i)));
141    }
142    table.put(puts);
143    admin.flush(tableName);
144    admin.compact(tableName);
145    HRegionServer regionServer = cluster.getRegionServer(0);
146    String rsName = regionServer.getServerName().getAddress().toString();
147    int numRegions = regionServer.getNumberOfOnlineRegions();
148    List<HRegion> hRegions = regionServer.getRegions().stream()
149      .filter(hRegion -> hRegion.getRegionInfo().getTable().equals(tableName))
150      .collect(Collectors.toList());
151
152    RegionMover.RegionMoverBuilder rmBuilder =
153      new RegionMover.RegionMoverBuilder(rsName, TEST_UTIL.getConfiguration()).ack(true)
154        .maxthreads(8);
155    try (RegionMover rm = rmBuilder.build()) {
156      LOG.debug("Unloading {}", regionServer.getServerName());
157      rm.unload();
158      Assert.assertEquals(0, regionServer.getNumberOfOnlineRegions());
159      LOG.debug("Successfully Unloaded, now Loading");
160      HRegion hRegion = hRegions.get(1);
161      if (hRegion.getRegionInfo().getStartKey().length == 0) {
162        hRegion = hRegions.get(0);
163      }
164      int startKey = 0;
165      int endKey = Integer.MAX_VALUE;
166      if (hRegion.getRegionInfo().getStartKey().length > 0) {
167        startKey = Bytes.toInt(hRegion.getRegionInfo().getStartKey());
168      }
169      if (hRegion.getRegionInfo().getEndKey().length > 0) {
170        endKey = Bytes.toInt(hRegion.getRegionInfo().getEndKey());
171      }
172      int midKey = startKey + (endKey - startKey) / 2;
173      admin.splitRegionAsync(hRegion.getRegionInfo().getRegionName(), Bytes.toBytes(midKey)).get(5,
174        TimeUnit.SECONDS);
175      Assert.assertTrue(rm.load());
176      Assert.assertEquals(numRegions - 1, regionServer.getNumberOfOnlineRegions());
177    }
178  }
179
180  @Test
181  public void testFailedRegionMove() throws Exception {
182    final TableName tableName = TableName.valueOf(name.getMethodName());
183    SingleProcessHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
184    Admin admin = TEST_UTIL.getAdmin();
185    Table table = TEST_UTIL.getConnection().getTable(tableName);
186    List<Put> puts = new ArrayList<>();
187    for (int i = 0; i < 1000; i++) {
188      puts.add(new Put(Bytes.toBytes("rowkey_" + i)).addColumn(Bytes.toBytes("fam1"),
189        Bytes.toBytes("q1"), Bytes.toBytes("val_" + i)));
190    }
191    table.put(puts);
192    admin.flush(tableName);
193    HRegionServer regionServer = cluster.getRegionServer(0);
194    String rsName = regionServer.getServerName().getAddress().toString();
195    int numRegions = regionServer.getNumberOfOnlineRegions();
196    List<HRegion> hRegions = regionServer.getRegions().stream()
197      .filter(hRegion -> hRegion.getRegionInfo().getTable().equals(tableName))
198      .collect(Collectors.toList());
199    RegionMover.RegionMoverBuilder rmBuilder =
200      new RegionMover.RegionMoverBuilder(rsName, TEST_UTIL.getConfiguration()).ack(true)
201        .maxthreads(8);
202    try (RegionMover rm = rmBuilder.build()) {
203      LOG.debug("Unloading {}", regionServer.getServerName());
204      rm.unload();
205      Assert.assertEquals(0, regionServer.getNumberOfOnlineRegions());
206      LOG.debug("Successfully Unloaded, now Loading");
207      admin.offline(hRegions.get(0).getRegionInfo().getRegionName());
208      // loading regions will fail because of offline region
209      Assert.assertFalse(rm.load());
210    }
211  }
212
213}