001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.coprocessor;
019
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertNull;
022
023import java.io.IOException;
024import java.util.List;
025import java.util.Optional;
026import java.util.concurrent.CountDownLatch;
027import org.apache.hadoop.conf.Configuration;
028import org.apache.hadoop.fs.FileSystem;
029import org.apache.hadoop.fs.Path;
030import org.apache.hadoop.hbase.Cell;
031import org.apache.hadoop.hbase.Coprocessor;
032import org.apache.hadoop.hbase.HBaseClassTestRule;
033import org.apache.hadoop.hbase.HBaseTestingUtility;
034import org.apache.hadoop.hbase.HColumnDescriptor;
035import org.apache.hadoop.hbase.HConstants;
036import org.apache.hadoop.hbase.HRegionInfo;
037import org.apache.hadoop.hbase.HTableDescriptor;
038import org.apache.hadoop.hbase.TableName;
039import org.apache.hadoop.hbase.client.Admin;
040import org.apache.hadoop.hbase.client.Get;
041import org.apache.hadoop.hbase.client.Put;
042import org.apache.hadoop.hbase.client.RegionInfo;
043import org.apache.hadoop.hbase.client.Result;
044import org.apache.hadoop.hbase.client.Scan;
045import org.apache.hadoop.hbase.client.Table;
046import org.apache.hadoop.hbase.client.TableDescriptor;
047import org.apache.hadoop.hbase.filter.FilterBase;
048import org.apache.hadoop.hbase.regionserver.ChunkCreator;
049import org.apache.hadoop.hbase.regionserver.FlushLifeCycleTracker;
050import org.apache.hadoop.hbase.regionserver.HRegion;
051import org.apache.hadoop.hbase.regionserver.HRegionServer;
052import org.apache.hadoop.hbase.regionserver.HStore;
053import org.apache.hadoop.hbase.regionserver.InternalScanner;
054import org.apache.hadoop.hbase.regionserver.MemStoreLAB;
055import org.apache.hadoop.hbase.regionserver.Region;
056import org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost;
057import org.apache.hadoop.hbase.regionserver.RegionServerServices;
058import org.apache.hadoop.hbase.regionserver.ScanType;
059import org.apache.hadoop.hbase.regionserver.ScannerContext;
060import org.apache.hadoop.hbase.regionserver.Store;
061import org.apache.hadoop.hbase.regionserver.StoreScanner;
062import org.apache.hadoop.hbase.regionserver.compactions.CompactionContext;
063import org.apache.hadoop.hbase.regionserver.compactions.CompactionLifeCycleTracker;
064import org.apache.hadoop.hbase.regionserver.compactions.CompactionRequest;
065import org.apache.hadoop.hbase.regionserver.throttle.ThroughputController;
066import org.apache.hadoop.hbase.security.User;
067import org.apache.hadoop.hbase.testclassification.CoprocessorTests;
068import org.apache.hadoop.hbase.testclassification.MediumTests;
069import org.apache.hadoop.hbase.util.Bytes;
070import org.apache.hadoop.hbase.wal.WAL;
071import org.junit.ClassRule;
072import org.junit.Rule;
073import org.junit.Test;
074import org.junit.experimental.categories.Category;
075import org.junit.rules.TestName;
076
077@Category({CoprocessorTests.class, MediumTests.class})
078public class TestRegionObserverScannerOpenHook {
079
080  @ClassRule
081  public static final HBaseClassTestRule CLASS_RULE =
082      HBaseClassTestRule.forClass(TestRegionObserverScannerOpenHook.class);
083
084  private static HBaseTestingUtility UTIL = new HBaseTestingUtility();
085  static final Path DIR = UTIL.getDataTestDir();
086
087  @Rule
088  public TestName name = new TestName();
089
090  public static class NoDataFilter extends FilterBase {
091
092    @Override
093    public ReturnCode filterCell(final Cell ignored) {
094      return ReturnCode.SKIP;
095    }
096
097    @Override
098    public boolean filterAllRemaining() throws IOException {
099      return true;
100    }
101
102    @Override
103    public boolean filterRow() throws IOException {
104      return true;
105    }
106  }
107
108  /**
109   * Do the default logic in {@link RegionObserver} interface.
110   */
111  public static class EmptyRegionObsever implements RegionCoprocessor, RegionObserver {
112    @Override
113    public Optional<RegionObserver> getRegionObserver() {
114      return Optional.of(this);
115    }
116  }
117
118  /**
119   * Don't return any data from a scan by creating a custom {@link StoreScanner}.
120   */
121  public static class NoDataFromScan implements RegionCoprocessor, RegionObserver {
122    @Override
123    public Optional<RegionObserver> getRegionObserver() {
124      return Optional.of(this);
125    }
126
127    @Override
128    public void preGetOp(ObserverContext<RegionCoprocessorEnvironment> c, Get get,
129        List<Cell> result) throws IOException {
130      c.bypass();
131    }
132
133    @Override
134    public void preScannerOpen(ObserverContext<RegionCoprocessorEnvironment> c, Scan scan)
135        throws IOException {
136      scan.setFilter(new NoDataFilter());
137    }
138  }
139
140  private static final InternalScanner NO_DATA = new InternalScanner() {
141
142    @Override
143    public boolean next(List<Cell> result, ScannerContext scannerContext) throws IOException {
144      return false;
145    }
146
147    @Override
148    public void close() throws IOException {}
149  };
150  /**
151   * Don't allow any data in a flush by creating a custom {@link StoreScanner}.
152   */
153  public static class NoDataFromFlush implements RegionCoprocessor, RegionObserver {
154    @Override
155    public Optional<RegionObserver> getRegionObserver() {
156      return Optional.of(this);
157    }
158
159    @Override
160    public InternalScanner preFlush(ObserverContext<RegionCoprocessorEnvironment> c, Store store,
161        InternalScanner scanner, FlushLifeCycleTracker tracker) throws IOException {
162      return NO_DATA;
163    }
164  }
165
166  /**
167   * Don't allow any data to be written out in the compaction by creating a custom
168   * {@link StoreScanner}.
169   */
170  public static class NoDataFromCompaction implements RegionCoprocessor, RegionObserver {
171    @Override
172    public Optional<RegionObserver> getRegionObserver() {
173      return Optional.of(this);
174    }
175
176    @Override
177    public InternalScanner preCompact(ObserverContext<RegionCoprocessorEnvironment> c, Store store,
178        InternalScanner scanner, ScanType scanType, CompactionLifeCycleTracker tracker,
179        CompactionRequest request) throws IOException {
180      return NO_DATA;
181    }
182  }
183
184  HRegion initHRegion(byte[] tableName, String callingMethod, Configuration conf,
185      byte[]... families) throws IOException {
186    HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName));
187    for (byte[] family : families) {
188      htd.addFamily(new HColumnDescriptor(family));
189    }
190    ChunkCreator.initialize(MemStoreLAB.CHUNK_SIZE_DEFAULT, false, 0, 0,
191      0, null, MemStoreLAB.INDEX_CHUNK_SIZE_PERCENTAGE_DEFAULT);
192    HRegionInfo info = new HRegionInfo(htd.getTableName(), null, null, false);
193    Path path = new Path(DIR + callingMethod);
194    WAL wal = HBaseTestingUtility.createWal(conf, path, info);
195    HRegion r = HRegion.createHRegion(info, path, conf, htd, wal);
196    // this following piece is a hack. currently a coprocessorHost
197    // is secretly loaded at OpenRegionHandler. we don't really
198    // start a region server here, so just manually create cphost
199    // and set it to region.
200    RegionCoprocessorHost host = new RegionCoprocessorHost(r, null, conf);
201    r.setCoprocessorHost(host);
202    return r;
203  }
204
205  @Test
206  public void testRegionObserverScanTimeStacking() throws Exception {
207    byte[] ROW = Bytes.toBytes("testRow");
208    byte[] TABLE = Bytes.toBytes(getClass().getName());
209    byte[] A = Bytes.toBytes("A");
210    byte[][] FAMILIES = new byte[][] { A };
211
212    // Use new HTU to not overlap with the DFS cluster started in #CompactionStacking
213    Configuration conf = new HBaseTestingUtility().getConfiguration();
214    HRegion region = initHRegion(TABLE, getClass().getName(), conf, FAMILIES);
215    RegionCoprocessorHost h = region.getCoprocessorHost();
216    h.load(NoDataFromScan.class, Coprocessor.PRIORITY_HIGHEST, conf);
217    h.load(EmptyRegionObsever.class, Coprocessor.PRIORITY_USER, conf);
218
219    Put put = new Put(ROW);
220    put.addColumn(A, A, A);
221    region.put(put);
222
223    Get get = new Get(ROW);
224    Result r = region.get(get);
225    assertNull(
226      "Got an unexpected number of rows - no data should be returned with the NoDataFromScan coprocessor. Found: "
227          + r, r.listCells());
228    HBaseTestingUtility.closeRegionAndWAL(region);
229  }
230
231  @Test
232  public void testRegionObserverFlushTimeStacking() throws Exception {
233    byte[] ROW = Bytes.toBytes("testRow");
234    byte[] TABLE = Bytes.toBytes(getClass().getName());
235    byte[] A = Bytes.toBytes("A");
236    byte[][] FAMILIES = new byte[][] { A };
237
238    // Use new HTU to not overlap with the DFS cluster started in #CompactionStacking
239    Configuration conf = new HBaseTestingUtility().getConfiguration();
240    HRegion region = initHRegion(TABLE, getClass().getName(), conf, FAMILIES);
241    RegionCoprocessorHost h = region.getCoprocessorHost();
242    h.load(NoDataFromFlush.class, Coprocessor.PRIORITY_HIGHEST, conf);
243    h.load(EmptyRegionObsever.class, Coprocessor.PRIORITY_USER, conf);
244
245    // put a row and flush it to disk
246    Put put = new Put(ROW);
247    put.addColumn(A, A, A);
248    region.put(put);
249    region.flush(true);
250    Get get = new Get(ROW);
251    Result r = region.get(get);
252    assertNull(
253      "Got an unexpected number of rows - no data should be returned with the NoDataFromScan coprocessor. Found: "
254          + r, r.listCells());
255    HBaseTestingUtility.closeRegionAndWAL(region);
256  }
257
258  /*
259   * Custom HRegion which uses CountDownLatch to signal the completion of compaction
260   */
261  public static class CompactionCompletionNotifyingRegion extends HRegion {
262    private static volatile CountDownLatch compactionStateChangeLatch = null;
263
264    @SuppressWarnings("deprecation")
265    public CompactionCompletionNotifyingRegion(Path tableDir, WAL log,
266        FileSystem fs, Configuration confParam, RegionInfo info,
267        TableDescriptor htd, RegionServerServices rsServices) {
268      super(tableDir, log, fs, confParam, info, htd, rsServices);
269    }
270
271    public CountDownLatch getCompactionStateChangeLatch() {
272      if (compactionStateChangeLatch == null) compactionStateChangeLatch = new CountDownLatch(1);
273      return compactionStateChangeLatch;
274    }
275
276    @Override
277    public boolean compact(CompactionContext compaction, HStore store,
278        ThroughputController throughputController) throws IOException {
279      boolean ret = super.compact(compaction, store, throughputController);
280      if (ret) compactionStateChangeLatch.countDown();
281      return ret;
282    }
283
284    @Override
285    public boolean compact(CompactionContext compaction, HStore store,
286        ThroughputController throughputController, User user) throws IOException {
287      boolean ret = super.compact(compaction, store, throughputController, user);
288      if (ret) compactionStateChangeLatch.countDown();
289      return ret;
290    }
291  }
292
293  /**
294   * Unfortunately, the easiest way to test this is to spin up a mini-cluster since we want to do
295   * the usual compaction mechanism on the region, rather than going through the backdoor to the
296   * region
297   */
298  @Test
299  public void testRegionObserverCompactionTimeStacking() throws Exception {
300    // setup a mini cluster so we can do a real compaction on a region
301    Configuration conf = UTIL.getConfiguration();
302    conf.setClass(HConstants.REGION_IMPL, CompactionCompletionNotifyingRegion.class, HRegion.class);
303    conf.setInt("hbase.hstore.compaction.min", 2);
304    UTIL.startMiniCluster();
305    byte[] ROW = Bytes.toBytes("testRow");
306    byte[] A = Bytes.toBytes("A");
307    HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(name.getMethodName()));
308    desc.addFamily(new HColumnDescriptor(A));
309    desc.addCoprocessor(EmptyRegionObsever.class.getName(), null, Coprocessor.PRIORITY_USER, null);
310    desc.addCoprocessor(NoDataFromCompaction.class.getName(), null, Coprocessor.PRIORITY_HIGHEST,
311      null);
312
313    Admin admin = UTIL.getAdmin();
314    admin.createTable(desc);
315
316    Table table = UTIL.getConnection().getTable(desc.getTableName());
317
318    // put a row and flush it to disk
319    Put put = new Put(ROW);
320    put.addColumn(A, A, A);
321    table.put(put);
322
323    HRegionServer rs = UTIL.getRSForFirstRegionInTable(desc.getTableName());
324    List<HRegion> regions = rs.getRegions(desc.getTableName());
325    assertEquals("More than 1 region serving test table with 1 row", 1, regions.size());
326    Region region = regions.get(0);
327    admin.flushRegion(region.getRegionInfo().getRegionName());
328    CountDownLatch latch = ((CompactionCompletionNotifyingRegion)region)
329        .getCompactionStateChangeLatch();
330
331    // put another row and flush that too
332    put = new Put(Bytes.toBytes("anotherrow"));
333    put.addColumn(A, A, A);
334    table.put(put);
335    admin.flushRegion(region.getRegionInfo().getRegionName());
336
337    // run a compaction, which normally would should get rid of the data
338    // wait for the compaction checker to complete
339    latch.await();
340    // check both rows to ensure that they aren't there
341    Get get = new Get(ROW);
342    Result r = table.get(get);
343    assertNull(
344      "Got an unexpected number of rows - no data should be returned with the NoDataFromScan coprocessor. Found: "
345          + r, r.listCells());
346
347    get = new Get(Bytes.toBytes("anotherrow"));
348    r = table.get(get);
349    assertNull(
350      "Got an unexpected number of rows - no data should be returned with the NoDataFromScan coprocessor Found: "
351          + r, r.listCells());
352
353    table.close();
354    UTIL.shutdownMiniCluster();
355  }
356}