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.assertNotNull; 021import static org.junit.jupiter.api.Assertions.assertNull; 022 023import java.io.IOException; 024import java.util.ArrayList; 025import java.util.List; 026import java.util.concurrent.CountDownLatch; 027import org.apache.hadoop.conf.Configuration; 028import org.apache.hadoop.hbase.HBaseTestingUtil; 029import org.apache.hadoop.hbase.HConstants; 030import org.apache.hadoop.hbase.PleaseHoldException; 031import org.apache.hadoop.hbase.ServerName; 032import org.apache.hadoop.hbase.StartTestingClusterOption; 033import org.apache.hadoop.hbase.TableName; 034import org.apache.hadoop.hbase.client.RegionInfo; 035import org.apache.hadoop.hbase.master.HMaster; 036import org.apache.hadoop.hbase.master.MasterServices; 037import org.apache.hadoop.hbase.master.RegionPlan; 038import org.apache.hadoop.hbase.master.RegionServerList; 039import org.apache.hadoop.hbase.master.ServerManager; 040import org.apache.hadoop.hbase.master.region.MasterRegion; 041import org.apache.hadoop.hbase.regionserver.HRegionServer; 042import org.apache.hadoop.hbase.testclassification.MasterTests; 043import org.apache.hadoop.hbase.testclassification.MediumTests; 044import org.apache.hadoop.hbase.util.Bytes; 045import org.junit.jupiter.api.AfterAll; 046import org.junit.jupiter.api.BeforeAll; 047import org.junit.jupiter.api.Tag; 048import org.junit.jupiter.api.Test; 049 050import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionStateTransition.TransitionCode; 051import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.ReportRegionStateTransitionRequest; 052import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.ReportRegionStateTransitionResponse; 053 054@Tag(MasterTests.TAG) 055@Tag(MediumTests.TAG) 056public class TestRegionAssignedToMultipleRegionServers { 057 058 private static final List<ServerName> EXCLUDE_SERVERS = new ArrayList<>(); 059 060 private static boolean HALT = false; 061 062 private static boolean KILL = false; 063 064 private static CountDownLatch ARRIVE; 065 066 private static final class ServerManagerForTest extends ServerManager { 067 068 public ServerManagerForTest(MasterServices master, RegionServerList storage) { 069 super(master, storage); 070 } 071 072 @Override 073 public List<ServerName> createDestinationServersList() { 074 return super.createDestinationServersList(EXCLUDE_SERVERS); 075 } 076 } 077 078 private static final class AssignmentManagerForTest extends AssignmentManager { 079 080 public AssignmentManagerForTest(MasterServices master, MasterRegion masterRegion) { 081 super(master, masterRegion); 082 } 083 084 @Override 085 public ReportRegionStateTransitionResponse reportRegionStateTransition( 086 ReportRegionStateTransitionRequest req) throws PleaseHoldException { 087 if (req.getTransition(0).getTransitionCode() == TransitionCode.OPENED) { 088 if (ARRIVE != null) { 089 ARRIVE.countDown(); 090 ARRIVE = null; 091 } 092 while (HALT) { 093 try { 094 Thread.sleep(100); 095 } catch (InterruptedException e) { 096 throw new RuntimeException(e); 097 } 098 if (KILL) { 099 throw new PleaseHoldException("Inject error!"); 100 } 101 } 102 } 103 return super.reportRegionStateTransition(req); 104 } 105 } 106 107 public static final class HMasterForTest extends HMaster { 108 109 public HMasterForTest(Configuration conf) throws IOException { 110 super(conf); 111 } 112 113 @Override 114 protected AssignmentManager createAssignmentManager(MasterServices master, 115 MasterRegion masterRegion) { 116 return new AssignmentManagerForTest(master, masterRegion); 117 } 118 119 @Override 120 protected ServerManager createServerManager(MasterServices master, RegionServerList storage) 121 throws IOException { 122 setupClusterConnection(); 123 return new ServerManagerForTest(master, storage); 124 } 125 } 126 127 private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); 128 129 private static TableName NAME = TableName.valueOf("Assign"); 130 131 private static byte[] CF = Bytes.toBytes("cf"); 132 133 @BeforeAll 134 public static void setUp() throws Exception { 135 UTIL.getConfiguration().setClass(HConstants.MASTER_IMPL, HMasterForTest.class, HMaster.class); 136 UTIL.startMiniCluster( 137 StartTestingClusterOption.builder().numMasters(2).numRegionServers(2).build()); 138 UTIL.createTable(NAME, CF); 139 UTIL.waitTableAvailable(NAME); 140 UTIL.getAdmin().balancerSwitch(false, true); 141 } 142 143 @AfterAll 144 public static void tearDown() throws Exception { 145 UTIL.shutdownMiniCluster(); 146 } 147 148 @Test 149 public void test() throws Exception { 150 RegionInfo region = UTIL.getMiniHBaseCluster().getRegions(NAME).get(0).getRegionInfo(); 151 AssignmentManager am = UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager(); 152 RegionStateNode rsn = am.getRegionStates().getRegionStateNode(region); 153 154 ServerName sn = rsn.getRegionLocation(); 155 ARRIVE = new CountDownLatch(1); 156 HALT = true; 157 am.moveAsync(new RegionPlan(region, sn, sn)); 158 ARRIVE.await(); 159 160 // let's restart the master 161 EXCLUDE_SERVERS.add(rsn.getRegionLocation()); 162 KILL = true; 163 HMaster activeMaster = UTIL.getMiniHBaseCluster().getMaster(); 164 activeMaster.abort("For testing"); 165 activeMaster.join(); 166 KILL = false; 167 168 // sleep a while to reproduce the problem, as after the fix in HBASE-21472 the execution logic 169 // is changed so the old code to reproduce the problem can not compile... 170 Thread.sleep(10000); 171 HALT = false; 172 Thread.sleep(5000); 173 174 HRegionServer rs = UTIL.getMiniHBaseCluster().getRegionServer(sn); 175 assertNotNull(rs.getRegion(region.getEncodedName())); 176 assertNull(UTIL.getOtherRegionServer(rs).getRegion(region.getEncodedName())); 177 } 178}