001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.io.hfile; 019 020import static org.junit.Assert.*; 021 022import java.io.IOException; 023import java.util.Random; 024import org.apache.hadoop.conf.Configuration; 025import org.apache.hadoop.fs.FileSystem; 026import org.apache.hadoop.fs.Path; 027import org.apache.hadoop.hbase.CellComparatorImpl; 028import org.apache.hadoop.hbase.HBaseClassTestRule; 029import org.apache.hadoop.hbase.HBaseConfiguration; 030import org.apache.hadoop.hbase.HBaseTestingUtility; 031import org.apache.hadoop.hbase.HColumnDescriptor; 032import org.apache.hadoop.hbase.KeyValue; 033import org.apache.hadoop.hbase.fs.HFileSystem; 034import org.apache.hadoop.hbase.regionserver.StoreFileWriter; 035import org.apache.hadoop.hbase.testclassification.IOTests; 036import org.apache.hadoop.hbase.testclassification.SmallTests; 037import org.apache.hadoop.hbase.util.Bytes; 038import org.junit.Before; 039import org.junit.ClassRule; 040import org.junit.Test; 041import org.junit.experimental.categories.Category; 042 043@Category({IOTests.class, SmallTests.class}) 044public class TestPrefetch { 045 046 @ClassRule 047 public static final HBaseClassTestRule CLASS_RULE = 048 HBaseClassTestRule.forClass(TestPrefetch.class); 049 050 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 051 052 private static final int NUM_VALID_KEY_TYPES = KeyValue.Type.values().length - 2; 053 private static final int DATA_BLOCK_SIZE = 2048; 054 private static final int NUM_KV = 1000; 055 private static final Random RNG = new Random(); 056 057 private Configuration conf; 058 private CacheConfig cacheConf; 059 private FileSystem fs; 060 061 @Before 062 public void setUp() throws IOException { 063 conf = TEST_UTIL.getConfiguration(); 064 conf.setBoolean(CacheConfig.PREFETCH_BLOCKS_ON_OPEN_KEY, true); 065 fs = HFileSystem.get(conf); 066 CacheConfig.blockCacheDisabled = false; 067 cacheConf = new CacheConfig(conf); 068 } 069 070 @Test 071 public void testPrefetchSetInHCDWorks() { 072 HColumnDescriptor hcd = new HColumnDescriptor(Bytes.toBytes("f")); 073 hcd.setPrefetchBlocksOnOpen(true); 074 Configuration c = HBaseConfiguration.create(); 075 assertFalse(c.getBoolean(CacheConfig.PREFETCH_BLOCKS_ON_OPEN_KEY, false)); 076 CacheConfig cc = new CacheConfig(c, hcd); 077 assertTrue(cc.shouldPrefetchOnOpen()); 078 } 079 080 @Test 081 public void testPrefetch() throws Exception { 082 Path storeFile = writeStoreFile(); 083 readStoreFile(storeFile); 084 } 085 086 private void readStoreFile(Path storeFilePath) throws Exception { 087 // Open the file 088 HFile.Reader reader = HFile.createReader(fs, storeFilePath, cacheConf, true, conf); 089 090 while (!reader.prefetchComplete()) { 091 // Sleep for a bit 092 Thread.sleep(1000); 093 } 094 095 // Check that all of the data blocks were preloaded 096 BlockCache blockCache = cacheConf.getBlockCache(); 097 long offset = 0; 098 while (offset < reader.getTrailer().getLoadOnOpenDataOffset()) { 099 HFileBlock block = reader.readBlock(offset, -1, false, true, false, true, null, null); 100 BlockCacheKey blockCacheKey = new BlockCacheKey(reader.getName(), offset); 101 boolean isCached = blockCache.getBlock(blockCacheKey, true, false, true) != null; 102 if (block.getBlockType() == BlockType.DATA || 103 block.getBlockType() == BlockType.ROOT_INDEX || 104 block.getBlockType() == BlockType.INTERMEDIATE_INDEX) { 105 assertTrue(isCached); 106 } 107 offset += block.getOnDiskSizeWithHeader(); 108 } 109 } 110 111 private Path writeStoreFile() throws IOException { 112 Path storeFileParentDir = new Path(TEST_UTIL.getDataTestDir(), "TestPrefetch"); 113 HFileContext meta = new HFileContextBuilder() 114 .withBlockSize(DATA_BLOCK_SIZE) 115 .build(); 116 StoreFileWriter sfw = new StoreFileWriter.Builder(conf, cacheConf, fs) 117 .withOutputDir(storeFileParentDir) 118 .withComparator(CellComparatorImpl.COMPARATOR) 119 .withFileContext(meta) 120 .build(); 121 122 final int rowLen = 32; 123 for (int i = 0; i < NUM_KV; ++i) { 124 byte[] k = RandomKeyValueUtil.randomOrderedKey(RNG, i); 125 byte[] v = RandomKeyValueUtil.randomValue(RNG); 126 int cfLen = RNG.nextInt(k.length - rowLen + 1); 127 KeyValue kv = new KeyValue( 128 k, 0, rowLen, 129 k, rowLen, cfLen, 130 k, rowLen + cfLen, k.length - rowLen - cfLen, 131 RNG.nextLong(), 132 generateKeyType(RNG), 133 v, 0, v.length); 134 sfw.append(kv); 135 } 136 137 sfw.close(); 138 return sfw.getPath(); 139 } 140 141 public static KeyValue.Type generateKeyType(Random rand) { 142 if (rand.nextBoolean()) { 143 // Let's make half of KVs puts. 144 return KeyValue.Type.Put; 145 } else { 146 KeyValue.Type keyType = 147 KeyValue.Type.values()[1 + rand.nextInt(NUM_VALID_KEY_TYPES)]; 148 if (keyType == KeyValue.Type.Minimum || keyType == KeyValue.Type.Maximum) 149 { 150 throw new RuntimeException("Generated an invalid key type: " + keyType 151 + ". " + "Probably the layout of KeyValue.Type has changed."); 152 } 153 return keyType; 154 } 155 } 156 157}