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.regionserver;
019
020import static org.junit.Assert.assertTrue;
021import static org.junit.Assert.fail;
022
023import java.io.IOException;
024import java.util.Optional;
025import java.util.TimerTask;
026
027import org.apache.hadoop.conf.Configuration;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HBaseTestingUtility;
030import org.apache.hadoop.hbase.StartMiniClusterOption;
031import org.apache.hadoop.hbase.TableName;
032import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
033import org.apache.hadoop.hbase.client.Put;
034import org.apache.hadoop.hbase.client.Table;
035import org.apache.hadoop.hbase.client.TableDescriptor;
036import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
037import org.apache.hadoop.hbase.coprocessor.ObserverContext;
038import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor;
039import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
040import org.apache.hadoop.hbase.coprocessor.RegionObserver;
041import org.apache.hadoop.hbase.master.procedure.ServerCrashProcedure;
042import org.apache.hadoop.hbase.testclassification.MediumTests;
043import org.apache.hadoop.hbase.testclassification.RegionServerTests;
044import org.apache.hadoop.hbase.util.Bytes;
045import org.apache.hadoop.hbase.util.Threads;
046import org.junit.AfterClass;
047import org.junit.BeforeClass;
048import org.junit.ClassRule;
049import org.junit.Test;
050import org.junit.experimental.categories.Category;
051import org.slf4j.Logger;
052import org.slf4j.LoggerFactory;
053
054@Category({ RegionServerTests.class, MediumTests.class })
055public class TestRegionServerAbortTimeout {
056
057  @ClassRule
058  public static final HBaseClassTestRule CLASS_RULE =
059      HBaseClassTestRule.forClass(TestRegionServerAbortTimeout.class);
060
061  private static final Logger LOG = LoggerFactory.getLogger(TestRegionServerAbortTimeout.class);
062
063  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
064
065  private static TableName TABLE_NAME = TableName.valueOf("RSAbort");
066
067  private static byte[] CF = Bytes.toBytes("cf");
068
069  private static byte[] CQ = Bytes.toBytes("cq");
070
071  private static final int REGIONS_NUM = 5;
072
073  private static final int SLEEP_TIME_WHEN_CLOSE_REGION = 1000;
074
075  private static volatile boolean abortTimeoutTaskScheduled = false;
076
077  @BeforeClass
078  public static void setUp() throws Exception {
079    Configuration conf = UTIL.getConfiguration();
080    // Will schedule a abort timeout task after SLEEP_TIME_WHEN_CLOSE_REGION ms
081    conf.setLong(HRegionServer.ABORT_TIMEOUT, SLEEP_TIME_WHEN_CLOSE_REGION);
082    conf.set(HRegionServer.ABORT_TIMEOUT_TASK, TestAbortTimeoutTask.class.getName());
083    StartMiniClusterOption option = StartMiniClusterOption.builder().numRegionServers(2).build();
084    UTIL.startMiniCluster(option);
085    TableDescriptor td = TableDescriptorBuilder.newBuilder(TABLE_NAME)
086        .setCoprocessor(SleepWhenCloseCoprocessor.class.getName())
087        .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(CF).build()).build();
088    UTIL.getAdmin().createTable(td, Bytes.toBytes("0"), Bytes.toBytes("9"), REGIONS_NUM);
089  }
090
091  @AfterClass
092  public static void tearDown() throws Exception {
093    // Wait the SCP of abort rs to finish
094    UTIL.waitFor(30000, () -> UTIL.getMiniHBaseCluster().getMaster().getProcedures().stream()
095        .filter(p -> p instanceof ServerCrashProcedure && p.isFinished()).count() > 0);
096    UTIL.getAdmin().disableTable(TABLE_NAME);
097    UTIL.getAdmin().deleteTable(TABLE_NAME);
098    UTIL.shutdownMiniCluster();
099  }
100
101  @Test
102  public void testAbortTimeout() throws Exception {
103    Thread writer = new Thread(() -> {
104      try {
105        try (Table table = UTIL.getConnection().getTable(TABLE_NAME)) {
106          for (int i = 0; i < 10000; i++) {
107            table.put(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, Bytes.toBytes(i)));
108          }
109        }
110      } catch (IOException e) {
111        LOG.warn("Failed to load data");
112      }
113    });
114    writer.setDaemon(true);
115    writer.start();
116
117    // Abort one region server
118    UTIL.getMiniHBaseCluster().getRegionServer(0).abort("Abort RS for test");
119
120    long startTime = System.currentTimeMillis();
121    long timeout = REGIONS_NUM * SLEEP_TIME_WHEN_CLOSE_REGION * 10;
122    while (System.currentTimeMillis() - startTime < timeout) {
123      if (UTIL.getMiniHBaseCluster().getLiveRegionServerThreads().size() == 1) {
124        assertTrue("Abort timer task should be scheduled", abortTimeoutTaskScheduled);
125        return;
126      }
127      Threads.sleep(SLEEP_TIME_WHEN_CLOSE_REGION);
128    }
129    fail("Failed to abort a region server in " + timeout + " ms");
130  }
131
132  static class TestAbortTimeoutTask extends TimerTask {
133
134    public TestAbortTimeoutTask() {
135    }
136
137    @Override
138    public void run() {
139      LOG.info("TestAbortTimeoutTask was scheduled");
140      abortTimeoutTaskScheduled = true;
141    }
142  }
143
144  public static class SleepWhenCloseCoprocessor implements RegionCoprocessor, RegionObserver {
145
146    public SleepWhenCloseCoprocessor() {
147    }
148
149    @Override
150    public Optional<RegionObserver> getRegionObserver() {
151      return Optional.of(this);
152    }
153
154    @Override
155    public void preClose(ObserverContext<RegionCoprocessorEnvironment> c, boolean abortRequested)
156        throws IOException {
157      Threads.sleep(SLEEP_TIME_WHEN_CLOSE_REGION);
158    }
159  }
160}