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