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