View Javadoc

1   /*
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.apache.hadoop.hbase.coprocessor;
17  
18  import java.io.IOException;
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.NavigableSet;
22  
23  import org.apache.hadoop.classification.InterfaceAudience;
24  import org.apache.hadoop.classification.InterfaceStability;
25  import org.apache.hadoop.fs.FileSystem;
26  import org.apache.hadoop.fs.Path;
27  import org.apache.hadoop.hbase.Cell;
28  import org.apache.hadoop.hbase.CoprocessorEnvironment;
29  import org.apache.hadoop.hbase.HBaseInterfaceAudience;
30  import org.apache.hadoop.hbase.HRegionInfo;
31  import org.apache.hadoop.hbase.KeyValue;
32  import org.apache.hadoop.hbase.KeyValueUtil;
33  import org.apache.hadoop.hbase.client.Append;
34  import org.apache.hadoop.hbase.client.Delete;
35  import org.apache.hadoop.hbase.client.Durability;
36  import org.apache.hadoop.hbase.client.Get;
37  import org.apache.hadoop.hbase.client.Increment;
38  import org.apache.hadoop.hbase.client.Mutation;
39  import org.apache.hadoop.hbase.client.Put;
40  import org.apache.hadoop.hbase.client.Result;
41  import org.apache.hadoop.hbase.client.Scan;
42  import org.apache.hadoop.hbase.filter.ByteArrayComparable;
43  import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
44  import org.apache.hadoop.hbase.io.FSDataInputStreamWrapper;
45  import org.apache.hadoop.hbase.io.Reference;
46  import org.apache.hadoop.hbase.io.hfile.CacheConfig;
47  import org.apache.hadoop.hbase.regionserver.DeleteTracker;
48  import org.apache.hadoop.hbase.regionserver.HRegion;
49  import org.apache.hadoop.hbase.regionserver.HRegion.Operation;
50  import org.apache.hadoop.hbase.regionserver.InternalScanner;
51  import org.apache.hadoop.hbase.regionserver.KeyValueScanner;
52  import org.apache.hadoop.hbase.regionserver.MiniBatchOperationInProgress;
53  import org.apache.hadoop.hbase.regionserver.RegionScanner;
54  import org.apache.hadoop.hbase.regionserver.ScanType;
55  import org.apache.hadoop.hbase.regionserver.Store;
56  import org.apache.hadoop.hbase.regionserver.StoreFile;
57  import org.apache.hadoop.hbase.regionserver.StoreFile.Reader;
58  import org.apache.hadoop.hbase.regionserver.compactions.CompactionRequest;
59  import org.apache.hadoop.hbase.regionserver.wal.HLogKey;
60  import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
61  import org.apache.hadoop.hbase.util.Pair;
62  
63  import com.google.common.collect.ImmutableList;
64  
65  /**
66   * An abstract class that implements RegionObserver.
67   * By extending it, you can create your own region observer without
68   * overriding all abstract methods of RegionObserver.
69   */
70  @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.COPROC)
71  @InterfaceStability.Evolving
72  public abstract class BaseRegionObserver implements RegionObserver {
73    @Override
74    public void start(CoprocessorEnvironment e) throws IOException { }
75  
76    @Override
77    public void stop(CoprocessorEnvironment e) throws IOException { }
78  
79    @Override
80    public void preOpen(ObserverContext<RegionCoprocessorEnvironment> e) throws IOException { }
81  
82    @Override
83    public void postOpen(ObserverContext<RegionCoprocessorEnvironment> e) { }
84  
85    @Override
86    public void postLogReplay(ObserverContext<RegionCoprocessorEnvironment> e) { }
87  
88    @Override
89    public void preClose(ObserverContext<RegionCoprocessorEnvironment> c, boolean abortRequested)
90        throws IOException { }
91  
92    @Override
93    public void postClose(ObserverContext<RegionCoprocessorEnvironment> e,
94        boolean abortRequested) { }
95  
96    @Override
97    public InternalScanner preFlushScannerOpen(final ObserverContext<RegionCoprocessorEnvironment> c,
98        final Store store, final KeyValueScanner memstoreScanner, final InternalScanner s)
99        throws IOException {
100     return s;
101   }
102 
103   @Override
104   public void preFlush(ObserverContext<RegionCoprocessorEnvironment> e) throws IOException {
105   }
106 
107   @Override
108   public void postFlush(ObserverContext<RegionCoprocessorEnvironment> e) throws IOException {
109   }
110 
111   @Override
112   public InternalScanner preFlush(ObserverContext<RegionCoprocessorEnvironment> e, Store store,
113       InternalScanner scanner) throws IOException {
114     return scanner;
115   }
116 
117   @Override
118   public void postFlush(ObserverContext<RegionCoprocessorEnvironment> e, Store store,
119       StoreFile resultFile) throws IOException {
120   }
121 
122   @Override
123   public void preSplit(ObserverContext<RegionCoprocessorEnvironment> e) throws IOException {
124   }
125   
126   @Override
127   public void preSplit(ObserverContext<RegionCoprocessorEnvironment> c,
128       byte[] splitRow) throws IOException {
129   }
130 
131   @Override
132   public void preSplitBeforePONR(ObserverContext<RegionCoprocessorEnvironment> ctx,
133       byte[] splitKey, List<Mutation> metaEntries) throws IOException {
134   }
135   
136   @Override
137   public void preSplitAfterPONR(
138       ObserverContext<RegionCoprocessorEnvironment> ctx) throws IOException {
139   }
140   
141   @Override
142   public void preRollBackSplit(ObserverContext<RegionCoprocessorEnvironment> ctx)
143       throws IOException {
144   }
145   
146   @Override
147   public void postRollBackSplit(
148       ObserverContext<RegionCoprocessorEnvironment> ctx) throws IOException {
149   }
150   
151   @Override
152   public void postCompleteSplit(
153       ObserverContext<RegionCoprocessorEnvironment> ctx) throws IOException {
154   }
155 
156   @Override
157   public void postSplit(ObserverContext<RegionCoprocessorEnvironment> e, HRegion l, HRegion r)
158       throws IOException {
159   }
160 
161   @Override
162   public void preCompactSelection(final ObserverContext<RegionCoprocessorEnvironment> c,
163       final Store store, final List<StoreFile> candidates) throws IOException { }
164 
165   @Override
166   public void preCompactSelection(final ObserverContext<RegionCoprocessorEnvironment> c,
167       final Store store, final List<StoreFile> candidates, final CompactionRequest request)
168       throws IOException {
169     preCompactSelection(c, store, candidates);
170   }
171 
172   @Override
173   public void postCompactSelection(final ObserverContext<RegionCoprocessorEnvironment> c,
174       final Store store, final ImmutableList<StoreFile> selected) { }
175 
176   @Override
177   public void postCompactSelection(final ObserverContext<RegionCoprocessorEnvironment> c,
178       final Store store, final ImmutableList<StoreFile> selected, CompactionRequest request) {
179     postCompactSelection(c, store, selected);
180   }
181 
182   @Override
183   public InternalScanner preCompact(ObserverContext<RegionCoprocessorEnvironment> e,
184       final Store store, final InternalScanner scanner, final ScanType scanType)
185       throws IOException {
186     return scanner;
187   }
188 
189   @Override
190   public InternalScanner preCompact(ObserverContext<RegionCoprocessorEnvironment> e,
191       final Store store, final InternalScanner scanner, final ScanType scanType,
192       CompactionRequest request) throws IOException {
193     return preCompact(e, store, scanner, scanType);
194   }
195 
196   @Override
197   public InternalScanner preCompactScannerOpen(
198       final ObserverContext<RegionCoprocessorEnvironment> c, final Store store,
199       List<? extends KeyValueScanner> scanners, final ScanType scanType, final long earliestPutTs,
200       final InternalScanner s) throws IOException {
201     return s;
202   }
203 
204   @Override
205   public InternalScanner preCompactScannerOpen(
206       final ObserverContext<RegionCoprocessorEnvironment> c, final Store store,
207       List<? extends KeyValueScanner> scanners, final ScanType scanType, final long earliestPutTs,
208       final InternalScanner s, CompactionRequest request) throws IOException {
209     return preCompactScannerOpen(c, store, scanners, scanType, earliestPutTs, s);
210   }
211 
212   @Override
213   public void postCompact(ObserverContext<RegionCoprocessorEnvironment> e, final Store store,
214       final StoreFile resultFile) throws IOException {
215   }
216 
217 @Override
218   public void postCompact(ObserverContext<RegionCoprocessorEnvironment> e, final Store store,
219       final StoreFile resultFile, CompactionRequest request) throws IOException {
220     postCompact(e, store, resultFile);
221   }
222 
223   @Override
224   public void preGetClosestRowBefore(final ObserverContext<RegionCoprocessorEnvironment> e,
225       final byte [] row, final byte [] family, final Result result)
226     throws IOException {
227   }
228 
229   @Override
230   public void postGetClosestRowBefore(final ObserverContext<RegionCoprocessorEnvironment> e,
231       final byte [] row, final byte [] family, final Result result)
232       throws IOException {
233   }
234 
235   @Override
236   public void preGetOp(final ObserverContext<RegionCoprocessorEnvironment> e,
237       final Get get, final List<Cell> results) throws IOException {
238     // By default we are executing the deprecated preGet to support legacy RegionObservers
239     // We may use the results coming in and we may return the results going out.
240     List<KeyValue> kvs = new ArrayList<KeyValue>(results.size());
241     for (Cell c : results) {
242       kvs.add(KeyValueUtil.ensureKeyValue(c));
243     }
244     preGet(e, get, kvs);
245     results.clear();
246     results.addAll(kvs);
247   }
248 
249   /**
250    * WARNING: please override preGetOp instead of this method.  This is to maintain some
251    * compatibility and to ease the transition from 0.94 -> 0.96.  It is super inefficient!
252    */
253   @Deprecated
254   @Override
255   public void preGet(final ObserverContext<RegionCoprocessorEnvironment> c, final Get get,
256       final List<KeyValue> result)
257     throws IOException {
258   }
259 
260   @Override
261   public void postGetOp(final ObserverContext<RegionCoprocessorEnvironment> e,
262       final Get get, final List<Cell> results) throws IOException {
263     // By default we are executing the deprecated preGet to support legacy RegionObservers
264     // We may use the results coming in and we may return the results going out.
265     List<KeyValue> kvs = new ArrayList<KeyValue>(results.size());
266     for (Cell c : results) {
267       kvs.add(KeyValueUtil.ensureKeyValue(c));
268     }
269     postGet(e, get, kvs);
270     results.clear();
271     results.addAll(kvs);
272   }
273 
274   /**
275    * WARNING: please override postGetOp instead of this method.  This is to maintain some
276    * compatibility and to ease the transition from 0.94 -> 0.96.  It is super inefficient!
277    */
278   @Deprecated
279   @Override
280   public void postGet(final ObserverContext<RegionCoprocessorEnvironment> c, final Get get,
281       final List<KeyValue> result)
282     throws IOException {
283   }
284 
285   
286   @Override
287   public boolean preExists(final ObserverContext<RegionCoprocessorEnvironment> e,
288       final Get get, final boolean exists) throws IOException {
289     return exists;
290   }
291 
292   @Override
293   public boolean postExists(final ObserverContext<RegionCoprocessorEnvironment> e,
294       final Get get, boolean exists) throws IOException {
295     return exists;
296   }
297 
298   @Override
299   public void prePut(final ObserverContext<RegionCoprocessorEnvironment> e, 
300       final Put put, final WALEdit edit, final Durability durability) throws IOException {
301   }
302 
303   @Override
304   public void postPut(final ObserverContext<RegionCoprocessorEnvironment> e, 
305       final Put put, final WALEdit edit, final Durability durability) throws IOException {
306   }
307 
308   @Override
309   public void preDelete(final ObserverContext<RegionCoprocessorEnvironment> e, final Delete delete,
310       final WALEdit edit, final Durability durability) throws IOException {
311   }
312 
313   @Override
314   public void postDelete(final ObserverContext<RegionCoprocessorEnvironment> e,
315       final Delete delete, final WALEdit edit, final Durability durability)
316       throws IOException {
317   }
318   
319   @Override
320   public void preBatchMutate(final ObserverContext<RegionCoprocessorEnvironment> c,
321       final MiniBatchOperationInProgress<Mutation> miniBatchOp) throws IOException {
322   }
323 
324   @Override
325   public void postBatchMutate(final ObserverContext<RegionCoprocessorEnvironment> c,
326       final MiniBatchOperationInProgress<Mutation> miniBatchOp) throws IOException {
327   }
328 
329   @Override
330   public void postBatchMutateIndispensably(final ObserverContext<RegionCoprocessorEnvironment> ctx,
331       MiniBatchOperationInProgress<Mutation> miniBatchOp, final boolean success) throws IOException {
332   }
333 
334   @Override
335   public boolean preCheckAndPut(final ObserverContext<RegionCoprocessorEnvironment> e,
336       final byte [] row, final byte [] family, final byte [] qualifier,
337       final CompareOp compareOp, final ByteArrayComparable comparator,
338       final Put put, final boolean result) throws IOException {
339     return result;
340   }
341 
342   @Override
343   public boolean postCheckAndPut(final ObserverContext<RegionCoprocessorEnvironment> e,
344       final byte [] row, final byte [] family, final byte [] qualifier,
345       final CompareOp compareOp, final ByteArrayComparable comparator,
346       final Put put, final boolean result) throws IOException {
347     return result;
348   }
349 
350   @Override
351   public boolean preCheckAndDelete(final ObserverContext<RegionCoprocessorEnvironment> e,
352       final byte [] row, final byte [] family, final byte [] qualifier,
353       final CompareOp compareOp, final ByteArrayComparable comparator,
354       final Delete delete, final boolean result) throws IOException {
355     return result;
356   }
357 
358   @Override
359   public boolean postCheckAndDelete(final ObserverContext<RegionCoprocessorEnvironment> e,
360       final byte [] row, final byte [] family, final byte [] qualifier,
361       final CompareOp compareOp, final ByteArrayComparable comparator,
362       final Delete delete, final boolean result) throws IOException {
363     return result;
364   }
365 
366   @Override
367   public Result preAppend(final ObserverContext<RegionCoprocessorEnvironment> e,
368       final Append append) throws IOException {
369     return null;
370   }
371 
372   @Override
373   public Result postAppend(final ObserverContext<RegionCoprocessorEnvironment> e,
374       final Append append, final Result result) throws IOException {
375     return result;
376   }
377 
378   @Override
379   public long preIncrementColumnValue(final ObserverContext<RegionCoprocessorEnvironment> e,
380       final byte [] row, final byte [] family, final byte [] qualifier,
381       final long amount, final boolean writeToWAL) throws IOException {
382     return amount;
383   }
384 
385   @Override
386   public long postIncrementColumnValue(final ObserverContext<RegionCoprocessorEnvironment> e,
387       final byte [] row, final byte [] family, final byte [] qualifier,
388       final long amount, final boolean writeToWAL, long result)
389       throws IOException {
390     return result;
391   }
392 
393   @Override
394   public Result preIncrement(final ObserverContext<RegionCoprocessorEnvironment> e,
395       final Increment increment) throws IOException {
396     return null;
397   }
398 
399   @Override
400   public Result postIncrement(final ObserverContext<RegionCoprocessorEnvironment> e,
401       final Increment increment, final Result result) throws IOException {
402     return result;
403   }
404 
405   @Override
406   public RegionScanner preScannerOpen(final ObserverContext<RegionCoprocessorEnvironment> e,
407       final Scan scan, final RegionScanner s) throws IOException {
408     return s;
409   }
410 
411   @Override
412   public KeyValueScanner preStoreScannerOpen(final ObserverContext<RegionCoprocessorEnvironment> c,
413       final Store store, final Scan scan, final NavigableSet<byte[]> targetCols,
414       final KeyValueScanner s) throws IOException {
415     return s;
416   }
417 
418   @Override
419   public RegionScanner postScannerOpen(final ObserverContext<RegionCoprocessorEnvironment> e,
420       final Scan scan, final RegionScanner s) throws IOException {
421     return s;
422   }
423 
424   @Override
425   public boolean preScannerNext(final ObserverContext<RegionCoprocessorEnvironment> e,
426       final InternalScanner s, final List<Result> results,
427       final int limit, final boolean hasMore) throws IOException {
428     return hasMore;
429   }
430 
431   @Override
432   public boolean postScannerNext(final ObserverContext<RegionCoprocessorEnvironment> e,
433       final InternalScanner s, final List<Result> results, final int limit,
434       final boolean hasMore) throws IOException {
435     return hasMore;
436   }
437 
438   @Override
439   public boolean postScannerFilterRow(final ObserverContext<RegionCoprocessorEnvironment> e,
440       final InternalScanner s, final byte[] currentRow, final int offset, final short length,
441       final boolean hasMore) throws IOException {
442     return hasMore;
443   }
444 
445   @Override
446   public void preScannerClose(final ObserverContext<RegionCoprocessorEnvironment> e,
447       final InternalScanner s) throws IOException {
448   }
449 
450   @Override
451   public void postScannerClose(final ObserverContext<RegionCoprocessorEnvironment> e,
452       final InternalScanner s) throws IOException {
453   }
454 
455   @Override
456   public void preWALRestore(ObserverContext<RegionCoprocessorEnvironment> env, HRegionInfo info,
457       HLogKey logKey, WALEdit logEdit) throws IOException {
458   }
459 
460   @Override
461   public void postWALRestore(ObserverContext<RegionCoprocessorEnvironment> env,
462       HRegionInfo info, HLogKey logKey, WALEdit logEdit) throws IOException {
463   }
464 
465   @Override
466   public void preBulkLoadHFile(final ObserverContext<RegionCoprocessorEnvironment> ctx,
467     List<Pair<byte[], String>> familyPaths) throws IOException {
468   }
469 
470   @Override
471   public boolean postBulkLoadHFile(ObserverContext<RegionCoprocessorEnvironment> ctx,
472     List<Pair<byte[], String>> familyPaths, boolean hasLoaded) throws IOException {
473     return hasLoaded;
474   }
475 
476   @Override
477   public Reader preStoreFileReaderOpen(ObserverContext<RegionCoprocessorEnvironment> ctx,
478       FileSystem fs, Path p, FSDataInputStreamWrapper in, long size, CacheConfig cacheConf,
479       Reference r, Reader reader) throws IOException {
480     return reader;
481   }
482 
483   @Override
484   public Reader postStoreFileReaderOpen(ObserverContext<RegionCoprocessorEnvironment> ctx,
485       FileSystem fs, Path p, FSDataInputStreamWrapper in, long size, CacheConfig cacheConf,
486       Reference r, Reader reader) throws IOException {
487     return reader;
488   }
489 
490   @Override
491   public Cell postMutationBeforeWAL(ObserverContext<RegionCoprocessorEnvironment> ctx,
492       MutationType opType, Mutation mutation, Cell oldCell, Cell newCell) throws IOException {
493     return newCell;
494   }
495 
496   @Override
497   public void postStartRegionOperation(final ObserverContext<RegionCoprocessorEnvironment> ctx,
498       Operation op) throws IOException {
499   }
500 
501   @Override
502   public void postCloseRegionOperation(final ObserverContext<RegionCoprocessorEnvironment> ctx,
503       Operation op) throws IOException {
504   }
505 
506   @Override
507   public DeleteTracker postInstantiateDeleteTracker(
508       final ObserverContext<RegionCoprocessorEnvironment> ctx, DeleteTracker delTracker)
509       throws IOException {
510     return delTracker;
511   }
512 }