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 java.io.IOException;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.hbase.*;
28 import org.apache.hadoop.hbase.client.HTable;
29 import org.apache.hadoop.hbase.client.Put;
30 import org.apache.hadoop.hbase.client.Durability;
31 import org.apache.hadoop.hbase.regionserver.HRegionServer;
32 import org.apache.hadoop.hbase.util.Bytes;
33 import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
34 import org.junit.AfterClass;
35 import org.junit.Assert;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.junit.experimental.categories.Category;
39
40 import static org.junit.Assert.*;
41
42
43
44
45
46
47
48 @Category(MediumTests.class)
49 public class TestRegionServerCoprocessorExceptionWithAbort {
50 static final Log LOG = LogFactory.getLog(TestRegionServerCoprocessorExceptionWithAbort.class);
51 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
52 private static final byte[] ROW = Bytes.toBytes("aaa");
53 private static final TableName TABLE_NAME =
54 TableName.valueOf("observed_table");
55
56 @BeforeClass
57 public static void setupBeforeClass() throws Exception {
58
59 Configuration conf = TEST_UTIL.getConfiguration();
60 conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 2);
61 conf.set(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY, BuggyRegionObserver.class.getName());
62 conf.set("hbase.coprocessor.abortonerror", "true");
63 TEST_UTIL.startMiniCluster(3);
64 }
65
66 @AfterClass
67 public static void teardownAfterClass() throws Exception {
68 TEST_UTIL.shutdownMiniCluster();
69 }
70
71 @Test
72 public void testExceptionFromCoprocessorDuringPut()
73 throws IOException, InterruptedException {
74
75
76
77 TableName TEST_TABLE = TABLE_NAME;
78 byte[] TEST_FAMILY = Bytes.toBytes("aaa");
79
80 HTable table = TEST_UTIL.createTable(TEST_TABLE, TEST_FAMILY);
81 TEST_UTIL.createMultiRegions(table, TEST_FAMILY);
82 TEST_UTIL.waitUntilAllRegionsAssigned(TEST_TABLE);
83
84
85 final HRegionServer regionServer = TEST_UTIL.getRSForFirstRegionInTable(TEST_TABLE);
86 Put put = new Put(ROW);
87 put.add(TEST_FAMILY, ROW, ROW);
88
89 Assert.assertFalse("The region server should be available", regionServer.isAborted());
90 try {
91 LOG.info("Running put " + put);
92 table.put(put);
93 fail("The put should have failed, as the coprocessor is buggy");
94 } catch (IOException ignored) {
95
96 }
97 Assert.assertTrue("The region server should have aborted", regionServer.isAborted());
98 table.close();
99 }
100
101 public static class BuggyRegionObserver extends SimpleRegionObserver {
102 @Override
103 public void prePut(final ObserverContext<RegionCoprocessorEnvironment> c,
104 final Put put, final WALEdit edit,
105 final Durability durability) {
106 TableName tableName =
107 c.getEnvironment().getRegion().getRegionInfo().getTable();
108 if (TABLE_NAME.equals(tableName) && Bytes.equals(put.getRow(), ROW)) {
109 throw new NullPointerException("Buggy coprocessor: " + put);
110 }
111 }
112 }
113 }