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