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.util.Optional;
022import org.apache.hadoop.hbase.HBaseTestingUtil;
023import org.apache.hadoop.hbase.HConstants;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.client.RegionInfo;
026import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
027import org.apache.hadoop.hbase.coprocessor.ObserverContext;
028import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor;
029import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
030import org.apache.hadoop.hbase.coprocessor.RegionObserver;
031import org.apache.hadoop.hbase.testclassification.LargeTests;
032import org.apache.hadoop.hbase.testclassification.MasterTests;
033import org.junit.jupiter.api.BeforeAll;
034import org.junit.jupiter.api.Tag;
035import org.junit.jupiter.api.Test;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039@Tag(MasterTests.TAG)
040@Tag(LargeTests.TAG)
041public class TestMasterHandlerFullWhenTransitRegion {
042
043  private static Logger LOG =
044    LoggerFactory.getLogger(TestMasterHandlerFullWhenTransitRegion.class.getName());
045
046  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
047
048  private static final String TABLENAME = "table";
049
050  @BeforeAll
051  public static void setUp() throws Exception {
052    UTIL.getConfiguration().setStrings(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
053      DelayOpenCP.class.getName());
054    // set handler number to 1.
055    UTIL.getConfiguration().setInt(HConstants.REGION_SERVER_HANDLER_COUNT, 1);
056    UTIL.startMiniCluster(2);
057    UTIL.createTable(TableName.valueOf(TABLENAME), "fa");
058  }
059
060  @Test
061  public void test() throws Exception {
062    RegionInfo regionInfo = UTIL.getAdmin().getRegions(TableName.valueOf(TABLENAME)).get(0);
063    // See HBASE-21754
064    // There is Only one handler, if ReportRegionStateTransitionRequest executes in the same kind
065    // of thread with moveRegion, it will lock each other. Making the move operation can not finish.
066    UTIL.getAdmin().move(regionInfo.getEncodedNameAsBytes());
067    LOG.info("Region move complete");
068  }
069
070  /**
071   * Make open region very slow
072   */
073  public static class DelayOpenCP implements RegionCoprocessor, RegionObserver {
074
075    @Override
076    public void preOpen(ObserverContext<? extends RegionCoprocessorEnvironment> c)
077      throws IOException {
078      try {
079        if (!c.getEnvironment().getRegion().getRegionInfo().getTable().isSystemTable()) {
080          LOG.info("begin to sleep");
081          Thread.sleep(10000);
082          LOG.info("finish sleep");
083        }
084      } catch (Throwable t) {
085
086      }
087    }
088
089    @Override
090    public Optional<RegionObserver> getRegionObserver() {
091      return Optional.of(this);
092    }
093  }
094
095}