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.TableName;
32  import org.apache.hadoop.hbase.HBaseTestingUtility;
33  import org.apache.hadoop.hbase.HColumnDescriptor;
34  import org.apache.hadoop.hbase.HRegionInfo;
35  import org.apache.hadoop.hbase.HTableDescriptor;
36  import org.apache.hadoop.hbase.KeyValue;
37  import org.apache.hadoop.hbase.MediumTests;
38  import org.apache.hadoop.hbase.client.Put;
39  import org.apache.hadoop.hbase.client.Scan;
40  import org.apache.hadoop.hbase.regionserver.HRegion;
41  import org.apache.hadoop.hbase.regionserver.HStore;
42  import org.apache.hadoop.hbase.regionserver.InternalScanner;
43  import org.apache.hadoop.hbase.util.Bytes;
44  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
45  import org.apache.hadoop.hbase.util.Threads;
46  import org.junit.Test;
47  import org.junit.experimental.categories.Category;
48  import org.junit.runner.RunWith;
49  import org.junit.runners.Parameterized;
50  import org.junit.runners.Parameterized.Parameters;
51  
52  /**
53   * Test the optimization that does not scan files where all timestamps are
54   * expired.
55   */
56  @RunWith(Parameterized.class)
57  @Category(MediumTests.class)
58  public class TestScannerSelectionUsingTTL {
59  
60    private static final Log LOG =
61        LogFactory.getLog(TestScannerSelectionUsingTTL.class);
62  
63    private static final HBaseTestingUtility TEST_UTIL =
64        new HBaseTestingUtility().createLocalHTU();
65    private static TableName TABLE = TableName.valueOf("myTable");
66    private static String FAMILY = "myCF";
67    private static byte[] FAMILY_BYTES = Bytes.toBytes(FAMILY);
68  
69    private static final int TTL_SECONDS = 10;
70    private static final int TTL_MS = TTL_SECONDS * 1000;
71  
72    private static final int NUM_EXPIRED_FILES = 2;
73    private static final int NUM_ROWS = 8;
74    private static final int NUM_COLS_PER_ROW = 5;
75  
76    public final int numFreshFiles, totalNumFiles;
77  
78    /** Whether we are specifying the exact files to compact */
79    private final boolean explicitCompaction;
80  
81    @Parameters
82    public static Collection<Object[]> parameters() {
83      List<Object[]> params = new ArrayList<Object[]>();
84      for (int numFreshFiles = 1; numFreshFiles <= 3; ++numFreshFiles) {
85        for (boolean explicitCompaction : new boolean[] { false, true }) {
86          params.add(new Object[] { numFreshFiles, explicitCompaction });
87        }
88      }
89      return params;
90    }
91  
92    public TestScannerSelectionUsingTTL(int numFreshFiles,
93        boolean explicitCompaction) {
94      this.numFreshFiles = numFreshFiles;
95      this.totalNumFiles = numFreshFiles + NUM_EXPIRED_FILES;
96      this.explicitCompaction = explicitCompaction;
97    }
98  
99    @Test
100   public void testScannerSelection() throws IOException {
101     Configuration conf = TEST_UTIL.getConfiguration();
102     conf.setBoolean("hbase.store.delete.expired.storefile", false);
103     HColumnDescriptor hcd =
104       new HColumnDescriptor(FAMILY_BYTES)
105           .setMaxVersions(Integer.MAX_VALUE)
106           .setTimeToLive(TTL_SECONDS);
107     HTableDescriptor htd = new HTableDescriptor(TABLE);
108     htd.addFamily(hcd);
109     HRegionInfo info = new HRegionInfo(TABLE);
110     HRegion region =
111         HRegion.createHRegion(info, TEST_UTIL.getDataTestDir(info.getEncodedName()),
112             conf, htd);
113 
114     long ts = EnvironmentEdgeManager.currentTimeMillis();
115     long version = 0; //make sure each new set of Put's have a new ts
116     for (int iFile = 0; iFile < totalNumFiles; ++iFile) {
117       if (iFile == NUM_EXPIRED_FILES) {
118         Threads.sleepWithoutInterrupt(TTL_MS);
119         version += TTL_MS;
120       }
121 
122       for (int iRow = 0; iRow < NUM_ROWS; ++iRow) {
123         Put put = new Put(Bytes.toBytes("row" + iRow));
124         for (int iCol = 0; iCol < NUM_COLS_PER_ROW; ++iCol) {
125           put.add(FAMILY_BYTES, Bytes.toBytes("col" + iCol),
126               ts + version, Bytes.toBytes("value" + iFile + "_" + iRow + "_" + iCol));
127         }
128         region.put(put);
129       }
130       region.flushcache();
131       version++;
132     }
133 
134     Scan scan = new Scan();
135     scan.setMaxVersions(Integer.MAX_VALUE);
136     CacheConfig cacheConf = new CacheConfig(conf);
137     LruBlockCache cache = (LruBlockCache) cacheConf.getBlockCache();
138     cache.clearCache();
139     InternalScanner scanner = region.getScanner(scan);
140     List<Cell> results = new ArrayList<Cell>();
141     final int expectedKVsPerRow = numFreshFiles * NUM_COLS_PER_ROW;
142     int numReturnedRows = 0;
143     LOG.info("Scanning the entire table");
144     while (scanner.next(results) || results.size() > 0) {
145       assertEquals(expectedKVsPerRow, results.size());
146       ++numReturnedRows;
147       results.clear();
148     }
149     assertEquals(NUM_ROWS, numReturnedRows);
150     Set<String> accessedFiles = cache.getCachedFileNamesForTest();
151     LOG.debug("Files accessed during scan: " + accessedFiles);
152 
153     // Exercise both compaction codepaths.
154     if (explicitCompaction) {
155       HStore store = (HStore)region.getStore(FAMILY_BYTES);
156       store.compactRecentForTestingAssumingDefaultPolicy(totalNumFiles);
157     } else {
158       region.compactStores();
159     }
160 
161     region.close();
162   }
163 }