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  
20  package org.apache.hadoop.hbase.coprocessor;
21  
22  import static org.junit.Assert.fail;
23  
24  import java.io.IOException;
25  
26  import junit.framework.Assert;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.conf.Configuration;
31  import org.apache.hadoop.hbase.HBaseTestingUtility;
32  import org.apache.hadoop.hbase.HConstants;
33  import org.apache.hadoop.hbase.MediumTests;
34  import org.apache.hadoop.hbase.client.HTable;
35  import org.apache.hadoop.hbase.client.Put;
36  import org.apache.hadoop.hbase.regionserver.HRegionServer;
37  import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
38  import org.apache.hadoop.hbase.util.Bytes;
39  import org.junit.AfterClass;
40  import org.junit.BeforeClass;
41  import org.junit.Test;
42  import org.junit.experimental.categories.Category;
43  
44  /**
45   * Tests unhandled exceptions thrown by coprocessors running on a regionserver..
46   * Expected result is that the regionserver will abort with an informative
47   * error message describing the set of its loaded coprocessors for crash
48   * diagnosis. (HBASE-4014).
49   */
50  @Category(MediumTests.class)
51  public class TestRegionServerCoprocessorExceptionWithAbort {
52    static final Log LOG = LogFactory.getLog(TestRegionServerCoprocessorExceptionWithAbort.class);
53    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
54    private static final String TABLE_NAME = "observed_table";
55  
56    @BeforeClass
57    public static void setupBeforeClass() throws Exception {
58      // set configure to indicate which cp should be loaded
59      Configuration conf = TEST_UTIL.getConfiguration();
60      conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);  // Let's fail fast.
61      conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY, BuggyRegionObserver.class.getName());
62      conf.set("hbase.coprocessor.abortonerror", "true");
63      TEST_UTIL.startMiniCluster(3);
64    }
65  
66    @AfterClass
67    public static void teardownAfterClass() throws Exception {
68      TEST_UTIL.shutdownMiniCluster();
69    }
70  
71    @Test
72    public void testExceptionFromCoprocessorDuringPut()
73      throws IOException, InterruptedException {
74      // When we try to write to TEST_TABLE, the buggy coprocessor will
75      // cause a NullPointerException, which will cause the regionserver (which
76      // hosts the region we attempted to write to) to abort.
77      byte[] TEST_TABLE = Bytes.toBytes(TABLE_NAME);
78      byte[] TEST_FAMILY = Bytes.toBytes("aaa");
79  
80      HTable table = TEST_UTIL.createTable(TEST_TABLE, TEST_FAMILY);
81      TEST_UTIL.createMultiRegions(table, TEST_FAMILY);
82      TEST_UTIL.waitUntilAllRegionsAssigned(TEST_TABLE);
83  
84      // Note which regionServer will abort (after put is attempted).
85      final HRegionServer regionServer = TEST_UTIL.getRSForFirstRegionInTable(TEST_TABLE);
86  
87      final byte[] ROW = Bytes.toBytes("aaa");
88      Put put = new Put(ROW);
89      put.add(TEST_FAMILY, ROW, ROW);
90  
91      Assert.assertFalse("The region server should be available", regionServer.isAborted());
92      try {
93        table.put(put);
94        fail("The put should have failed, as the coprocessor is buggy");
95      } catch (IOException ignored) {
96        // Expected.
97      }
98      Assert.assertTrue("The region server should have aborted", regionServer.isAborted());
99      table.close();
100   }
101 
102   public static class BuggyRegionObserver extends SimpleRegionObserver {
103     @Override
104     public void prePut(final ObserverContext<RegionCoprocessorEnvironment> c,
105                        final Put put, final WALEdit edit,
106                        final boolean writeToWAL) {
107       String tableName = c.getEnvironment().getRegion().getRegionInfo().getTableNameAsString();
108       if (TABLE_NAME.equals(tableName)) {
109         throw new NullPointerException("Buggy coprocessor");
110       }
111     }
112   }
113 
114   @org.junit.Rule
115   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
116     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
117 }
118