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 junit.framework.TestCase;
25
26 import org.apache.hadoop.conf.Configuration;
27 import org.apache.hadoop.fs.Path;
28 import org.apache.hadoop.hbase.Coprocessor;
29 import org.apache.hadoop.hbase.HBaseConfiguration;
30 import org.apache.hadoop.hbase.HBaseTestingUtility;
31 import org.apache.hadoop.hbase.HColumnDescriptor;
32 import org.apache.hadoop.hbase.HRegionInfo;
33 import org.apache.hadoop.hbase.HTableDescriptor;
34 import org.apache.hadoop.hbase.testclassification.SmallTests;
35 import org.apache.hadoop.hbase.TableName;
36 import org.apache.hadoop.hbase.client.Put;
37 import org.apache.hadoop.hbase.client.Durability;
38 import org.apache.hadoop.hbase.regionserver.HRegion;
39 import org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost;
40 import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
41 import org.apache.hadoop.hbase.util.Bytes;
42 import org.junit.experimental.categories.Category;
43
44 @Category(SmallTests.class)
45 public class TestRegionObserverStacking extends TestCase {
46 private static HBaseTestingUtility TEST_UTIL
47 = new HBaseTestingUtility();
48 static final Path DIR = TEST_UTIL.getDataTestDir();
49
50 public static class ObserverA extends BaseRegionObserver {
51 long id;
52 @Override
53 public void postPut(final ObserverContext<RegionCoprocessorEnvironment> c,
54 final Put put, final WALEdit edit,
55 final Durability durability)
56 throws IOException {
57 id = System.currentTimeMillis();
58 try {
59 Thread.sleep(10);
60 } catch (InterruptedException ex) {
61 }
62 }
63 }
64
65 public static class ObserverB extends BaseRegionObserver {
66 long id;
67 @Override
68 public void postPut(final ObserverContext<RegionCoprocessorEnvironment> c,
69 final Put put, final WALEdit edit,
70 final Durability durability)
71 throws IOException {
72 id = System.currentTimeMillis();
73 try {
74 Thread.sleep(10);
75 } catch (InterruptedException ex) {
76 }
77 }
78 }
79
80 public static class ObserverC extends BaseRegionObserver {
81 long id;
82
83 @Override
84 public void postPut(final ObserverContext<RegionCoprocessorEnvironment> c,
85 final Put put, final WALEdit edit,
86 final Durability durability)
87 throws IOException {
88 id = System.currentTimeMillis();
89 try {
90 Thread.sleep(10);
91 } catch (InterruptedException ex) {
92 }
93 }
94 }
95
96 HRegion initHRegion (byte [] tableName, String callingMethod,
97 Configuration conf, byte [] ... families) throws IOException {
98 HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
99 for(byte [] family : families) {
100 htd.addFamily(new HColumnDescriptor(family));
101 }
102 HRegionInfo info = new HRegionInfo(htd.getTableName(), null, null, false);
103 Path path = new Path(DIR + callingMethod);
104 HRegion r = HRegion.createHRegion(info, path, conf, htd);
105
106
107
108
109 RegionCoprocessorHost host = new RegionCoprocessorHost(r, null, conf);
110 r.setCoprocessorHost(host);
111 return r;
112 }
113
114 public void testRegionObserverStacking() throws Exception {
115 byte[] ROW = Bytes.toBytes("testRow");
116 byte[] TABLE = Bytes.toBytes(this.getClass().getSimpleName());
117 byte[] A = Bytes.toBytes("A");
118 byte[][] FAMILIES = new byte[][] { A } ;
119
120 Configuration conf = HBaseConfiguration.create();
121 HRegion region = initHRegion(TABLE, getClass().getName(),
122 conf, FAMILIES);
123 RegionCoprocessorHost h = region.getCoprocessorHost();
124 h.load(ObserverA.class, Coprocessor.PRIORITY_HIGHEST, conf);
125 h.load(ObserverB.class, Coprocessor.PRIORITY_USER, conf);
126 h.load(ObserverC.class, Coprocessor.PRIORITY_LOWEST, conf);
127
128 Put put = new Put(ROW);
129 put.add(A, A, A);
130 region.put(put);
131
132 Coprocessor c = h.findCoprocessor(ObserverA.class.getName());
133 long idA = ((ObserverA)c).id;
134 c = h.findCoprocessor(ObserverB.class.getName());
135 long idB = ((ObserverB)c).id;
136 c = h.findCoprocessor(ObserverC.class.getName());
137 long idC = ((ObserverC)c).id;
138
139 assertTrue(idA < idB);
140 assertTrue(idB < idC);
141 }
142 }