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