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