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.HBaseTestingUtil;
026import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
027import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
028import org.apache.hadoop.hbase.client.Get;
029import org.apache.hadoop.hbase.client.Put;
030import org.apache.hadoop.hbase.filter.TimestampsFilter;
031import org.apache.hadoop.hbase.testclassification.LargeTests;
032import org.apache.hadoop.hbase.testclassification.RegionServerTests;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.junit.Before;
035import org.junit.ClassRule;
036import org.junit.Test;
037import org.junit.experimental.categories.Category;
038
039import org.apache.hbase.thirdparty.com.google.common.collect.ImmutableList;
040
041@Category({ RegionServerTests.class, LargeTests.class })
042public class TestTimestampFilterSeekHint {
043
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046    HBaseClassTestRule.forClass(TestTimestampFilterSeekHint.class);
047
048  private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
049  private final static String RK = "myRK";
050  private final static byte[] RK_BYTES = Bytes.toBytes(RK);
051
052  private final static String FAMILY = "D";
053  private final static byte[] FAMILY_BYTES = Bytes.toBytes(FAMILY);
054
055  private final static String QUAL = "0";
056  private final static byte[] QUAL_BYTES = Bytes.toBytes(QUAL);
057
058  public static final int MAX_VERSIONS = 50000;
059  private HRegion region;
060  private int regionCount = 0;
061
062  @Test
063  public void testGetSeek() throws IOException {
064    StoreFileScanner.instrument();
065    prepareRegion();
066
067    Get g = new Get(RK_BYTES);
068    final TimestampsFilter timestampsFilter = new TimestampsFilter(ImmutableList.of(5L), true);
069    g.setFilter(timestampsFilter);
070    final long initialSeekCount = StoreFileScanner.getSeekCount();
071    region.get(g);
072    final long finalSeekCount = StoreFileScanner.getSeekCount();
073
074    /*
075     * Make sure there's more than one. 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    ColumnFamilyDescriptor columnFamilyDescriptor = ColumnFamilyDescriptorBuilder
098      .newBuilder(Bytes.toBytes(FAMILY)).setBlocksize(1024).setMaxVersions(MAX_VERSIONS).build();
099    region = TEST_UTIL.createTestRegion("TestTimestampFilterSeekHint" + regionCount++,
100      columnFamilyDescriptor);
101
102    for (long i = 0; i < MAX_VERSIONS - 2; i++) {
103      Put p = new Put(RK_BYTES, i);
104      p.addColumn(FAMILY_BYTES, QUAL_BYTES, Bytes.toBytes(RandomStringUtils.randomAlphabetic(255)));
105      region.put(p);
106    }
107    region.flush(true);
108  }
109}