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