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.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertNull; 022 023import java.util.Optional; 024import org.apache.hadoop.hbase.HBaseTestingUtil; 025import org.apache.hadoop.hbase.TableName; 026import org.apache.hadoop.hbase.client.RegionInfo; 027import org.apache.hadoop.hbase.coprocessor.CoprocessorHost; 028import org.apache.hadoop.hbase.coprocessor.ObserverContext; 029import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor; 030import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment; 031import org.apache.hadoop.hbase.coprocessor.RegionObserver; 032import org.apache.hadoop.hbase.procedure2.ProcedureExecutor; 033import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility; 034import org.apache.hadoop.hbase.regionserver.HRegionServer; 035import org.apache.hadoop.hbase.testclassification.MasterTests; 036import org.apache.hadoop.hbase.testclassification.MediumTests; 037import org.apache.hadoop.hbase.util.Bytes; 038import org.apache.hadoop.hbase.util.JVMClusterUtil; 039import org.junit.jupiter.api.AfterAll; 040import org.junit.jupiter.api.BeforeAll; 041import org.junit.jupiter.api.Tag; 042import org.junit.jupiter.api.Test; 043 044@Tag(MasterTests.TAG) 045@Tag(MediumTests.TAG) 046public class TestExceptionInUnassignedRegion { 047 048 private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); 049 050 private static final TableName TABLE_NAME = TableName.valueOf("test"); 051 052 private static final byte[] CF = Bytes.toBytes("cf"); 053 054 @BeforeAll 055 public static void setUp() throws Exception { 056 UTIL.getConfiguration().setStrings(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY, 057 ThrowInCloseCP.class.getName()); 058 UTIL.startMiniCluster(3); 059 UTIL.getAdmin().balancerSwitch(false, true); 060 UTIL.createTable(TABLE_NAME, CF); 061 UTIL.waitTableAvailable(TABLE_NAME); 062 } 063 064 @AfterAll 065 public static void tearDown() throws Exception { 066 UTIL.shutdownMiniCluster(); 067 } 068 069 @Test 070 public void testExceptionInUnassignRegion() { 071 ProcedureExecutor procedureExecutor = 072 UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor(); 073 074 JVMClusterUtil.RegionServerThread rsThread = null; 075 for (JVMClusterUtil.RegionServerThread t : UTIL.getMiniHBaseCluster() 076 .getRegionServerThreads()) { 077 if (!t.getRegionServer().getRegions(TABLE_NAME).isEmpty()) { 078 rsThread = t; 079 break; 080 } 081 } 082 // find the rs and hri of the table 083 HRegionServer rs = rsThread.getRegionServer(); 084 RegionInfo hri = rs.getRegions(TABLE_NAME).get(0).getRegionInfo(); 085 TransitRegionStateProcedure moveRegionProcedure = TransitRegionStateProcedure.reopen( 086 UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor().getEnvironment(), hri); 087 RegionStateNode regionNode = UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager() 088 .getRegionStates().getOrCreateRegionStateNode(hri); 089 regionNode.setProcedure(moveRegionProcedure); 090 long prodId = procedureExecutor.submitProcedure(moveRegionProcedure); 091 ProcedureTestingUtility.waitProcedure(procedureExecutor, prodId); 092 093 assertEquals(2, UTIL.getMiniHBaseCluster().getLiveRegionServerThreads().size(), 094 "Should be two RS since other is aborted"); 095 assertNull(getRegionServer(0).getRegionsInTransitionInRS().get(hri.getEncodedNameAsBytes()), 096 "RIT Map doesn't have correct value"); 097 assertNull(getRegionServer(1).getRegionsInTransitionInRS().get(hri.getEncodedNameAsBytes()), 098 "RIT Map doesn't have correct value"); 099 assertNull(getRegionServer(2).getRegionsInTransitionInRS().get(hri.getEncodedNameAsBytes()), 100 "RIT Map doesn't have correct value"); 101 } 102 103 private HRegionServer getRegionServer(int index) { 104 return UTIL.getMiniHBaseCluster().getRegionServer(index); 105 } 106 107 public static class ThrowInCloseCP implements RegionCoprocessor, RegionObserver { 108 109 @Override 110 public void preClose(ObserverContext<? extends RegionCoprocessorEnvironment> c, 111 boolean abortRequested) { 112 if (!c.getEnvironment().getRegion().getRegionInfo().getTable().isSystemTable()) { 113 throw new RuntimeException(); 114 } 115 } 116 117 @Override 118 public Optional<RegionObserver> getRegionObserver() { 119 return Optional.of(this); 120 } 121 } 122}