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;
019
020import static org.junit.Assert.assertTrue;
021
022import java.io.IOException;
023import org.apache.commons.lang3.RandomStringUtils;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HBaseTestingUtility;
026import org.apache.hadoop.hbase.HColumnDescriptor;
027import org.apache.hadoop.hbase.client.Get;
028import org.apache.hadoop.hbase.client.Put;
029import org.apache.hadoop.hbase.filter.TimestampsFilter;
030import org.apache.hadoop.hbase.testclassification.LargeTests;
031import org.apache.hadoop.hbase.testclassification.RegionServerTests;
032import org.apache.hadoop.hbase.util.Bytes;
033import org.junit.Before;
034import org.junit.ClassRule;
035import org.junit.Test;
036import org.junit.experimental.categories.Category;
037
038import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableList;
039
040@Category({ RegionServerTests.class, LargeTests.class })
041public class TestTimestampFilterSeekHint {
042
043  @ClassRule
044  public static final HBaseClassTestRule CLASS_RULE =
045    HBaseClassTestRule.forClass(TestTimestampFilterSeekHint.class);
046
047  private final static HBaseTestingUtility TEST_UTIL = HBaseTestingUtility.createLocalHTU();
048  private final static String RK = "myRK";
049  private final static byte[] RK_BYTES = Bytes.toBytes(RK);
050
051  private final static String FAMILY = "D";
052  private final static byte[] FAMILY_BYTES = Bytes.toBytes(FAMILY);
053
054  private final static String QUAL = "0";
055  private final static byte[] QUAL_BYTES = Bytes.toBytes(QUAL);
056
057  public static final int MAX_VERSIONS = 50000;
058  private HRegion region;
059  private int regionCount = 0;
060
061  @Test
062  public void testGetSeek() throws IOException {
063    StoreFileScanner.instrument();
064    prepareRegion();
065
066    Get g = new Get(RK_BYTES);
067    final TimestampsFilter timestampsFilter = new TimestampsFilter(ImmutableList.of(5L), true);
068    g.setFilter(timestampsFilter);
069    final long initialSeekCount = StoreFileScanner.getSeekCount();
070    region.get(g);
071    final long finalSeekCount = StoreFileScanner.getSeekCount();
072
073    /*
074     * Make sure there's more than one. Aka one seek to get to the row, and one to get to the time.
075     */
076    assertTrue(finalSeekCount >= initialSeekCount + 3);
077  }
078
079  @Test
080  public void testGetDoesntSeekWithNoHint() throws IOException {
081    StoreFileScanner.instrument();
082    prepareRegion();
083
084    Get g = new Get(RK_BYTES);
085    g.setFilter(new TimestampsFilter(ImmutableList.of(5L)));
086    final long initialSeekCount = StoreFileScanner.getSeekCount();
087    region.get(g);
088    final long finalSeekCount = StoreFileScanner.getSeekCount();
089
090    assertTrue(finalSeekCount >= initialSeekCount);
091    assertTrue(finalSeekCount < initialSeekCount + 3);
092  }
093
094  @Before
095  public void prepareRegion() throws IOException {
096    region = TEST_UTIL.createTestRegion("TestTimestampFilterSeekHint" + regionCount++,
097      new HColumnDescriptor(FAMILY).setBlocksize(1024).setMaxVersions(MAX_VERSIONS));
098
099    for (long i = 0; i < MAX_VERSIONS - 2; i++) {
100      Put p = new Put(RK_BYTES, i);
101      p.addColumn(FAMILY_BYTES, QUAL_BYTES, Bytes.toBytes(RandomStringUtils.randomAlphabetic(255)));
102      region.put(p);
103    }
104    region.flush(true);
105  }
106}