View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.regionserver;
19  
20  import static org.junit.Assert.assertTrue;
21  import static org.junit.Assert.fail;
22  
23  import java.io.IOException;
24  import java.util.TimerTask;
25  
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.hbase.HBaseTestingUtility;
28  import org.apache.hadoop.hbase.HColumnDescriptor;
29  import org.apache.hadoop.hbase.HTableDescriptor;
30  import org.apache.hadoop.hbase.TableName;
31  import org.apache.hadoop.hbase.client.Put;
32  import org.apache.hadoop.hbase.client.Table;
33  import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
34  import org.apache.hadoop.hbase.coprocessor.ObserverContext;
35  import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
36  import org.apache.hadoop.hbase.testclassification.MediumTests;
37  import org.apache.hadoop.hbase.testclassification.RegionServerTests;
38  import org.apache.hadoop.hbase.util.Bytes;
39  import org.apache.hadoop.hbase.util.Threads;
40  import org.junit.AfterClass;
41  import org.junit.BeforeClass;
42  import org.junit.Test;
43  import org.junit.experimental.categories.Category;
44  import org.slf4j.Logger;
45  import org.slf4j.LoggerFactory;
46  
47  @Category({ RegionServerTests.class, MediumTests.class })
48  public class TestRegionServerAbortTimeout {
49    private static final Logger LOG = LoggerFactory.getLogger(TestRegionServerAbortTimeout.class);
50  
51    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
52  
53    private static TableName TABLE_NAME = TableName.valueOf("RSAbort");
54  
55    private static byte[] CF = Bytes.toBytes("cf");
56  
57    private static byte[] CQ = Bytes.toBytes("cq");
58  
59    private static final int REGIONS_NUM = 5;
60  
61    private static final int SLEEP_TIME_WHEN_CLOSE_REGION = 1000;
62  
63    private static volatile boolean abortTimeoutTaskScheduled = false;
64  
65    @BeforeClass
66    public static void setUp() throws Exception {
67      Configuration conf = UTIL.getConfiguration();
68      // Will schedule a abort timeout task after SLEEP_TIME_WHEN_CLOSE_REGION ms
69      conf.setLong(HRegionServer.ABORT_TIMEOUT, SLEEP_TIME_WHEN_CLOSE_REGION);
70      conf.set(HRegionServer.ABORT_TIMEOUT_TASK, TestAbortTimeoutTask.class.getName());
71      UTIL.startMiniCluster(2);
72      HTableDescriptor td = new HTableDescriptor(TABLE_NAME);
73      td.addCoprocessor(SleepWhenCloseCoprocessor.class.getName());
74      td.addFamily(new HColumnDescriptor(CF));
75      UTIL.getHBaseAdmin().createTable(td, Bytes.toBytes("0"), Bytes.toBytes("9"), REGIONS_NUM);
76    }
77  
78    @AfterClass
79    public static void tearDown() throws Exception {
80      UTIL.getHBaseAdmin().disableTable(TABLE_NAME);
81      UTIL.getHBaseAdmin().deleteTable(TABLE_NAME);
82      UTIL.shutdownMiniCluster();
83    }
84  
85    @Test
86    public void testAbortTimeout() throws Exception {
87      Thread writer = new Thread() {
88        public void run() {
89          try {
90            try (Table table = UTIL.getConnection().getTable(TABLE_NAME)) {
91              for (int i = 0; i < 10000; i++) {
92                table.put(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, Bytes.toBytes(i)));
93              }
94            }
95          } catch (IOException e) {
96            LOG.warn("Failed to load data");
97          }
98        }
99      };
100     writer.setDaemon(true);
101     writer.start();
102 
103     // Abort one region server
104     UTIL.getMiniHBaseCluster().getRegionServer(0).abort("Abort RS for test");
105 
106     long startTime = System.currentTimeMillis();
107     long timeout = REGIONS_NUM * SLEEP_TIME_WHEN_CLOSE_REGION * 10;
108     while (System.currentTimeMillis() - startTime < timeout) {
109       if (UTIL.getMiniHBaseCluster().getLiveRegionServerThreads().size() == 1) {
110         assertTrue("Abort timer task should be scheduled", abortTimeoutTaskScheduled);
111         return;
112       }
113       Threads.sleep(SLEEP_TIME_WHEN_CLOSE_REGION);
114     }
115     fail("Failed to abort a region server in " + timeout + " ms");
116   }
117 
118   static class TestAbortTimeoutTask extends TimerTask {
119 
120     public TestAbortTimeoutTask() {
121     }
122 
123     @Override
124     public void run() {
125       LOG.info("TestAbortTimeoutTask was scheduled");
126       abortTimeoutTaskScheduled = true;
127     }
128   }
129 
130   public static class SleepWhenCloseCoprocessor extends BaseRegionObserver {
131     @Override
132     public void preClose(ObserverContext<RegionCoprocessorEnvironment> c, boolean abortRequested)
133         throws IOException {
134       Threads.sleep(SLEEP_TIME_WHEN_CLOSE_REGION);
135     }
136   }
137 }