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