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; 019 020import java.io.IOException; 021import java.io.UncheckedIOException; 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.PleaseHoldException; 028import org.apache.hadoop.hbase.StartMiniClusterOption; 029import org.apache.hadoop.hbase.TableName; 030import org.apache.hadoop.hbase.client.Put; 031import org.apache.hadoop.hbase.client.RegionInfo; 032import org.apache.hadoop.hbase.client.Table; 033import org.apache.hadoop.hbase.master.assignment.AssignmentManager; 034import org.apache.hadoop.hbase.regionserver.HRegionServer; 035import org.apache.hadoop.hbase.testclassification.MasterTests; 036import org.apache.hadoop.hbase.testclassification.MediumTests; 037import org.apache.hadoop.hbase.util.Bytes; 038import org.apache.zookeeper.KeeperException; 039import org.junit.AfterClass; 040import org.junit.BeforeClass; 041import org.junit.ClassRule; 042import org.junit.Test; 043import org.junit.experimental.categories.Category; 044 045import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionStateTransition.TransitionCode; 046import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.ReportRegionStateTransitionRequest; 047import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.ReportRegionStateTransitionResponse; 048 049@Category({ MasterTests.class, MediumTests.class }) 050public class TestCloseAnOpeningRegion { 051 052 @ClassRule 053 public static final HBaseClassTestRule CLASS_RULE = 054 HBaseClassTestRule.forClass(TestCloseAnOpeningRegion.class); 055 056 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility(); 057 058 private static TableName TABLE_NAME = TableName.valueOf("race"); 059 060 private static byte[] CF = Bytes.toBytes("cf"); 061 062 private static volatile CountDownLatch ARRIVE; 063 064 private static volatile CountDownLatch RESUME; 065 066 public static final class MockHMaster extends HMaster { 067 068 public MockHMaster(Configuration conf) throws IOException, KeeperException { 069 super(conf); 070 } 071 072 @Override 073 protected AssignmentManager createAssignmentManager(MasterServices master) { 074 return new AssignmentManager(master) { 075 076 @Override 077 public ReportRegionStateTransitionResponse reportRegionStateTransition( 078 ReportRegionStateTransitionRequest req) throws PleaseHoldException { 079 ReportRegionStateTransitionResponse resp = super.reportRegionStateTransition(req); 080 TransitionCode code = req.getTransition(0).getTransitionCode(); 081 if (code == TransitionCode.OPENED && ARRIVE != null) { 082 ARRIVE.countDown(); 083 try { 084 RESUME.await(); 085 } catch (InterruptedException e) { 086 throw new RuntimeException(e); 087 } 088 } 089 return resp; 090 } 091 }; 092 } 093 } 094 095 @BeforeClass 096 public static void setUp() throws Exception { 097 UTIL.getConfiguration().setInt(HConstants.HBASE_RPC_SHORTOPERATION_TIMEOUT_KEY, 60000); 098 UTIL.startMiniCluster( 099 StartMiniClusterOption.builder().numRegionServers(2).masterClass(MockHMaster.class).build()); 100 UTIL.createTable(TABLE_NAME, CF); 101 UTIL.getAdmin().balancerSwitch(false, true); 102 } 103 104 @AfterClass 105 public static void tearDown() throws Exception { 106 UTIL.shutdownMiniCluster(); 107 } 108 109 @Test 110 public void test() throws IOException, InterruptedException { 111 ARRIVE = new CountDownLatch(1); 112 RESUME = new CountDownLatch(1); 113 RegionInfo region = UTIL.getAdmin().getRegions(TABLE_NAME).get(0); 114 HRegionServer src = UTIL.getRSForFirstRegionInTable(TABLE_NAME); 115 HRegionServer dst = UTIL.getOtherRegionServer(src); 116 Thread move0 = new Thread(() -> { 117 try { 118 UTIL.getAdmin().move(region.getEncodedNameAsBytes(), dst.getServerName()); 119 } catch (IOException e) { 120 throw new UncheckedIOException(e); 121 } 122 }); 123 move0.start(); 124 ARRIVE.await(); 125 Thread move1 = new Thread(() -> { 126 try { 127 UTIL.getAdmin().move(region.getEncodedNameAsBytes(), src.getServerName()); 128 } catch (IOException e) { 129 throw new UncheckedIOException(e); 130 } 131 }); 132 move1.start(); 133 // No simple way to determine when it is safe to go on and produce the race so let's sleep for a 134 // well... 135 Thread.sleep(10000); 136 RESUME.countDown(); 137 move0.join(); 138 move1.join(); 139 try (Table table = UTIL.getConnection().getTable(TABLE_NAME)) { 140 // make sure that we can write to the table, which means the region is online 141 table.put(new Put(Bytes.toBytes(0)).addColumn(CF, Bytes.toBytes("cq"), Bytes.toBytes(0))); 142 } 143 } 144}