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