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.
075      Aka one seek to get to the row, and one to get to the time.
076    */
077    assertTrue(finalSeekCount >= initialSeekCount + 3 );
078  }
079
080  @Test
081  public void testGetDoesntSeekWithNoHint() throws IOException {
082    StoreFileScanner.instrument();
083    prepareRegion();
084
085    Get g = new Get(RK_BYTES);
086    g.setFilter(new TimestampsFilter(ImmutableList.of(5L)));
087    final long initialSeekCount = StoreFileScanner.getSeekCount();
088    region.get(g);
089    final long finalSeekCount = StoreFileScanner.getSeekCount();
090
091    assertTrue(finalSeekCount >= initialSeekCount );
092    assertTrue(finalSeekCount < initialSeekCount + 3);
093  }
094
095  @Before
096  public void prepareRegion() throws IOException {
097    region =
098        TEST_UTIL.createTestRegion("TestTimestampFilterSeekHint" + regionCount++,
099            new HColumnDescriptor(FAMILY)
100                .setBlocksize(1024)
101                .setMaxVersions(MAX_VERSIONS)
102        );
103
104    for (long i = 0; i <MAX_VERSIONS - 2; i++) {
105      Put p = new Put(RK_BYTES, i);
106      p.addColumn(FAMILY_BYTES, QUAL_BYTES, Bytes.toBytes(RandomStringUtils.randomAlphabetic(255)));
107      region.put(p);
108    }
109    region.flush(true);
110  }
111}