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.handler;
19  
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.io.IOException;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.hbase.HBaseTestingUtility;
29  import org.apache.hadoop.hbase.HConstants;
30  import org.apache.hadoop.hbase.HRegionInfo;
31  import org.apache.hadoop.hbase.HTableDescriptor;
32  import org.apache.hadoop.hbase.Server;
33  import org.apache.hadoop.hbase.regionserver.HRegion;
34  import org.apache.hadoop.hbase.regionserver.RegionServerServices;
35  import org.apache.hadoop.hbase.util.MockRegionServerServices;
36  import org.apache.hadoop.hbase.util.MockServer;
37  import org.apache.zookeeper.KeeperException;
38  import org.apache.zookeeper.KeeperException.NodeExistsException;
39  import org.junit.Test;
40  import org.mockito.Mockito;
41  
42  /**
43   * Test of the {@link CloseRegionHandler}.
44   */
45  public class TestCloseRegionHandler {
46    static final Log LOG = LogFactory.getLog(TestCloseRegionHandler.class);
47    private final static HBaseTestingUtility HTU = new HBaseTestingUtility();
48  
49    /**
50     * Test that if we fail a flush, abort gets set on close.
51     * @see <a href="https://issues.apache.org/jira/browse/HBASE-4270">HBASE-4270</a>
52     * @throws IOException
53     * @throws NodeExistsException
54     * @throws KeeperException
55     */
56    @Test public void testFailedFlushAborts()
57    throws IOException, NodeExistsException, KeeperException {
58      final Server server = new MockServer(HTU, false);
59      final RegionServerServices rss = new MockRegionServerServices();
60      HTableDescriptor htd = new HTableDescriptor("testFailedFlushAborts");
61      final HRegionInfo hri =
62        new HRegionInfo(htd.getName(), HConstants.EMPTY_END_ROW,
63          HConstants.EMPTY_END_ROW);
64      HRegion region =
65        HRegion.createHRegion(hri, HTU.getDataTestDir(),
66          HTU.getConfiguration(), htd);
67      assertNotNull(region);
68      // Spy on the region so can throw exception when close is called.
69      HRegion spy = Mockito.spy(region);
70      final boolean abort = false;
71      Mockito.when(spy.close(abort)).
72        thenThrow(new RuntimeException("Mocked failed close!"));
73      // The CloseRegionHandler will try to get an HRegion that corresponds
74      // to the passed hri -- so insert the region into the online region Set.
75      rss.addToOnlineRegions(spy);
76      // Assert the Server is NOT stopped before we call close region.
77      assertFalse(server.isStopped());
78      CloseRegionHandler handler =
79        new CloseRegionHandler(server, rss, hri, false, false);
80      boolean throwable = false;
81      try {
82        handler.process();
83      } catch (Throwable t) {
84        throwable = true;
85      } finally {
86        assertTrue(throwable);
87        // Abort calls stop so stopped flag should be set.
88        assertTrue(server.isStopped());
89      }
90    }
91  }