diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RegionReplicaUtil.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RegionReplicaUtil.java index 9c5af37..3538e2a 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RegionReplicaUtil.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/RegionReplicaUtil.java @@ -176,6 +176,7 @@ public class RegionReplicaUtil { } List hRegionInfos = new ArrayList<>((newReplicaCount) * regions.size()); for (int i = 0; i < regions.size(); i++) { + hRegionInfos.add(regions.get(i)); if (RegionReplicaUtil.isDefaultReplica(regions.get(i))) { // region level replica index starts from 0. So if oldReplicaCount was 2 then the max replicaId for // the existing regions would be 1 @@ -184,7 +185,6 @@ public class RegionReplicaUtil { } } } - hRegionInfos.addAll(regions); return hRegionInfos; } } diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/assignment/AssignmentManager.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/assignment/AssignmentManager.java index 1c193f9..3c12957 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/assignment/AssignmentManager.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/assignment/AssignmentManager.java @@ -618,6 +618,21 @@ public class AssignmentManager implements ServerListener { return procs; } + public AssignProcedure[] + createRoundRobinAssignProcedures(final Collection regionInfo) { + List onlineServers = master.getServerManager().createDestinationServersList(); + if (regionInfo.isEmpty()) return null; + final AssignProcedure[] procs = new AssignProcedure[regionInfo.size()]; + int index = 0; + int serverIndex = 0; + for (RegionInfo hri : regionInfo) { + serverIndex = serverIndex % onlineServers.size(); + procs[index++] = createAssignProcedure(hri, onlineServers.get(serverIndex)); + serverIndex++; + } + return procs; + } + // Needed for the following method so it can type the created Array we return private static final UnassignProcedure [] UNASSIGNED_PROCEDURE_FOR_TYPE_INFO = new UnassignProcedure[0]; diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/procedure/CreateTableProcedure.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/procedure/CreateTableProcedure.java index e9804dd..c72b877 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/procedure/CreateTableProcedure.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/procedure/CreateTableProcedure.java @@ -28,6 +28,7 @@ import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; import org.apache.hadoop.hbase.DoNotRetryIOException; import org.apache.hadoop.hbase.MetaTableAccessor; +import org.apache.hadoop.hbase.ServerName; import org.apache.hadoop.hbase.TableExistsException; import org.apache.hadoop.hbase.TableName; import org.apache.hadoop.hbase.client.RegionInfo; @@ -106,7 +107,12 @@ public class CreateTableProcedure break; case CREATE_TABLE_ASSIGN_REGIONS: setEnablingState(env, getTableName()); - addChildProcedure(env.getAssignmentManager().createAssignProcedures(newRegions)); + if (tableDescriptor.getRegionReplication() > 1) { + addChildProcedure( + env.getAssignmentManager().createRoundRobinAssignProcedures(newRegions)); + } else { + addChildProcedure(env.getAssignmentManager().createAssignProcedures(newRegions)); + } setNextState(CreateTableState.CREATE_TABLE_UPDATE_DESC_CACHE); break; case CREATE_TABLE_UPDATE_DESC_CACHE: diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestCreateTableProcWithReplicas.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestCreateTableProcWithReplicas.java new file mode 100644 index 0000000..29faf42 --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestCreateTableProcWithReplicas.java @@ -0,0 +1,159 @@ +/** + * 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.regionserver; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.HConstants; +import org.apache.hadoop.hbase.HTableDescriptor; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.RegionInfo; +import org.apache.hadoop.hbase.client.RegionReplicaUtil; +import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.testclassification.MediumTests; +import org.apache.hadoop.hbase.testclassification.RegionServerTests; +import org.apache.hadoop.hbase.util.Bytes; +import org.apache.hadoop.hbase.util.RegionSplitter; +import org.apache.hadoop.hdfs.DFSConfigKeys; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category({ RegionServerTests.class, MediumTests.class }) +public class TestCreateTableProcWithReplicas { + + private static final Log LOG = LogFactory.getLog(TestCreateTableProcWithReplicas.class); + + private static final int NB_SERVERS = 3; + private static Table table; + private static final byte[] row = "TestCreateTableProcWithReplicas".getBytes(); + + private static final HBaseTestingUtility HTU = new HBaseTestingUtility(); + private static final byte[] f = HConstants.CATALOG_FAMILY; + + @BeforeClass + public static void before() throws Exception { + // Reduce the hdfs block size and prefetch to trigger the file-link reopen + // when the file is moved to archive (e.g. compaction) + HTU.getConfiguration().setInt(DFSConfigKeys.DFS_BLOCK_SIZE_KEY, 8192); + HTU.getConfiguration().setInt(DFSConfigKeys.DFS_CLIENT_READ_PREFETCH_SIZE_KEY, 1); + HTU.getConfiguration().setInt(HConstants.HREGION_MEMSTORE_FLUSH_SIZE, 128 * 1024 * 1024); + HTU.getConfiguration().setInt(">hbase.master.wait.on.regionservers.mintostart", 3); + + HTU.startMiniCluster(NB_SERVERS); + Thread.sleep(5000); + final TableName tableName = TableName.valueOf(TestCreateTableProcWithReplicas.class.getSimpleName()); + + // Create table then get the single region for our new table. + createTableDirectlyFromHTD(tableName); + //enableReplicationByModification(tableName); + } + + private static void enableReplicationByModification(final TableName tableName) + throws IOException, InterruptedException { + table = HTU.createTable(tableName, f); + HBaseTestingUtility.setReplicas(HTU.getAdmin(), table.getName(), 3); + } + + private static void createTableDirectlyFromHTD(final TableName tableName) throws IOException { + HTableDescriptor htd = new HTableDescriptor(tableName); + htd.setRegionReplication(3); + // create a table with 3 replication + + table = HTU.createTable(htd, new byte[][] { f }, getSplits(20), + new Configuration(HTU.getConfiguration())); + } + + private static byte[][] getSplits(int numRegions) { + RegionSplitter.UniformSplit split = new RegionSplitter.UniformSplit(); + split.setFirstRow(Bytes.toBytes(0L)); + split.setLastRow(Bytes.toBytes(Long.MAX_VALUE)); + return split.split(numRegions); + } + + @AfterClass + public static void afterClass() throws Exception { + HRegionServer.TEST_SKIP_REPORTING_TRANSITION = false; + table.close(); + HTU.shutdownMiniCluster(); + } + + private HRegionServer getRS() { + return HTU.getMiniHBaseCluster().getRegionServer(0); + } + + private HRegionServer getSecondaryRS() { + return HTU.getMiniHBaseCluster().getRegionServer(1); + } + + private HRegionServer getTertiaryRS() { + return HTU.getMiniHBaseCluster().getRegionServer(2); + } + + @Test(timeout = 60000) + public void testRegionReplicasCreated() throws Exception { + Collection onlineRegions = getRS().getOnlineRegionsLocalContext(); + boolean res = checkDuplicates(onlineRegions); + assertFalse(res); + Collection onlineRegions2 = getSecondaryRS().getOnlineRegionsLocalContext(); + res = checkDuplicates(onlineRegions2); + assertFalse(res); + Collection onlineRegions3 = getTertiaryRS().getOnlineRegionsLocalContext(); + checkDuplicates(onlineRegions3); + assertFalse(res); + int totalRegions = onlineRegions.size() + onlineRegions2.size() + onlineRegions3.size(); + // includes system tables + assertEquals("the number of regions should be 62", totalRegions, 62); + } + + private boolean checkDuplicates(Collection onlineRegions3) throws Exception { + ArrayList copyOfRegion = new ArrayList(onlineRegions3); + for (HRegion region : copyOfRegion) { + RegionInfo regionInfo = region.getRegionInfo(); + RegionInfo regionInfoForReplica = + RegionReplicaUtil.getRegionInfoForDefaultReplica(regionInfo); + int i = 0; + for (Region actualRegion : onlineRegions3) { + if (regionInfoForReplica.equals( + RegionReplicaUtil.getRegionInfoForDefaultReplica(actualRegion.getRegionInfo()))) { + i++; + if (i > 1) { + System.out.println("duplicate found " + actualRegion.getRegionInfo() + " "+region.getRegionInfo()); + assertTrue(Bytes.equals(region.getRegionInfo().getStartKey(), + actualRegion.getRegionInfo().getStartKey())); + assertTrue(Bytes.equals(region.getRegionInfo().getEndKey(), + actualRegion.getRegionInfo().getEndKey())); + return true; + } + } + } + } + return false; + } +} \ No newline at end of file