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.List;
022import java.util.concurrent.CountDownLatch;
023import java.util.concurrent.Future;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseTestingUtility;
027import org.apache.hadoop.hbase.HConstants;
028import org.apache.hadoop.hbase.ServerName;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.client.RegionInfo;
031import org.apache.hadoop.hbase.master.HMaster;
032import org.apache.hadoop.hbase.master.MasterServices;
033import org.apache.hadoop.hbase.master.RegionPlan;
034import org.apache.hadoop.hbase.master.procedure.ServerCrashProcedure;
035import org.apache.hadoop.hbase.master.region.MasterRegion;
036import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
037import org.apache.hadoop.hbase.testclassification.LargeTests;
038import org.apache.hadoop.hbase.testclassification.MasterTests;
039import org.apache.hadoop.hbase.util.Bytes;
040import org.apache.zookeeper.KeeperException;
041import org.junit.AfterClass;
042import org.junit.BeforeClass;
043import org.junit.ClassRule;
044import org.junit.Test;
045import org.junit.experimental.categories.Category;
046
047/**
048 * Testcase for HBASE-23594.
049 */
050@Category({ MasterTests.class, LargeTests.class })
051public class TestRaceBetweenSCPAndTRSP {
052
053  @ClassRule
054  public static final HBaseClassTestRule CLASS_RULE =
055    HBaseClassTestRule.forClass(TestRaceBetweenSCPAndTRSP.class);
056
057  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
058
059  private static TableName NAME = TableName.valueOf("Race");
060
061  private static byte[] CF = Bytes.toBytes("cf");
062
063  private static CountDownLatch ARRIVE_REGION_OPENING;
064
065  private static CountDownLatch RESUME_REGION_OPENING;
066
067  private static CountDownLatch ARRIVE_GET_REGIONS_ON_SERVER;
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    void regionOpening(RegionStateNode regionNode) throws IOException {
079      super.regionOpening(regionNode);
080      if (regionNode.getRegionInfo().getTable().equals(NAME) && ARRIVE_REGION_OPENING != null) {
081        ARRIVE_REGION_OPENING.countDown();
082        ARRIVE_REGION_OPENING = null;
083        try {
084          RESUME_REGION_OPENING.await();
085        } catch (InterruptedException e) {
086        }
087      }
088    }
089
090    @Override
091    public List<RegionInfo> getRegionsOnServer(ServerName serverName) {
092      List<RegionInfo> regions = super.getRegionsOnServer(serverName);
093      if (ARRIVE_GET_REGIONS_ON_SERVER != null) {
094        ARRIVE_GET_REGIONS_ON_SERVER.countDown();
095        ARRIVE_GET_REGIONS_ON_SERVER = null;
096        try {
097          RESUME_GET_REGIONS_ON_SERVER.await();
098        } catch (InterruptedException e) {
099        }
100      }
101      return regions;
102    }
103  }
104
105  public static final class HMasterForTest extends HMaster {
106
107    public HMasterForTest(Configuration conf) throws IOException, KeeperException {
108      super(conf);
109    }
110
111    @Override
112    protected AssignmentManager createAssignmentManager(MasterServices master,
113      MasterRegion masterRegion) {
114      return new AssignmentManagerForTest(master, masterRegion);
115    }
116  }
117
118  @BeforeClass
119  public static void setUp() throws Exception {
120    UTIL.getConfiguration().setClass(HConstants.MASTER_IMPL, HMasterForTest.class, HMaster.class);
121    UTIL.startMiniCluster(2);
122    UTIL.createTable(NAME, CF);
123    UTIL.waitTableAvailable(NAME);
124    UTIL.getAdmin().balancerSwitch(false, true);
125  }
126
127  @AfterClass
128  public static void tearDown() throws Exception {
129    UTIL.shutdownMiniCluster();
130  }
131
132  @Test
133  public void test() throws Exception {
134    RegionInfo region = UTIL.getMiniHBaseCluster().getRegions(NAME).get(0).getRegionInfo();
135    AssignmentManager am = UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager();
136    ServerName sn = am.getRegionStates().getRegionState(region).getServerName();
137
138    // Assign the CountDownLatches that get nulled in background threads else we NPE checking
139    // the static.
140    ARRIVE_REGION_OPENING = new CountDownLatch(1);
141    CountDownLatch arriveRegionOpening = ARRIVE_REGION_OPENING;
142    RESUME_REGION_OPENING = new CountDownLatch(1);
143    ARRIVE_GET_REGIONS_ON_SERVER = new CountDownLatch(1);
144    CountDownLatch arriveGetRegionsOnServer = ARRIVE_GET_REGIONS_ON_SERVER;
145    RESUME_GET_REGIONS_ON_SERVER = new CountDownLatch(1);
146
147    Future<byte[]> moveFuture = am.moveAsync(new RegionPlan(region, sn, sn));
148    arriveRegionOpening.await();
149
150    UTIL.getMiniHBaseCluster().killRegionServer(sn);
151    arriveGetRegionsOnServer.await();
152    RESUME_REGION_OPENING.countDown();
153
154    moveFuture.get();
155    ProcedureExecutor<?> procExec =
156      UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor();
157    long scpProcId =
158      procExec.getProcedures().stream().filter(p -> p instanceof ServerCrashProcedure)
159        .map(p -> (ServerCrashProcedure) p).findAny().get().getProcId();
160    RESUME_GET_REGIONS_ON_SERVER.countDown();
161    UTIL.waitFor(60000, () -> procExec.isFinished(scpProcId));
162  }
163}