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 static org.junit.Assert.fail;
22  
23  import java.io.IOException;
24  import java.util.concurrent.atomic.AtomicBoolean;
25  
26  import org.apache.hadoop.hbase.MediumTests;
27  import org.apache.hadoop.hbase.RegionTooBusyException;
28  import org.apache.hadoop.hbase.client.Get;
29  import org.apache.hadoop.hbase.util.Bytes;
30  import org.junit.Before;
31  import org.junit.Test;
32  import org.junit.experimental.categories.Category;
33  
34  /**
35   * TestHRegion with hbase.busy.wait.duration set to 1000 (1 second).
36   * We can't use parameterized test since TestHRegion is old fashion.
37   */
38  @Category(MediumTests.class)
39  public class TestHRegionBusyWait extends TestHRegion {
40    // TODO: This subclass runs all the tests in TestHRegion as well as the test below which means
41    // all TestHRegion tests are run twice.
42    @Before
43    public void setup() throws IOException {
44      super.setup();
45      CONF.set("hbase.busy.wait.duration", "1000");
46    }
47  
48    /**
49     * Test RegionTooBusyException thrown when region is busy
50     */
51    @Test (timeout=6000)
52    public void testRegionTooBusy() throws IOException {
53      String method = "testRegionTooBusy";
54      byte[] tableName = Bytes.toBytes(method);
55      byte[] family = Bytes.toBytes("family");
56      region = initHRegion(tableName, method, CONF, family);
57      final AtomicBoolean stopped = new AtomicBoolean(true);
58      Thread t = new Thread(new Runnable() {
59        @Override
60        public void run() {
61          try {
62            region.lock.writeLock().lock();
63            stopped.set(false);
64            while (!stopped.get()) {
65              Thread.sleep(100);
66            }
67          } catch (InterruptedException ie) {
68          } finally {
69            region.lock.writeLock().unlock();
70          }
71        }
72      });
73      t.start();
74      Get get = new Get(row);
75      try {
76        while (stopped.get()) {
77          Thread.sleep(100);
78        }
79        region.get(get);
80        fail("Should throw RegionTooBusyException");
81      } catch (InterruptedException ie) {
82        fail("test interrupted");
83      } catch (RegionTooBusyException e) {
84        // Good, expected
85      } finally {
86        stopped.set(true);
87        try {
88          t.join();
89        } catch (Throwable e) {
90        }
91  
92        HRegion.closeHRegion(region);
93        region = null;
94      }
95    }
96  }