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.assertEquals; 021import java.io.IOException; 022import org.apache.hadoop.conf.Configuration; 023import org.apache.hadoop.fs.FSDataOutputStream; 024import org.apache.hadoop.fs.FileSystem; 025import org.apache.hadoop.fs.Path; 026import org.apache.hadoop.hbase.Cell; 027import org.apache.hadoop.hbase.HBaseClassTestRule; 028import org.apache.hadoop.hbase.HBaseTestingUtility; 029import org.apache.hadoop.hbase.KeyValue; 030import org.apache.hadoop.hbase.io.hfile.bucket.BucketCache; 031import org.apache.hadoop.hbase.testclassification.IOTests; 032import org.apache.hadoop.hbase.testclassification.SmallTests; 033import org.apache.hadoop.hbase.util.Bytes; 034import org.junit.ClassRule; 035import org.junit.Test; 036import org.junit.experimental.categories.Category; 037 038import org.apache.hbase.thirdparty.com.google.common.collect.Lists; 039 040/** 041 * Test 042 */ 043@Category({ IOTests.class, SmallTests.class}) 044public class TestHFileReaderImpl { 045 046 @ClassRule 047 public static final HBaseClassTestRule CLASS_RULE = 048 HBaseClassTestRule.forClass(TestHFileReaderImpl.class); 049 050 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 051 052 static KeyValue toKV(String row) { 053 return new KeyValue(Bytes.toBytes(row), Bytes.toBytes("family"), Bytes.toBytes("qualifier"), 054 Bytes.toBytes("value")); 055 } 056 057 static String toRowStr(Cell c) { 058 return Bytes.toString(c.getRowArray(), c.getRowOffset(), c.getRowLength()); 059 } 060 061 Path makeNewFile() throws IOException { 062 Path ncTFile = new Path(TEST_UTIL.getDataTestDir(), "basic.hfile"); 063 FSDataOutputStream fout = TEST_UTIL.getTestFileSystem().create(ncTFile); 064 int blocksize = toKV("a").getLength() * 3; 065 HFileContext context = 066 new HFileContextBuilder().withBlockSize(blocksize).withIncludesTags(true).build(); 067 Configuration conf = TEST_UTIL.getConfiguration(); 068 HFile.Writer writer = HFile.getWriterFactoryNoCache(conf) 069 .withOutputStream(fout).withFileContext(context).create(); 070 // 4 bytes * 3 * 2 for each key/value + 071 // 3 for keys, 15 for values = 42 (woot) 072 writer.append(toKV("c")); 073 writer.append(toKV("e")); 074 writer.append(toKV("g")); 075 // block transition 076 writer.append(toKV("i")); 077 writer.append(toKV("k")); 078 writer.close(); 079 fout.close(); 080 return ncTFile; 081 } 082 083 @Test 084 public void testSeekBefore() throws Exception { 085 Path p = makeNewFile(); 086 FileSystem fs = TEST_UTIL.getTestFileSystem(); 087 Configuration conf = TEST_UTIL.getConfiguration(); 088 int[] bucketSizes = { 512, 2048, 4096, 64 * 1024, 128 * 1024 }; 089 BucketCache bucketcache = 090 new BucketCache("offheap", 128 * 1024 * 1024, 64 * 1024, bucketSizes, 5, 64 * 100, null); 091 092 HFile.Reader reader = HFile.createReader(fs, p, new CacheConfig(conf, bucketcache), true, conf); 093 094 // warm cache 095 HFileScanner scanner = reader.getScanner(true, true); 096 scanner.seekTo(toKV("i")); 097 assertEquals("i", toRowStr(scanner.getCell())); 098 scanner.close(); 099 100 while (bucketcache.getBlockCount() <= 0) { 101 Thread.sleep(10); 102 } 103 104 // reopen again. 105 scanner = reader.getScanner(true, true); 106 scanner.seekTo(toKV("i")); 107 assertEquals("i", toRowStr(scanner.getCell())); 108 scanner.seekBefore(toKV("i")); 109 assertEquals("g", toRowStr(scanner.getCell())); 110 scanner.close(); 111 112 for (CachedBlock cachedBlock : Lists.newArrayList(bucketcache)) { 113 BlockCacheKey cacheKey = 114 new BlockCacheKey(cachedBlock.getFilename(), cachedBlock.getOffset()); 115 int refCount = bucketcache.getRpcRefCount(cacheKey); 116 assertEquals(0, refCount); 117 } 118 119 // case 2 120 scanner = reader.getScanner(true, true); 121 scanner.seekTo(toKV("i")); 122 assertEquals("i", toRowStr(scanner.getCell())); 123 scanner.seekBefore(toKV("c")); 124 scanner.close(); 125 for (CachedBlock cachedBlock : Lists.newArrayList(bucketcache)) { 126 BlockCacheKey cacheKey = 127 new BlockCacheKey(cachedBlock.getFilename(), cachedBlock.getOffset()); 128 int refCount = bucketcache.getRpcRefCount(cacheKey); 129 assertEquals(0, refCount); 130 } 131 132 reader.close(); 133 134 // clear bucketcache 135 for (CachedBlock cachedBlock : Lists.newArrayList(bucketcache)) { 136 BlockCacheKey cacheKey = 137 new BlockCacheKey(cachedBlock.getFilename(), cachedBlock.getOffset()); 138 bucketcache.evictBlock(cacheKey); 139 } 140 bucketcache.shutdown(); 141 142 deleteTestDir(fs); 143 } 144 145 protected void deleteTestDir(FileSystem fs) throws IOException { 146 Path dataTestDir = TEST_UTIL.getDataTestDir(); 147 if(fs.exists(dataTestDir)) { 148 fs.delete(dataTestDir, true); 149 } 150 } 151 152}