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.HBaseTestingUtility; 026import org.apache.hadoop.hbase.MiniHBaseCluster; 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 HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 068 069 @BeforeClass 070 public static void setUpBeforeClass() throws Exception { 071 TEST_UTIL.getConfiguration().setInt("hbase.client.retries.number", 4); 072 TEST_UTIL.startMiniCluster(3); 073 TEST_UTIL.getAdmin().balancerSwitch(false, true); 074 } 075 076 @AfterClass 077 public static void tearDownAfterClass() throws Exception { 078 TEST_UTIL.shutdownMiniCluster(); 079 } 080 081 @Before 082 public void setUp() throws Exception { 083 final TableName tableName = TableName.valueOf(name.getMethodName()); 084 TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(tableName) 085 .setColumnFamily(ColumnFamilyDescriptorBuilder.of("fam1")).build(); 086 int startKey = 0; 087 int endKey = 80000; 088 TEST_UTIL.getAdmin().createTable(tableDesc, Bytes.toBytes(startKey), Bytes.toBytes(endKey), 9); 089 } 090 091 @After 092 public void tearDown() throws Exception { 093 final TableName tableName = TableName.valueOf(name.getMethodName()); 094 TEST_UTIL.getAdmin().disableTable(tableName); 095 TEST_UTIL.getAdmin().deleteTable(tableName); 096 } 097 098 @Test 099 public void testWithSplitRegions() throws Exception { 100 final TableName tableName = TableName.valueOf(name.getMethodName()); 101 MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster(); 102 Admin admin = TEST_UTIL.getAdmin(); 103 Table table = TEST_UTIL.getConnection().getTable(tableName); 104 List<Put> puts = new ArrayList<>(); 105 for (int i = 10; i < 50000; i++) { 106 puts.add(new Put(Bytes.toBytes(i)).addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("q1"), 107 Bytes.toBytes("val_" + i))); 108 } 109 table.put(puts); 110 admin.flush(tableName); 111 admin.compact(tableName); 112 HRegionServer regionServer = cluster.getRegionServer(0); 113 String rsName = regionServer.getServerName().getAddress().toString(); 114 int numRegions = regionServer.getNumberOfOnlineRegions(); 115 List<HRegion> hRegions = regionServer.getRegions().stream() 116 .filter(hRegion -> hRegion.getRegionInfo().getTable().equals(tableName)) 117 .collect(Collectors.toList()); 118 119 RegionMover.RegionMoverBuilder rmBuilder = 120 new RegionMover.RegionMoverBuilder(rsName, TEST_UTIL.getConfiguration()).ack(true) 121 .maxthreads(8); 122 try (RegionMover rm = rmBuilder.build()) { 123 LOG.debug("Unloading {}", regionServer.getServerName()); 124 rm.unload(); 125 Assert.assertEquals(0, regionServer.getNumberOfOnlineRegions()); 126 LOG.debug("Successfully Unloaded, now Loading"); 127 HRegion hRegion = hRegions.get(1); 128 if (hRegion.getRegionInfo().getStartKey().length == 0) { 129 hRegion = hRegions.get(0); 130 } 131 int startKey = 0; 132 int endKey = Integer.MAX_VALUE; 133 if (hRegion.getRegionInfo().getStartKey().length > 0) { 134 startKey = Bytes.toInt(hRegion.getRegionInfo().getStartKey()); 135 } 136 if (hRegion.getRegionInfo().getEndKey().length > 0) { 137 endKey = Bytes.toInt(hRegion.getRegionInfo().getEndKey()); 138 } 139 int midKey = startKey + (endKey - startKey) / 2; 140 admin.splitRegionAsync(hRegion.getRegionInfo().getRegionName(), Bytes.toBytes(midKey)).get(5, 141 TimeUnit.SECONDS); 142 Assert.assertTrue(rm.load()); 143 Assert.assertEquals(numRegions - 1, regionServer.getNumberOfOnlineRegions()); 144 } 145 } 146 147 @Test 148 public void testFailedRegionMove() throws Exception { 149 final TableName tableName = TableName.valueOf(name.getMethodName()); 150 MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster(); 151 Admin admin = TEST_UTIL.getAdmin(); 152 Table table = TEST_UTIL.getConnection().getTable(tableName); 153 List<Put> puts = new ArrayList<>(); 154 for (int i = 0; i < 1000; i++) { 155 puts.add(new Put(Bytes.toBytes("rowkey_" + i)).addColumn(Bytes.toBytes("fam1"), 156 Bytes.toBytes("q1"), Bytes.toBytes("val_" + i))); 157 } 158 table.put(puts); 159 admin.flush(tableName); 160 HRegionServer regionServer = cluster.getRegionServer(0); 161 String rsName = regionServer.getServerName().getAddress().toString(); 162 List<HRegion> hRegions = regionServer.getRegions().stream() 163 .filter(hRegion -> hRegion.getRegionInfo().getTable().equals(tableName)) 164 .collect(Collectors.toList()); 165 RegionMover.RegionMoverBuilder rmBuilder = 166 new RegionMover.RegionMoverBuilder(rsName, TEST_UTIL.getConfiguration()).ack(true) 167 .maxthreads(8); 168 try (RegionMover rm = rmBuilder.build()) { 169 LOG.debug("Unloading {}", regionServer.getServerName()); 170 rm.unload(); 171 Assert.assertEquals(0, regionServer.getNumberOfOnlineRegions()); 172 LOG.debug("Successfully Unloaded, now Loading"); 173 admin.offline(hRegions.get(0).getRegionInfo().getRegionName()); 174 // loading regions will fail because of offline region 175 Assert.assertFalse(rm.load()); 176 } 177 } 178 179}