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.apache.hadoop.hbase.master.assignment.AssignmentTestingUtil.insertData;
021import static org.junit.jupiter.api.Assertions.assertEquals;
022import static org.junit.jupiter.api.Assertions.assertNotEquals;
023import static org.junit.jupiter.api.Assertions.assertTrue;
024
025import java.util.List;
026import java.util.Map;
027import org.apache.hadoop.conf.Configuration;
028import org.apache.hadoop.hbase.HBaseTestingUtil;
029import org.apache.hadoop.hbase.HConstants;
030import org.apache.hadoop.hbase.ServerName;
031import org.apache.hadoop.hbase.StartTestingClusterOption;
032import org.apache.hadoop.hbase.TableName;
033import org.apache.hadoop.hbase.client.RegionInfo;
034import org.apache.hadoop.hbase.client.TableDescriptor;
035import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
036import org.apache.hadoop.hbase.master.procedure.MasterProcedureTestingUtility;
037import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
038import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
039import org.apache.hadoop.hbase.regionserver.HRegion;
040import org.apache.hadoop.hbase.testclassification.MasterTests;
041import org.apache.hadoop.hbase.testclassification.MediumTests;
042import org.apache.hadoop.hbase.util.Bytes;
043import org.apache.hadoop.hbase.util.JVMClusterUtil;
044import org.junit.jupiter.api.AfterAll;
045import org.junit.jupiter.api.AfterEach;
046import org.junit.jupiter.api.BeforeAll;
047import org.junit.jupiter.api.BeforeEach;
048import org.junit.jupiter.api.Tag;
049import org.junit.jupiter.api.Test;
050import org.junit.jupiter.api.TestInfo;
051import org.slf4j.Logger;
052import org.slf4j.LoggerFactory;
053
054@Tag(MasterTests.TAG)
055@Tag(MediumTests.TAG)
056public class TestRegionSplitAndSeparateChildren {
057
058  private static final Logger LOG =
059    LoggerFactory.getLogger(TestRegionSplitAndSeparateChildren.class);
060
061  protected static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
062
063  private static String columnFamilyName = "cf";
064
065  private static final int startRowNum = 11;
066  private static final int rowCount = 60;
067  private String testMethodName;
068
069  @BeforeEach
070  public void setTestMethod(TestInfo testInfo) {
071    testMethodName = testInfo.getTestMethod().get().getName();
072  }
073
074  private static void setupConf(Configuration conf) {
075    // enable automatically separate child regions
076    conf.setBoolean(HConstants.HBASE_ENABLE_SEPARATE_CHILD_REGIONS, true);
077  }
078
079  @BeforeAll
080  public static void setupCluster() throws Exception {
081    setupConf(UTIL.getConfiguration());
082    StartTestingClusterOption option =
083      StartTestingClusterOption.builder().numMasters(1).numRegionServers(3).numDataNodes(3).build();
084    UTIL.startMiniCluster(option);
085  }
086
087  @AfterAll
088  public static void cleanupTest() throws Exception {
089    try {
090      UTIL.shutdownMiniCluster();
091    } catch (Exception e) {
092      LOG.warn("failure shutting down cluster", e);
093    }
094  }
095
096  @BeforeEach
097  public void setup() throws Exception {
098    // Turn off the meta scanner so it don't remove parent on us.
099    UTIL.getHBaseCluster().getMaster().setCatalogJanitorEnabled(false);
100    // Disable compaction.
101    for (int i = 0; i < UTIL.getHBaseCluster().getLiveRegionServerThreads().size(); i++) {
102      UTIL.getHBaseCluster().getRegionServer(i).getCompactSplitThread().switchCompaction(false);
103    }
104  }
105
106  @AfterEach
107  public void tearDown() throws Exception {
108    for (TableDescriptor htd : UTIL.getAdmin().listTableDescriptors()) {
109      UTIL.deleteTable(htd.getTableName());
110    }
111  }
112
113  @Test
114  public void testSplitTableRegionAndSeparateChildRegions() throws Exception {
115    final TableName tableName = TableName.valueOf(testMethodName);
116    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
117
118    RegionInfo[] regions =
119      MasterProcedureTestingUtility.createTable(procExec, tableName, null, columnFamilyName);
120    insertData(UTIL, tableName, rowCount, startRowNum, columnFamilyName);
121    int splitRowNum = startRowNum + rowCount / 2;
122    byte[] splitKey = Bytes.toBytes("" + splitRowNum);
123
124    assertTrue(regions != null, "not able to find a splittable region");
125    assertTrue(regions.length == 1, "not able to find a splittable region");
126
127    // Split region of the table
128    long procId = procExec.submitProcedure(
129      new SplitTableRegionProcedure(procExec.getEnvironment(), regions[0], splitKey));
130    // Wait the completion
131    ProcedureTestingUtility.waitProcedure(procExec, procId);
132    ProcedureTestingUtility.assertProcNotFailed(procExec, procId);
133
134    assertTrue(UTIL.getHBaseCluster().getRegions(tableName).size() == 2, "not able to split table");
135
136    // disable table
137    UTIL.getAdmin().disableTable(tableName);
138    Thread.sleep(500);
139
140    // stop master
141    UTIL.getHBaseCluster().stopMaster(0);
142    UTIL.getHBaseCluster().waitOnMaster(0);
143    Thread.sleep(500);
144
145    // restart master
146    JVMClusterUtil.MasterThread t = UTIL.getHBaseCluster().startMaster();
147    Thread.sleep(500);
148
149    UTIL.invalidateConnection();
150    // enable table
151    UTIL.getAdmin().enableTable(tableName);
152    Thread.sleep(500);
153
154    List<HRegion> tableRegions = UTIL.getHBaseCluster().getRegions(tableName);
155    assertEquals(2, tableRegions.size(), "Table region not correct.");
156    Map<RegionInfo, ServerName> regionInfoMap = UTIL.getHBaseCluster().getMaster()
157      .getAssignmentManager().getRegionStates().getRegionAssignments();
158    assertNotEquals(regionInfoMap.get(tableRegions.get(0).getRegionInfo()),
159      regionInfoMap.get(tableRegions.get(1).getRegionInfo()));
160  }
161
162  private ProcedureExecutor<MasterProcedureEnv> getMasterProcedureExecutor() {
163    return UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor();
164  }
165}