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  
20  package org.apache.hadoop.hbase.regionserver;
21  
22  import static org.junit.Assert.assertTrue;
23  
24  import java.io.IOException;
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.List;
28  import java.util.Random;
29  
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.hadoop.conf.Configuration;
33  import org.apache.hadoop.fs.FileSystem;
34  import org.apache.hadoop.fs.Path;
35  import org.apache.hadoop.hbase.HBaseTestingUtility;
36  import org.apache.hadoop.hbase.HColumnDescriptor;
37  import org.apache.hadoop.hbase.HRegionInfo;
38  import org.apache.hadoop.hbase.HTableDescriptor;
39  import org.apache.hadoop.hbase.KeyValue;
40  import org.apache.hadoop.hbase.testclassification.MediumTests;
41  import org.apache.hadoop.hbase.TableName;
42  import org.apache.hadoop.hbase.fs.HFileSystem;
43  import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
44  import org.apache.hadoop.hbase.io.hfile.BlockCache;
45  import org.apache.hadoop.hbase.io.hfile.BlockCacheKey;
46  import org.apache.hadoop.hbase.io.hfile.BlockType;
47  import org.apache.hadoop.hbase.io.hfile.CacheConfig;
48  import org.apache.hadoop.hbase.io.hfile.HFile;
49  import org.apache.hadoop.hbase.io.hfile.HFileBlock;
50  import org.apache.hadoop.hbase.io.hfile.HFileReaderV2;
51  import org.apache.hadoop.hbase.io.hfile.HFileScanner;
52  import org.apache.hadoop.hbase.io.hfile.TestHFileWriterV2;
53  import org.apache.hadoop.hbase.wal.DefaultWALProvider;
54  import org.apache.hadoop.hbase.wal.WALFactory;
55  import org.apache.hadoop.hbase.util.Bytes;
56  import org.apache.hadoop.hbase.util.FSUtils;
57  import org.junit.After;
58  import org.junit.Before;
59  import org.junit.Rule;
60  import org.junit.Test;
61  import org.junit.experimental.categories.Category;
62  import org.junit.rules.TestName;
63  import org.junit.runner.RunWith;
64  import org.junit.runners.Parameterized;
65  import org.junit.runners.Parameterized.Parameters;
66  
67  /**
68   * Tests {@link HFile} cache-on-write functionality for data blocks, non-root
69   * index blocks, and Bloom filter blocks, as specified by the column family.
70   */
71  @RunWith(Parameterized.class)
72  @Category(MediumTests.class)
73  public class TestCacheOnWriteInSchema {
74  
75    private static final Log LOG = LogFactory.getLog(TestCacheOnWriteInSchema.class);
76    @Rule public TestName name = new TestName();
77  
78    private static final HBaseTestingUtility TEST_UTIL = HBaseTestingUtility.createLocalHTU();
79    private static final String DIR = TEST_UTIL.getDataTestDir("TestCacheOnWriteInSchema").toString();
80    private static byte [] table;
81    private static byte [] family = Bytes.toBytes("family");
82    private static final int NUM_KV = 25000;
83    private static final Random rand = new Random(12983177L);
84    /** The number of valid key types possible in a store file */
85    private static final int NUM_VALID_KEY_TYPES =
86        KeyValue.Type.values().length - 2;
87  
88    private static enum CacheOnWriteType {
89      DATA_BLOCKS(BlockType.DATA, BlockType.ENCODED_DATA),
90      BLOOM_BLOCKS(BlockType.BLOOM_CHUNK),
91      INDEX_BLOCKS(BlockType.LEAF_INDEX, BlockType.INTERMEDIATE_INDEX);
92  
93      private final BlockType blockType1;
94      private final BlockType blockType2;
95  
96      private CacheOnWriteType(BlockType blockType) {
97        this(blockType, blockType);
98      }
99  
100     private CacheOnWriteType(BlockType blockType1, BlockType blockType2) {
101       this.blockType1 = blockType1;
102       this.blockType2 = blockType2;
103     }
104 
105     public boolean shouldBeCached(BlockType blockType) {
106       return blockType == blockType1 || blockType == blockType2;
107     }
108 
109     public void modifyFamilySchema(HColumnDescriptor family) {
110       switch (this) {
111       case DATA_BLOCKS:
112         family.setCacheDataOnWrite(true);
113         break;
114       case BLOOM_BLOCKS:
115         family.setCacheBloomsOnWrite(true);
116         break;
117       case INDEX_BLOCKS:
118         family.setCacheIndexesOnWrite(true);
119         break;
120       }
121     }
122   }
123 
124   private final CacheOnWriteType cowType;
125   private Configuration conf;
126   private final String testDescription;
127   private HRegion region;
128   private HStore store;
129   private WALFactory walFactory;
130   private FileSystem fs;
131 
132   public TestCacheOnWriteInSchema(CacheOnWriteType cowType) {
133     this.cowType = cowType;
134     testDescription = "[cacheOnWrite=" + cowType + "]";
135     System.out.println(testDescription);
136   }
137 
138   @Parameters
139   public static Collection<Object[]> getParameters() {
140     List<Object[]> cowTypes = new ArrayList<Object[]>();
141     for (CacheOnWriteType cowType : CacheOnWriteType.values()) {
142       cowTypes.add(new Object[] { cowType });
143     }
144     return cowTypes;
145   }
146 
147   @Before
148   public void setUp() throws IOException {
149     // parameterized tests add [#] suffix get rid of [ and ].
150     table = Bytes.toBytes(name.getMethodName().replaceAll("[\\[\\]]", "_"));
151 
152     conf = TEST_UTIL.getConfiguration();
153     conf.setInt(HFile.FORMAT_VERSION_KEY, HFile.MAX_FORMAT_VERSION);
154     conf.setBoolean(CacheConfig.CACHE_BLOCKS_ON_WRITE_KEY, false);
155     conf.setBoolean(CacheConfig.CACHE_INDEX_BLOCKS_ON_WRITE_KEY, false);
156     conf.setBoolean(CacheConfig.CACHE_BLOOM_BLOCKS_ON_WRITE_KEY, false);
157 
158     fs = HFileSystem.get(conf);
159 
160     // Create the schema
161     HColumnDescriptor hcd = new HColumnDescriptor(family);
162     hcd.setBloomFilterType(BloomType.ROWCOL);
163     cowType.modifyFamilySchema(hcd);
164     HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(table));
165     htd.addFamily(hcd);
166 
167     // Create a store based on the schema
168     final String id = TestCacheOnWriteInSchema.class.getName();
169     final Path logdir = new Path(FSUtils.getRootDir(conf),
170         DefaultWALProvider.getWALDirectoryName(id));
171     fs.delete(logdir, true);
172 
173     HRegionInfo info = new HRegionInfo(htd.getTableName(), null, null, false);
174     walFactory = new WALFactory(conf, null, id);
175 
176     region = TEST_UTIL.createLocalHRegion(info, htd,
177         walFactory.getWAL(info.getEncodedNameAsBytes()));
178     store = new HStore(region, hcd, conf);
179   }
180 
181   @After
182   public void tearDown() throws IOException {
183     IOException ex = null;
184     try {
185       region.close();
186     } catch (IOException e) {
187       LOG.warn("Caught Exception", e);
188       ex = e;
189     }
190     try {
191       walFactory.close();
192     } catch (IOException e) {
193       LOG.warn("Caught Exception", e);
194       ex = e;
195     }
196     try {
197       fs.delete(new Path(DIR), true);
198     } catch (IOException e) {
199       LOG.error("Could not delete " + DIR, e);
200       ex = e;
201     }
202     if (ex != null) {
203       throw ex;
204     }
205   }
206 
207   @Test
208   public void testCacheOnWriteInSchema() throws IOException {
209     // Write some random data into the store
210     StoreFile.Writer writer = store.createWriterInTmp(Integer.MAX_VALUE,
211         HFile.DEFAULT_COMPRESSION_ALGORITHM, false, true, false);
212     writeStoreFile(writer);
213     writer.close();
214     // Verify the block types of interest were cached on write
215     readStoreFile(writer.getPath());
216   }
217 
218   private void readStoreFile(Path path) throws IOException {
219     CacheConfig cacheConf = store.getCacheConfig();
220     BlockCache cache = cacheConf.getBlockCache();
221     StoreFile sf = new StoreFile(fs, path, conf, cacheConf,
222       BloomType.ROWCOL);
223     HFileReaderV2 reader = (HFileReaderV2) sf.createReader().getHFileReader();
224     try {
225       // Open a scanner with (on read) caching disabled
226       HFileScanner scanner = reader.getScanner(false, false);
227       assertTrue(testDescription, scanner.seekTo());
228       // Cribbed from io.hfile.TestCacheOnWrite
229       long offset = 0;
230       HFileBlock prevBlock = null;
231       while (offset < reader.getTrailer().getLoadOnOpenDataOffset()) {
232         long onDiskSize = -1;
233         if (prevBlock != null) {
234           onDiskSize = prevBlock.getNextBlockOnDiskSizeWithHeader();
235         }
236         // Flags: don't cache the block, use pread, this is not a compaction.
237         // Also, pass null for expected block type to avoid checking it.
238         HFileBlock block = reader.readBlock(offset, onDiskSize, false, true,
239           false, true, null, DataBlockEncoding.NONE);
240         BlockCacheKey blockCacheKey = new BlockCacheKey(reader.getName(),
241           offset);
242         boolean isCached = cache.getBlock(blockCacheKey, true, false, true) != null;
243         boolean shouldBeCached = cowType.shouldBeCached(block.getBlockType());
244         if (shouldBeCached != isCached) {
245           throw new AssertionError(
246             "shouldBeCached: " + shouldBeCached+ "\n" +
247             "isCached: " + isCached + "\n" +
248             "Test description: " + testDescription + "\n" +
249             "block: " + block + "\n" +
250             "blockCacheKey: " + blockCacheKey);
251         }
252         prevBlock = block;
253         offset += block.getOnDiskSizeWithHeader();
254       }
255     } finally {
256       reader.close();
257     }
258   }
259 
260   private static KeyValue.Type generateKeyType(Random rand) {
261     if (rand.nextBoolean()) {
262       // Let's make half of KVs puts.
263       return KeyValue.Type.Put;
264     } else {
265       KeyValue.Type keyType =
266           KeyValue.Type.values()[1 + rand.nextInt(NUM_VALID_KEY_TYPES)];
267       if (keyType == KeyValue.Type.Minimum || keyType == KeyValue.Type.Maximum)
268       {
269         throw new RuntimeException("Generated an invalid key type: " + keyType
270             + ". " + "Probably the layout of KeyValue.Type has changed.");
271       }
272       return keyType;
273     }
274   }
275 
276   private void writeStoreFile(StoreFile.Writer writer) throws IOException {
277     final int rowLen = 32;
278     for (int i = 0; i < NUM_KV; ++i) {
279       byte[] k = TestHFileWriterV2.randomOrderedKey(rand, i);
280       byte[] v = TestHFileWriterV2.randomValue(rand);
281       int cfLen = rand.nextInt(k.length - rowLen + 1);
282       KeyValue kv = new KeyValue(
283           k, 0, rowLen,
284           k, rowLen, cfLen,
285           k, rowLen + cfLen, k.length - rowLen - cfLen,
286           rand.nextLong(),
287           generateKeyType(rand),
288           v, 0, v.length);
289       writer.append(kv);
290     }
291   }
292 
293 }
294