View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import java.io.IOException;
22  
23  import com.google.common.collect.ImmutableList;
24  import org.apache.commons.lang.RandomStringUtils;
25  import org.apache.hadoop.hbase.HBaseTestingUtility;
26  import org.apache.hadoop.hbase.HColumnDescriptor;
27  import org.apache.hadoop.hbase.client.Get;
28  import org.apache.hadoop.hbase.client.Put;
29  import org.apache.hadoop.hbase.filter.TimestampsFilter;
30  import org.apache.hadoop.hbase.testclassification.LargeTests;
31  import org.apache.hadoop.hbase.testclassification.RegionServerTests;
32  import org.apache.hadoop.hbase.util.Bytes;
33  import org.junit.Before;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  
37  import static org.junit.Assert.assertTrue;
38  
39  @Category({RegionServerTests.class, LargeTests.class})
40  public class TestTimestampFilterSeekHint {
41  
42    private final static HBaseTestingUtility TEST_UTIL = HBaseTestingUtility.createLocalHTU();
43    private final static String RK = "myRK";
44    private final static byte[] RK_BYTES = Bytes.toBytes(RK);
45  
46    private final static String FAMILY = "D";
47    private final static byte[] FAMILY_BYTES = Bytes.toBytes(FAMILY);
48  
49    private final static String QUAL = "0";
50    private final static byte[] QUAL_BYTES = Bytes.toBytes(QUAL);
51  
52    public static final int MAX_VERSIONS = 50000;
53    private HRegion region;
54    private int regionCount = 0;
55  
56    @Test
57    public void testGetSeek() throws IOException {
58      StoreFileScanner.instrument();
59      prepareRegion();
60  
61      Get g = new Get(RK_BYTES);
62      final TimestampsFilter timestampsFilter = new TimestampsFilter(ImmutableList.of(5L), true);
63      g.setFilter(timestampsFilter);
64      final long initialSeekCount = StoreFileScanner.getSeekCount();
65      region.get(g);
66      final long finalSeekCount = StoreFileScanner.getSeekCount();
67  
68      /*
69        Make sure there's more than one.
70        Aka one seek to get to the row, and one to get to the time.
71      */
72      assertTrue(finalSeekCount >= initialSeekCount + 3 );
73    }
74  
75    @Test
76    public void testGetDoesntSeekWithNoHint() throws IOException {
77      StoreFileScanner.instrument();
78      prepareRegion();
79  
80      Get g = new Get(RK_BYTES);
81      g.setFilter(new TimestampsFilter(ImmutableList.of(5L)));
82      final long initialSeekCount = StoreFileScanner.getSeekCount();
83      region.get(g);
84      final long finalSeekCount = StoreFileScanner.getSeekCount();
85  
86      assertTrue(finalSeekCount >= initialSeekCount );
87      assertTrue(finalSeekCount < initialSeekCount + 3);
88    }
89  
90    @Before
91    public void prepareRegion() throws IOException {
92      region =
93          TEST_UTIL.createTestRegion("TestTimestampFilterSeekHint" + regionCount++,
94              new HColumnDescriptor(FAMILY)
95                  .setBlocksize(1024)
96                  .setMaxVersions(MAX_VERSIONS)
97          );
98  
99      for (long i = 0; i <MAX_VERSIONS - 2; i++) {
100       Put p = new Put(RK_BYTES, i);
101       p.add(FAMILY_BYTES, QUAL_BYTES, Bytes.toBytes(RandomStringUtils.randomAlphabetic(255)));
102       region.put(p);
103     }
104     region.flushcache();
105   }
106 }