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 java.util.concurrent.CountDownLatch;
023
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HBaseTestingUtility;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.RegionInfo;
028import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
029import org.apache.hadoop.hbase.coprocessor.ObserverContext;
030import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor;
031import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
032import org.apache.hadoop.hbase.coprocessor.RegionObserver;
033import org.apache.hadoop.hbase.master.assignment.MoveRegionProcedure;
034import org.apache.hadoop.hbase.regionserver.HRegionServer;
035import org.apache.hadoop.hbase.testclassification.MasterTests;
036import org.apache.hadoop.hbase.testclassification.MediumTests;
037import org.apache.hadoop.hbase.util.Bytes;
038import org.apache.hadoop.hbase.util.JVMClusterUtil;
039import org.junit.AfterClass;
040import org.junit.Assert;
041import org.junit.BeforeClass;
042import org.junit.ClassRule;
043import org.junit.Test;
044import org.junit.experimental.categories.Category;
045import org.slf4j.Logger;
046import org.slf4j.LoggerFactory;
047
048
049@Category({ MasterTests.class, MediumTests.class })
050public class TestMasterAbortAndRSGotKilled {
051  private static Logger LOG = LoggerFactory
052      .getLogger(TestMasterAbortAndRSGotKilled.class.getName());
053
054  @ClassRule
055  public static final HBaseClassTestRule CLASS_RULE =
056      HBaseClassTestRule.forClass(TestMasterAbortAndRSGotKilled.class);
057
058  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
059
060  private static TableName TABLE_NAME = TableName.valueOf("test");
061
062  private static CountDownLatch countDownLatch = new CountDownLatch(1);
063
064
065
066  private static byte[] CF = Bytes.toBytes("cf");
067
068  @BeforeClass
069  public static void setUp() throws Exception {
070    UTIL.getConfiguration().setStrings(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
071        DelayCloseCP.class.getName());
072    UTIL.startMiniCluster(3);
073    UTIL.getAdmin().balancerSwitch(false, true);
074    UTIL.createTable(TABLE_NAME, CF);
075    UTIL.waitTableAvailable(TABLE_NAME);
076  }
077
078  @AfterClass
079  public static void tearDown() throws Exception {
080    UTIL.shutdownMiniCluster();
081  }
082
083  @Test
084  public void test() throws Exception {
085    JVMClusterUtil.RegionServerThread rsThread = null;
086    for (JVMClusterUtil.RegionServerThread t : UTIL.getMiniHBaseCluster()
087        .getRegionServerThreads()) {
088      if (!t.getRegionServer().getRegions(TABLE_NAME).isEmpty()) {
089        rsThread = t;
090        break;
091      }
092    }
093    //find the rs and hri of the table
094    HRegionServer rs = rsThread.getRegionServer();
095    RegionInfo hri = rs.getRegions(TABLE_NAME).get(0).getRegionInfo();
096    MoveRegionProcedure moveRegionProcedure = new MoveRegionProcedure(
097      UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor()
098        .getEnvironment(),
099        new RegionPlan(hri, rs.getServerName(), rs.getServerName()), true);
100    long procID = UTIL.getMiniHBaseCluster().getMaster()
101      .getMasterProcedureExecutor().submitProcedure(moveRegionProcedure);
102    countDownLatch.await();
103    UTIL.getMiniHBaseCluster().stopMaster(0);
104    UTIL.getMiniHBaseCluster().startMaster();
105    //wait until master initialized
106    UTIL.waitFor(30000,
107      () -> UTIL.getMiniHBaseCluster().getMaster() != null && UTIL
108        .getMiniHBaseCluster().getMaster().isInitialized());
109    Assert.assertTrue("Should be 3 RS after master restart",
110        UTIL.getMiniHBaseCluster().getLiveRegionServerThreads().size() == 3);
111
112  }
113
114  public static class DelayCloseCP implements RegionCoprocessor,
115      RegionObserver {
116    @Override
117    public void preClose(ObserverContext<RegionCoprocessorEnvironment> c,
118        boolean abortRequested) throws IOException {
119      try {
120        if (!c.getEnvironment().getRegion().getRegionInfo().getTable().isSystemTable()) {
121          LOG.error("begin to sleep");
122          countDownLatch.countDown();
123          //Sleep here so we can stuck the RPC call
124          Thread.sleep(10000);
125          LOG.error("finish sleep");
126        }
127      } catch (Throwable t) {
128
129      }
130    }
131
132    @Override
133    public Optional<RegionObserver> getRegionObserver() {
134      return Optional.of(this);
135    }
136  }
137
138}