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.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.conf.Configuration;
29 import org.apache.hadoop.hbase.CoprocessorEnvironment;
30 import org.apache.hadoop.hbase.HBaseTestingUtility;
31 import org.apache.hadoop.hbase.HConstants;
32 import org.apache.hadoop.hbase.MiniHBaseCluster;
33 import org.apache.hadoop.hbase.TableName;
34 import org.apache.hadoop.hbase.Waiter.Predicate;
35 import org.apache.hadoop.hbase.client.Durability;
36 import org.apache.hadoop.hbase.client.HTable;
37 import org.apache.hadoop.hbase.client.Put;
38 import org.apache.hadoop.hbase.regionserver.HRegionServer;
39 import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
40 import org.apache.hadoop.hbase.testclassification.MediumTests;
41 import org.apache.hadoop.hbase.util.Bytes;
42 import org.junit.Assert;
43 import org.junit.Test;
44 import org.junit.experimental.categories.Category;
45
46
47
48
49
50
51
52 @Category(MediumTests.class)
53 public class TestRegionServerCoprocessorExceptionWithAbort {
54 static final Log LOG = LogFactory.getLog(TestRegionServerCoprocessorExceptionWithAbort.class);
55 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
56 private static final TableName TABLE_NAME = TableName.valueOf("observed_table");
57
58 @Test(timeout=60000)
59 public void testExceptionDuringInitialization() throws Exception {
60 Configuration conf = TEST_UTIL.getConfiguration();
61 conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);
62 conf.setBoolean(CoprocessorHost.ABORT_ON_ERROR_KEY, true);
63 conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY, "");
64 TEST_UTIL.startMiniCluster(2);
65 try {
66 MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
67
68
69 final HRegionServer regionServer = cluster.getRegionServer(0);
70 conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
71 FailedInitializationObserver.class.getName());
72 regionServer.getRegionServerCoprocessorHost().loadSystemCoprocessors(conf,
73 CoprocessorHost.REGION_COPROCESSOR_CONF_KEY);
74 TEST_UTIL.waitFor(10000, 1000, new Predicate<Exception>() {
75 @Override
76 public boolean evaluate() throws Exception {
77 return regionServer.isAborted();
78 }
79 });
80 } finally {
81 TEST_UTIL.shutdownMiniCluster();
82 }
83 }
84
85 @Test(timeout=60000)
86 public void testExceptionFromCoprocessorDuringPut() throws Exception {
87
88 Configuration conf = TEST_UTIL.getConfiguration();
89 conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);
90 conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY, BuggyRegionObserver.class.getName());
91 conf.setBoolean(CoprocessorHost.ABORT_ON_ERROR_KEY, true);
92 TEST_UTIL.startMiniCluster(2);
93 try {
94
95
96
97 final byte[] TEST_FAMILY = Bytes.toBytes("aaa");
98
99 HTable table = TEST_UTIL.createMultiRegionTable(TABLE_NAME, TEST_FAMILY);
100 TEST_UTIL.waitUntilAllRegionsAssigned(TABLE_NAME);
101
102
103 final HRegionServer regionServer = TEST_UTIL.getRSForFirstRegionInTable(TABLE_NAME);
104
105 try {
106 final byte[] ROW = Bytes.toBytes("aaa");
107 Put put = new Put(ROW);
108 put.add(TEST_FAMILY, ROW, ROW);
109 table.put(put);
110 table.flushCommits();
111 } catch (IOException e) {
112
113
114
115 }
116
117
118
119 boolean aborted = false;
120 for (int i = 0; i < 10; i++) {
121 aborted = regionServer.isAborted();
122 if (aborted) {
123 break;
124 }
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 Assert.assertTrue("The region server should have aborted", aborted);
133 table.close();
134 } finally {
135 TEST_UTIL.shutdownMiniCluster();
136 }
137 }
138
139 public static class FailedInitializationObserver extends SimpleRegionObserver {
140 @SuppressWarnings("null")
141 @Override
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 @SuppressWarnings("null")
151 @Override
152 public void prePut(final ObserverContext<RegionCoprocessorEnvironment> c,
153 final Put put, final WALEdit edit,
154 final Durability durability) {
155 String tableName =
156 c.getEnvironment().getRegion().getRegionInfo().getTable().getNameAsString();
157 if (tableName.equals("observed_table")) {
158
159 Integer i = null;
160 i = i + 1;
161 }
162 }
163 }
164 }