1   /*
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.hadoop.hbase.coprocessor;
22  
23  import java.io.IOException;
24  
25  import org.apache.hadoop.conf.Configuration;
26  import org.apache.hadoop.hbase.*;
27  import org.apache.hadoop.hbase.client.HTable;
28  import org.apache.hadoop.hbase.client.Put;
29  import org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException;
30  import org.apache.hadoop.hbase.regionserver.HRegionServer;
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 master will remove the buggy coprocessor from
43   * its set of coprocessors and throw a org.apache.hadoop.hbase.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 boolean writeToWAL) {
55        String tableName =
56            c.getEnvironment().getRegion().getRegionInfo().getTableNameAsString();
57        if (tableName.equals("observed_table")) {
58          Integer i = null;
59          i = i + 1;
60        }
61      }
62    }
63  
64    private static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
65  
66    @BeforeClass
67    public static void setupBeforeClass() throws Exception {
68      // set configure to indicate which cp should be loaded
69      Configuration conf = TEST_UTIL.getConfiguration();
70      conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
71          BuggyRegionObserver.class.getName());
72      TEST_UTIL.startMiniCluster(2);
73    }
74  
75    @AfterClass
76    public static void teardownAfterClass() throws Exception {
77      TEST_UTIL.shutdownMiniCluster();
78    }
79  
80    @Test(timeout=30000)
81    public void testExceptionFromCoprocessorDuringPut()
82        throws IOException {
83      // Set watches on the zookeeper nodes for all of the regionservers in the
84      // cluster. When we try to write to TEST_TABLE, the buggy coprocessor will
85      // cause a NullPointerException, which will cause the regionserver (which
86      // hosts the region we attempted to write to) to abort. In turn, this will
87      // cause the nodeDeleted() method of the DeadRegionServer tracker to
88      // execute, which will set the rsZKNodeDeleted flag to true, which will
89      // pass this test.
90  
91      byte[] TEST_TABLE = Bytes.toBytes("observed_table");
92      byte[] TEST_FAMILY = Bytes.toBytes("aaa");
93  
94      HTable table = TEST_UTIL.createTable(TEST_TABLE, TEST_FAMILY);
95      TEST_UTIL.waitUntilAllRegionsAssigned(
96          TEST_UTIL.createMultiRegions(table, TEST_FAMILY));
97      // Note which regionServer that should survive the buggy coprocessor's
98      // prePut().
99      HRegionServer regionServer =
100         TEST_UTIL.getRSForFirstRegionInTable(TEST_TABLE);
101 
102     // same logic as {@link TestMasterCoprocessorExceptionWithRemove},
103     // but exception will be RetriesExhaustedWithDetailException rather
104     // than DoNotRetryIOException. The latter exception is what the RegionServer
105     // will have actually thrown, but the client will wrap this in a
106     // RetriesExhaustedWithDetailException.
107     // We will verify that "DoNotRetryIOException" appears in the text of the
108     // the exception's detailMessage.
109     boolean threwDNRE = false;
110     try {
111       final byte[] ROW = Bytes.toBytes("aaa");
112       Put put = new Put(ROW);
113       put.add(TEST_FAMILY, ROW, ROW);
114       table.put(put);
115     } catch (RetriesExhaustedWithDetailsException e) {
116       // below, could call instead :
117       // startsWith("Failed 1 action: DoNotRetryIOException.")
118       // But that might be too brittle if client-side
119       // DoNotRetryIOException-handler changes its message.
120       assertTrue(e.getMessage().contains("DoNotRetryIOException"));
121       threwDNRE = true;
122     } finally {
123       assertTrue(threwDNRE);
124     }
125 
126     // Wait 3 seconds for the regionserver to abort: expected result is that
127     // it will survive and not abort.
128     for (int i = 0; i < 3; i++) {
129       assertFalse(regionServer.isAborted());
130       try {
131         Thread.sleep(1000);
132       } catch (InterruptedException e) {
133         fail("InterruptedException while waiting for regionserver " +
134             "zk node to be deleted.");
135       }
136     }
137     table.close();
138   }
139 
140   @org.junit.Rule
141   public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
142     new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();
143 }
144