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