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.HBaseTestingUtil;
030import org.apache.hadoop.hbase.StartTestingClusterOption;
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.testclassification.MediumTests;
042import org.apache.hadoop.hbase.testclassification.RegionServerTests;
043import org.apache.hadoop.hbase.util.Bytes;
044import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
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 HBaseTestingUtil UTIL = new HBaseTestingUtil();
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    StartTestingClusterOption option =
084      StartTestingClusterOption.builder().numRegionServers(2).build();
085    UTIL.startMiniCluster(option);
086    TableDescriptor td = TableDescriptorBuilder.newBuilder(TABLE_NAME)
087      .setCoprocessor(SleepWhenCloseCoprocessor.class.getName())
088      .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(CF).build()).build();
089    UTIL.getAdmin().createTable(td, Bytes.toBytes("0"), Bytes.toBytes("9"), REGIONS_NUM);
090  }
091
092  @AfterClass
093  public static void tearDown() throws Exception {
094    UTIL.shutdownMiniCluster();
095  }
096
097  @Test
098  public void testAbortTimeout() throws Exception {
099    Thread writer = new Thread(() -> {
100      try {
101        try (Table table = UTIL.getConnection().getTable(TABLE_NAME)) {
102          for (int i = 0; i < 10000; i++) {
103            table.put(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, Bytes.toBytes(i)));
104          }
105        }
106      } catch (IOException e) {
107        LOG.warn("Failed to load data");
108      }
109    });
110    writer.setDaemon(true);
111    writer.start();
112
113    // Abort one region server
114    UTIL.getMiniHBaseCluster().getRegionServer(0).abort("Abort RS for test");
115
116    long startTime = EnvironmentEdgeManager.currentTime();
117    long timeout = REGIONS_NUM * SLEEP_TIME_WHEN_CLOSE_REGION * 10;
118    while (EnvironmentEdgeManager.currentTime() - startTime < timeout) {
119      if (UTIL.getMiniHBaseCluster().getLiveRegionServerThreads().size() == 1) {
120        assertTrue("Abort timer task should be scheduled", abortTimeoutTaskScheduled);
121        return;
122      }
123      Threads.sleep(SLEEP_TIME_WHEN_CLOSE_REGION);
124    }
125    fail("Failed to abort a region server in " + timeout + " ms");
126  }
127
128  static class TestAbortTimeoutTask extends TimerTask {
129
130    public TestAbortTimeoutTask() {
131    }
132
133    @Override
134    public void run() {
135      LOG.info("TestAbortTimeoutTask was scheduled");
136      abortTimeoutTaskScheduled = true;
137    }
138  }
139
140  public static class SleepWhenCloseCoprocessor implements RegionCoprocessor, RegionObserver {
141
142    public SleepWhenCloseCoprocessor() {
143    }
144
145    @Override
146    public Optional<RegionObserver> getRegionObserver() {
147      return Optional.of(this);
148    }
149
150    @Override
151    public void preClose(ObserverContext<RegionCoprocessorEnvironment> c, boolean abortRequested)
152        throws IOException {
153      Threads.sleep(SLEEP_TIME_WHEN_CLOSE_REGION);
154    }
155  }
156}