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 static org.junit.Assert.fail;
23  
24  import java.io.IOException;
25  
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.hbase.CoprocessorEnvironment;
28  import org.apache.hadoop.hbase.HBaseTestingUtility;
29  import org.apache.hadoop.hbase.HConstants;
30  import org.apache.hadoop.hbase.MiniHBaseCluster;
31  import org.apache.hadoop.hbase.TableName;
32  import org.apache.hadoop.hbase.Waiter.Predicate;
33  import org.apache.hadoop.hbase.client.Durability;
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.testclassification.MediumTests;
39  import org.apache.hadoop.hbase.util.Bytes;
40  import org.junit.Assert;
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  
53    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
54    private static final TableName TABLE_NAME = TableName.valueOf("observed_table");
55  
56    @Test(timeout=60000)
57    public void testExceptionDuringInitialization() throws Exception {
58      Configuration conf = TEST_UTIL.getConfiguration();
59      conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);  // Let's fail fast.
60      conf.setBoolean(CoprocessorHost.ABORT_ON_ERROR_KEY, true);
61      conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY, "");
62      TEST_UTIL.startMiniCluster(2);
63      try {
64        MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
65        // Trigger one regionserver to fail as if it came up with a coprocessor
66        // that fails during initialization
67        final HRegionServer regionServer = cluster.getRegionServer(0);
68        conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
69          FailedInitializationObserver.class.getName());
70        regionServer.getRegionServerCoprocessorHost().loadSystemCoprocessors(conf,
71          CoprocessorHost.REGION_COPROCESSOR_CONF_KEY);
72        TEST_UTIL.waitFor(10000, 1000, new Predicate<Exception>() {
73          @Override
74          public boolean evaluate() throws Exception {
75            return regionServer.isAborted();
76          }
77        });
78      } finally {
79        TEST_UTIL.shutdownMiniCluster();
80      }
81    }
82  
83    @Test(timeout=60000)
84    public void testExceptionFromCoprocessorDuringPut() throws Exception {
85      // set configure to indicate which cp should be loaded
86      Configuration conf = TEST_UTIL.getConfiguration();
87      conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);  // Let's fail fast.
88      conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY, BuggyRegionObserver.class.getName());
89      conf.setBoolean(CoprocessorHost.ABORT_ON_ERROR_KEY, true);
90      TEST_UTIL.startMiniCluster(2);
91      try {
92        // When we try to write to TEST_TABLE, the buggy coprocessor will
93        // cause a NullPointerException, which will cause the regionserver (which
94        // hosts the region we attempted to write to) to abort.
95        final byte[] TEST_FAMILY = Bytes.toBytes("aaa");
96  
97        HTable table = TEST_UTIL.createMultiRegionTable(TABLE_NAME, TEST_FAMILY);
98        TEST_UTIL.waitUntilAllRegionsAssigned(TABLE_NAME);
99  
100       // Note which regionServer will abort (after put is attempted).
101       final HRegionServer regionServer = TEST_UTIL.getRSForFirstRegionInTable(TABLE_NAME);
102 
103       try {
104         final byte[] ROW = Bytes.toBytes("aaa");
105         Put put = new Put(ROW);
106         put.add(TEST_FAMILY, ROW, ROW);
107         table.put(put);
108         table.flushCommits();
109       } catch (IOException e) {
110         // The region server is going to be aborted.
111         // We may get an exception if we retry,
112         // which is not guaranteed.
113       }
114 
115       // Wait 10 seconds for the regionserver to abort: expected result is that
116       // it will abort.
117       boolean aborted = false;
118       for (int i = 0; i < 10; i++) {
119         aborted = regionServer.isAborted(); 
120         if (aborted) {
121           break;
122         }
123         try {
124           Thread.sleep(1000);
125         } catch (InterruptedException e) {
126           fail("InterruptedException while waiting for regionserver " +
127             "zk node to be deleted.");
128         }
129       }
130       Assert.assertTrue("The region server should have aborted", aborted);
131       table.close();
132     } finally {
133       TEST_UTIL.shutdownMiniCluster();
134     }
135   }
136 
137   public static class FailedInitializationObserver extends SimpleRegionObserver {
138     @Override
139     @SuppressWarnings("null")
140     @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="NP_NULL_ON_SOME_PATH",
141       justification="Preconditions checks insure we are not going to dereference a null value")
142     public void start(CoprocessorEnvironment e) throws IOException {
143       // Trigger a NPE to fail the coprocessor
144       Integer i = null;
145       i = i + 1;
146     }
147   }
148 
149   public static class BuggyRegionObserver extends SimpleRegionObserver {
150     @Override
151     @SuppressWarnings("null")
152     @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="NP_NULL_ON_SOME_PATH",
153       justification="Preconditions checks insure we are not going to dereference a null value")
154     public void prePut(final ObserverContext<RegionCoprocessorEnvironment> c,
155                        final Put put, final WALEdit edit,
156                        final Durability durability) {
157       String tableName =
158           c.getEnvironment().getRegion().getRegionInfo().getTable().getNameAsString();
159       if (tableName.equals("observed_table")) {
160         // Trigger a NPE to fail the coprocessor
161         Integer i = null;
162         i = i + 1;
163       }
164     }
165   }
166 }