1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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.regionserver.HRegionServer;
30 import org.apache.hadoop.hbase.util.Bytes;
31 import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35
36 import static org.junit.Assert.*;
37
38
39
40
41
42
43
44 public class TestRegionServerCoprocessorExceptionWithAbort {
45 private static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
46
47 @BeforeClass
48 public static void setupBeforeClass() throws Exception {
49
50 Configuration conf = TEST_UTIL.getConfiguration();
51 conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
52 BuggyRegionObserver.class.getName());
53 conf.set("hbase.coprocessor.abortonerror", "true");
54 TEST_UTIL.startMiniCluster(2);
55 }
56
57 @AfterClass
58 public static void teardownAfterClass() throws Exception {
59 TEST_UTIL.shutdownMiniCluster();
60 }
61
62 @Test(timeout=30000)
63 public void testExceptionFromCoprocessorDuringPut()
64 throws IOException {
65
66
67
68 byte[] TEST_TABLE = Bytes.toBytes("observed_table");
69 byte[] TEST_FAMILY = Bytes.toBytes("aaa");
70
71 HTable table = TEST_UTIL.createTable(TEST_TABLE, TEST_FAMILY);
72 TEST_UTIL.waitUntilAllRegionsAssigned(
73 TEST_UTIL.createMultiRegions(table, TEST_FAMILY));
74
75
76 HRegionServer regionServer =
77 TEST_UTIL.getRSForFirstRegionInTable(TEST_TABLE);
78 try {
79 final byte[] ROW = Bytes.toBytes("aaa");
80 Put put = new Put(ROW);
81 put.add(TEST_FAMILY, ROW, ROW);
82 table.put(put);
83 } catch (IOException e) {
84 fail("put() failed: " + e);
85 }
86
87 boolean regionServerAborted = false;
88 for (int i = 0; i < 30; i++) {
89 if (regionServer.isAborted()) {
90 regionServerAborted = true;
91 break;
92 }
93 try {
94 Thread.sleep(1000);
95 } catch (InterruptedException e) {
96 fail("InterruptedException while waiting for regionserver " +
97 "zk node to be deleted.");
98 }
99 }
100 assertTrue("RegionServer aborted on coprocessor exception, as expected.",
101 regionServerAborted);
102 }
103
104 public static class BuggyRegionObserver extends SimpleRegionObserver {
105 @Override
106 public void prePut(final ObserverContext<RegionCoprocessorEnvironment> c,
107 final Put put, final WALEdit edit,
108 final boolean writeToWAL) {
109 String tableName =
110 c.getEnvironment().getRegion().getRegionInfo().getTableNameAsString();
111 if (tableName.equals("observed_table")) {
112 Integer i = null;
113 i = i + 1;
114 }
115 }
116 }
117 }