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("TestPrefetch"); 083 readStoreFile(storeFile); 084 } 085 086 @Test 087 public void testPrefetchRace() throws Exception { 088 for (int i = 0; i < 10; i++) { 089 Path storeFile = writeStoreFile("TestPrefetchRace-" + i); 090 readStoreFileLikeScanner(storeFile); 091 } 092 } 093 094 /** 095 * Read a storefile in the same manner as a scanner -- using non-positional reads and 096 * without waiting for prefetch to complete. 097 */ 098 private void readStoreFileLikeScanner(Path storeFilePath) throws Exception { 099 // Open the file 100 HFile.Reader reader = HFile.createReader(fs, storeFilePath, cacheConf, true, conf); 101 do { 102 long offset = 0; 103 while (offset < reader.getTrailer().getLoadOnOpenDataOffset()) { 104 HFileBlock block = reader.readBlock(offset, -1, false, /*pread=*/false, 105 false, true, null, null); 106 offset += block.getOnDiskSizeWithHeader(); 107 } 108 } while (!reader.prefetchComplete()); 109 } 110 111 private void readStoreFile(Path storeFilePath) throws Exception { 112 // Open the file 113 HFile.Reader reader = HFile.createReader(fs, storeFilePath, cacheConf, true, conf); 114 115 while (!reader.prefetchComplete()) { 116 // Sleep for a bit 117 Thread.sleep(1000); 118 } 119 120 // Check that all of the data blocks were preloaded 121 BlockCache blockCache = cacheConf.getBlockCache(); 122 long offset = 0; 123 while (offset < reader.getTrailer().getLoadOnOpenDataOffset()) { 124 HFileBlock block = reader.readBlock(offset, -1, false, true, false, true, null, null); 125 BlockCacheKey blockCacheKey = new BlockCacheKey(reader.getName(), offset); 126 boolean isCached = blockCache.getBlock(blockCacheKey, true, false, true) != null; 127 if (block.getBlockType() == BlockType.DATA || 128 block.getBlockType() == BlockType.ROOT_INDEX || 129 block.getBlockType() == BlockType.INTERMEDIATE_INDEX) { 130 assertTrue(isCached); 131 } 132 offset += block.getOnDiskSizeWithHeader(); 133 } 134 } 135 136 private Path writeStoreFile(String fname) throws IOException { 137 Path storeFileParentDir = new Path(TEST_UTIL.getDataTestDir(), fname); 138 HFileContext meta = new HFileContextBuilder() 139 .withBlockSize(DATA_BLOCK_SIZE) 140 .build(); 141 StoreFileWriter sfw = new StoreFileWriter.Builder(conf, cacheConf, fs) 142 .withOutputDir(storeFileParentDir) 143 .withComparator(CellComparatorImpl.COMPARATOR) 144 .withFileContext(meta) 145 .build(); 146 147 final int rowLen = 32; 148 for (int i = 0; i < NUM_KV; ++i) { 149 byte[] k = RandomKeyValueUtil.randomOrderedKey(RNG, i); 150 byte[] v = RandomKeyValueUtil.randomValue(RNG); 151 int cfLen = RNG.nextInt(k.length - rowLen + 1); 152 KeyValue kv = new KeyValue( 153 k, 0, rowLen, 154 k, rowLen, cfLen, 155 k, rowLen + cfLen, k.length - rowLen - cfLen, 156 RNG.nextLong(), 157 generateKeyType(RNG), 158 v, 0, v.length); 159 sfw.append(kv); 160 } 161 162 sfw.close(); 163 return sfw.getPath(); 164 } 165 166 public static KeyValue.Type generateKeyType(Random rand) { 167 if (rand.nextBoolean()) { 168 // Let's make half of KVs puts. 169 return KeyValue.Type.Put; 170 } else { 171 KeyValue.Type keyType = 172 KeyValue.Type.values()[1 + rand.nextInt(NUM_VALID_KEY_TYPES)]; 173 if (keyType == KeyValue.Type.Minimum || keyType == KeyValue.Type.Maximum) 174 { 175 throw new RuntimeException("Generated an invalid key type: " + keyType 176 + ". " + "Probably the layout of KeyValue.Type has changed."); 177 } 178 return keyType; 179 } 180 } 181 182}