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.regionserver.querymatcher;
019
020import static org.junit.Assert.assertArrayEquals;
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.assertFalse;
023
024import java.io.IOException;
025import java.util.ArrayList;
026import java.util.List;
027import org.apache.hadoop.hbase.Cell;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HConstants;
030import org.apache.hadoop.hbase.KeepDeletedCells;
031import org.apache.hadoop.hbase.KeyValue;
032import org.apache.hadoop.hbase.PrivateCellUtil;
033import org.apache.hadoop.hbase.client.Scan;
034import org.apache.hadoop.hbase.filter.FilterBase;
035import org.apache.hadoop.hbase.regionserver.ScanInfo;
036import org.apache.hadoop.hbase.regionserver.querymatcher.ScanQueryMatcher.MatchCode;
037import org.apache.hadoop.hbase.testclassification.RegionServerTests;
038import org.apache.hadoop.hbase.testclassification.SmallTests;
039import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
040import org.junit.ClassRule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043import org.slf4j.Logger;
044import org.slf4j.LoggerFactory;
045
046@Category({ RegionServerTests.class, SmallTests.class })
047public class TestUserScanQueryMatcher extends AbstractTestScanQueryMatcher {
048
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE =
051      HBaseClassTestRule.forClass(TestUserScanQueryMatcher.class);
052
053  private static final Logger LOG = LoggerFactory.getLogger(TestUserScanQueryMatcher.class);
054
055  /**
056   * This is a cryptic test. It is checking that we don't include a fake cell. See HBASE-16074 for
057   * background.
058   */
059  @Test
060  public void testNeverIncludeFakeCell() throws IOException {
061    long now = EnvironmentEdgeManager.currentTime();
062    // Do with fam2 which has a col2 qualifier.
063    UserScanQueryMatcher qm = UserScanQueryMatcher.create(scan,
064      new ScanInfo(this.conf, fam2, 10, 1, ttl, KeepDeletedCells.FALSE,
065          HConstants.DEFAULT_BLOCKSIZE, 0, rowComparator, false),
066      get.getFamilyMap().get(fam2), now - ttl, now, null);
067    Cell kv = new KeyValue(row1, fam2, col2, 1, data);
068    Cell cell = PrivateCellUtil.createLastOnRowCol(kv);
069    qm.setToNewRow(kv);
070    MatchCode code = qm.match(cell);
071    assertFalse(code.compareTo(MatchCode.SEEK_NEXT_COL) != 0);
072  }
073
074  @Test
075  public void testMatchExplicitColumns() throws IOException {
076    // Moving up from the Tracker by using Gets and List<KeyValue> instead
077    // of just byte []
078
079    // Expected result
080    List<MatchCode> expected = new ArrayList<>(6);
081    expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
082    expected.add(ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_COL);
083    expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
084    expected.add(ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_COL);
085    expected.add(ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_ROW);
086    expected.add(ScanQueryMatcher.MatchCode.DONE);
087
088    long now = EnvironmentEdgeManager.currentTime();
089    // 2,4,5
090    UserScanQueryMatcher qm = UserScanQueryMatcher.create(
091      scan, new ScanInfo(this.conf, fam2, 0, 1, ttl, KeepDeletedCells.FALSE,
092          HConstants.DEFAULT_BLOCKSIZE, 0, rowComparator, false),
093      get.getFamilyMap().get(fam2), now - ttl, now, null);
094
095    List<KeyValue> memstore = new ArrayList<>(6);
096    memstore.add(new KeyValue(row1, fam2, col1, 1, data));
097    memstore.add(new KeyValue(row1, fam2, col2, 1, data));
098    memstore.add(new KeyValue(row1, fam2, col3, 1, data));
099    memstore.add(new KeyValue(row1, fam2, col4, 1, data));
100    memstore.add(new KeyValue(row1, fam2, col5, 1, data));
101
102    memstore.add(new KeyValue(row2, fam1, col1, data));
103
104    List<ScanQueryMatcher.MatchCode> actual = new ArrayList<>(memstore.size());
105    KeyValue k = memstore.get(0);
106    qm.setToNewRow(k);
107
108    for (KeyValue kv : memstore) {
109      actual.add(qm.match(kv));
110    }
111
112    assertEquals(expected.size(), actual.size());
113    for (int i = 0; i < expected.size(); i++) {
114      LOG.debug("expected " + expected.get(i) + ", actual " + actual.get(i));
115      assertEquals(expected.get(i), actual.get(i));
116    }
117  }
118
119  @Test
120  public void testMatch_Wildcard() throws IOException {
121    // Moving up from the Tracker by using Gets and List<KeyValue> instead
122    // of just byte []
123
124    // Expected result
125    List<MatchCode> expected = new ArrayList<>(6);
126    expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
127    expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
128    expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
129    expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
130    expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
131    expected.add(ScanQueryMatcher.MatchCode.DONE);
132
133    long now = EnvironmentEdgeManager.currentTime();
134    UserScanQueryMatcher qm = UserScanQueryMatcher.create(scan, new ScanInfo(this.conf, fam2, 0, 1,
135        ttl, KeepDeletedCells.FALSE, HConstants.DEFAULT_BLOCKSIZE, 0, rowComparator, false),
136      null, now - ttl, now, null);
137
138    List<KeyValue> memstore = new ArrayList<>(6);
139    memstore.add(new KeyValue(row1, fam2, col1, 1, data));
140    memstore.add(new KeyValue(row1, fam2, col2, 1, data));
141    memstore.add(new KeyValue(row1, fam2, col3, 1, data));
142    memstore.add(new KeyValue(row1, fam2, col4, 1, data));
143    memstore.add(new KeyValue(row1, fam2, col5, 1, data));
144    memstore.add(new KeyValue(row2, fam1, col1, 1, data));
145
146    List<ScanQueryMatcher.MatchCode> actual = new ArrayList<>(memstore.size());
147
148    KeyValue k = memstore.get(0);
149    qm.setToNewRow(k);
150
151    for (KeyValue kv : memstore) {
152      actual.add(qm.match(kv));
153    }
154
155    assertEquals(expected.size(), actual.size());
156    for (int i = 0; i < expected.size(); i++) {
157      LOG.debug("expected " + expected.get(i) + ", actual " + actual.get(i));
158      assertEquals(expected.get(i), actual.get(i));
159    }
160  }
161
162  /**
163   * Verify that {@link ScanQueryMatcher} only skips expired KeyValue instances and does not exit
164   * early from the row (skipping later non-expired KeyValues). This version mimics a Get with
165   * explicitly specified column qualifiers.
166   * @throws IOException
167   */
168  @Test
169  public void testMatch_ExpiredExplicit() throws IOException {
170
171    long testTTL = 1000;
172    MatchCode[] expected = new MatchCode[] { ScanQueryMatcher.MatchCode.SEEK_NEXT_COL,
173        ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_COL,
174        ScanQueryMatcher.MatchCode.SEEK_NEXT_COL,
175        ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_COL,
176        ScanQueryMatcher.MatchCode.SEEK_NEXT_ROW, ScanQueryMatcher.MatchCode.DONE };
177
178    long now = EnvironmentEdgeManager.currentTime();
179    UserScanQueryMatcher qm = UserScanQueryMatcher.create(scan,
180      new ScanInfo(this.conf, fam2, 0, 1, testTTL, KeepDeletedCells.FALSE,
181          HConstants.DEFAULT_BLOCKSIZE, 0, rowComparator, false),
182      get.getFamilyMap().get(fam2), now - testTTL, now, null);
183
184    KeyValue[] kvs = new KeyValue[] { new KeyValue(row1, fam2, col1, now - 100, data),
185        new KeyValue(row1, fam2, col2, now - 50, data),
186        new KeyValue(row1, fam2, col3, now - 5000, data),
187        new KeyValue(row1, fam2, col4, now - 500, data),
188        new KeyValue(row1, fam2, col5, now - 10000, data),
189        new KeyValue(row2, fam1, col1, now - 10, data) };
190
191    KeyValue k = kvs[0];
192    qm.setToNewRow(k);
193
194    List<MatchCode> actual = new ArrayList<>(kvs.length);
195    for (KeyValue kv : kvs) {
196      actual.add(qm.match(kv));
197    }
198
199    assertEquals(expected.length, actual.size());
200    for (int i = 0; i < expected.length; i++) {
201      LOG.debug("expected " + expected[i] + ", actual " + actual.get(i));
202      assertEquals(expected[i], actual.get(i));
203    }
204  }
205
206  /**
207   * Verify that {@link ScanQueryMatcher} only skips expired KeyValue instances and does not exit
208   * early from the row (skipping later non-expired KeyValues). This version mimics a Get with
209   * wildcard-inferred column qualifiers.
210   * @throws IOException
211   */
212  @Test
213  public void testMatch_ExpiredWildcard() throws IOException {
214
215    long testTTL = 1000;
216    MatchCode[] expected =
217        new MatchCode[] { ScanQueryMatcher.MatchCode.INCLUDE, ScanQueryMatcher.MatchCode.INCLUDE,
218          ScanQueryMatcher.MatchCode.SEEK_NEXT_COL, ScanQueryMatcher.MatchCode.INCLUDE,
219          ScanQueryMatcher.MatchCode.SEEK_NEXT_COL, ScanQueryMatcher.MatchCode.DONE };
220
221    long now = EnvironmentEdgeManager.currentTime();
222    UserScanQueryMatcher qm =
223        UserScanQueryMatcher.create(scan,
224          new ScanInfo(this.conf, fam2, 0, 1, testTTL, KeepDeletedCells.FALSE,
225              HConstants.DEFAULT_BLOCKSIZE, 0, rowComparator, false),
226          null, now - testTTL, now, null);
227
228    KeyValue[] kvs = new KeyValue[] { new KeyValue(row1, fam2, col1, now - 100, data),
229        new KeyValue(row1, fam2, col2, now - 50, data),
230        new KeyValue(row1, fam2, col3, now - 5000, data),
231        new KeyValue(row1, fam2, col4, now - 500, data),
232        new KeyValue(row1, fam2, col5, now - 10000, data),
233        new KeyValue(row2, fam1, col1, now - 10, data) };
234    KeyValue k = kvs[0];
235    qm.setToNewRow(k);
236
237    List<ScanQueryMatcher.MatchCode> actual = new ArrayList<>(kvs.length);
238    for (KeyValue kv : kvs) {
239      actual.add(qm.match(kv));
240    }
241
242    assertEquals(expected.length, actual.size());
243    for (int i = 0; i < expected.length; i++) {
244      LOG.debug("expected " + expected[i] + ", actual " + actual.get(i));
245      assertEquals(expected[i], actual.get(i));
246    }
247  }
248
249  private static class AlwaysIncludeAndSeekNextRowFilter extends FilterBase {
250    @Override
251    public ReturnCode filterCell(final Cell c) {
252      return ReturnCode.INCLUDE_AND_SEEK_NEXT_ROW;
253    }
254  }
255
256  @Test
257  public void testMatchWhenFilterReturnsIncludeAndSeekNextRow() throws IOException {
258    List<MatchCode> expected = new ArrayList<>();
259    expected.add(ScanQueryMatcher.MatchCode.INCLUDE_AND_SEEK_NEXT_ROW);
260    expected.add(ScanQueryMatcher.MatchCode.DONE);
261
262    Scan scanWithFilter = new Scan(scan).setFilter(new AlwaysIncludeAndSeekNextRowFilter());
263
264    long now = EnvironmentEdgeManager.currentTime();
265
266    // scan with column 2,4,5
267    UserScanQueryMatcher qm = UserScanQueryMatcher.create(
268      scanWithFilter, new ScanInfo(this.conf, fam2, 0, 1, ttl, KeepDeletedCells.FALSE,
269          HConstants.DEFAULT_BLOCKSIZE, 0, rowComparator, false),
270      get.getFamilyMap().get(fam2), now - ttl, now, null);
271
272    List<KeyValue> memstore = new ArrayList<>();
273    // ColumnTracker will return INCLUDE_AND_SEEK_NEXT_COL , and filter will return
274    // INCLUDE_AND_SEEK_NEXT_ROW, so final match code will be INCLUDE_AND_SEEK_NEXT_ROW.
275    memstore.add(new KeyValue(row1, fam2, col2, 1, data));
276    memstore.add(new KeyValue(row2, fam1, col1, data));
277
278    List<ScanQueryMatcher.MatchCode> actual = new ArrayList<>(memstore.size());
279    KeyValue k = memstore.get(0);
280    qm.setToNewRow(k);
281
282    for (KeyValue kv : memstore) {
283      actual.add(qm.match(kv));
284    }
285
286    assertEquals(expected.size(), actual.size());
287    for (int i = 0; i < expected.size(); i++) {
288      LOG.debug("expected " + expected.get(i) + ", actual " + actual.get(i));
289      assertEquals(expected.get(i), actual.get(i));
290    }
291  }
292
293  private static class AlwaysIncludeFilter extends FilterBase {
294    @Override
295    public ReturnCode filterCell(final Cell c) {
296      return ReturnCode.INCLUDE;
297    }
298  }
299
300  /**
301   * Here is the unit test for UserScanQueryMatcher#mergeFilterResponse, when the number of cells
302   * exceed the versions requested in scan, we should return SEEK_NEXT_COL, but if current match
303   * code is INCLUDE_AND_SEEK_NEXT_ROW, we can optimize to choose the max step between SEEK_NEXT_COL
304   * and INCLUDE_AND_SEEK_NEXT_ROW, which is SEEK_NEXT_ROW. <br/>
305   */
306  @Test
307  public void testMergeFilterResponseCase1() throws IOException {
308    List<MatchCode> expected = new ArrayList<>();
309    expected.add(MatchCode.INCLUDE);
310    expected.add(MatchCode.INCLUDE);
311    expected.add(MatchCode.SEEK_NEXT_ROW);
312
313    Scan scanWithFilter = new Scan(scan).setFilter(new AlwaysIncludeFilter()).readVersions(2);
314
315    long now = EnvironmentEdgeManager.currentTime();
316    // scan with column 2,4,5, the family with maxVersion = 3
317    UserScanQueryMatcher qm = UserScanQueryMatcher.create(
318      scanWithFilter, new ScanInfo(this.conf, fam2, 0, 3, ttl, KeepDeletedCells.FALSE,
319          HConstants.DEFAULT_BLOCKSIZE, 0, rowComparator, false),
320      get.getFamilyMap().get(fam2), now - ttl, now, null);
321
322    List<KeyValue> memstore = new ArrayList<>();
323    memstore.add(new KeyValue(row1, fam1, col5, 1, data)); // match code will be INCLUDE
324    memstore.add(new KeyValue(row1, fam1, col5, 2, data)); // match code will be INCLUDE
325
326    // match code will be SEEK_NEXT_ROW , which is max(INCLUDE_AND_SEEK_NEXT_ROW, SEEK_NEXT_COL).
327    memstore.add(new KeyValue(row1, fam1, col5, 3, data));
328
329    KeyValue k = memstore.get(0);
330    qm.setToNewRow(k);
331
332    for (int i = 0; i < memstore.size(); i++) {
333      assertEquals(expected.get(i), qm.match(memstore.get(i)));
334    }
335
336    scanWithFilter = new Scan(scan).setFilter(new AlwaysIncludeFilter()).readVersions(1);
337    qm = UserScanQueryMatcher.create(
338      scanWithFilter, new ScanInfo(this.conf, fam2, 0, 2, ttl, KeepDeletedCells.FALSE,
339          HConstants.DEFAULT_BLOCKSIZE, 0, rowComparator, false),
340      get.getFamilyMap().get(fam2), now - ttl, now, null);
341
342    List<KeyValue> memstore2 = new ArrayList<>();
343    memstore2.add(new KeyValue(row2, fam1, col2, 1, data)); // match code will be INCLUDE
344    // match code will be SEEK_NEXT_COL, which is max(INCLUDE_AND_SEEK_NEXT_COL, SEEK_NEXT_COL).
345    memstore2.add(new KeyValue(row2, fam1, col2, 2, data));
346
347    k = memstore2.get(0);
348    qm.setToNewRow(k);
349
350    assertEquals(MatchCode.INCLUDE, qm.match(memstore2.get(0)));
351    assertEquals(MatchCode.SEEK_NEXT_COL, qm.match(memstore2.get(1)));
352  }
353
354  /**
355   * Here is the unit test for UserScanQueryMatcher#mergeFilterResponse: the match code may be
356   * changed to SEEK_NEXT_COL or INCLUDE_AND_SEEK_NEXT_COL after merging with filterResponse, even
357   * if the passed match code is neither SEEK_NEXT_COL nor INCLUDE_AND_SEEK_NEXT_COL. In that case,
358   * we need to make sure that the ColumnTracker has been switched to the next column. <br/>
359   * An effective test way is: we only need to check the cell from getKeyForNextColumn(). because
360   * that as long as the UserScanQueryMatcher returns SEEK_NEXT_COL or INCLUDE_AND_SEEK_NEXT_COL,
361   * UserScanQueryMatcher#getKeyForNextColumn should return an cell whose column is larger than the
362   * current cell's.
363   */
364  @Test
365  public void testMergeFilterResponseCase2() throws Exception {
366    List<MatchCode> expected = new ArrayList<>();
367    expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
368    expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
369    expected.add(ScanQueryMatcher.MatchCode.INCLUDE);
370    expected.add(ScanQueryMatcher.MatchCode.SEEK_NEXT_COL);
371
372    Scan scanWithFilter = new Scan(scan).setFilter(new AlwaysIncludeFilter()).readVersions(3);
373
374    long now = EnvironmentEdgeManager.currentTime();
375
376    // scan with column 2,4,5, the family with maxVersion = 5
377    UserScanQueryMatcher qm = UserScanQueryMatcher.create(
378      scanWithFilter, new ScanInfo(this.conf, fam2, 0, 5, ttl, KeepDeletedCells.FALSE,
379          HConstants.DEFAULT_BLOCKSIZE, 0, rowComparator, false),
380      get.getFamilyMap().get(fam2), now - ttl, now, null);
381
382    List<KeyValue> memstore = new ArrayList<>();
383
384    memstore.add(new KeyValue(row1, fam1, col2, 1, data)); // match code will be INCLUDE
385    memstore.add(new KeyValue(row1, fam1, col2, 2, data)); // match code will be INCLUDE
386    memstore.add(new KeyValue(row1, fam1, col2, 3, data)); // match code will be INCLUDE
387    memstore.add(new KeyValue(row1, fam1, col2, 4, data)); // match code will be SEEK_NEXT_COL
388
389    KeyValue k = memstore.get(0);
390    qm.setToNewRow(k);
391
392    for (int i = 0; i < memstore.size(); i++) {
393      assertEquals(expected.get(i), qm.match(memstore.get(i)));
394    }
395
396    // For last cell, the query matcher will return SEEK_NEXT_COL, and the
397    // ColumnTracker will skip to the next column, which is col4.
398    Cell lastCell = memstore.get(memstore.size() - 1);
399    Cell nextCell = qm.getKeyForNextColumn(lastCell);
400    assertArrayEquals(nextCell.getQualifierArray(), col4);
401  }
402}