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.assertNotNull; 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.ServerName; 030import org.apache.hadoop.hbase.StartTestingClusterOption; 031import org.apache.hadoop.hbase.TableName; 032import org.apache.hadoop.hbase.client.RegionInfo; 033import org.apache.hadoop.hbase.client.TableDescriptor; 034import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv; 035import org.apache.hadoop.hbase.master.procedure.MasterProcedureTestingUtility; 036import org.apache.hadoop.hbase.procedure2.ProcedureExecutor; 037import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility; 038import org.apache.hadoop.hbase.regionserver.HRegion; 039import org.apache.hadoop.hbase.testclassification.MasterTests; 040import org.apache.hadoop.hbase.testclassification.MediumTests; 041import org.apache.hadoop.hbase.util.Bytes; 042import org.apache.hadoop.hbase.util.JVMClusterUtil; 043import org.junit.jupiter.api.AfterAll; 044import org.junit.jupiter.api.AfterEach; 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(MasterTests.TAG) 054@Tag(MediumTests.TAG) 055public class TestRegionSplit { 056 057 private static final Logger LOG = LoggerFactory.getLogger(TestRegionSplit.class); 058 059 protected static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); 060 061 private static String columnFamilyName = "cf"; 062 063 private static final int startRowNum = 11; 064 private static final int rowCount = 60; 065 private String testMethodName; 066 067 @BeforeEach 068 public void setTestMethod(TestInfo testInfo) { 069 testMethodName = testInfo.getTestMethod().get().getName(); 070 } 071 072 private static void setupConf(Configuration conf) { 073 } 074 075 @BeforeAll 076 public static void setupCluster() throws Exception { 077 setupConf(UTIL.getConfiguration()); 078 StartTestingClusterOption option = 079 StartTestingClusterOption.builder().numMasters(1).numRegionServers(3).numDataNodes(3).build(); 080 UTIL.startMiniCluster(option); 081 } 082 083 @AfterAll 084 public static void cleanupTest() throws Exception { 085 try { 086 UTIL.shutdownMiniCluster(); 087 } catch (Exception e) { 088 LOG.warn("failure shutting down cluster", e); 089 } 090 } 091 092 @BeforeEach 093 public void setup() throws Exception { 094 // Turn off the meta scanner so it don't remove parent on us. 095 UTIL.getHBaseCluster().getMaster().setCatalogJanitorEnabled(false); 096 // Disable compaction. 097 for (int i = 0; i < UTIL.getHBaseCluster().getLiveRegionServerThreads().size(); i++) { 098 UTIL.getHBaseCluster().getRegionServer(i).getCompactSplitThread().switchCompaction(false); 099 } 100 } 101 102 @AfterEach 103 public void tearDown() throws Exception { 104 for (TableDescriptor htd : UTIL.getAdmin().listTableDescriptors()) { 105 UTIL.deleteTable(htd.getTableName()); 106 } 107 } 108 109 @Test 110 public void testSplitTableRegion() throws Exception { 111 final TableName tableName = TableName.valueOf(testMethodName); 112 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 113 114 RegionInfo[] regions = 115 MasterProcedureTestingUtility.createTable(procExec, tableName, null, columnFamilyName); 116 insertData(UTIL, tableName, rowCount, startRowNum, columnFamilyName); 117 int splitRowNum = startRowNum + rowCount / 2; 118 byte[] splitKey = Bytes.toBytes("" + splitRowNum); 119 120 assertTrue(regions != null, "not able to find a splittable region"); 121 assertTrue(regions.length == 1, "not able to find a splittable region"); 122 123 // Split region of the table 124 long procId = procExec.submitProcedure( 125 new SplitTableRegionProcedure(procExec.getEnvironment(), regions[0], splitKey)); 126 // Wait the completion 127 ProcedureTestingUtility.waitProcedure(procExec, procId); 128 ProcedureTestingUtility.assertProcNotFailed(procExec, procId); 129 130 assertTrue(UTIL.getHBaseCluster().getRegions(tableName).size() == 2, "not able to split table"); 131 132 // disable table 133 UTIL.getAdmin().disableTable(tableName); 134 Thread.sleep(500); 135 136 // stop master 137 UTIL.getHBaseCluster().stopMaster(0); 138 UTIL.getHBaseCluster().waitOnMaster(0); 139 Thread.sleep(500); 140 141 // restart master 142 JVMClusterUtil.MasterThread t = UTIL.getHBaseCluster().startMaster(); 143 Thread.sleep(500); 144 145 UTIL.invalidateConnection(); 146 // enable table 147 UTIL.getAdmin().enableTable(tableName); 148 Thread.sleep(500); 149 150 List<HRegion> tableRegions = UTIL.getHBaseCluster().getRegions(tableName); 151 assertEquals(2, tableRegions.size(), "Table region not correct."); 152 Map<RegionInfo, ServerName> regionInfoMap = UTIL.getHBaseCluster().getMaster() 153 .getAssignmentManager().getRegionStates().getRegionAssignments(); 154 assertEquals(regionInfoMap.get(tableRegions.get(0).getRegionInfo()), 155 regionInfoMap.get(tableRegions.get(1).getRegionInfo())); 156 } 157 158 @Test 159 public void testSplitStoreFiles() throws Exception { 160 final TableName tableName = TableName.valueOf(testMethodName); 161 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 162 163 RegionInfo[] regions = 164 MasterProcedureTestingUtility.createTable(procExec, tableName, null, columnFamilyName); 165 // flush the memstore 166 insertData(UTIL, tableName, rowCount, startRowNum, true, columnFamilyName); 167 168 // assert the hfile count of the table 169 int storeFilesCountSum = 0; 170 for (HRegion region : UTIL.getHBaseCluster().getRegions(tableName)) { 171 storeFilesCountSum += region.getStore(Bytes.toBytes(columnFamilyName)).getStorefiles().size(); 172 } 173 assertEquals(1, storeFilesCountSum); 174 175 // split at the start row 176 byte[] splitKey = Bytes.toBytes("" + startRowNum); 177 178 assertNotNull(regions, "Not able to find a splittable region"); 179 assertEquals(1, regions.length, "Not able to find a splittable region"); 180 181 // Split region of the table 182 long procId = procExec.submitProcedure( 183 new SplitTableRegionProcedure(procExec.getEnvironment(), regions[0], splitKey)); 184 // Wait the completion 185 ProcedureTestingUtility.waitProcedure(procExec, procId); 186 ProcedureTestingUtility.assertProcNotFailed(procExec, procId); 187 188 assertEquals(2, UTIL.getHBaseCluster().getRegions(tableName).size(), "Not able to split table"); 189 190 // assert sum of the hfiles of all regions 191 int childStoreFilesSum = 0; 192 for (HRegion region : UTIL.getHBaseCluster().getRegions(tableName)) { 193 childStoreFilesSum += region.getStore(Bytes.toBytes(columnFamilyName)).getStorefiles().size(); 194 } 195 assertEquals(1, childStoreFilesSum); 196 197 List<HRegion> tableRegions = UTIL.getHBaseCluster().getRegions(tableName); 198 assertEquals(2, tableRegions.size(), "Table region not correct."); 199 Map<RegionInfo, ServerName> regionInfoMap = UTIL.getHBaseCluster().getMaster() 200 .getAssignmentManager().getRegionStates().getRegionAssignments(); 201 assertEquals(regionInfoMap.get(tableRegions.get(0).getRegionInfo()), 202 regionInfoMap.get(tableRegions.get(1).getRegionInfo())); 203 } 204 205 private ProcedureExecutor<MasterProcedureEnv> getMasterProcedureExecutor() { 206 return UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor(); 207 } 208}