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