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 org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.hbase.*;
28  import org.apache.hadoop.hbase.Waiter.Predicate;
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.Assert;
36  import org.junit.Test;
37  import org.junit.experimental.categories.Category;
38  
39  import static org.junit.Assert.*;
40  
41  /**
42   * Tests unhandled exceptions thrown by coprocessors running on a regionserver..
43   * Expected result is that the regionserver will abort with an informative
44   * error message describing the set of its loaded coprocessors for crash
45   * diagnosis. (HBASE-4014).
46   */
47  @Category(MediumTests.class)
48  public class TestRegionServerCoprocessorExceptionWithAbort {
49    static final Log LOG = LogFactory.getLog(TestRegionServerCoprocessorExceptionWithAbort.class);
50    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
51    private static final TableName TABLE_NAME = TableName.valueOf("observed_table");
52  
53    @Test(timeout=60000)
54    public void testExceptionDuringInitialization() throws Exception {
55      Configuration conf = TEST_UTIL.getConfiguration();
56      conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);  // Let's fail fast.
57      conf.setBoolean(CoprocessorHost.ABORT_ON_ERROR_KEY, true);
58      conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY, "");
59      TEST_UTIL.startMiniCluster(2);
60      try {
61        MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
62        // Trigger one regionserver to fail as if it came up with a coprocessor
63        // that fails during initialization
64        final HRegionServer regionServer = cluster.getRegionServer(0);
65        conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
66          FailedInitializationObserver.class.getName());
67        regionServer.getCoprocessorHost().loadSystemCoprocessors(conf,
68          CoprocessorHost.REGION_COPROCESSOR_CONF_KEY);
69        TEST_UTIL.waitFor(10000, 1000, new Predicate<Exception>() {
70          @Override
71          public boolean evaluate() throws Exception {
72            return regionServer.isAborted();
73          }
74        });
75      } finally {
76        TEST_UTIL.shutdownMiniCluster();
77      }
78    }
79  
80    @Test(timeout=60000)
81    public void testExceptionFromCoprocessorDuringPut() throws Exception {
82      // set configure to indicate which cp should be loaded
83      Configuration conf = TEST_UTIL.getConfiguration();
84      conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);  // Let's fail fast.
85      conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY, BuggyRegionObserver.class.getName());
86      conf.setBoolean(CoprocessorHost.ABORT_ON_ERROR_KEY, true);
87      TEST_UTIL.startMiniCluster(2);
88      try {
89        // When we try to write to TEST_TABLE, the buggy coprocessor will
90        // cause a NullPointerException, which will cause the regionserver (which
91        // hosts the region we attempted to write to) to abort.
92        final byte[] TEST_FAMILY = Bytes.toBytes("aaa");
93  
94        HTable table = TEST_UTIL.createTable(TABLE_NAME, TEST_FAMILY);
95        TEST_UTIL.createMultiRegions(table, TEST_FAMILY);
96        TEST_UTIL.waitUntilAllRegionsAssigned(TABLE_NAME);
97  
98        // Note which regionServer will abort (after put is attempted).
99        final HRegionServer regionServer = TEST_UTIL.getRSForFirstRegionInTable(TABLE_NAME);
100 
101       boolean threwIOE = false;
102       try {
103         final byte[] ROW = Bytes.toBytes("aaa");
104         Put put = new Put(ROW);
105         put.add(TEST_FAMILY, ROW, ROW);
106         table.put(put);
107         table.flushCommits();
108         // We may need two puts to reliably get an exception
109         table.put(put);
110         table.flushCommits();
111       } catch (IOException e) {
112         threwIOE = true;
113       } finally {
114         assertTrue("The regionserver should have thrown an exception", threwIOE);
115       }
116 
117       // Wait 10 seconds for the regionserver to abort: expected result is that
118       // it will abort.
119       boolean aborted = false;
120       for (int i = 0; i < 10; i++) {
121         aborted = regionServer.isAborted(); 
122         if (aborted) {
123           break;
124         }
125         try {
126           Thread.sleep(1000);
127         } catch (InterruptedException e) {
128           fail("InterruptedException while waiting for regionserver " +
129             "zk node to be deleted.");
130         }
131       }
132       Assert.assertTrue("The region server should have aborted", aborted);
133       table.close();
134     } finally {
135       TEST_UTIL.shutdownMiniCluster();
136     }
137   }
138 
139   public static class FailedInitializationObserver extends SimpleRegionObserver {
140     @SuppressWarnings("null")
141     @Override
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     @SuppressWarnings("null")
151     @Override
152     public void prePut(final ObserverContext<RegionCoprocessorEnvironment> c,
153                        final Put put, final WALEdit edit,
154                        final Durability durability) {
155       String tableName =
156           c.getEnvironment().getRegion().getRegionInfo().getTable().getNameAsString();
157       if (tableName.equals("observed_table")) {
158         // Trigger a NPE to fail the coprocessor
159         Integer i = null;
160         i = i + 1;
161       }
162     }
163   }
164 }