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