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.assertFalse; 023import static org.junit.jupiter.api.Assertions.assertNotNull; 024import static org.junit.jupiter.api.Assertions.assertTrue; 025 026import java.util.List; 027import java.util.Map; 028import org.apache.hadoop.conf.Configuration; 029import org.apache.hadoop.hbase.HBaseTestingUtil; 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.RegionState; 036import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv; 037import org.apache.hadoop.hbase.master.procedure.MasterProcedureTestingUtility; 038import org.apache.hadoop.hbase.procedure2.ProcedureExecutor; 039import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility; 040import org.apache.hadoop.hbase.regionserver.HRegion; 041import org.apache.hadoop.hbase.testclassification.MasterTests; 042import org.apache.hadoop.hbase.testclassification.MediumTests; 043import org.apache.hadoop.hbase.util.Bytes; 044import org.apache.hadoop.hbase.util.JVMClusterUtil; 045import org.junit.jupiter.api.AfterAll; 046import org.junit.jupiter.api.AfterEach; 047import org.junit.jupiter.api.BeforeAll; 048import org.junit.jupiter.api.BeforeEach; 049import org.junit.jupiter.api.Tag; 050import org.junit.jupiter.api.Test; 051import org.junit.jupiter.api.TestInfo; 052import org.slf4j.Logger; 053import org.slf4j.LoggerFactory; 054 055@Tag(MasterTests.TAG) 056@Tag(MediumTests.TAG) 057public class TestRegionSplit { 058 059 private static final Logger LOG = LoggerFactory.getLogger(TestRegionSplit.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 } 076 077 @BeforeAll 078 public static void setupCluster() throws Exception { 079 setupConf(UTIL.getConfiguration()); 080 StartTestingClusterOption option = 081 StartTestingClusterOption.builder().numMasters(1).numRegionServers(3).numDataNodes(3).build(); 082 UTIL.startMiniCluster(option); 083 } 084 085 @AfterAll 086 public static void cleanupTest() throws Exception { 087 try { 088 UTIL.shutdownMiniCluster(); 089 } catch (Exception e) { 090 LOG.warn("failure shutting down cluster", e); 091 } 092 } 093 094 @BeforeEach 095 public void setup() throws Exception { 096 // Turn off the meta scanner so it don't remove parent on us. 097 UTIL.getHBaseCluster().getMaster().setCatalogJanitorEnabled(false); 098 // Disable compaction. 099 for (int i = 0; i < UTIL.getHBaseCluster().getLiveRegionServerThreads().size(); i++) { 100 UTIL.getHBaseCluster().getRegionServer(i).getCompactSplitThread().switchCompaction(false); 101 } 102 } 103 104 @AfterEach 105 public void tearDown() throws Exception { 106 for (TableDescriptor htd : UTIL.getAdmin().listTableDescriptors()) { 107 UTIL.deleteTable(htd.getTableName()); 108 } 109 } 110 111 @Test 112 public void testSplitTableRegion() throws Exception { 113 final TableName tableName = TableName.valueOf(testMethodName); 114 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 115 116 RegionInfo[] regions = 117 MasterProcedureTestingUtility.createTable(procExec, tableName, null, columnFamilyName); 118 insertData(UTIL, tableName, rowCount, startRowNum, columnFamilyName); 119 int splitRowNum = startRowNum + rowCount / 2; 120 byte[] splitKey = Bytes.toBytes("" + splitRowNum); 121 122 assertNotNull(regions, "not able to find a splittable region"); 123 assertEquals(1, regions.length, "not able to find a splittable region"); 124 125 // Split region of the table 126 long procId = procExec.submitProcedure( 127 new SplitTableRegionProcedure(procExec.getEnvironment(), regions[0], splitKey)); 128 // Wait the completion 129 ProcedureTestingUtility.waitProcedure(procExec, procId); 130 ProcedureTestingUtility.assertProcNotFailed(procExec, procId); 131 132 assertEquals(2, UTIL.getHBaseCluster().getRegions(tableName).size(), "not able to split table"); 133 134 // disable table 135 UTIL.getAdmin().disableTable(tableName); 136 Thread.sleep(500); 137 138 // stop master 139 UTIL.getHBaseCluster().stopMaster(0); 140 UTIL.getHBaseCluster().waitOnMaster(0); 141 Thread.sleep(500); 142 143 // restart master 144 JVMClusterUtil.MasterThread t = UTIL.getHBaseCluster().startMaster(); 145 Thread.sleep(500); 146 147 UTIL.invalidateConnection(); 148 // enable table 149 UTIL.getAdmin().enableTable(tableName); 150 Thread.sleep(500); 151 152 List<HRegion> tableRegions = UTIL.getHBaseCluster().getRegions(tableName); 153 assertEquals(2, tableRegions.size(), "Table region not correct."); 154 Map<RegionInfo, ServerName> regionInfoMap = UTIL.getHBaseCluster().getMaster() 155 .getAssignmentManager().getRegionStates().getRegionAssignments(); 156 assertEquals(regionInfoMap.get(tableRegions.get(0).getRegionInfo()), 157 regionInfoMap.get(tableRegions.get(1).getRegionInfo())); 158 } 159 160 @Test 161 public void testRITWithSplitTableRegion() throws Exception { 162 final TableName tableName = TableName.valueOf(testMethodName); 163 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 164 // Disable CatalogJanitor to keep the split region in hbase:meta throughout the test 165 UTIL.getHBaseCluster().getMaster().getCatalogJanitor().setEnabled(false); 166 167 RegionInfo[] regions = 168 MasterProcedureTestingUtility.createTable(procExec, tableName, null, columnFamilyName); 169 insertData(UTIL, tableName, rowCount, startRowNum, columnFamilyName); 170 int splitRowNum = startRowNum + rowCount / 2; 171 byte[] splitKey = Bytes.toBytes("" + splitRowNum); 172 173 assertNotNull(regions, "not able to find a splittable region"); 174 assertEquals(1, regions.length, "not able to find a splittable region"); 175 assertFalse(AssignmentTestingUtil.isRegionInTransition(regions[0], 176 UTIL.getHBaseCluster().getMaster().getAssignmentManager())); 177 178 ServerName targetRS = UTIL.getHBaseCluster().getMaster().getAssignmentManager() 179 .getRegionStates().getRegionServerOfRegion(regions[0]); 180 // Split region of the table 181 long procId = procExec.submitProcedure( 182 new SplitTableRegionProcedure(procExec.getEnvironment(), regions[0], splitKey)); 183 // Wait the completion 184 ProcedureTestingUtility.waitProcedure(procExec, procId); 185 ProcedureTestingUtility.assertProcNotFailed(procExec, procId); 186 187 assertEquals(2, UTIL.getHBaseCluster().getRegions(tableName).size(), "not able to split table"); 188 assertFalse(AssignmentTestingUtil.isRegionInTransition(regions[0], 189 UTIL.getHBaseCluster().getMaster().getAssignmentManager())); 190 assertTrue(UTIL.getHBaseCluster().getMaster().getAssignmentManager().getRegionStates() 191 .getOrCreateRegionStateNode(regions[0]).isSplit()); 192 // As there are only 3 RS, start one more RS before expiring one 193 UTIL.getHBaseCluster().startRegionServer(); 194 195 // We don't want SCP to complete so kill PR it after store update 196 ProcedureTestingUtility 197 .toggleKillAfterStoreUpdate(UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor()); 198 // stop RS holding split parent to create SCP and add RS into deadServerList 199 UTIL.getHBaseCluster().getMaster().getServerManager().expireServer(targetRS); 200 201 // stop master 202 UTIL.getHBaseCluster().stopMaster(0); 203 UTIL.getHBaseCluster().waitOnMaster(0); 204 205 // restart master 206 UTIL.getHBaseCluster().startMaster(); 207 assertTrue(UTIL.getHBaseCluster().waitForActiveAndReadyMaster(30000), 208 "Master failed to initialize in in 30 seconds"); 209 UTIL.invalidateConnection(); 210 211 assertFalse(AssignmentTestingUtil.isRegionInTransition(regions[0], 212 UTIL.getHBaseCluster().getMaster().getAssignmentManager())); 213 assertTrue(UTIL.getHBaseCluster().getMaster().getAssignmentManager().getRegionStates() 214 .getOrCreateRegionStateNode(regions[0]).isSplit()); 215 assertEquals(RegionState.State.SPLIT, UTIL.getHBaseCluster().getMaster().getAssignmentManager() 216 .getRegionStates().getOrCreateRegionStateNode(regions[0]).getState()); 217 } 218 219 @Test 220 public void testSplitStoreFiles() throws Exception { 221 final TableName tableName = TableName.valueOf(testMethodName); 222 final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor(); 223 224 RegionInfo[] regions = 225 MasterProcedureTestingUtility.createTable(procExec, tableName, null, columnFamilyName); 226 // flush the memstore 227 insertData(UTIL, tableName, rowCount, startRowNum, true, columnFamilyName); 228 229 // assert the hfile count of the table 230 int storeFilesCountSum = 0; 231 for (HRegion region : UTIL.getHBaseCluster().getRegions(tableName)) { 232 storeFilesCountSum += region.getStore(Bytes.toBytes(columnFamilyName)).getStorefiles().size(); 233 } 234 assertEquals(1, storeFilesCountSum); 235 236 // split at the start row 237 byte[] splitKey = Bytes.toBytes("" + startRowNum); 238 239 assertNotNull(regions, "Not able to find a splittable region"); 240 assertEquals(1, regions.length, "Not able to find a splittable region"); 241 242 // Split region of the table 243 long procId = procExec.submitProcedure( 244 new SplitTableRegionProcedure(procExec.getEnvironment(), regions[0], splitKey)); 245 // Wait the completion 246 ProcedureTestingUtility.waitProcedure(procExec, procId); 247 ProcedureTestingUtility.assertProcNotFailed(procExec, procId); 248 249 assertEquals(2, UTIL.getHBaseCluster().getRegions(tableName).size(), "Not able to split table"); 250 251 // assert sum of the hfiles of all regions 252 int childStoreFilesSum = 0; 253 for (HRegion region : UTIL.getHBaseCluster().getRegions(tableName)) { 254 childStoreFilesSum += region.getStore(Bytes.toBytes(columnFamilyName)).getStorefiles().size(); 255 } 256 assertEquals(1, childStoreFilesSum); 257 258 List<HRegion> tableRegions = UTIL.getHBaseCluster().getRegions(tableName); 259 assertEquals(2, tableRegions.size(), "Table region not correct."); 260 Map<RegionInfo, ServerName> regionInfoMap = UTIL.getHBaseCluster().getMaster() 261 .getAssignmentManager().getRegionStates().getRegionAssignments(); 262 assertEquals(regionInfoMap.get(tableRegions.get(0).getRegionInfo()), 263 regionInfoMap.get(tableRegions.get(1).getRegionInfo())); 264 } 265 266 private ProcedureExecutor<MasterProcedureEnv> getMasterProcedureExecutor() { 267 return UTIL.getHBaseCluster().getMaster().getMasterProcedureExecutor(); 268 } 269}