001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.master.assignment;
020
021import static org.junit.Assert.assertNotEquals;
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.List;
025
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseTestingUtility;
029import org.apache.hadoop.hbase.HConstants;
030import org.apache.hadoop.hbase.TableName;
031import org.apache.hadoop.hbase.client.RegionInfo;
032import org.apache.hadoop.hbase.client.RegionInfoBuilder;
033import org.apache.hadoop.hbase.client.RegionReplicaTestHelper;
034import org.apache.hadoop.hbase.client.Table;
035import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
036import org.apache.hadoop.hbase.regionserver.HRegionServer;
037import org.apache.hadoop.hbase.regionserver.Region;
038import org.apache.hadoop.hbase.testclassification.MediumTests;
039import org.apache.hadoop.hbase.testclassification.RegionServerTests;
040import org.apache.hadoop.hbase.util.Bytes;
041import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
042import org.apache.hadoop.hbase.util.RegionSplitter;
043import org.junit.AfterClass;
044import org.junit.BeforeClass;
045import org.junit.ClassRule;
046import org.junit.Rule;
047import org.junit.Test;
048import org.junit.experimental.categories.Category;
049import org.junit.rules.TestName;
050import org.slf4j.Logger;
051import org.slf4j.LoggerFactory;
052
053@Category({ RegionServerTests.class, MediumTests.class })
054public class TestRegionReplicaSplit {
055  @ClassRule
056  public static final HBaseClassTestRule CLASS_RULE =
057      HBaseClassTestRule.forClass(TestRegionReplicaSplit.class);
058  private static final Logger LOG = LoggerFactory.getLogger(TestRegionReplicaSplit.class);
059
060  private static final int NB_SERVERS = 4;
061
062  private static final HBaseTestingUtility HTU = new HBaseTestingUtility();
063  private static final byte[] f = HConstants.CATALOG_FAMILY;
064
065  @BeforeClass
066  public static void beforeClass() throws Exception {
067    HTU.getConfiguration().setInt("hbase.master.wait.on.regionservers.mintostart", 3);
068    HTU.startMiniCluster(NB_SERVERS);
069  }
070
071  @Rule
072  public TestName name = new TestName();
073
074  private static Table createTableAndLoadData(final TableName tableName) throws IOException {
075    TableDescriptorBuilder builder = TableDescriptorBuilder.newBuilder(tableName);
076    builder.setRegionReplication(3);
077    // create a table with 3 replication
078    Table table = HTU.createTable(builder.build(), new byte[][] { f }, getSplits(2),
079      new Configuration(HTU.getConfiguration()));
080    HTU.loadTable(HTU.getConnection().getTable(tableName), f);
081    return table;
082  }
083
084  private static byte[][] getSplits(int numRegions) {
085    RegionSplitter.UniformSplit split = new RegionSplitter.UniformSplit();
086    split.setFirstRow(Bytes.toBytes(0L));
087    split.setLastRow(Bytes.toBytes(Long.MAX_VALUE));
088    return split.split(numRegions);
089  }
090
091  @AfterClass
092  public static void afterClass() throws Exception {
093    HRegionServer.TEST_SKIP_REPORTING_TRANSITION = false;
094    HTU.shutdownMiniCluster();
095  }
096
097  @Test
098  public void testRegionReplicaSplitRegionAssignment() throws Exception {
099    TableName tn = TableName.valueOf(this.name.getMethodName());
100    Table table = null;
101    try {
102      table = createTableAndLoadData(tn);
103      HTU.loadNumericRows(table, f, 0, 3);
104      // split the table
105      List<RegionInfo> regions = new ArrayList<RegionInfo>();
106      for (RegionServerThread rs : HTU.getMiniHBaseCluster().getRegionServerThreads()) {
107        for (Region r : rs.getRegionServer().getRegions(table.getName())) {
108          regions.add(r.getRegionInfo());
109        }
110      }
111      // There are 6 regions before split, 9 regions after split.
112      HTU.getAdmin().split(table.getName(), Bytes.toBytes(1));
113      int count = 0;
114      while (true) {
115        for (RegionServerThread rs : HTU.getMiniHBaseCluster().getRegionServerThreads()) {
116          for (Region r : rs.getRegionServer().getRegions(table.getName())) {
117            count++;
118          }
119        }
120        if (count >= 9) {
121          break;
122        }
123        count = 0;
124      }
125      RegionReplicaTestHelper.assertReplicaDistributed(HTU, table);
126    } finally {
127      if (table != null) {
128        HTU.deleteTable(tn);
129      }
130    }
131  }
132
133  @Test
134  public void testAssignFakeReplicaRegion() throws Exception {
135    TableName tn = TableName.valueOf(this.name.getMethodName());
136    Table table = null;
137    try {
138      table = createTableAndLoadData(tn);
139      final RegionInfo fakeHri =
140        RegionInfoBuilder.newBuilder(table.getName()).setStartKey(Bytes.toBytes("a"))
141          .setEndKey(Bytes.toBytes("b")).setReplicaId(1)
142          .setRegionId(System.currentTimeMillis()).build();
143
144      // To test AssignProcedure can defend this case.
145      HTU.getMiniHBaseCluster().getMaster().getAssignmentManager().assign(fakeHri);
146      // Wait until all assigns are done.
147      HBaseTestingUtility.await(50, () -> {
148        return HTU.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor().getActiveProcIds()
149          .isEmpty();
150      });
151
152      // Make sure the region is not online.
153      for (RegionServerThread rs : HTU.getMiniHBaseCluster().getRegionServerThreads()) {
154        for (Region r : rs.getRegionServer().getRegions(table.getName())) {
155          assertNotEquals(r.getRegionInfo(), fakeHri);
156        }
157      }
158    } finally {
159      if (table != null) {
160        HTU.deleteTable(tn);
161      }
162    }
163  }
164}