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.Map;
26  import java.util.Set;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.apache.hadoop.conf.Configuration;
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.KeyValue;
36  import org.apache.hadoop.hbase.SmallTests;
37  import org.apache.hadoop.hbase.client.Put;
38  import org.apache.hadoop.hbase.client.Scan;
39  import org.apache.hadoop.hbase.io.hfile.BlockType.BlockCategory;
40  import org.apache.hadoop.hbase.regionserver.HRegion;
41  import org.apache.hadoop.hbase.regionserver.InternalScanner;
42  import org.apache.hadoop.hbase.regionserver.metrics.SchemaMetrics;
43  import org.apache.hadoop.hbase.regionserver.metrics.SchemaMetrics.BlockMetricType;
44  import org.apache.hadoop.hbase.util.Bytes;
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(SmallTests.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();
65    private static String TABLE = "myTable";
66    private static String FAMILY = "myCF";
67    private static byte[] FAMILY_BYTES = Bytes.toBytes(FAMILY);
68  
69    private static final int TTL_SECONDS = 2;
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(Bytes.toBytes(TABLE));
110     HRegion region =
111         HRegion.createHRegion(info, TEST_UTIL.getClusterTestDir(),
112             conf, htd);
113 
114     for (int iFile = 0; iFile < totalNumFiles; ++iFile) {
115       if (iFile == NUM_EXPIRED_FILES) {
116         Threads.sleepWithoutInterrupt(TTL_MS);
117       }
118 
119       for (int iRow = 0; iRow < NUM_ROWS; ++iRow) {
120         Put put = new Put(Bytes.toBytes("row" + iRow));
121         for (int iCol = 0; iCol < NUM_COLS_PER_ROW; ++iCol) {
122           put.add(FAMILY_BYTES, Bytes.toBytes("col" + iCol),
123               Bytes.toBytes("value" + iFile + "_" + iRow + "_" + iCol));
124         }
125         region.put(put);
126       }
127       region.flushcache();
128     }
129 
130     Scan scan = new Scan();
131     scan.setMaxVersions(Integer.MAX_VALUE);
132     CacheConfig cacheConf = new CacheConfig(conf);
133     LruBlockCache cache = (LruBlockCache) cacheConf.getBlockCache();
134     cache.clearCache();
135     InternalScanner scanner = region.getScanner(scan);
136     List<KeyValue> results = new ArrayList<KeyValue>();
137     final int expectedKVsPerRow = numFreshFiles * NUM_COLS_PER_ROW;
138     int numReturnedRows = 0;
139     LOG.info("Scanning the entire table");
140     while (scanner.next(results) || results.size() > 0) {
141       assertEquals(expectedKVsPerRow, results.size());
142       ++numReturnedRows;
143       results.clear();
144     }
145     assertEquals(NUM_ROWS, numReturnedRows);
146     Set<String> accessedFiles = cache.getCachedFileNamesForTest();
147     LOG.debug("Files accessed during scan: " + accessedFiles);
148 
149     Map<String, Long> metricsBeforeCompaction =
150       SchemaMetrics.getMetricsSnapshot();
151 
152     // Exercise both compaction codepaths.
153     if (explicitCompaction) {
154       region.getStore(FAMILY_BYTES).compactRecentForTesting(totalNumFiles);
155     } else {
156       region.compactStores();
157     }
158 
159     SchemaMetrics.validateMetricChanges(metricsBeforeCompaction);
160     Map<String, Long> compactionMetrics =
161         SchemaMetrics.diffMetrics(metricsBeforeCompaction,
162             SchemaMetrics.getMetricsSnapshot());
163     long compactionDataBlocksRead = SchemaMetrics.getLong(
164         compactionMetrics,
165         SchemaMetrics.getInstance(TABLE, FAMILY).getBlockMetricName(
166             BlockCategory.DATA, true, BlockMetricType.READ_COUNT));
167     assertEquals("Invalid number of blocks accessed during compaction. " +
168         "We only expect non-expired files to be accessed.",
169         numFreshFiles, compactionDataBlocksRead);
170     region.close();
171   }
172 
173 }