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