busbey commented on a change in pull request #2172: URL: https://github.com/apache/hbase/pull/2172#discussion_r463338779
########## File path: hbase-server/src/main/java/org/apache/hadoop/hbase/util/MoveWithAck.java ########## @@ -0,0 +1,153 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hbase.util; + +import org.apache.hadoop.hbase.HRegionLocation; +import org.apache.hadoop.hbase.ServerName; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.hadoop.hbase.client.Connection; +import org.apache.hadoop.hbase.client.RegionInfo; +import org.apache.hadoop.hbase.client.ResultScanner; +import org.apache.hadoop.hbase.client.Scan; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter; +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.List; +import java.util.concurrent.Callable; + +/** + * Move Regions and make sure that they are up on the target server.If a region movement fails we + * exit as failure + */ [email protected] +class MoveWithAck implements Callable<Boolean> { + + private static final Logger LOG = LoggerFactory.getLogger(MoveWithAck.class); + + private final RegionInfo region; + private final ServerName targetServer; + private final List<RegionInfo> movedRegions; + private final ServerName sourceServer; + private final Connection conn; + private final Admin admin; + + MoveWithAck(Connection conn, RegionInfo regionInfo, ServerName sourceServer, + ServerName targetServer, List<RegionInfo> movedRegions) throws IOException { + this.conn = conn; + this.region = regionInfo; + this.targetServer = targetServer; + this.movedRegions = movedRegions; + this.sourceServer = sourceServer; + this.admin = conn.getAdmin(); + } + + @Override + public Boolean call() throws IOException, InterruptedException { + boolean moved = false; + int count = 0; + int retries = admin.getConfiguration() + .getInt(RegionMover.MOVE_RETRIES_MAX_KEY, RegionMover.DEFAULT_MOVE_RETRIES_MAX); + int maxWaitInSeconds = admin.getConfiguration() + .getInt(RegionMover.MOVE_WAIT_MAX_KEY, RegionMover.DEFAULT_MOVE_WAIT_MAX); + long startTime = EnvironmentEdgeManager.currentTime(); + boolean sameServer = true; + // Assert we can scan the region in its current location + isSuccessfulScan(region); + LOG + .info("Moving region: {} from {} to {}", region.getEncodedName(), sourceServer, targetServer); + while (count < retries && sameServer) { + if (count > 0) { + LOG.info("Retry " + count + " of maximum " + retries); Review comment: nit: should be at debug and include the name of the region. ########## File path: hbase-server/src/main/java/org/apache/hadoop/hbase/util/MoveWithAck.java ########## @@ -0,0 +1,153 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hbase.util; + +import org.apache.hadoop.hbase.HRegionLocation; +import org.apache.hadoop.hbase.ServerName; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.hadoop.hbase.client.Connection; +import org.apache.hadoop.hbase.client.RegionInfo; +import org.apache.hadoop.hbase.client.ResultScanner; +import org.apache.hadoop.hbase.client.Scan; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter; +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.List; +import java.util.concurrent.Callable; + +/** + * Move Regions and make sure that they are up on the target server.If a region movement fails we + * exit as failure + */ [email protected] +class MoveWithAck implements Callable<Boolean> { + + private static final Logger LOG = LoggerFactory.getLogger(MoveWithAck.class); + + private final RegionInfo region; + private final ServerName targetServer; + private final List<RegionInfo> movedRegions; + private final ServerName sourceServer; + private final Connection conn; + private final Admin admin; + + MoveWithAck(Connection conn, RegionInfo regionInfo, ServerName sourceServer, + ServerName targetServer, List<RegionInfo> movedRegions) throws IOException { + this.conn = conn; + this.region = regionInfo; + this.targetServer = targetServer; + this.movedRegions = movedRegions; + this.sourceServer = sourceServer; + this.admin = conn.getAdmin(); + } + + @Override + public Boolean call() throws IOException, InterruptedException { + boolean moved = false; + int count = 0; + int retries = admin.getConfiguration() + .getInt(RegionMover.MOVE_RETRIES_MAX_KEY, RegionMover.DEFAULT_MOVE_RETRIES_MAX); + int maxWaitInSeconds = admin.getConfiguration() + .getInt(RegionMover.MOVE_WAIT_MAX_KEY, RegionMover.DEFAULT_MOVE_WAIT_MAX); + long startTime = EnvironmentEdgeManager.currentTime(); + boolean sameServer = true; + // Assert we can scan the region in its current location + isSuccessfulScan(region); + LOG + .info("Moving region: {} from {} to {}", region.getEncodedName(), sourceServer, targetServer); + while (count < retries && sameServer) { + if (count > 0) { + LOG.info("Retry " + count + " of maximum " + retries); + } + count = count + 1; + admin.move(region.getEncodedNameAsBytes(), targetServer); + long maxWait = startTime + (maxWaitInSeconds * 1000); + while (EnvironmentEdgeManager.currentTime() < maxWait) { + sameServer = isSameServer(region, sourceServer); + if (!sameServer) { + break; + } + Thread.sleep(1000); + } + } + if (sameServer) { + LOG.error("Region: {} stuck on {} ,newServer={}", region.getRegionNameAsString(), + this.sourceServer, this.targetServer); + } else { + isSuccessfulScan(region); + LOG.info("Moved Region " + region.getRegionNameAsString() + " cost:" + String + .format("%.3f", (float) (EnvironmentEdgeManager.currentTime() - startTime) / 1000)); Review comment: nit: specify the cost is in seconds ########## File path: hbase-server/src/main/java/org/apache/hadoop/hbase/util/MoveWithAck.java ########## @@ -0,0 +1,153 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hbase.util; + +import org.apache.hadoop.hbase.HRegionLocation; +import org.apache.hadoop.hbase.ServerName; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.hadoop.hbase.client.Connection; +import org.apache.hadoop.hbase.client.RegionInfo; +import org.apache.hadoop.hbase.client.ResultScanner; +import org.apache.hadoop.hbase.client.Scan; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter; +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.List; +import java.util.concurrent.Callable; + +/** + * Move Regions and make sure that they are up on the target server.If a region movement fails we + * exit as failure + */ [email protected] +class MoveWithAck implements Callable<Boolean> { + + private static final Logger LOG = LoggerFactory.getLogger(MoveWithAck.class); + + private final RegionInfo region; + private final ServerName targetServer; + private final List<RegionInfo> movedRegions; + private final ServerName sourceServer; + private final Connection conn; + private final Admin admin; + + MoveWithAck(Connection conn, RegionInfo regionInfo, ServerName sourceServer, + ServerName targetServer, List<RegionInfo> movedRegions) throws IOException { + this.conn = conn; + this.region = regionInfo; + this.targetServer = targetServer; + this.movedRegions = movedRegions; + this.sourceServer = sourceServer; + this.admin = conn.getAdmin(); + } + + @Override + public Boolean call() throws IOException, InterruptedException { + boolean moved = false; + int count = 0; + int retries = admin.getConfiguration() + .getInt(RegionMover.MOVE_RETRIES_MAX_KEY, RegionMover.DEFAULT_MOVE_RETRIES_MAX); + int maxWaitInSeconds = admin.getConfiguration() + .getInt(RegionMover.MOVE_WAIT_MAX_KEY, RegionMover.DEFAULT_MOVE_WAIT_MAX); + long startTime = EnvironmentEdgeManager.currentTime(); + boolean sameServer = true; + // Assert we can scan the region in its current location + isSuccessfulScan(region); + LOG + .info("Moving region: {} from {} to {}", region.getEncodedName(), sourceServer, targetServer); + while (count < retries && sameServer) { + if (count > 0) { + LOG.info("Retry " + count + " of maximum " + retries); Review comment: nit: should be param substitution ########## File path: hbase-server/src/main/java/org/apache/hadoop/hbase/util/RegionMover.java ########## @@ -587,8 +485,14 @@ private void waitMoveTasksToFinish(ExecutorService moveRegionsPool, LOG.error("Interrupted while waiting for Thread to Complete " + e.getMessage(), e); throw e; } catch (ExecutionException e) { - LOG.error("Got Exception From Thread While moving region " + e.getMessage(), e); - throw e; + boolean ignoreFailure = ignoreRegionMoveFailure(e); + if (ignoreFailure) { + LOG.debug("Ignore region move failure, it might have been split/merged, " Review comment: please just pass the exception to the logging system so it will include a stack trace rather than try to build the message into our log. ########## File path: hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestRegionMover.java ########## @@ -83,12 +85,11 @@ public static void tearDownAfterClass() throws Exception { @Before public void setUp() throws Exception { // Create a pre-split table just to populate some regions - TableName tableName = TableName.valueOf("testRegionMover"); Admin admin = TEST_UTIL.getAdmin(); - if (admin.tableExists(tableName)) { - TEST_UTIL.deleteTable(tableName); + if (admin.tableExists(TABLE_NAME)) { Review comment: this is race-y and won't work properly if tests are run in parallel. we should use a junit rule to get the name of the test method and use that for the table name. ########## File path: hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestRegionMover2.java ########## @@ -0,0 +1,202 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hbase.util; + +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.MiniHBaseCluster; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; +import org.apache.hadoop.hbase.client.Put; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.client.TableDescriptor; +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; +import org.apache.hadoop.hbase.regionserver.HRegion; +import org.apache.hadoop.hbase.regionserver.HRegionServer; +import org.apache.hadoop.hbase.testclassification.LargeTests; +import org.apache.hadoop.hbase.testclassification.MiscTests; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +/** + * Tests for Region Mover Load/Unload functionality with and without ack mode and also to test + * exclude functionality useful for rack decommissioning + */ +@Category({ MiscTests.class, LargeTests.class}) +public class TestRegionMover2 { + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestRegionMover2.class); + + private static final Logger LOG = LoggerFactory.getLogger(TestRegionMover2.class); + + private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); + + private static final TableName TABLE_NAME = TableName.valueOf("testRegionMover2"); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + TEST_UTIL.startMiniCluster(3); + TEST_UTIL.getAdmin().balancerSwitch(false, true); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + TEST_UTIL.shutdownMiniCluster(); + } + + @Before + public void setUp() throws Exception { + // Create a pre-split table just to populate some regions + Admin admin = TEST_UTIL.getAdmin(); + if (admin.tableExists(TABLE_NAME)) { + TEST_UTIL.deleteTable(TABLE_NAME); + } + TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(TABLE_NAME) + .setColumnFamily(ColumnFamilyDescriptorBuilder.of("fam1")).build(); + int startKey = 0; + int endKey = 80000; + admin.createTable(tableDesc, Bytes.toBytes(startKey), Bytes.toBytes(endKey), 9); + } + + @Test + public void testWithMergedRegions() throws Exception { + MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster(); + Admin admin = TEST_UTIL.getAdmin(); + Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME); + List<Put> puts = new ArrayList<>(); + for (int i = 0; i < 10000; i++) { + puts.add(new Put(Bytes.toBytes("rowkey_" + i)) + .addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("q1"), Bytes.toBytes("val_" + i))); + } + table.put(puts); + admin.flush(TABLE_NAME); + HRegionServer regionServer = cluster.getRegionServer(0); + String rsName = regionServer.getServerName().getAddress().toString(); + int numRegions = regionServer.getNumberOfOnlineRegions(); + List<HRegion> hRegions = regionServer.getRegions().stream() + .filter(hRegion -> hRegion.getRegionInfo().getTable().equals(TABLE_NAME)) + .collect(Collectors.toList()); + RegionMover.RegionMoverBuilder rmBuilder = + new RegionMover.RegionMoverBuilder(rsName, TEST_UTIL.getConfiguration()).ack(true) + .maxthreads(8); + try (RegionMover rm = rmBuilder.build()) { + LOG.debug("Unloading " + regionServer.getServerName()); + rm.unload(); + Assert.assertEquals(0, regionServer.getNumberOfOnlineRegions()); + LOG.debug("Successfully Unloaded\nNow Loading"); + admin.mergeRegionsAsync(new byte[][] { hRegions.get(0).getRegionInfo().getRegionName(), + hRegions.get(1).getRegionInfo().getRegionName() }, true) + .get(5, TimeUnit.SECONDS); + Assert.assertTrue(rm.load()); + Assert.assertEquals(numRegions - 2, regionServer.getNumberOfOnlineRegions()); + } + } + + @Test + public void testWithSplitRegions() throws Exception { + MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster(); + Admin admin = TEST_UTIL.getAdmin(); + Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME); + List<Put> puts = new ArrayList<>(); + for (int i = 10; i < 50000; i++) { + puts.add(new Put(Bytes.toBytes(i)) + .addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("q1"), Bytes.toBytes("val_" + i))); + } + table.put(puts); + admin.flush(TABLE_NAME); + admin.compact(TABLE_NAME); + HRegionServer regionServer = cluster.getRegionServer(0); + String rsName = regionServer.getServerName().getAddress().toString(); + int numRegions = regionServer.getNumberOfOnlineRegions(); + List<HRegion> hRegions = regionServer.getRegions().stream() + .filter(hRegion -> hRegion.getRegionInfo().getTable().equals(TABLE_NAME)) + .collect(Collectors.toList()); + + RegionMover.RegionMoverBuilder rmBuilder = + new RegionMover.RegionMoverBuilder(rsName, TEST_UTIL.getConfiguration()).ack(true) + .maxthreads(8); + try (RegionMover rm = rmBuilder.build()) { + LOG.debug("Unloading " + regionServer.getServerName()); + rm.unload(); + Assert.assertEquals(0, regionServer.getNumberOfOnlineRegions()); + LOG.debug("Successfully Unloaded\nNow Loading"); + HRegion hRegion = hRegions.get(1); + int startKey = 0; + int endKey = Integer.MAX_VALUE; + if (hRegion.getRegionInfo().getStartKey().length > 0) { + startKey = Bytes.toInt(hRegion.getRegionInfo().getStartKey()); + } + if (hRegion.getRegionInfo().getEndKey().length > 0) { + endKey = Bytes.toInt(hRegion.getRegionInfo().getEndKey()); + } + int midKey = startKey + (endKey - startKey) / 2; + admin.splitRegionAsync(hRegion.getRegionInfo().getRegionName(), Bytes.toBytes(midKey)) + .get(5, TimeUnit.SECONDS); + Assert.assertTrue(rm.load()); + Assert.assertEquals(numRegions - 1, regionServer.getNumberOfOnlineRegions()); + } + } + + @Test + public void testFailedRegionMove() throws Exception { + MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster(); + Admin admin = TEST_UTIL.getAdmin(); + Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME); + List<Put> puts = new ArrayList<>(); + for (int i = 0; i < 1000; i++) { + puts.add(new Put(Bytes.toBytes("rowkey_" + i)) + .addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("q1"), Bytes.toBytes("val_" + i))); + } + table.put(puts); + admin.flush(TABLE_NAME); + HRegionServer regionServer = cluster.getRegionServer(0); + String rsName = regionServer.getServerName().getAddress().toString(); + int numRegions = regionServer.getNumberOfOnlineRegions(); + List<HRegion> hRegions = regionServer.getRegions().stream() + .filter(hRegion -> hRegion.getRegionInfo().getTable().equals(TABLE_NAME)) + .collect(Collectors.toList()); + RegionMover.RegionMoverBuilder rmBuilder = + new RegionMover.RegionMoverBuilder(rsName, TEST_UTIL.getConfiguration()).ack(true) + .maxthreads(8); + try (RegionMover rm = rmBuilder.build()) { + LOG.debug("Unloading " + regionServer.getServerName()); + rm.unload(); + Assert.assertEquals(0, regionServer.getNumberOfOnlineRegions()); + LOG.debug("Successfully Unloaded\nNow Loading"); Review comment: no newlines in log messages please ########## File path: hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestRegionMover2.java ########## @@ -0,0 +1,202 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hbase.util; + +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.MiniHBaseCluster; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; +import org.apache.hadoop.hbase.client.Put; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.client.TableDescriptor; +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; +import org.apache.hadoop.hbase.regionserver.HRegion; +import org.apache.hadoop.hbase.regionserver.HRegionServer; +import org.apache.hadoop.hbase.testclassification.LargeTests; +import org.apache.hadoop.hbase.testclassification.MiscTests; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +/** + * Tests for Region Mover Load/Unload functionality with and without ack mode and also to test + * exclude functionality useful for rack decommissioning + */ +@Category({ MiscTests.class, LargeTests.class}) +public class TestRegionMover2 { + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestRegionMover2.class); + + private static final Logger LOG = LoggerFactory.getLogger(TestRegionMover2.class); + + private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); + + private static final TableName TABLE_NAME = TableName.valueOf("testRegionMover2"); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + TEST_UTIL.startMiniCluster(3); + TEST_UTIL.getAdmin().balancerSwitch(false, true); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + TEST_UTIL.shutdownMiniCluster(); + } + + @Before + public void setUp() throws Exception { + // Create a pre-split table just to populate some regions + Admin admin = TEST_UTIL.getAdmin(); + if (admin.tableExists(TABLE_NAME)) { + TEST_UTIL.deleteTable(TABLE_NAME); + } + TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(TABLE_NAME) + .setColumnFamily(ColumnFamilyDescriptorBuilder.of("fam1")).build(); + int startKey = 0; + int endKey = 80000; + admin.createTable(tableDesc, Bytes.toBytes(startKey), Bytes.toBytes(endKey), 9); + } + + @Test + public void testWithMergedRegions() throws Exception { + MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster(); + Admin admin = TEST_UTIL.getAdmin(); + Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME); + List<Put> puts = new ArrayList<>(); + for (int i = 0; i < 10000; i++) { + puts.add(new Put(Bytes.toBytes("rowkey_" + i)) + .addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("q1"), Bytes.toBytes("val_" + i))); + } + table.put(puts); + admin.flush(TABLE_NAME); + HRegionServer regionServer = cluster.getRegionServer(0); + String rsName = regionServer.getServerName().getAddress().toString(); + int numRegions = regionServer.getNumberOfOnlineRegions(); + List<HRegion> hRegions = regionServer.getRegions().stream() + .filter(hRegion -> hRegion.getRegionInfo().getTable().equals(TABLE_NAME)) + .collect(Collectors.toList()); + RegionMover.RegionMoverBuilder rmBuilder = + new RegionMover.RegionMoverBuilder(rsName, TEST_UTIL.getConfiguration()).ack(true) + .maxthreads(8); + try (RegionMover rm = rmBuilder.build()) { + LOG.debug("Unloading " + regionServer.getServerName()); + rm.unload(); + Assert.assertEquals(0, regionServer.getNumberOfOnlineRegions()); + LOG.debug("Successfully Unloaded\nNow Loading"); + admin.mergeRegionsAsync(new byte[][] { hRegions.get(0).getRegionInfo().getRegionName(), + hRegions.get(1).getRegionInfo().getRegionName() }, true) + .get(5, TimeUnit.SECONDS); + Assert.assertTrue(rm.load()); + Assert.assertEquals(numRegions - 2, regionServer.getNumberOfOnlineRegions()); + } + } + + @Test + public void testWithSplitRegions() throws Exception { + MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster(); + Admin admin = TEST_UTIL.getAdmin(); + Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME); Review comment: the javadoc for HBaseTestingUtility claims that getConnection isn't threadsafe. if that is true then we need to do a bunch of work to safely do this line. ########## File path: hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestRegionMover2.java ########## @@ -0,0 +1,202 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hbase.util; + +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.MiniHBaseCluster; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; +import org.apache.hadoop.hbase.client.Put; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.client.TableDescriptor; +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; +import org.apache.hadoop.hbase.regionserver.HRegion; +import org.apache.hadoop.hbase.regionserver.HRegionServer; +import org.apache.hadoop.hbase.testclassification.LargeTests; +import org.apache.hadoop.hbase.testclassification.MiscTests; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +/** + * Tests for Region Mover Load/Unload functionality with and without ack mode and also to test + * exclude functionality useful for rack decommissioning + */ +@Category({ MiscTests.class, LargeTests.class}) +public class TestRegionMover2 { + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestRegionMover2.class); + + private static final Logger LOG = LoggerFactory.getLogger(TestRegionMover2.class); + + private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); + + private static final TableName TABLE_NAME = TableName.valueOf("testRegionMover2"); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + TEST_UTIL.startMiniCluster(3); + TEST_UTIL.getAdmin().balancerSwitch(false, true); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + TEST_UTIL.shutdownMiniCluster(); + } + + @Before + public void setUp() throws Exception { + // Create a pre-split table just to populate some regions + Admin admin = TEST_UTIL.getAdmin(); + if (admin.tableExists(TABLE_NAME)) { + TEST_UTIL.deleteTable(TABLE_NAME); + } + TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(TABLE_NAME) + .setColumnFamily(ColumnFamilyDescriptorBuilder.of("fam1")).build(); + int startKey = 0; + int endKey = 80000; + admin.createTable(tableDesc, Bytes.toBytes(startKey), Bytes.toBytes(endKey), 9); + } + + @Test + public void testWithMergedRegions() throws Exception { + MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster(); + Admin admin = TEST_UTIL.getAdmin(); + Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME); + List<Put> puts = new ArrayList<>(); + for (int i = 0; i < 10000; i++) { + puts.add(new Put(Bytes.toBytes("rowkey_" + i)) + .addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("q1"), Bytes.toBytes("val_" + i))); + } + table.put(puts); + admin.flush(TABLE_NAME); + HRegionServer regionServer = cluster.getRegionServer(0); + String rsName = regionServer.getServerName().getAddress().toString(); + int numRegions = regionServer.getNumberOfOnlineRegions(); + List<HRegion> hRegions = regionServer.getRegions().stream() + .filter(hRegion -> hRegion.getRegionInfo().getTable().equals(TABLE_NAME)) + .collect(Collectors.toList()); + RegionMover.RegionMoverBuilder rmBuilder = + new RegionMover.RegionMoverBuilder(rsName, TEST_UTIL.getConfiguration()).ack(true) + .maxthreads(8); + try (RegionMover rm = rmBuilder.build()) { + LOG.debug("Unloading " + regionServer.getServerName()); + rm.unload(); + Assert.assertEquals(0, regionServer.getNumberOfOnlineRegions()); + LOG.debug("Successfully Unloaded\nNow Loading"); + admin.mergeRegionsAsync(new byte[][] { hRegions.get(0).getRegionInfo().getRegionName(), + hRegions.get(1).getRegionInfo().getRegionName() }, true) + .get(5, TimeUnit.SECONDS); + Assert.assertTrue(rm.load()); + Assert.assertEquals(numRegions - 2, regionServer.getNumberOfOnlineRegions()); + } + } + + @Test + public void testWithSplitRegions() throws Exception { + MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster(); + Admin admin = TEST_UTIL.getAdmin(); + Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME); + List<Put> puts = new ArrayList<>(); + for (int i = 10; i < 50000; i++) { + puts.add(new Put(Bytes.toBytes(i)) + .addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("q1"), Bytes.toBytes("val_" + i))); + } + table.put(puts); + admin.flush(TABLE_NAME); + admin.compact(TABLE_NAME); + HRegionServer regionServer = cluster.getRegionServer(0); + String rsName = regionServer.getServerName().getAddress().toString(); + int numRegions = regionServer.getNumberOfOnlineRegions(); + List<HRegion> hRegions = regionServer.getRegions().stream() + .filter(hRegion -> hRegion.getRegionInfo().getTable().equals(TABLE_NAME)) + .collect(Collectors.toList()); + + RegionMover.RegionMoverBuilder rmBuilder = + new RegionMover.RegionMoverBuilder(rsName, TEST_UTIL.getConfiguration()).ack(true) + .maxthreads(8); + try (RegionMover rm = rmBuilder.build()) { + LOG.debug("Unloading " + regionServer.getServerName()); Review comment: nit: should use parameter substitution ########## File path: hbase-server/src/main/java/org/apache/hadoop/hbase/util/MoveWithAck.java ########## @@ -0,0 +1,153 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hbase.util; + +import org.apache.hadoop.hbase.HRegionLocation; +import org.apache.hadoop.hbase.ServerName; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.hadoop.hbase.client.Connection; +import org.apache.hadoop.hbase.client.RegionInfo; +import org.apache.hadoop.hbase.client.ResultScanner; +import org.apache.hadoop.hbase.client.Scan; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter; +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.List; +import java.util.concurrent.Callable; + +/** + * Move Regions and make sure that they are up on the target server.If a region movement fails we + * exit as failure + */ [email protected] +class MoveWithAck implements Callable<Boolean> { + + private static final Logger LOG = LoggerFactory.getLogger(MoveWithAck.class); + + private final RegionInfo region; + private final ServerName targetServer; + private final List<RegionInfo> movedRegions; + private final ServerName sourceServer; + private final Connection conn; + private final Admin admin; + + MoveWithAck(Connection conn, RegionInfo regionInfo, ServerName sourceServer, + ServerName targetServer, List<RegionInfo> movedRegions) throws IOException { + this.conn = conn; + this.region = regionInfo; + this.targetServer = targetServer; + this.movedRegions = movedRegions; + this.sourceServer = sourceServer; + this.admin = conn.getAdmin(); + } + + @Override + public Boolean call() throws IOException, InterruptedException { + boolean moved = false; + int count = 0; + int retries = admin.getConfiguration() + .getInt(RegionMover.MOVE_RETRIES_MAX_KEY, RegionMover.DEFAULT_MOVE_RETRIES_MAX); + int maxWaitInSeconds = admin.getConfiguration() + .getInt(RegionMover.MOVE_WAIT_MAX_KEY, RegionMover.DEFAULT_MOVE_WAIT_MAX); + long startTime = EnvironmentEdgeManager.currentTime(); + boolean sameServer = true; + // Assert we can scan the region in its current location + isSuccessfulScan(region); + LOG + .info("Moving region: {} from {} to {}", region.getEncodedName(), sourceServer, targetServer); + while (count < retries && sameServer) { + if (count > 0) { + LOG.info("Retry " + count + " of maximum " + retries); + } + count = count + 1; + admin.move(region.getEncodedNameAsBytes(), targetServer); + long maxWait = startTime + (maxWaitInSeconds * 1000); + while (EnvironmentEdgeManager.currentTime() < maxWait) { + sameServer = isSameServer(region, sourceServer); + if (!sameServer) { + break; + } + Thread.sleep(1000); + } + } + if (sameServer) { + LOG.error("Region: {} stuck on {} ,newServer={}", region.getRegionNameAsString(), Review comment: nit: how long did this take? ########## File path: hbase-server/src/main/java/org/apache/hadoop/hbase/util/RegionMover.java ########## @@ -522,13 +420,13 @@ private void unloadRegions(ServerName server, List<ServerName> regionServers, while (counter < regionsToMove.size()) { Review comment: nit: this should be a for each loop on the `regionsToMove` list ########## File path: hbase-server/src/main/java/org/apache/hadoop/hbase/util/MoveWithAck.java ########## @@ -0,0 +1,153 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hbase.util; + +import org.apache.hadoop.hbase.HRegionLocation; +import org.apache.hadoop.hbase.ServerName; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.hadoop.hbase.client.Connection; +import org.apache.hadoop.hbase.client.RegionInfo; +import org.apache.hadoop.hbase.client.ResultScanner; +import org.apache.hadoop.hbase.client.Scan; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter; +import org.apache.yetus.audience.InterfaceAudience; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.List; +import java.util.concurrent.Callable; + +/** + * Move Regions and make sure that they are up on the target server.If a region movement fails we + * exit as failure + */ [email protected] +class MoveWithAck implements Callable<Boolean> { + + private static final Logger LOG = LoggerFactory.getLogger(MoveWithAck.class); + + private final RegionInfo region; + private final ServerName targetServer; + private final List<RegionInfo> movedRegions; + private final ServerName sourceServer; + private final Connection conn; + private final Admin admin; + + MoveWithAck(Connection conn, RegionInfo regionInfo, ServerName sourceServer, + ServerName targetServer, List<RegionInfo> movedRegions) throws IOException { + this.conn = conn; + this.region = regionInfo; + this.targetServer = targetServer; + this.movedRegions = movedRegions; + this.sourceServer = sourceServer; + this.admin = conn.getAdmin(); + } + + @Override + public Boolean call() throws IOException, InterruptedException { + boolean moved = false; + int count = 0; + int retries = admin.getConfiguration() + .getInt(RegionMover.MOVE_RETRIES_MAX_KEY, RegionMover.DEFAULT_MOVE_RETRIES_MAX); + int maxWaitInSeconds = admin.getConfiguration() + .getInt(RegionMover.MOVE_WAIT_MAX_KEY, RegionMover.DEFAULT_MOVE_WAIT_MAX); + long startTime = EnvironmentEdgeManager.currentTime(); + boolean sameServer = true; + // Assert we can scan the region in its current location + isSuccessfulScan(region); + LOG + .info("Moving region: {} from {} to {}", region.getEncodedName(), sourceServer, targetServer); + while (count < retries && sameServer) { + if (count > 0) { + LOG.info("Retry " + count + " of maximum " + retries); + } + count = count + 1; + admin.move(region.getEncodedNameAsBytes(), targetServer); + long maxWait = startTime + (maxWaitInSeconds * 1000); + while (EnvironmentEdgeManager.currentTime() < maxWait) { + sameServer = isSameServer(region, sourceServer); + if (!sameServer) { + break; + } + Thread.sleep(1000); + } + } + if (sameServer) { + LOG.error("Region: {} stuck on {} ,newServer={}", region.getRegionNameAsString(), + this.sourceServer, this.targetServer); + } else { + isSuccessfulScan(region); + LOG.info("Moved Region " + region.getRegionNameAsString() + " cost:" + String + .format("%.3f", (float) (EnvironmentEdgeManager.currentTime() - startTime) / 1000)); + moved = true; + movedRegions.add(region); + } + return moved; + } + + /** + * Tries to scan a row from passed region + */ + private void isSuccessfulScan(RegionInfo region) throws IOException { + Scan scan = new Scan().withStartRow(region.getStartKey()).setRaw(true).setOneRowLimit() + .setMaxResultSize(1L).setCaching(1).setFilter(new FirstKeyOnlyFilter()) + .setCacheBlocks(false); + try (Table table = conn.getTable(region.getTable()); + ResultScanner scanner = table.getScanner(scan)) { + scanner.next(); + } catch (IOException e) { + LOG.error("Could not scan region:" + region.getEncodedName(), e); Review comment: nit: should use parameter substitution ########## File path: hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestRegionMover2.java ########## @@ -0,0 +1,202 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hbase.util; + +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.MiniHBaseCluster; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; +import org.apache.hadoop.hbase.client.Put; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.client.TableDescriptor; +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; +import org.apache.hadoop.hbase.regionserver.HRegion; +import org.apache.hadoop.hbase.regionserver.HRegionServer; +import org.apache.hadoop.hbase.testclassification.LargeTests; +import org.apache.hadoop.hbase.testclassification.MiscTests; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +/** + * Tests for Region Mover Load/Unload functionality with and without ack mode and also to test + * exclude functionality useful for rack decommissioning + */ +@Category({ MiscTests.class, LargeTests.class}) +public class TestRegionMover2 { + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestRegionMover2.class); + + private static final Logger LOG = LoggerFactory.getLogger(TestRegionMover2.class); + + private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); + + private static final TableName TABLE_NAME = TableName.valueOf("testRegionMover2"); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + TEST_UTIL.startMiniCluster(3); + TEST_UTIL.getAdmin().balancerSwitch(false, true); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + TEST_UTIL.shutdownMiniCluster(); + } + + @Before + public void setUp() throws Exception { + // Create a pre-split table just to populate some regions + Admin admin = TEST_UTIL.getAdmin(); + if (admin.tableExists(TABLE_NAME)) { Review comment: this is race-y and won't work properly if tests are run in parallel. we should use a junit rule to get the name of the test method and use that for the table name. ########## File path: hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestRegionMover2.java ########## @@ -0,0 +1,202 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hbase.util; + +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.MiniHBaseCluster; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; +import org.apache.hadoop.hbase.client.Put; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.client.TableDescriptor; +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; +import org.apache.hadoop.hbase.regionserver.HRegion; +import org.apache.hadoop.hbase.regionserver.HRegionServer; +import org.apache.hadoop.hbase.testclassification.LargeTests; +import org.apache.hadoop.hbase.testclassification.MiscTests; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +/** + * Tests for Region Mover Load/Unload functionality with and without ack mode and also to test + * exclude functionality useful for rack decommissioning + */ +@Category({ MiscTests.class, LargeTests.class}) +public class TestRegionMover2 { + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestRegionMover2.class); + + private static final Logger LOG = LoggerFactory.getLogger(TestRegionMover2.class); + + private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); + + private static final TableName TABLE_NAME = TableName.valueOf("testRegionMover2"); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + TEST_UTIL.startMiniCluster(3); + TEST_UTIL.getAdmin().balancerSwitch(false, true); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + TEST_UTIL.shutdownMiniCluster(); + } + + @Before + public void setUp() throws Exception { + // Create a pre-split table just to populate some regions + Admin admin = TEST_UTIL.getAdmin(); + if (admin.tableExists(TABLE_NAME)) { + TEST_UTIL.deleteTable(TABLE_NAME); + } + TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(TABLE_NAME) + .setColumnFamily(ColumnFamilyDescriptorBuilder.of("fam1")).build(); + int startKey = 0; + int endKey = 80000; + admin.createTable(tableDesc, Bytes.toBytes(startKey), Bytes.toBytes(endKey), 9); + } + + @Test + public void testWithMergedRegions() throws Exception { + MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster(); + Admin admin = TEST_UTIL.getAdmin(); + Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME); Review comment: the javadoc for `HBaseTestingUtility` claims that `getConnection` isn't threadsafe. if that is true then we need to do a bunch of work to safely do this line. ########## File path: hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestRegionMover2.java ########## @@ -0,0 +1,202 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hbase.util; + +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.MiniHBaseCluster; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; +import org.apache.hadoop.hbase.client.Put; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.client.TableDescriptor; +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; +import org.apache.hadoop.hbase.regionserver.HRegion; +import org.apache.hadoop.hbase.regionserver.HRegionServer; +import org.apache.hadoop.hbase.testclassification.LargeTests; +import org.apache.hadoop.hbase.testclassification.MiscTests; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +/** + * Tests for Region Mover Load/Unload functionality with and without ack mode and also to test + * exclude functionality useful for rack decommissioning + */ +@Category({ MiscTests.class, LargeTests.class}) +public class TestRegionMover2 { + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestRegionMover2.class); + + private static final Logger LOG = LoggerFactory.getLogger(TestRegionMover2.class); + + private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); + + private static final TableName TABLE_NAME = TableName.valueOf("testRegionMover2"); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + TEST_UTIL.startMiniCluster(3); + TEST_UTIL.getAdmin().balancerSwitch(false, true); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + TEST_UTIL.shutdownMiniCluster(); + } + + @Before + public void setUp() throws Exception { + // Create a pre-split table just to populate some regions + Admin admin = TEST_UTIL.getAdmin(); + if (admin.tableExists(TABLE_NAME)) { + TEST_UTIL.deleteTable(TABLE_NAME); + } + TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(TABLE_NAME) + .setColumnFamily(ColumnFamilyDescriptorBuilder.of("fam1")).build(); + int startKey = 0; + int endKey = 80000; + admin.createTable(tableDesc, Bytes.toBytes(startKey), Bytes.toBytes(endKey), 9); + } + + @Test + public void testWithMergedRegions() throws Exception { + MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster(); + Admin admin = TEST_UTIL.getAdmin(); + Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME); + List<Put> puts = new ArrayList<>(); + for (int i = 0; i < 10000; i++) { + puts.add(new Put(Bytes.toBytes("rowkey_" + i)) + .addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("q1"), Bytes.toBytes("val_" + i))); + } + table.put(puts); + admin.flush(TABLE_NAME); + HRegionServer regionServer = cluster.getRegionServer(0); + String rsName = regionServer.getServerName().getAddress().toString(); + int numRegions = regionServer.getNumberOfOnlineRegions(); + List<HRegion> hRegions = regionServer.getRegions().stream() + .filter(hRegion -> hRegion.getRegionInfo().getTable().equals(TABLE_NAME)) + .collect(Collectors.toList()); + RegionMover.RegionMoverBuilder rmBuilder = + new RegionMover.RegionMoverBuilder(rsName, TEST_UTIL.getConfiguration()).ack(true) + .maxthreads(8); + try (RegionMover rm = rmBuilder.build()) { + LOG.debug("Unloading " + regionServer.getServerName()); + rm.unload(); + Assert.assertEquals(0, regionServer.getNumberOfOnlineRegions()); + LOG.debug("Successfully Unloaded\nNow Loading"); + admin.mergeRegionsAsync(new byte[][] { hRegions.get(0).getRegionInfo().getRegionName(), + hRegions.get(1).getRegionInfo().getRegionName() }, true) + .get(5, TimeUnit.SECONDS); + Assert.assertTrue(rm.load()); + Assert.assertEquals(numRegions - 2, regionServer.getNumberOfOnlineRegions()); + } + } + + @Test + public void testWithSplitRegions() throws Exception { + MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster(); + Admin admin = TEST_UTIL.getAdmin(); + Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME); + List<Put> puts = new ArrayList<>(); + for (int i = 10; i < 50000; i++) { + puts.add(new Put(Bytes.toBytes(i)) + .addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("q1"), Bytes.toBytes("val_" + i))); + } + table.put(puts); + admin.flush(TABLE_NAME); + admin.compact(TABLE_NAME); + HRegionServer regionServer = cluster.getRegionServer(0); + String rsName = regionServer.getServerName().getAddress().toString(); + int numRegions = regionServer.getNumberOfOnlineRegions(); + List<HRegion> hRegions = regionServer.getRegions().stream() + .filter(hRegion -> hRegion.getRegionInfo().getTable().equals(TABLE_NAME)) + .collect(Collectors.toList()); + + RegionMover.RegionMoverBuilder rmBuilder = + new RegionMover.RegionMoverBuilder(rsName, TEST_UTIL.getConfiguration()).ack(true) + .maxthreads(8); + try (RegionMover rm = rmBuilder.build()) { + LOG.debug("Unloading " + regionServer.getServerName()); + rm.unload(); + Assert.assertEquals(0, regionServer.getNumberOfOnlineRegions()); + LOG.debug("Successfully Unloaded\nNow Loading"); + HRegion hRegion = hRegions.get(1); + int startKey = 0; + int endKey = Integer.MAX_VALUE; + if (hRegion.getRegionInfo().getStartKey().length > 0) { + startKey = Bytes.toInt(hRegion.getRegionInfo().getStartKey()); + } + if (hRegion.getRegionInfo().getEndKey().length > 0) { + endKey = Bytes.toInt(hRegion.getRegionInfo().getEndKey()); + } + int midKey = startKey + (endKey - startKey) / 2; + admin.splitRegionAsync(hRegion.getRegionInfo().getRegionName(), Bytes.toBytes(midKey)) + .get(5, TimeUnit.SECONDS); + Assert.assertTrue(rm.load()); + Assert.assertEquals(numRegions - 1, regionServer.getNumberOfOnlineRegions()); + } + } + + @Test + public void testFailedRegionMove() throws Exception { + MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster(); + Admin admin = TEST_UTIL.getAdmin(); + Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME); Review comment: the javadoc for HBaseTestingUtility claims that getConnection isn't threadsafe. if that is true then we need to do a bunch of work to safely do this line. ########## File path: hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestRegionMover2.java ########## @@ -0,0 +1,202 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hbase.util; + +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.MiniHBaseCluster; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; +import org.apache.hadoop.hbase.client.Put; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.client.TableDescriptor; +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; +import org.apache.hadoop.hbase.regionserver.HRegion; +import org.apache.hadoop.hbase.regionserver.HRegionServer; +import org.apache.hadoop.hbase.testclassification.LargeTests; +import org.apache.hadoop.hbase.testclassification.MiscTests; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +/** + * Tests for Region Mover Load/Unload functionality with and without ack mode and also to test + * exclude functionality useful for rack decommissioning + */ +@Category({ MiscTests.class, LargeTests.class}) +public class TestRegionMover2 { + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestRegionMover2.class); + + private static final Logger LOG = LoggerFactory.getLogger(TestRegionMover2.class); + + private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); + + private static final TableName TABLE_NAME = TableName.valueOf("testRegionMover2"); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + TEST_UTIL.startMiniCluster(3); + TEST_UTIL.getAdmin().balancerSwitch(false, true); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + TEST_UTIL.shutdownMiniCluster(); + } + + @Before + public void setUp() throws Exception { + // Create a pre-split table just to populate some regions + Admin admin = TEST_UTIL.getAdmin(); + if (admin.tableExists(TABLE_NAME)) { + TEST_UTIL.deleteTable(TABLE_NAME); + } + TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(TABLE_NAME) + .setColumnFamily(ColumnFamilyDescriptorBuilder.of("fam1")).build(); + int startKey = 0; + int endKey = 80000; + admin.createTable(tableDesc, Bytes.toBytes(startKey), Bytes.toBytes(endKey), 9); + } + + @Test + public void testWithMergedRegions() throws Exception { + MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster(); + Admin admin = TEST_UTIL.getAdmin(); + Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME); + List<Put> puts = new ArrayList<>(); + for (int i = 0; i < 10000; i++) { + puts.add(new Put(Bytes.toBytes("rowkey_" + i)) + .addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("q1"), Bytes.toBytes("val_" + i))); + } + table.put(puts); + admin.flush(TABLE_NAME); + HRegionServer regionServer = cluster.getRegionServer(0); + String rsName = regionServer.getServerName().getAddress().toString(); + int numRegions = regionServer.getNumberOfOnlineRegions(); + List<HRegion> hRegions = regionServer.getRegions().stream() + .filter(hRegion -> hRegion.getRegionInfo().getTable().equals(TABLE_NAME)) + .collect(Collectors.toList()); + RegionMover.RegionMoverBuilder rmBuilder = + new RegionMover.RegionMoverBuilder(rsName, TEST_UTIL.getConfiguration()).ack(true) + .maxthreads(8); + try (RegionMover rm = rmBuilder.build()) { + LOG.debug("Unloading " + regionServer.getServerName()); + rm.unload(); + Assert.assertEquals(0, regionServer.getNumberOfOnlineRegions()); + LOG.debug("Successfully Unloaded\nNow Loading"); + admin.mergeRegionsAsync(new byte[][] { hRegions.get(0).getRegionInfo().getRegionName(), + hRegions.get(1).getRegionInfo().getRegionName() }, true) + .get(5, TimeUnit.SECONDS); + Assert.assertTrue(rm.load()); + Assert.assertEquals(numRegions - 2, regionServer.getNumberOfOnlineRegions()); + } + } + + @Test + public void testWithSplitRegions() throws Exception { + MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster(); + Admin admin = TEST_UTIL.getAdmin(); + Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME); + List<Put> puts = new ArrayList<>(); + for (int i = 10; i < 50000; i++) { + puts.add(new Put(Bytes.toBytes(i)) + .addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("q1"), Bytes.toBytes("val_" + i))); + } + table.put(puts); + admin.flush(TABLE_NAME); + admin.compact(TABLE_NAME); + HRegionServer regionServer = cluster.getRegionServer(0); + String rsName = regionServer.getServerName().getAddress().toString(); + int numRegions = regionServer.getNumberOfOnlineRegions(); + List<HRegion> hRegions = regionServer.getRegions().stream() + .filter(hRegion -> hRegion.getRegionInfo().getTable().equals(TABLE_NAME)) + .collect(Collectors.toList()); + + RegionMover.RegionMoverBuilder rmBuilder = + new RegionMover.RegionMoverBuilder(rsName, TEST_UTIL.getConfiguration()).ack(true) + .maxthreads(8); + try (RegionMover rm = rmBuilder.build()) { + LOG.debug("Unloading " + regionServer.getServerName()); + rm.unload(); + Assert.assertEquals(0, regionServer.getNumberOfOnlineRegions()); + LOG.debug("Successfully Unloaded\nNow Loading"); + HRegion hRegion = hRegions.get(1); + int startKey = 0; + int endKey = Integer.MAX_VALUE; + if (hRegion.getRegionInfo().getStartKey().length > 0) { + startKey = Bytes.toInt(hRegion.getRegionInfo().getStartKey()); + } + if (hRegion.getRegionInfo().getEndKey().length > 0) { + endKey = Bytes.toInt(hRegion.getRegionInfo().getEndKey()); + } + int midKey = startKey + (endKey - startKey) / 2; + admin.splitRegionAsync(hRegion.getRegionInfo().getRegionName(), Bytes.toBytes(midKey)) + .get(5, TimeUnit.SECONDS); + Assert.assertTrue(rm.load()); + Assert.assertEquals(numRegions - 1, regionServer.getNumberOfOnlineRegions()); + } + } + + @Test + public void testFailedRegionMove() throws Exception { + MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster(); + Admin admin = TEST_UTIL.getAdmin(); + Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME); + List<Put> puts = new ArrayList<>(); + for (int i = 0; i < 1000; i++) { + puts.add(new Put(Bytes.toBytes("rowkey_" + i)) + .addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("q1"), Bytes.toBytes("val_" + i))); + } + table.put(puts); + admin.flush(TABLE_NAME); + HRegionServer regionServer = cluster.getRegionServer(0); + String rsName = regionServer.getServerName().getAddress().toString(); + int numRegions = regionServer.getNumberOfOnlineRegions(); + List<HRegion> hRegions = regionServer.getRegions().stream() + .filter(hRegion -> hRegion.getRegionInfo().getTable().equals(TABLE_NAME)) + .collect(Collectors.toList()); + RegionMover.RegionMoverBuilder rmBuilder = + new RegionMover.RegionMoverBuilder(rsName, TEST_UTIL.getConfiguration()).ack(true) + .maxthreads(8); + try (RegionMover rm = rmBuilder.build()) { + LOG.debug("Unloading " + regionServer.getServerName()); Review comment: nit: should use parameter substitution ########## File path: hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestRegionMover2.java ########## @@ -0,0 +1,202 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hbase.util; + +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.MiniHBaseCluster; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; +import org.apache.hadoop.hbase.client.Put; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.client.TableDescriptor; +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; +import org.apache.hadoop.hbase.regionserver.HRegion; +import org.apache.hadoop.hbase.regionserver.HRegionServer; +import org.apache.hadoop.hbase.testclassification.LargeTests; +import org.apache.hadoop.hbase.testclassification.MiscTests; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +/** + * Tests for Region Mover Load/Unload functionality with and without ack mode and also to test + * exclude functionality useful for rack decommissioning + */ +@Category({ MiscTests.class, LargeTests.class}) +public class TestRegionMover2 { + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestRegionMover2.class); + + private static final Logger LOG = LoggerFactory.getLogger(TestRegionMover2.class); + + private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); + + private static final TableName TABLE_NAME = TableName.valueOf("testRegionMover2"); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + TEST_UTIL.startMiniCluster(3); + TEST_UTIL.getAdmin().balancerSwitch(false, true); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + TEST_UTIL.shutdownMiniCluster(); + } + + @Before + public void setUp() throws Exception { + // Create a pre-split table just to populate some regions + Admin admin = TEST_UTIL.getAdmin(); + if (admin.tableExists(TABLE_NAME)) { + TEST_UTIL.deleteTable(TABLE_NAME); + } + TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(TABLE_NAME) + .setColumnFamily(ColumnFamilyDescriptorBuilder.of("fam1")).build(); + int startKey = 0; + int endKey = 80000; + admin.createTable(tableDesc, Bytes.toBytes(startKey), Bytes.toBytes(endKey), 9); + } + + @Test + public void testWithMergedRegions() throws Exception { + MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster(); + Admin admin = TEST_UTIL.getAdmin(); + Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME); + List<Put> puts = new ArrayList<>(); + for (int i = 0; i < 10000; i++) { + puts.add(new Put(Bytes.toBytes("rowkey_" + i)) + .addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("q1"), Bytes.toBytes("val_" + i))); + } + table.put(puts); + admin.flush(TABLE_NAME); + HRegionServer regionServer = cluster.getRegionServer(0); + String rsName = regionServer.getServerName().getAddress().toString(); + int numRegions = regionServer.getNumberOfOnlineRegions(); + List<HRegion> hRegions = regionServer.getRegions().stream() + .filter(hRegion -> hRegion.getRegionInfo().getTable().equals(TABLE_NAME)) + .collect(Collectors.toList()); + RegionMover.RegionMoverBuilder rmBuilder = + new RegionMover.RegionMoverBuilder(rsName, TEST_UTIL.getConfiguration()).ack(true) + .maxthreads(8); + try (RegionMover rm = rmBuilder.build()) { + LOG.debug("Unloading " + regionServer.getServerName()); + rm.unload(); + Assert.assertEquals(0, regionServer.getNumberOfOnlineRegions()); + LOG.debug("Successfully Unloaded\nNow Loading"); Review comment: no newlines in log messages please ########## File path: hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestRegionMover2.java ########## @@ -0,0 +1,202 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hbase.util; + +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.MiniHBaseCluster; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; +import org.apache.hadoop.hbase.client.Put; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.client.TableDescriptor; +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; +import org.apache.hadoop.hbase.regionserver.HRegion; +import org.apache.hadoop.hbase.regionserver.HRegionServer; +import org.apache.hadoop.hbase.testclassification.LargeTests; +import org.apache.hadoop.hbase.testclassification.MiscTests; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +/** + * Tests for Region Mover Load/Unload functionality with and without ack mode and also to test + * exclude functionality useful for rack decommissioning + */ +@Category({ MiscTests.class, LargeTests.class}) +public class TestRegionMover2 { + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestRegionMover2.class); + + private static final Logger LOG = LoggerFactory.getLogger(TestRegionMover2.class); + + private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); + + private static final TableName TABLE_NAME = TableName.valueOf("testRegionMover2"); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + TEST_UTIL.startMiniCluster(3); + TEST_UTIL.getAdmin().balancerSwitch(false, true); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + TEST_UTIL.shutdownMiniCluster(); + } + + @Before + public void setUp() throws Exception { + // Create a pre-split table just to populate some regions + Admin admin = TEST_UTIL.getAdmin(); + if (admin.tableExists(TABLE_NAME)) { + TEST_UTIL.deleteTable(TABLE_NAME); + } + TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(TABLE_NAME) + .setColumnFamily(ColumnFamilyDescriptorBuilder.of("fam1")).build(); + int startKey = 0; + int endKey = 80000; + admin.createTable(tableDesc, Bytes.toBytes(startKey), Bytes.toBytes(endKey), 9); + } + + @Test + public void testWithMergedRegions() throws Exception { + MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster(); + Admin admin = TEST_UTIL.getAdmin(); + Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME); + List<Put> puts = new ArrayList<>(); + for (int i = 0; i < 10000; i++) { + puts.add(new Put(Bytes.toBytes("rowkey_" + i)) + .addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("q1"), Bytes.toBytes("val_" + i))); + } + table.put(puts); + admin.flush(TABLE_NAME); + HRegionServer regionServer = cluster.getRegionServer(0); + String rsName = regionServer.getServerName().getAddress().toString(); + int numRegions = regionServer.getNumberOfOnlineRegions(); + List<HRegion> hRegions = regionServer.getRegions().stream() + .filter(hRegion -> hRegion.getRegionInfo().getTable().equals(TABLE_NAME)) + .collect(Collectors.toList()); + RegionMover.RegionMoverBuilder rmBuilder = + new RegionMover.RegionMoverBuilder(rsName, TEST_UTIL.getConfiguration()).ack(true) + .maxthreads(8); + try (RegionMover rm = rmBuilder.build()) { + LOG.debug("Unloading " + regionServer.getServerName()); Review comment: nit: should use parameter substitution ########## File path: hbase-server/src/test/java/org/apache/hadoop/hbase/util/TestRegionMover2.java ########## @@ -0,0 +1,202 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hbase.util; + +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.MiniHBaseCluster; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.Admin; +import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; +import org.apache.hadoop.hbase.client.Put; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.client.TableDescriptor; +import org.apache.hadoop.hbase.client.TableDescriptorBuilder; +import org.apache.hadoop.hbase.regionserver.HRegion; +import org.apache.hadoop.hbase.regionserver.HRegionServer; +import org.apache.hadoop.hbase.testclassification.LargeTests; +import org.apache.hadoop.hbase.testclassification.MiscTests; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +/** + * Tests for Region Mover Load/Unload functionality with and without ack mode and also to test + * exclude functionality useful for rack decommissioning + */ +@Category({ MiscTests.class, LargeTests.class}) +public class TestRegionMover2 { + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestRegionMover2.class); + + private static final Logger LOG = LoggerFactory.getLogger(TestRegionMover2.class); + + private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); + + private static final TableName TABLE_NAME = TableName.valueOf("testRegionMover2"); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + TEST_UTIL.startMiniCluster(3); + TEST_UTIL.getAdmin().balancerSwitch(false, true); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + TEST_UTIL.shutdownMiniCluster(); + } + + @Before + public void setUp() throws Exception { + // Create a pre-split table just to populate some regions + Admin admin = TEST_UTIL.getAdmin(); + if (admin.tableExists(TABLE_NAME)) { + TEST_UTIL.deleteTable(TABLE_NAME); + } + TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(TABLE_NAME) + .setColumnFamily(ColumnFamilyDescriptorBuilder.of("fam1")).build(); + int startKey = 0; + int endKey = 80000; + admin.createTable(tableDesc, Bytes.toBytes(startKey), Bytes.toBytes(endKey), 9); + } + + @Test + public void testWithMergedRegions() throws Exception { + MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster(); + Admin admin = TEST_UTIL.getAdmin(); + Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME); + List<Put> puts = new ArrayList<>(); + for (int i = 0; i < 10000; i++) { + puts.add(new Put(Bytes.toBytes("rowkey_" + i)) + .addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("q1"), Bytes.toBytes("val_" + i))); + } + table.put(puts); + admin.flush(TABLE_NAME); + HRegionServer regionServer = cluster.getRegionServer(0); + String rsName = regionServer.getServerName().getAddress().toString(); + int numRegions = regionServer.getNumberOfOnlineRegions(); + List<HRegion> hRegions = regionServer.getRegions().stream() + .filter(hRegion -> hRegion.getRegionInfo().getTable().equals(TABLE_NAME)) + .collect(Collectors.toList()); + RegionMover.RegionMoverBuilder rmBuilder = + new RegionMover.RegionMoverBuilder(rsName, TEST_UTIL.getConfiguration()).ack(true) + .maxthreads(8); + try (RegionMover rm = rmBuilder.build()) { + LOG.debug("Unloading " + regionServer.getServerName()); + rm.unload(); + Assert.assertEquals(0, regionServer.getNumberOfOnlineRegions()); + LOG.debug("Successfully Unloaded\nNow Loading"); + admin.mergeRegionsAsync(new byte[][] { hRegions.get(0).getRegionInfo().getRegionName(), + hRegions.get(1).getRegionInfo().getRegionName() }, true) + .get(5, TimeUnit.SECONDS); + Assert.assertTrue(rm.load()); + Assert.assertEquals(numRegions - 2, regionServer.getNumberOfOnlineRegions()); + } + } + + @Test + public void testWithSplitRegions() throws Exception { + MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster(); + Admin admin = TEST_UTIL.getAdmin(); + Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME); + List<Put> puts = new ArrayList<>(); + for (int i = 10; i < 50000; i++) { + puts.add(new Put(Bytes.toBytes(i)) + .addColumn(Bytes.toBytes("fam1"), Bytes.toBytes("q1"), Bytes.toBytes("val_" + i))); + } + table.put(puts); + admin.flush(TABLE_NAME); + admin.compact(TABLE_NAME); + HRegionServer regionServer = cluster.getRegionServer(0); + String rsName = regionServer.getServerName().getAddress().toString(); + int numRegions = regionServer.getNumberOfOnlineRegions(); + List<HRegion> hRegions = regionServer.getRegions().stream() + .filter(hRegion -> hRegion.getRegionInfo().getTable().equals(TABLE_NAME)) + .collect(Collectors.toList()); + + RegionMover.RegionMoverBuilder rmBuilder = + new RegionMover.RegionMoverBuilder(rsName, TEST_UTIL.getConfiguration()).ack(true) + .maxthreads(8); + try (RegionMover rm = rmBuilder.build()) { + LOG.debug("Unloading " + regionServer.getServerName()); + rm.unload(); + Assert.assertEquals(0, regionServer.getNumberOfOnlineRegions()); + LOG.debug("Successfully Unloaded\nNow Loading"); Review comment: no newlines in log messages please. ########## File path: hbase-server/src/main/java/org/apache/hadoop/hbase/util/RegionMover.java ########## @@ -597,6 +501,22 @@ private void waitMoveTasksToFinish(ExecutorService moveRegionsPool, } } + private boolean ignoreRegionMoveFailure(ExecutionException e) { + boolean ignoreFailure; Review comment: nit: initialize to false and drop the final else clause. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
