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.HBaseTestingUtil;
026import org.apache.hadoop.hbase.HConstants;
027import org.apache.hadoop.hbase.PleaseHoldException;
028import org.apache.hadoop.hbase.StartTestingClusterOption;
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.master.region.MasterRegion;
035import org.apache.hadoop.hbase.regionserver.HRegionServer;
036import org.apache.hadoop.hbase.testclassification.MasterTests;
037import org.apache.hadoop.hbase.testclassification.MediumTests;
038import org.apache.hadoop.hbase.util.Bytes;
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 HBaseTestingUtil UTIL = new HBaseTestingUtil();
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 {
069      super(conf);
070    }
071
072    @Override
073    protected AssignmentManager createAssignmentManager(MasterServices master,
074      MasterRegion masterRegion) {
075      return new AssignmentManager(master, masterRegion) {
076
077        @Override
078        public ReportRegionStateTransitionResponse reportRegionStateTransition(
079          ReportRegionStateTransitionRequest req) throws PleaseHoldException {
080          ReportRegionStateTransitionResponse resp = super.reportRegionStateTransition(req);
081          TransitionCode code = req.getTransition(0).getTransitionCode();
082          if (code == TransitionCode.OPENED && ARRIVE != null) {
083            ARRIVE.countDown();
084            try {
085              RESUME.await();
086            } catch (InterruptedException e) {
087              throw new RuntimeException(e);
088            }
089          }
090          return resp;
091        }
092      };
093    }
094  }
095
096  @BeforeClass
097  public static void setUp() throws Exception {
098    UTIL.getConfiguration().setInt(HConstants.HBASE_RPC_SHORTOPERATION_TIMEOUT_KEY, 60000);
099    UTIL.startMiniCluster(StartTestingClusterOption.builder().numRegionServers(2)
100      .masterClass(MockHMaster.class).build());
101    UTIL.createTable(TABLE_NAME, CF);
102    UTIL.getAdmin().balancerSwitch(false, true);
103  }
104
105  @AfterClass
106  public static void tearDown() throws Exception {
107    UTIL.shutdownMiniCluster();
108  }
109
110  @Test
111  public void test() throws IOException, InterruptedException {
112    ARRIVE = new CountDownLatch(1);
113    RESUME = new CountDownLatch(1);
114    RegionInfo region = UTIL.getAdmin().getRegions(TABLE_NAME).get(0);
115    HRegionServer src = UTIL.getRSForFirstRegionInTable(TABLE_NAME);
116    HRegionServer dst = UTIL.getOtherRegionServer(src);
117    Thread move0 = new Thread(() -> {
118      try {
119        UTIL.getAdmin().move(region.getEncodedNameAsBytes(), dst.getServerName());
120      } catch (IOException e) {
121        throw new UncheckedIOException(e);
122      }
123    });
124    move0.start();
125    ARRIVE.await();
126    Thread move1 = new Thread(() -> {
127      try {
128        UTIL.getAdmin().move(region.getEncodedNameAsBytes(), src.getServerName());
129      } catch (IOException e) {
130        throw new UncheckedIOException(e);
131      }
132    });
133    move1.start();
134    // No simple way to determine when it is safe to go on and produce the race so let's sleep for a
135    // well...
136    Thread.sleep(10000);
137    RESUME.countDown();
138    move0.join();
139    move1.join();
140    try (Table table = UTIL.getConnection().getTable(TABLE_NAME)) {
141      // make sure that we can write to the table, which means the region is online
142      table.put(new Put(Bytes.toBytes(0)).addColumn(CF, Bytes.toBytes("cq"), Bytes.toBytes(0)));
143    }
144  }
145}