View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to you under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14   * License for the specific language governing permissions and limitations
15   * under the License.
16   */
17  package org.apache.hadoop.hbase.io.hfile;
18  
19  import static org.junit.Assert.assertEquals;
20  
21  import java.io.IOException;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.List;
25  import java.util.Set;
26  
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.apache.hadoop.conf.Configuration;
30  import org.apache.hadoop.hbase.Cell;
31  import org.apache.hadoop.hbase.HBaseTestingUtility;
32  import org.apache.hadoop.hbase.HColumnDescriptor;
33  import org.apache.hadoop.hbase.HRegionInfo;
34  import org.apache.hadoop.hbase.HTableDescriptor;
35  import org.apache.hadoop.hbase.TableName;
36  import org.apache.hadoop.hbase.client.Put;
37  import org.apache.hadoop.hbase.client.Scan;
38  import org.apache.hadoop.hbase.regionserver.HRegion;
39  import org.apache.hadoop.hbase.regionserver.HStore;
40  import org.apache.hadoop.hbase.regionserver.InternalScanner;
41  import org.apache.hadoop.hbase.testclassification.MediumTests;
42  import org.apache.hadoop.hbase.util.Bytes;
43  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
44  import org.apache.hadoop.hbase.util.Threads;
45  import org.junit.Test;
46  import org.junit.experimental.categories.Category;
47  import org.junit.runner.RunWith;
48  import org.junit.runners.Parameterized;
49  import org.junit.runners.Parameterized.Parameters;
50  
51  /**
52   * Test the optimization that does not scan files where all timestamps are
53   * expired.
54   */
55  @RunWith(Parameterized.class)
56  @Category(MediumTests.class)
57  public class TestScannerSelectionUsingTTL {
58  
59    private static final Log LOG =
60        LogFactory.getLog(TestScannerSelectionUsingTTL.class);
61  
62    private static final HBaseTestingUtility TEST_UTIL =
63        new HBaseTestingUtility().createLocalHTU();
64    private static TableName TABLE = TableName.valueOf("myTable");
65    private static String FAMILY = "myCF";
66    private static byte[] FAMILY_BYTES = Bytes.toBytes(FAMILY);
67  
68    private static final int TTL_SECONDS = 10;
69    private static final int TTL_MS = TTL_SECONDS * 1000;
70  
71    private static final int NUM_EXPIRED_FILES = 2;
72    private static final int NUM_ROWS = 8;
73    private static final int NUM_COLS_PER_ROW = 5;
74  
75    public final int numFreshFiles, totalNumFiles;
76  
77    /** Whether we are specifying the exact files to compact */
78    private final boolean explicitCompaction;
79  
80    @Parameters
81    public static Collection<Object[]> parameters() {
82      List<Object[]> params = new ArrayList<Object[]>();
83      for (int numFreshFiles = 1; numFreshFiles <= 3; ++numFreshFiles) {
84        for (boolean explicitCompaction : new boolean[] { false, true }) {
85          params.add(new Object[] { numFreshFiles, explicitCompaction });
86        }
87      }
88      return params;
89    }
90  
91    public TestScannerSelectionUsingTTL(int numFreshFiles,
92        boolean explicitCompaction) {
93      this.numFreshFiles = numFreshFiles;
94      this.totalNumFiles = numFreshFiles + NUM_EXPIRED_FILES;
95      this.explicitCompaction = explicitCompaction;
96    }
97  
98    @Test
99    public void testScannerSelection() throws IOException {
100     Configuration conf = TEST_UTIL.getConfiguration();
101     conf.setBoolean("hbase.store.delete.expired.storefile", false);
102     HColumnDescriptor hcd =
103       new HColumnDescriptor(FAMILY_BYTES)
104           .setMaxVersions(Integer.MAX_VALUE)
105           .setTimeToLive(TTL_SECONDS);
106     HTableDescriptor htd = new HTableDescriptor(TABLE);
107     htd.addFamily(hcd);
108     HRegionInfo info = new HRegionInfo(TABLE);
109     HRegion region =
110         HRegion.createHRegion(info, TEST_UTIL.getDataTestDir(info.getEncodedName()),
111             conf, htd);
112 
113     long ts = EnvironmentEdgeManager.currentTime();
114     long version = 0; //make sure each new set of Put's have a new ts
115     for (int iFile = 0; iFile < totalNumFiles; ++iFile) {
116       if (iFile == NUM_EXPIRED_FILES) {
117         Threads.sleepWithoutInterrupt(TTL_MS);
118         version += TTL_MS;
119       }
120 
121       for (int iRow = 0; iRow < NUM_ROWS; ++iRow) {
122         Put put = new Put(Bytes.toBytes("row" + iRow));
123         for (int iCol = 0; iCol < NUM_COLS_PER_ROW; ++iCol) {
124           put.add(FAMILY_BYTES, Bytes.toBytes("col" + iCol),
125               ts + version, Bytes.toBytes("value" + iFile + "_" + iRow + "_" + iCol));
126         }
127         region.put(put);
128       }
129       region.flush(true);
130       version++;
131     }
132 
133     Scan scan = new Scan();
134     scan.setMaxVersions(Integer.MAX_VALUE);
135     CacheConfig cacheConf = new CacheConfig(conf);
136     LruBlockCache cache = (LruBlockCache) cacheConf.getBlockCache();
137     cache.clearCache();
138     InternalScanner scanner = region.getScanner(scan);
139     List<Cell> results = new ArrayList<Cell>();
140     final int expectedKVsPerRow = numFreshFiles * NUM_COLS_PER_ROW;
141     int numReturnedRows = 0;
142     LOG.info("Scanning the entire table");
143     while (scanner.next(results) || results.size() > 0) {
144       assertEquals(expectedKVsPerRow, results.size());
145       ++numReturnedRows;
146       results.clear();
147     }
148     assertEquals(NUM_ROWS, numReturnedRows);
149     Set<String> accessedFiles = cache.getCachedFileNamesForTest();
150     LOG.debug("Files accessed during scan: " + accessedFiles);
151 
152     // Exercise both compaction codepaths.
153     if (explicitCompaction) {
154       HStore store = (HStore)region.getStore(FAMILY_BYTES);
155       store.compactRecentForTestingAssumingDefaultPolicy(totalNumFiles);
156     } else {
157       region.compact(false);
158     }
159 
160     region.close();
161   }
162 }