View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import java.io.IOException;
22  import java.util.concurrent.atomic.AtomicBoolean;
23  
24  import org.apache.hadoop.hbase.MediumTests;
25  import org.apache.hadoop.hbase.RegionTooBusyException;
26  import org.apache.hadoop.hbase.client.Get;
27  import org.apache.hadoop.hbase.util.Bytes;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  /**
32   * TestHRegion with hbase.busy.wait.duration set to 1000 (1 second).
33   * We can't use parameterized test since TestHRegion is old fashion.
34   */
35  @Category(MediumTests.class)
36  @SuppressWarnings("deprecation")
37  public class TestHRegionBusyWait extends TestHRegion {
38    // TODO: This subclass runs all the tests in TestHRegion as well as the test below which means
39    // all TestHRegion tests are run twice.
40    public TestHRegionBusyWait() {
41      conf.set("hbase.busy.wait.duration", "1000");
42    }
43  
44    /**
45     * Test RegionTooBusyException thrown when region is busy
46     */
47    @Test (timeout=2000)
48    public void testRegionTooBusy() throws IOException {
49      String method = "testRegionTooBusy";
50      byte[] tableName = Bytes.toBytes(method);
51      byte[] family = Bytes.toBytes("family");
52      region = initHRegion(tableName, method, conf, family);
53      final AtomicBoolean stopped = new AtomicBoolean(true);
54      Thread t = new Thread(new Runnable() {
55        @Override
56        public void run() {
57          try {
58            region.lock.writeLock().lock();
59            stopped.set(false);
60            while (!stopped.get()) {
61              Thread.sleep(100);
62            }
63          } catch (InterruptedException ie) {
64          } finally {
65            region.lock.writeLock().unlock();
66          }
67        }
68      });
69      t.start();
70      Get get = new Get(row);
71      try {
72        while (stopped.get()) {
73          Thread.sleep(100);
74        }
75        region.get(get);
76        fail("Should throw RegionTooBusyException");
77      } catch (InterruptedException ie) {
78        fail("test interrupted");
79      } catch (RegionTooBusyException e) {
80        // Good, expected
81      } finally {
82        stopped.set(true);
83        try {
84          t.join();
85        } catch (Throwable e) {
86        }
87  
88        HRegion.closeHRegion(region);
89        region = null;
90      }
91    }
92  }