1   /*
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.coprocessor;
22  
23  import java.io.IOException;
24  
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.*;
27  import org.apache.hadoop.hbase.client.HTable;
28  import org.apache.hadoop.hbase.client.Put;
29  import org.apache.hadoop.hbase.regionserver.HRegionServer;
30  import org.apache.hadoop.hbase.util.Bytes;
31  import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
32  import org.junit.AfterClass;
33  import org.junit.BeforeClass;
34  import org.junit.Test;
35  
36  import static org.junit.Assert.*;
37  
38  /**
39   * Tests unhandled exceptions thrown by coprocessors running on a regionserver..
40   * Expected result is that the regionserver will abort with an informative
41   * error message describing the set of its loaded coprocessors for crash
42   * diagnosis. (HBASE-4014).
43   */
44  public class TestRegionServerCoprocessorExceptionWithAbort {
45    private static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
46  
47    @BeforeClass
48    public static void setupBeforeClass() throws Exception {
49      // set configure to indicate which cp should be loaded
50      Configuration conf = TEST_UTIL.getConfiguration();
51      conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
52          BuggyRegionObserver.class.getName());
53      conf.set("hbase.coprocessor.abortonerror", "true");
54      TEST_UTIL.startMiniCluster(2);
55    }
56  
57    @AfterClass
58    public static void teardownAfterClass() throws Exception {
59      TEST_UTIL.shutdownMiniCluster();
60    }
61  
62    @Test(timeout=30000)
63    public void testExceptionFromCoprocessorDuringPut()
64        throws IOException {
65      // When we try to write to TEST_TABLE, the buggy coprocessor will
66      // cause a NullPointerException, which will cause the regionserver (which
67      // hosts the region we attempted to write to) to abort.
68      byte[] TEST_TABLE = Bytes.toBytes("observed_table");
69      byte[] TEST_FAMILY = Bytes.toBytes("aaa");
70  
71      HTable table = TEST_UTIL.createTable(TEST_TABLE, TEST_FAMILY);
72      TEST_UTIL.waitUntilAllRegionsAssigned(
73          TEST_UTIL.createMultiRegions(table, TEST_FAMILY));
74  
75      // Note which regionServer will abort (after put is attempted).
76      HRegionServer regionServer =
77          TEST_UTIL.getRSForFirstRegionInTable(TEST_TABLE);
78      try {
79        final byte[] ROW = Bytes.toBytes("aaa");
80        Put put = new Put(ROW);
81        put.add(TEST_FAMILY, ROW, ROW);
82        table.put(put);
83      } catch (IOException e) {
84        fail("put() failed: " + e);
85      }
86      // Wait up to 30 seconds for regionserver to abort.
87      boolean regionServerAborted = false;
88      for (int i = 0; i < 30; i++) {
89        if (regionServer.isAborted()) {
90          regionServerAborted = true;
91          break;
92        }
93        try {
94          Thread.sleep(1000);
95        } catch (InterruptedException e) {
96          fail("InterruptedException while waiting for regionserver " +
97              "zk node to be deleted.");
98        }
99      }
100     assertTrue("RegionServer aborted on coprocessor exception, as expected.",
101         regionServerAborted);
102   }
103 
104     public static class BuggyRegionObserver extends SimpleRegionObserver {
105     @Override
106     public void prePut(final ObserverContext<RegionCoprocessorEnvironment> c,
107                        final Put put, final WALEdit edit,
108                        final boolean writeToWAL) {
109       String tableName =
110           c.getEnvironment().getRegion().getRegionInfo().getTableNameAsString();
111       if (tableName.equals("observed_table")) {
112         Integer i = null;
113         i = i + 1;
114       }
115     }
116   }
117 }