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