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.concurrent.CountDownLatch;
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.HBaseClassTestRule;
024import org.apache.hadoop.hbase.HBaseTestingUtility;
025import org.apache.hadoop.hbase.HConstants;
026import org.apache.hadoop.hbase.ServerName;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.client.RegionInfo;
029import org.apache.hadoop.hbase.master.HMaster;
030import org.apache.hadoop.hbase.master.MasterServices;
031import org.apache.hadoop.hbase.master.procedure.DisableTableProcedure;
032import org.apache.hadoop.hbase.master.procedure.ServerCrashProcedure;
033import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
034import org.apache.hadoop.hbase.testclassification.LargeTests;
035import org.apache.hadoop.hbase.testclassification.MasterTests;
036import org.apache.hadoop.hbase.util.Bytes;
037import org.apache.zookeeper.KeeperException;
038import org.junit.AfterClass;
039import org.junit.BeforeClass;
040import org.junit.ClassRule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043
044/**
045 * Testcase for HBASE-23636.
046 */
047@Category({ MasterTests.class, LargeTests.class })
048public class TestRaceBetweenSCPAndDTP {
049
050  @ClassRule
051  public static final HBaseClassTestRule CLASS_RULE =
052    HBaseClassTestRule.forClass(TestRaceBetweenSCPAndDTP.class);
053
054  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
055
056  private static TableName NAME = TableName.valueOf("Race");
057
058  private static byte[] CF = Bytes.toBytes("cf");
059
060  private static CountDownLatch ARRIVE_GET_REGIONS_ON_TABLE;
061
062  private static CountDownLatch RESUME_GET_REGIONS_ON_SERVER;
063
064  private static final class AssignmentManagerForTest extends AssignmentManager {
065
066    public AssignmentManagerForTest(MasterServices master) {
067      super(master);
068    }
069
070    @Override
071    public TransitRegionStateProcedure[] createUnassignProceduresForDisabling(TableName tableName) {
072      if (ARRIVE_GET_REGIONS_ON_TABLE != null) {
073        ARRIVE_GET_REGIONS_ON_TABLE.countDown();
074        ARRIVE_GET_REGIONS_ON_TABLE = null;
075        try {
076          RESUME_GET_REGIONS_ON_SERVER.await();
077        } catch (InterruptedException e) {
078        }
079      }
080      TransitRegionStateProcedure[] procs = super.createUnassignProceduresForDisabling(tableName);
081      return procs;
082    }
083  }
084
085  public static final class HMasterForTest extends HMaster {
086
087    public HMasterForTest(Configuration conf) throws IOException, KeeperException {
088      super(conf);
089    }
090
091    @Override
092    protected AssignmentManager createAssignmentManager(MasterServices master) {
093      return new AssignmentManagerForTest(master);
094    }
095  }
096
097  @BeforeClass
098  public static void setUp() throws Exception {
099    UTIL.getConfiguration().setClass(HConstants.MASTER_IMPL, HMasterForTest.class, HMaster.class);
100    UTIL.startMiniCluster(2);
101    UTIL.createTable(NAME, CF);
102    UTIL.waitTableAvailable(NAME);
103    UTIL.getAdmin().balancerSwitch(false, true);
104  }
105
106  @AfterClass
107  public static void tearDown() throws Exception {
108    UTIL.shutdownMiniCluster();
109  }
110
111  @Test
112  public void test() throws Exception {
113    RegionInfo region = UTIL.getMiniHBaseCluster().getRegions(NAME).get(0).getRegionInfo();
114    AssignmentManager am = UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager();
115    ServerName sn = am.getRegionStates().getRegionState(region).getServerName();
116
117    ARRIVE_GET_REGIONS_ON_TABLE = new CountDownLatch(1);
118    RESUME_GET_REGIONS_ON_SERVER = new CountDownLatch(1);
119
120    UTIL.getAdmin().disableTableAsync(NAME);
121    ARRIVE_GET_REGIONS_ON_TABLE.await();
122
123    UTIL.getMiniHBaseCluster().stopRegionServer(sn);
124    // Wait ServerCrashProcedure to init.
125    Thread.sleep(1000);
126    ProcedureExecutor<?> procExec =
127        UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor();
128    long scpProcId =
129        procExec.getProcedures().stream().filter(p -> p instanceof ServerCrashProcedure)
130            .map(p -> (ServerCrashProcedure) p).findAny().get().getProcId();
131    UTIL.waitFor(60000, () -> procExec.isFinished(scpProcId));
132    RESUME_GET_REGIONS_ON_SERVER.countDown();
133
134    long dtpProcId =
135        procExec.getProcedures().stream().filter(p -> p instanceof DisableTableProcedure)
136            .map(p -> (DisableTableProcedure) p).findAny().get().getProcId();
137    UTIL.waitFor(60000, () -> procExec.isFinished(dtpProcId));
138  }
139}