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.io.IOException; 021import java.util.Optional; 022import java.util.concurrent.CountDownLatch; 023import org.apache.hadoop.conf.Configuration; 024import org.apache.hadoop.hbase.HBaseClassTestRule; 025import org.apache.hadoop.hbase.HBaseTestingUtility; 026import org.apache.hadoop.hbase.HConstants; 027import org.apache.hadoop.hbase.ServerName; 028import org.apache.hadoop.hbase.TableName; 029import org.apache.hadoop.hbase.client.RegionInfo; 030import org.apache.hadoop.hbase.master.HMaster; 031import org.apache.hadoop.hbase.master.MasterServices; 032import org.apache.hadoop.hbase.master.procedure.DisableTableProcedure; 033import org.apache.hadoop.hbase.master.procedure.ServerCrashProcedure; 034import org.apache.hadoop.hbase.procedure2.Procedure; 035import org.apache.hadoop.hbase.procedure2.ProcedureExecutor; 036import org.apache.hadoop.hbase.testclassification.MasterTests; 037import org.apache.hadoop.hbase.testclassification.MediumTests; 038import org.apache.hadoop.hbase.util.Bytes; 039import org.apache.hadoop.hbase.util.Threads; 040import org.apache.zookeeper.KeeperException; 041import org.junit.AfterClass; 042import org.junit.BeforeClass; 043import org.junit.ClassRule; 044import org.junit.Test; 045import org.junit.experimental.categories.Category; 046import org.slf4j.Logger; 047import org.slf4j.LoggerFactory; 048 049/** 050 * Testcase for HBASE-23636. 051 */ 052@Category({ MasterTests.class, MediumTests.class }) 053public class TestRaceBetweenSCPAndDTP { 054 private static final Logger LOG = LoggerFactory.getLogger(TestRaceBetweenSCPAndDTP.class); 055 056 @ClassRule 057 public static final HBaseClassTestRule CLASS_RULE = 058 HBaseClassTestRule.forClass(TestRaceBetweenSCPAndDTP.class); 059 060 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility(); 061 062 private static TableName NAME = TableName.valueOf("Race"); 063 064 private static byte[] CF = Bytes.toBytes("cf"); 065 066 private static CountDownLatch ARRIVE_GET_REGIONS_ON_TABLE; 067 068 private static CountDownLatch RESUME_GET_REGIONS_ON_SERVER; 069 070 private static final class AssignmentManagerForTest extends AssignmentManager { 071 072 public AssignmentManagerForTest(MasterServices master) { 073 super(master); 074 } 075 076 @Override 077 public TransitRegionStateProcedure[] createUnassignProceduresForDisabling(TableName tableName) { 078 if (ARRIVE_GET_REGIONS_ON_TABLE != null) { 079 ARRIVE_GET_REGIONS_ON_TABLE.countDown(); 080 ARRIVE_GET_REGIONS_ON_TABLE = null; 081 try { 082 RESUME_GET_REGIONS_ON_SERVER.await(); 083 } catch (InterruptedException e) { 084 } 085 } 086 TransitRegionStateProcedure[] procs = super.createUnassignProceduresForDisabling(tableName); 087 return procs; 088 } 089 } 090 091 public static final class HMasterForTest extends HMaster { 092 093 public HMasterForTest(Configuration conf) throws IOException, KeeperException { 094 super(conf); 095 } 096 097 @Override 098 protected AssignmentManager createAssignmentManager(MasterServices master) { 099 return new AssignmentManagerForTest(master); 100 } 101 } 102 103 @BeforeClass 104 public static void setUp() throws Exception { 105 UTIL.getConfiguration().setClass(HConstants.MASTER_IMPL, HMasterForTest.class, HMaster.class); 106 UTIL.startMiniCluster(2); 107 UTIL.createTable(NAME, CF); 108 UTIL.waitTableAvailable(NAME); 109 UTIL.getAdmin().balancerSwitch(false, true); 110 } 111 112 @AfterClass 113 public static void tearDown() throws Exception { 114 UTIL.shutdownMiniCluster(); 115 } 116 117 @Test 118 public void test() throws Exception { 119 RegionInfo region = UTIL.getMiniHBaseCluster().getRegions(NAME).get(0).getRegionInfo(); 120 AssignmentManager am = UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager(); 121 ServerName sn = am.getRegionStates().getRegionState(region).getServerName(); 122 LOG.info("ServerName={}, region={}", sn, region); 123 124 ARRIVE_GET_REGIONS_ON_TABLE = new CountDownLatch(1); 125 RESUME_GET_REGIONS_ON_SERVER = new CountDownLatch(1); 126 // Assign to local variable because this static gets set to null in above running thread and 127 // so NPE. 128 CountDownLatch cdl = ARRIVE_GET_REGIONS_ON_TABLE; 129 UTIL.getAdmin().disableTableAsync(NAME); 130 cdl.await(); 131 132 ProcedureExecutor<?> procExec = 133 UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor(); 134 UTIL.getMiniHBaseCluster().stopRegionServer(sn); 135 long pid = Procedure.NO_PROC_ID; 136 do { 137 Threads.sleep(1); 138 pid = getSCPPID(procExec); 139 } while (pid != Procedure.NO_PROC_ID); 140 final long scppid = pid; 141 UTIL.waitFor(60000, () -> procExec.isFinished(scppid)); 142 RESUME_GET_REGIONS_ON_SERVER.countDown(); 143 144 long dtpProcId = 145 procExec.getProcedures().stream().filter(p -> p instanceof DisableTableProcedure) 146 .map(p -> (DisableTableProcedure) p).findAny().get().getProcId(); 147 UTIL.waitFor(60000, () -> procExec.isFinished(dtpProcId)); 148 } 149 150 /** 151 * @return Returns {@link Procedure#NO_PROC_ID} if no SCP found else actual pid. 152 */ 153 private long getSCPPID(ProcedureExecutor<?> e) { 154 Optional<ServerCrashProcedure> optional = e.getProcedures().stream(). 155 filter(p -> p instanceof ServerCrashProcedure).map(p -> (ServerCrashProcedure) p).findAny(); 156 return optional.isPresent()? optional.get().getProcId(): Procedure.NO_PROC_ID; 157 } 158}