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.assertFalse;
23  import static org.junit.Assert.assertTrue;
24  import static org.junit.Assert.fail;
25  
26  import java.io.IOException;
27  
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.hbase.HBaseTestingUtility;
30  import org.apache.hadoop.hbase.TableName;
31  import org.apache.hadoop.hbase.client.Durability;
32  import org.apache.hadoop.hbase.client.HTable;
33  import org.apache.hadoop.hbase.client.Put;
34  import org.apache.hadoop.hbase.regionserver.HRegionServer;
35  import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
36  import org.apache.hadoop.hbase.testclassification.MediumTests;
37  import org.apache.hadoop.hbase.util.Bytes;
38  import org.junit.AfterClass;
39  import org.junit.BeforeClass;
40  import org.junit.Test;
41  import org.junit.experimental.categories.Category;
42  
43  /**
44   * Tests unhandled exceptions thrown by coprocessors running on regionserver.
45   * Expected result is that the region server will remove the buggy coprocessor from
46   * its set of coprocessors and throw a org.apache.hadoop.hbase.exceptions.DoNotRetryIOException
47   * back to the client.
48   * (HBASE-4014).
49   */
50  @Category(MediumTests.class)
51  public class TestRegionServerCoprocessorExceptionWithRemove {
52    public static class BuggyRegionObserver extends SimpleRegionObserver {
53      @Override
54      @SuppressWarnings("null")
55      @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="NP_NULL_ON_SOME_PATH",
56        justification="Preconditions checks insure we are not going to dereference a null value")
57      public void prePut(final ObserverContext<RegionCoprocessorEnvironment> c,
58                         final Put put, final WALEdit edit,
59                         final Durability durability) {
60        String tableName =
61            c.getEnvironment().getRegion().getRegionInfo().getTable().getNameAsString();
62        if (tableName.equals("observed_table")) {
63          // Trigger a NPE to fail the coprocessor
64          Integer i = null;
65          i = i + 1;
66        }
67      }
68    }
69  
70    private static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
71  
72    @BeforeClass
73    public static void setupBeforeClass() throws Exception {
74      // set configure to indicate which cp should be loaded
75      Configuration conf = TEST_UTIL.getConfiguration();
76      conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
77          BuggyRegionObserver.class.getName());
78      TEST_UTIL.getConfiguration().setBoolean(CoprocessorHost.ABORT_ON_ERROR_KEY, false);
79      TEST_UTIL.startMiniCluster();
80    }
81  
82    @AfterClass
83    public static void teardownAfterClass() throws Exception {
84      TEST_UTIL.shutdownMiniCluster();
85    }
86  
87    @Test(timeout=60000)
88    public void testExceptionFromCoprocessorDuringPut()
89        throws IOException, InterruptedException {
90      // Set watches on the zookeeper nodes for all of the regionservers in the
91      // cluster. When we try to write to TEST_TABLE, the buggy coprocessor will
92      // cause a NullPointerException, which will cause the regionserver (which
93      // hosts the region we attempted to write to) to abort. In turn, this will
94      // cause the nodeDeleted() method of the DeadRegionServer tracker to
95      // execute, which will set the rsZKNodeDeleted flag to true, which will
96      // pass this test.
97  
98      TableName TEST_TABLE = TableName.valueOf("observed_table");
99      byte[] TEST_FAMILY = Bytes.toBytes("aaa");
100 
101     HTable table = TEST_UTIL.createMultiRegionTable(TEST_TABLE, TEST_FAMILY);
102     TEST_UTIL.waitUntilAllRegionsAssigned(TEST_TABLE);
103     // Note which regionServer that should survive the buggy coprocessor's
104     // prePut().
105     HRegionServer regionServer =
106         TEST_UTIL.getRSForFirstRegionInTable(TEST_TABLE);
107 
108     boolean threwIOE = false;
109     try {
110       final byte[] ROW = Bytes.toBytes("aaa");
111       Put put = new Put(ROW);
112       put.add(TEST_FAMILY, ROW, ROW);
113       table.put(put);
114       table.flushCommits();
115       // We may need two puts to reliably get an exception
116       table.put(put);
117       table.flushCommits();
118     } catch (IOException e) {
119       threwIOE = true;
120     } finally {
121       assertTrue("The regionserver should have thrown an exception", threwIOE);
122     }
123 
124     // Wait 10 seconds for the regionserver to abort: expected result is that
125     // it will survive and not abort.
126     for (int i = 0; i < 10; i++) {
127       assertFalse(regionServer.isAborted());
128       try {
129         Thread.sleep(1000);
130       } catch (InterruptedException e) {
131         fail("InterruptedException while waiting for regionserver " +
132             "zk node to be deleted.");
133       }
134     }
135     table.close();
136   }
137 
138 }
139