1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.coprocessor;
21
22 import static org.junit.Assert.fail;
23
24 import java.io.IOException;
25
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.hbase.CoprocessorEnvironment;
28 import org.apache.hadoop.hbase.HBaseTestingUtility;
29 import org.apache.hadoop.hbase.HConstants;
30 import org.apache.hadoop.hbase.MiniHBaseCluster;
31 import org.apache.hadoop.hbase.TableName;
32 import org.apache.hadoop.hbase.Waiter.Predicate;
33 import org.apache.hadoop.hbase.client.Durability;
34 import org.apache.hadoop.hbase.client.HTable;
35 import org.apache.hadoop.hbase.client.Put;
36 import org.apache.hadoop.hbase.regionserver.HRegionServer;
37 import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
38 import org.apache.hadoop.hbase.testclassification.MediumTests;
39 import org.apache.hadoop.hbase.util.Bytes;
40 import org.junit.Assert;
41 import org.junit.Test;
42 import org.junit.experimental.categories.Category;
43
44
45
46
47
48
49
50 @Category(MediumTests.class)
51 public class TestRegionServerCoprocessorExceptionWithAbort {
52
53 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
54 private static final TableName TABLE_NAME = TableName.valueOf("observed_table");
55
56 @Test(timeout=60000)
57 public void testExceptionDuringInitialization() throws Exception {
58 Configuration conf = TEST_UTIL.getConfiguration();
59 conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);
60 conf.setBoolean(CoprocessorHost.ABORT_ON_ERROR_KEY, true);
61 conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY, "");
62 TEST_UTIL.startMiniCluster(2);
63 try {
64 MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
65
66
67 final HRegionServer regionServer = cluster.getRegionServer(0);
68 conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
69 FailedInitializationObserver.class.getName());
70 regionServer.getRegionServerCoprocessorHost().loadSystemCoprocessors(conf,
71 CoprocessorHost.REGION_COPROCESSOR_CONF_KEY);
72 TEST_UTIL.waitFor(10000, 1000, new Predicate<Exception>() {
73 @Override
74 public boolean evaluate() throws Exception {
75 return regionServer.isAborted();
76 }
77 });
78 } finally {
79 TEST_UTIL.shutdownMiniCluster();
80 }
81 }
82
83 @Test(timeout=60000)
84 public void testExceptionFromCoprocessorDuringPut() throws Exception {
85
86 Configuration conf = TEST_UTIL.getConfiguration();
87 conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);
88 conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY, BuggyRegionObserver.class.getName());
89 conf.setBoolean(CoprocessorHost.ABORT_ON_ERROR_KEY, true);
90 TEST_UTIL.startMiniCluster(2);
91 try {
92
93
94
95 final byte[] TEST_FAMILY = Bytes.toBytes("aaa");
96
97 HTable table = TEST_UTIL.createMultiRegionTable(TABLE_NAME, TEST_FAMILY);
98 TEST_UTIL.waitUntilAllRegionsAssigned(TABLE_NAME);
99
100
101 final HRegionServer regionServer = TEST_UTIL.getRSForFirstRegionInTable(TABLE_NAME);
102
103 try {
104 final byte[] ROW = Bytes.toBytes("aaa");
105 Put put = new Put(ROW);
106 put.add(TEST_FAMILY, ROW, ROW);
107 table.put(put);
108 table.flushCommits();
109 } catch (IOException e) {
110
111
112
113 }
114
115
116
117 boolean aborted = false;
118 for (int i = 0; i < 10; i++) {
119 aborted = regionServer.isAborted();
120 if (aborted) {
121 break;
122 }
123 try {
124 Thread.sleep(1000);
125 } catch (InterruptedException e) {
126 fail("InterruptedException while waiting for regionserver " +
127 "zk node to be deleted.");
128 }
129 }
130 Assert.assertTrue("The region server should have aborted", aborted);
131 table.close();
132 } finally {
133 TEST_UTIL.shutdownMiniCluster();
134 }
135 }
136
137 public static class FailedInitializationObserver extends SimpleRegionObserver {
138 @Override
139 @SuppressWarnings("null")
140 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="NP_NULL_ON_SOME_PATH",
141 justification="Preconditions checks insure we are not going to dereference a null value")
142 public void start(CoprocessorEnvironment e) throws IOException {
143
144 Integer i = null;
145 i = i + 1;
146 }
147 }
148
149 public static class BuggyRegionObserver extends SimpleRegionObserver {
150 @Override
151 @SuppressWarnings("null")
152 @edu.umd.cs.findbugs.annotations.SuppressWarnings(value="NP_NULL_ON_SOME_PATH",
153 justification="Preconditions checks insure we are not going to dereference a null value")
154 public void prePut(final ObserverContext<RegionCoprocessorEnvironment> c,
155 final Put put, final WALEdit edit,
156 final Durability durability) {
157 String tableName =
158 c.getEnvironment().getRegion().getRegionInfo().getTable().getNameAsString();
159 if (tableName.equals("observed_table")) {
160
161 Integer i = null;
162 i = i + 1;
163 }
164 }
165 }
166 }