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