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      @SuppressWarnings("null")
54      @Override
55      public void prePut(final ObserverContext<RegionCoprocessorEnvironment> c,
56                         final Put put, final WALEdit edit,
57                         final Durability durability) {
58        String tableName =
59            c.getEnvironment().getRegion().getRegionInfo().getTable().getNameAsString();
60        if (tableName.equals("observed_table")) {
61          // Trigger a NPE to fail the coprocessor
62          Integer i = null;
63          i = i + 1;
64        }
65      }
66    }
67  
68    private static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
69  
70    @BeforeClass
71    public static void setupBeforeClass() throws Exception {
72      // set configure to indicate which cp should be loaded
73      Configuration conf = TEST_UTIL.getConfiguration();
74      conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
75          BuggyRegionObserver.class.getName());
76      TEST_UTIL.getConfiguration().setBoolean(CoprocessorHost.ABORT_ON_ERROR_KEY, false);
77      TEST_UTIL.startMiniCluster();
78    }
79  
80    @AfterClass
81    public static void teardownAfterClass() throws Exception {
82      TEST_UTIL.shutdownMiniCluster();
83    }
84  
85    @Test(timeout=60000)
86    public void testExceptionFromCoprocessorDuringPut()
87        throws IOException, InterruptedException {
88      // Set watches on the zookeeper nodes for all of the regionservers in the
89      // cluster. 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. In turn, this will
92      // cause the nodeDeleted() method of the DeadRegionServer tracker to
93      // execute, which will set the rsZKNodeDeleted flag to true, which will
94      // pass this test.
95  
96      TableName TEST_TABLE = TableName.valueOf("observed_table");
97      byte[] TEST_FAMILY = Bytes.toBytes("aaa");
98  
99      HTable table = TEST_UTIL.createMultiRegionTable(TEST_TABLE, TEST_FAMILY);
100     TEST_UTIL.waitUntilAllRegionsAssigned(TEST_TABLE);
101     // Note which regionServer that should survive the buggy coprocessor's
102     // prePut().
103     HRegionServer regionServer =
104         TEST_UTIL.getRSForFirstRegionInTable(TEST_TABLE);
105 
106     boolean threwIOE = false;
107     try {
108       final byte[] ROW = Bytes.toBytes("aaa");
109       Put put = new Put(ROW);
110       put.add(TEST_FAMILY, ROW, ROW);
111       table.put(put);
112       table.flushCommits();
113       // We may need two puts to reliably get an exception
114       table.put(put);
115       table.flushCommits();
116     } catch (IOException e) {
117       threwIOE = true;
118     } finally {
119       assertTrue("The regionserver should have thrown an exception", threwIOE);
120     }
121 
122     // Wait 10 seconds for the regionserver to abort: expected result is that
123     // it will survive and not abort.
124     for (int i = 0; i < 10; i++) {
125       assertFalse(regionServer.isAborted());
126       try {
127         Thread.sleep(1000);
128       } catch (InterruptedException e) {
129         fail("InterruptedException while waiting for regionserver " +
130             "zk node to be deleted.");
131       }
132     }
133     table.close();
134   }
135 
136 }
137