View Javadoc

1   /*
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.io.hfile;
21  
22  import static org.junit.Assert.*;
23  
24  import java.io.IOException;
25  import java.util.Random;
26  
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.fs.FileSystem;
29  import org.apache.hadoop.fs.Path;
30  import org.apache.hadoop.hbase.HBaseTestingUtility;
31  import org.apache.hadoop.hbase.KeyValue;
32  import org.apache.hadoop.hbase.SmallTests;
33  import org.apache.hadoop.hbase.fs.HFileSystem;
34  import org.apache.hadoop.hbase.regionserver.StoreFile;
35  
36  import org.junit.Before;
37  import org.junit.Test;
38  import org.junit.experimental.categories.Category;
39  
40  @Category(SmallTests.class)
41  public class TestPrefetch {
42  
43    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
44  
45    private static final int NUM_VALID_KEY_TYPES = KeyValue.Type.values().length - 2;
46    private static final int DATA_BLOCK_SIZE = 2048;
47    private static final int NUM_KV = 1000;
48    private static final Random RNG = new Random();
49  
50    private Configuration conf;
51    private CacheConfig cacheConf;
52    private FileSystem fs;
53  
54    @Before
55    public void setUp() throws IOException {
56      conf = TEST_UTIL.getConfiguration();
57      conf.setInt(HFile.FORMAT_VERSION_KEY, 3);
58      conf.setBoolean(CacheConfig.PREFETCH_BLOCKS_ON_OPEN_KEY, true);
59      fs = HFileSystem.get(conf);
60      cacheConf = new CacheConfig(conf);
61    }
62  
63    @Test(timeout=60000)
64    public void testPrefetch() throws Exception {
65      Path storeFile = writeStoreFile();
66      readStoreFile(storeFile);
67    }
68  
69    private void readStoreFile(Path storeFilePath) throws Exception {
70      // Open the file
71      HFileReaderV2 reader = (HFileReaderV2) HFile.createReader(fs,
72        storeFilePath, cacheConf, conf);
73  
74      while (!((HFileReaderV3)reader).prefetchComplete()) {
75        // Sleep for a bit
76        Thread.sleep(1000);
77      }
78  
79      // Check that all of the data blocks were preloaded
80      BlockCache blockCache = cacheConf.getBlockCache();
81      long offset = 0;
82      HFileBlock prevBlock = null;
83      while (offset < reader.getTrailer().getLoadOnOpenDataOffset()) {
84        long onDiskSize = -1;
85        if (prevBlock != null) {
86           onDiskSize = prevBlock.getNextBlockOnDiskSizeWithHeader();
87        }
88        HFileBlock block = reader.readBlock(offset, onDiskSize, false, true, false, true, null);
89        BlockCacheKey blockCacheKey = new BlockCacheKey(reader.getName(), offset);
90        boolean isCached = blockCache.getBlock(blockCacheKey, true, false, true) != null;
91        if (block.getBlockType() == BlockType.DATA ||
92            block.getBlockType() == BlockType.ROOT_INDEX ||
93            block.getBlockType() == BlockType.INTERMEDIATE_INDEX) {
94          assertTrue(isCached);
95        }
96        prevBlock = block;
97        offset += block.getOnDiskSizeWithHeader();
98      }
99    }
100 
101   private Path writeStoreFile() throws IOException {
102     Path storeFileParentDir = new Path(TEST_UTIL.getDataTestDir(), "TestPrefetch");
103     HFileContext meta = new HFileContextBuilder()
104       .withBlockSize(DATA_BLOCK_SIZE)
105       .build();
106     StoreFile.Writer sfw = new StoreFile.WriterBuilder(conf, cacheConf, fs)
107       .withOutputDir(storeFileParentDir)
108       .withComparator(KeyValue.COMPARATOR)
109       .withFileContext(meta)
110       .build();
111 
112     final int rowLen = 32;
113     for (int i = 0; i < NUM_KV; ++i) {
114       byte[] k = TestHFileWriterV2.randomOrderedKey(RNG, i);
115       byte[] v = TestHFileWriterV2.randomValue(RNG);
116       int cfLen = RNG.nextInt(k.length - rowLen + 1);
117       KeyValue kv = new KeyValue(
118           k, 0, rowLen,
119           k, rowLen, cfLen,
120           k, rowLen + cfLen, k.length - rowLen - cfLen,
121           RNG.nextLong(),
122           generateKeyType(RNG),
123           v, 0, v.length);
124       sfw.append(kv);
125     }
126 
127     sfw.close();
128     return sfw.getPath();
129   }
130 
131   public static KeyValue.Type generateKeyType(Random rand) {
132     if (rand.nextBoolean()) {
133       // Let's make half of KVs puts.
134       return KeyValue.Type.Put;
135     } else {
136       KeyValue.Type keyType =
137           KeyValue.Type.values()[1 + rand.nextInt(NUM_VALID_KEY_TYPES)];
138       if (keyType == KeyValue.Type.Minimum || keyType == KeyValue.Type.Maximum)
139       {
140         throw new RuntimeException("Generated an invalid key type: " + keyType
141             + ". " + "Probably the layout of KeyValue.Type has changed.");
142       }
143       return keyType;
144     }
145   }
146 
147 }