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 java.io.IOException;
021import java.nio.ByteBuffer;
022import java.util.ArrayList;
023import java.util.List;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.fs.FSDataInputStream;
026import org.apache.hadoop.fs.FileSystem;
027import org.apache.hadoop.fs.Path;
028import org.apache.hadoop.hbase.CellComparatorImpl;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.HBaseTestingUtility;
031import org.apache.hadoop.hbase.KeyValue;
032import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
033import org.apache.hadoop.hbase.testclassification.IOTests;
034import org.apache.hadoop.hbase.testclassification.MediumTests;
035import org.junit.Assert;
036import org.junit.Before;
037import org.junit.ClassRule;
038import org.junit.Test;
039import org.junit.experimental.categories.Category;
040
041@Category({ IOTests.class, MediumTests.class })
042public class TestRowIndexV1DataEncoder {
043  @ClassRule
044  public static final HBaseClassTestRule CLASS_RULE =
045    HBaseClassTestRule.forClass(TestRowIndexV1DataEncoder.class);
046
047  private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
048
049  private Configuration conf;
050  private FileSystem fs;
051  private DataBlockEncoding dataBlockEncoding;
052
053  @Before
054  public void setUp() throws IOException {
055    conf = TEST_UTIL.getConfiguration();
056    fs = FileSystem.get(conf);
057    dataBlockEncoding = DataBlockEncoding.ROW_INDEX_V1;
058  }
059
060  @Test
061  public void testBlockCountWritten() throws IOException {
062    Path hfilePath = new Path(TEST_UTIL.getDataTestDir(), "testHFileFormatV3");
063    final int entryCount = 10000;
064    writeDataToHFile(hfilePath, entryCount);
065  }
066
067  private void writeDataToHFile(Path hfilePath, int entryCount) throws IOException {
068    HFileContext context =
069      new HFileContextBuilder().withBlockSize(1024).withDataBlockEncoding(dataBlockEncoding)
070        .withCellComparator(CellComparatorImpl.COMPARATOR).build();
071    CacheConfig cacheConfig = new CacheConfig(conf);
072    HFile.Writer writer =
073      new HFile.WriterFactory(conf, cacheConfig).withPath(fs, hfilePath).withFileContext(context)
074        .create();
075
076    List<KeyValue> keyValues = new ArrayList<>(entryCount);
077
078    writeKeyValues(entryCount, writer, keyValues);
079
080    FSDataInputStream fsdis = fs.open(hfilePath);
081
082    long fileSize = fs.getFileStatus(hfilePath).getLen();
083    FixedFileTrailer trailer = FixedFileTrailer.readFromStream(fsdis, fileSize);
084
085    // HBASE-23788
086    // kv size = 24 bytes, block size = 1024 bytes
087    // per row encoded data written = (4 (Row index) + 24 (Cell size) + 1 (MVCC)) bytes = 29 bytes
088    // creating block size of (29 * 36) bytes = 1044 bytes
089    // Number of blocks = ceil((29 * 10000) / 1044) = 278
090    // Without the patch it would have produced 244 blocks (each block of 1236 bytes)
091    // Earlier this would create blocks ~20% greater than the block size of 1024 bytes
092    // After this patch actual block size is ~2% greater than the block size of 1024 bytes
093    Assert.assertEquals(278, trailer.getDataIndexCount());
094  }
095
096  private void writeKeyValues(int entryCount, HFile.Writer writer, List<KeyValue> keyValues)
097    throws IOException {
098    for (int i = 0; i < entryCount; ++i) {
099      byte[] keyBytes = intToBytes(i);
100
101      byte[] valueBytes = new byte[0];
102      KeyValue keyValue = new KeyValue(keyBytes, null, null, valueBytes);
103
104      writer.append(keyValue);
105      keyValues.add(keyValue);
106    }
107    writer.close();
108  }
109
110  private byte[] intToBytes(final int i) {
111    ByteBuffer bb = ByteBuffer.allocate(4);
112    bb.putInt(i);
113    return bb.array();
114  }
115}