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.Optional;
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.ServerName;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.RegionInfo;
030import org.apache.hadoop.hbase.master.HMaster;
031import org.apache.hadoop.hbase.master.MasterServices;
032import org.apache.hadoop.hbase.master.procedure.DisableTableProcedure;
033import org.apache.hadoop.hbase.master.procedure.ServerCrashProcedure;
034import org.apache.hadoop.hbase.master.region.MasterRegion;
035import org.apache.hadoop.hbase.procedure2.Procedure;
036import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
037import org.apache.hadoop.hbase.testclassification.MasterTests;
038import org.apache.hadoop.hbase.testclassification.MediumTests;
039import org.apache.hadoop.hbase.util.Bytes;
040import org.apache.hadoop.hbase.util.Threads;
041import org.apache.zookeeper.KeeperException;
042import org.junit.AfterClass;
043import org.junit.BeforeClass;
044import org.junit.ClassRule;
045import org.junit.Test;
046import org.junit.experimental.categories.Category;
047import org.slf4j.Logger;
048import org.slf4j.LoggerFactory;
049
050/**
051 * Testcase for HBASE-23636.
052 */
053@Category({ MasterTests.class, MediumTests.class })
054public class TestRaceBetweenSCPAndDTP {
055  private static final Logger LOG = LoggerFactory.getLogger(TestRaceBetweenSCPAndDTP.class);
056
057  @ClassRule
058  public static final HBaseClassTestRule CLASS_RULE =
059    HBaseClassTestRule.forClass(TestRaceBetweenSCPAndDTP.class);
060
061  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
062
063  private static TableName NAME = TableName.valueOf("Race");
064
065  private static byte[] CF = Bytes.toBytes("cf");
066
067  private static CountDownLatch ARRIVE_GET_REGIONS_ON_TABLE;
068
069  private static CountDownLatch RESUME_GET_REGIONS_ON_SERVER;
070
071  private static final class AssignmentManagerForTest extends AssignmentManager {
072
073    public AssignmentManagerForTest(MasterServices master, MasterRegion masterRegion) {
074      super(master, masterRegion);
075    }
076
077    @Override
078    public TransitRegionStateProcedure[] createUnassignProceduresForDisabling(TableName tableName) {
079      if (ARRIVE_GET_REGIONS_ON_TABLE != null) {
080        ARRIVE_GET_REGIONS_ON_TABLE.countDown();
081        ARRIVE_GET_REGIONS_ON_TABLE = null;
082        try {
083          RESUME_GET_REGIONS_ON_SERVER.await();
084        } catch (InterruptedException e) {
085        }
086      }
087      TransitRegionStateProcedure[] procs = super.createUnassignProceduresForDisabling(tableName);
088      return procs;
089    }
090  }
091
092  public static final class HMasterForTest extends HMaster {
093
094    public HMasterForTest(Configuration conf) throws IOException, KeeperException {
095      super(conf);
096    }
097
098    @Override
099    protected AssignmentManager createAssignmentManager(MasterServices master,
100      MasterRegion masterRegion) {
101      return new AssignmentManagerForTest(master, masterRegion);
102    }
103  }
104
105  @BeforeClass
106  public static void setUp() throws Exception {
107    UTIL.getConfiguration().setClass(HConstants.MASTER_IMPL, HMasterForTest.class, HMaster.class);
108    UTIL.startMiniCluster(2);
109    UTIL.createTable(NAME, CF);
110    UTIL.waitTableAvailable(NAME);
111    UTIL.getAdmin().balancerSwitch(false, true);
112  }
113
114  @AfterClass
115  public static void tearDown() throws Exception {
116    UTIL.shutdownMiniCluster();
117  }
118
119  @Test
120  public void test() throws Exception {
121    RegionInfo region = UTIL.getMiniHBaseCluster().getRegions(NAME).get(0).getRegionInfo();
122    AssignmentManager am = UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager();
123    ServerName sn = am.getRegionStates().getRegionState(region).getServerName();
124    LOG.info("ServerName={}, region={}", sn, region);
125
126    ARRIVE_GET_REGIONS_ON_TABLE = new CountDownLatch(1);
127    RESUME_GET_REGIONS_ON_SERVER = new CountDownLatch(1);
128    // Assign to local variable because this static gets set to null in above running thread and
129    // so NPE.
130    CountDownLatch cdl = ARRIVE_GET_REGIONS_ON_TABLE;
131    UTIL.getAdmin().disableTableAsync(NAME);
132    cdl.await();
133
134    ProcedureExecutor<?> procExec =
135      UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor();
136    UTIL.getMiniHBaseCluster().stopRegionServer(sn);
137    long pid = Procedure.NO_PROC_ID;
138    do {
139      Threads.sleep(1);
140      pid = getSCPPID(procExec);
141    } while (pid != Procedure.NO_PROC_ID);
142    final long scppid = pid;
143    UTIL.waitFor(60000, () -> procExec.isFinished(scppid));
144    RESUME_GET_REGIONS_ON_SERVER.countDown();
145
146    long dtpProcId =
147      procExec.getProcedures().stream().filter(p -> p instanceof DisableTableProcedure)
148        .map(p -> (DisableTableProcedure) p).findAny().get().getProcId();
149    UTIL.waitFor(60000, () -> procExec.isFinished(dtpProcId));
150  }
151
152  /** Returns Returns {@link Procedure#NO_PROC_ID} if no SCP found else actual pid. */
153  private long getSCPPID(ProcedureExecutor<?> e) {
154    Optional<ServerCrashProcedure> optional = e.getProcedures().stream()
155      .filter(p -> p instanceof ServerCrashProcedure).map(p -> (ServerCrashProcedure) p).findAny();
156    return optional.isPresent() ? optional.get().getProcId() : Procedure.NO_PROC_ID;
157  }
158}