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