View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with this
4    * work for additional information regarding copyright ownership. The ASF
5    * licenses this file to you under the Apache License, Version 2.0 (the
6    * "License"); you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    * http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14   * License for the specific language governing permissions and limitations
15   * under the License.
16   */
17  package org.apache.hadoop.hbase.io.hfile;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.apache.hadoop.conf.Configuration;
23  import org.apache.hadoop.fs.FileSystem;
24  import org.apache.hadoop.fs.Path;
25  import org.apache.hadoop.hbase.HBaseTestingUtility;
26  import org.apache.hadoop.hbase.SmallTests;
27  import org.apache.hadoop.hbase.util.Bytes;
28  import org.junit.Test;
29  import org.junit.experimental.categories.Category;
30  
31  /**
32   * Test a case when an inline index chunk is converted to a root one. This reproduces the bug in
33   * HBASE-6871. We write a carefully selected number of relatively large keys so that we accumulate
34   * a leaf index chunk that only goes over the configured index chunk size after adding the last
35   * key/value. The bug is in that when we close the file, we convert that inline (leaf-level) chunk
36   * into a root chunk, but then look at the size of that root chunk, find that it is greater than
37   * the configured chunk size, and split it into a number of intermediate index blocks that should
38   * really be leaf-level blocks. If more keys were added, we would flush the leaf-level block, add
39   * another entry to the root-level block, and that would prevent us from upgrading the leaf-level
40   * chunk to the root chunk, thus not triggering the bug. 
41   */
42  @Category(SmallTests.class)
43  public class TestHFileInlineToRootChunkConversion {
44    private final HBaseTestingUtility testUtil = new HBaseTestingUtility();
45    private final Configuration conf = testUtil.getConfiguration();
46    
47    @Test
48    public void testWriteHFile() throws Exception {
49      Path hfPath = new Path(testUtil.getDataTestDir(),
50          TestHFileInlineToRootChunkConversion.class.getSimpleName() + ".hfile");
51      int maxChunkSize = 1024;
52      FileSystem fs = FileSystem.get(conf);
53      CacheConfig cacheConf = new CacheConfig(conf);
54      conf.setInt(HFileBlockIndex.MAX_CHUNK_SIZE_KEY, maxChunkSize); 
55      HFileContext context = new HFileContextBuilder().withBlockSize(16).build();
56      HFileWriterV2 hfw =
57          (HFileWriterV2) new HFileWriterV2.WriterFactoryV2(conf, cacheConf)
58              .withFileContext(context)
59              .withPath(fs, hfPath).create();
60      List<byte[]> keys = new ArrayList<byte[]>();
61      StringBuilder sb = new StringBuilder();
62  
63      for (int i = 0; i < 4; ++i) {
64        sb.append("key" + String.format("%05d", i));
65        sb.append("_");
66        for (int j = 0; j < 100; ++j) {
67          sb.append('0' + j);
68        }
69        String keyStr = sb.toString();
70        sb.setLength(0);
71  
72        byte[] k = Bytes.toBytes(keyStr);
73        System.out.println("Key: " + Bytes.toString(k));
74        keys.add(k);
75        byte[] v = Bytes.toBytes("value" + i);
76        hfw.append(k, v);
77      }
78      hfw.close();
79  
80      HFileReaderV2 reader = (HFileReaderV2) HFile.createReader(fs, hfPath, cacheConf, conf);
81      HFileScanner scanner = reader.getScanner(true, true);
82      for (int i = 0; i < keys.size(); ++i) {
83        scanner.seekTo(keys.get(i));
84      }
85      reader.close();
86    }
87  }