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 static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNull;
24
25 import java.io.IOException;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.NavigableSet;
29 import java.util.concurrent.CountDownLatch;
30
31 import org.apache.hadoop.conf.Configuration;
32 import org.apache.hadoop.fs.FileSystem;
33 import org.apache.hadoop.fs.Path;
34 import org.apache.hadoop.hbase.Cell;
35 import org.apache.hadoop.hbase.Coprocessor;
36 import org.apache.hadoop.hbase.HBaseConfiguration;
37 import org.apache.hadoop.hbase.HBaseTestingUtility;
38 import org.apache.hadoop.hbase.HColumnDescriptor;
39 import org.apache.hadoop.hbase.HConstants;
40 import org.apache.hadoop.hbase.HRegionInfo;
41 import org.apache.hadoop.hbase.HTableDescriptor;
42 import org.apache.hadoop.hbase.testclassification.MediumTests;
43 import org.apache.hadoop.hbase.TableName;
44 import org.apache.hadoop.hbase.client.Admin;
45 import org.apache.hadoop.hbase.client.Get;
46 import org.apache.hadoop.hbase.client.HTable;
47 import org.apache.hadoop.hbase.client.IsolationLevel;
48 import org.apache.hadoop.hbase.client.Put;
49 import org.apache.hadoop.hbase.client.Result;
50 import org.apache.hadoop.hbase.client.Scan;
51 import org.apache.hadoop.hbase.client.Table;
52 import org.apache.hadoop.hbase.filter.FilterBase;
53 import org.apache.hadoop.hbase.regionserver.HRegion;
54 import org.apache.hadoop.hbase.regionserver.HRegionServer;
55 import org.apache.hadoop.hbase.regionserver.HStore;
56 import org.apache.hadoop.hbase.regionserver.InternalScanner;
57 import org.apache.hadoop.hbase.regionserver.KeyValueScanner;
58 import org.apache.hadoop.hbase.regionserver.Region;
59 import org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost;
60 import org.apache.hadoop.hbase.regionserver.RegionServerServices;
61 import org.apache.hadoop.hbase.regionserver.ScanType;
62 import org.apache.hadoop.hbase.regionserver.Store;
63 import org.apache.hadoop.hbase.regionserver.StoreScanner;
64 import org.apache.hadoop.hbase.regionserver.compactions.CompactionContext;
65 import org.apache.hadoop.hbase.regionserver.compactions.CompactionThroughputController;
66 import org.apache.hadoop.hbase.wal.WAL;
67 import org.apache.hadoop.hbase.util.Bytes;
68 import org.junit.Test;
69 import org.junit.experimental.categories.Category;
70
71 @Category(MediumTests.class)
72 public class TestRegionObserverScannerOpenHook {
73 private static HBaseTestingUtility UTIL = new HBaseTestingUtility();
74 static final Path DIR = UTIL.getDataTestDir();
75
76 public static class NoDataFilter extends FilterBase {
77
78 @Override
79 public ReturnCode filterKeyValue(Cell ignored) throws IOException {
80 return ReturnCode.SKIP;
81 }
82
83 @Override
84 public boolean filterAllRemaining() throws IOException {
85 return true;
86 }
87
88 @Override
89 public boolean filterRow() throws IOException {
90 return true;
91 }
92 }
93
94
95
96
97
98 public static class EmptyRegionObsever extends BaseRegionObserver {
99 }
100
101
102
103
104 public static class NoDataFromScan extends BaseRegionObserver {
105 @Override
106 public KeyValueScanner preStoreScannerOpen(ObserverContext<RegionCoprocessorEnvironment> c,
107 Store store, Scan scan, NavigableSet<byte[]> targetCols, KeyValueScanner s)
108 throws IOException {
109 scan.setFilter(new NoDataFilter());
110 return new StoreScanner(store, store.getScanInfo(), scan, targetCols,
111 ((HStore)store).getHRegion().getReadpoint(IsolationLevel.READ_COMMITTED));
112 }
113 }
114
115
116
117
118 public static class NoDataFromFlush extends BaseRegionObserver {
119 @Override
120 public InternalScanner preFlushScannerOpen(ObserverContext<RegionCoprocessorEnvironment> c,
121 Store store, KeyValueScanner memstoreScanner, InternalScanner s) throws IOException {
122 Scan scan = new Scan();
123 scan.setFilter(new NoDataFilter());
124 return new StoreScanner(store, store.getScanInfo(), scan,
125 Collections.singletonList(memstoreScanner), ScanType.COMPACT_RETAIN_DELETES,
126 store.getSmallestReadPoint(), HConstants.OLDEST_TIMESTAMP);
127 }
128 }
129
130
131
132
133
134 public static class NoDataFromCompaction extends BaseRegionObserver {
135 @Override
136 public InternalScanner preCompactScannerOpen(ObserverContext<RegionCoprocessorEnvironment> c,
137 Store store, List<? extends KeyValueScanner> scanners, ScanType scanType,
138 long earliestPutTs, InternalScanner s) throws IOException {
139 Scan scan = new Scan();
140 scan.setFilter(new NoDataFilter());
141 return new StoreScanner(store, store.getScanInfo(), scan, scanners,
142 ScanType.COMPACT_RETAIN_DELETES, store.getSmallestReadPoint(),
143 HConstants.OLDEST_TIMESTAMP);
144 }
145 }
146
147 Region initHRegion(byte[] tableName, String callingMethod, Configuration conf,
148 byte[]... families) throws IOException {
149 HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
150 for (byte[] family : families) {
151 htd.addFamily(new HColumnDescriptor(family));
152 }
153 HRegionInfo info = new HRegionInfo(htd.getTableName(), null, null, false);
154 Path path = new Path(DIR + callingMethod);
155 HRegion r = HRegion.createHRegion(info, path, conf, htd);
156
157
158
159
160 RegionCoprocessorHost host = new RegionCoprocessorHost(r, null, conf);
161 r.setCoprocessorHost(host);
162 return r;
163 }
164
165 @Test
166 public void testRegionObserverScanTimeStacking() throws Exception {
167 byte[] ROW = Bytes.toBytes("testRow");
168 byte[] TABLE = Bytes.toBytes(getClass().getName());
169 byte[] A = Bytes.toBytes("A");
170 byte[][] FAMILIES = new byte[][] { A };
171
172 Configuration conf = HBaseConfiguration.create();
173 Region region = initHRegion(TABLE, getClass().getName(), conf, FAMILIES);
174 RegionCoprocessorHost h = region.getCoprocessorHost();
175 h.load(NoDataFromScan.class, Coprocessor.PRIORITY_HIGHEST, conf);
176 h.load(EmptyRegionObsever.class, Coprocessor.PRIORITY_USER, conf);
177
178 Put put = new Put(ROW);
179 put.add(A, A, A);
180 region.put(put);
181
182 Get get = new Get(ROW);
183 Result r = region.get(get);
184 assertNull(
185 "Got an unexpected number of rows - no data should be returned with the NoDataFromScan coprocessor. Found: "
186 + r, r.listCells());
187 }
188
189 @Test
190 public void testRegionObserverFlushTimeStacking() throws Exception {
191 byte[] ROW = Bytes.toBytes("testRow");
192 byte[] TABLE = Bytes.toBytes(getClass().getName());
193 byte[] A = Bytes.toBytes("A");
194 byte[][] FAMILIES = new byte[][] { A };
195
196 Configuration conf = HBaseConfiguration.create();
197 Region region = initHRegion(TABLE, getClass().getName(), conf, FAMILIES);
198 RegionCoprocessorHost h = region.getCoprocessorHost();
199 h.load(NoDataFromFlush.class, Coprocessor.PRIORITY_HIGHEST, conf);
200 h.load(EmptyRegionObsever.class, Coprocessor.PRIORITY_USER, conf);
201
202
203 Put put = new Put(ROW);
204 put.add(A, A, A);
205 region.put(put);
206 region.flush(true);
207 Get get = new Get(ROW);
208 Result r = region.get(get);
209 assertNull(
210 "Got an unexpected number of rows - no data should be returned with the NoDataFromScan coprocessor. Found: "
211 + r, r.listCells());
212 }
213
214
215
216
217 public static class CompactionCompletionNotifyingRegion extends HRegion {
218 private static volatile CountDownLatch compactionStateChangeLatch = null;
219
220 @SuppressWarnings("deprecation")
221 public CompactionCompletionNotifyingRegion(Path tableDir, WAL log,
222 FileSystem fs, Configuration confParam, HRegionInfo info,
223 HTableDescriptor htd, RegionServerServices rsServices) {
224 super(tableDir, log, fs, confParam, info, htd, rsServices);
225 }
226
227 public CountDownLatch getCompactionStateChangeLatch() {
228 if (compactionStateChangeLatch == null) compactionStateChangeLatch = new CountDownLatch(1);
229 return compactionStateChangeLatch;
230 }
231
232 @Override
233 public boolean compact(CompactionContext compaction, Store store,
234 CompactionThroughputController throughputController) throws IOException {
235 boolean ret = super.compact(compaction, store, throughputController);
236 if (ret) compactionStateChangeLatch.countDown();
237 return ret;
238 }
239 }
240
241
242
243
244
245
246 @Test
247 public void testRegionObserverCompactionTimeStacking() throws Exception {
248
249 Configuration conf = UTIL.getConfiguration();
250 conf.setClass(HConstants.REGION_IMPL, CompactionCompletionNotifyingRegion.class, HRegion.class);
251 conf.setInt("hbase.hstore.compaction.min", 2);
252 UTIL.startMiniCluster();
253 String tableName = "testRegionObserverCompactionTimeStacking";
254 byte[] ROW = Bytes.toBytes("testRow");
255 byte[] A = Bytes.toBytes("A");
256 HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(tableName));
257 desc.addFamily(new HColumnDescriptor(A));
258 desc.addCoprocessor(EmptyRegionObsever.class.getName(), null, Coprocessor.PRIORITY_USER, null);
259 desc.addCoprocessor(NoDataFromCompaction.class.getName(), null, Coprocessor.PRIORITY_HIGHEST,
260 null);
261
262 Admin admin = UTIL.getHBaseAdmin();
263 admin.createTable(desc);
264
265 Table table = new HTable(conf, desc.getTableName());
266
267
268 Put put = new Put(ROW);
269 put.add(A, A, A);
270 table.put(put);
271
272 HRegionServer rs = UTIL.getRSForFirstRegionInTable(desc.getTableName());
273 List<Region> regions = rs.getOnlineRegions(desc.getTableName());
274 assertEquals("More than 1 region serving test table with 1 row", 1, regions.size());
275 Region region = regions.get(0);
276 admin.flushRegion(region.getRegionInfo().getRegionName());
277 CountDownLatch latch = ((CompactionCompletionNotifyingRegion)region)
278 .getCompactionStateChangeLatch();
279
280
281 put = new Put(Bytes.toBytes("anotherrow"));
282 put.add(A, A, A);
283 table.put(put);
284 admin.flushRegion(region.getRegionInfo().getRegionName());
285
286
287
288 latch.await();
289
290 Get get = new Get(ROW);
291 Result r = table.get(get);
292 assertNull(
293 "Got an unexpected number of rows - no data should be returned with the NoDataFromScan coprocessor. Found: "
294 + r, r.listCells());
295
296 get = new Get(Bytes.toBytes("anotherrow"));
297 r = table.get(get);
298 assertNull(
299 "Got an unexpected number of rows - no data should be returned with the NoDataFromScan coprocessor Found: "
300 + r, r.listCells());
301
302 table.close();
303 UTIL.shutdownMiniCluster();
304 }
305 }