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