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.regionserver; 019 020import java.io.IOException; 021 022import org.apache.hadoop.conf.Configuration; 023import org.apache.hadoop.fs.FileSystem; 024import org.apache.hadoop.fs.Path; 025import org.apache.hadoop.hbase.ArrayBackedTag; 026import org.apache.hadoop.hbase.HBaseClassTestRule; 027import org.apache.hadoop.hbase.HBaseConfiguration; 028import org.apache.hadoop.hbase.HBaseTestingUtility; 029import org.apache.hadoop.hbase.KeyValue; 030import org.apache.hadoop.hbase.Tag; 031import org.apache.hadoop.hbase.io.compress.Compression; 032import org.apache.hadoop.hbase.io.hfile.HFileContext; 033import org.apache.hadoop.hbase.io.hfile.HFileContextBuilder; 034import org.apache.hadoop.hbase.testclassification.MiscTests; 035import org.apache.hadoop.hbase.testclassification.SmallTests; 036import org.apache.hadoop.hbase.util.Bytes; 037import org.junit.Before; 038import org.junit.ClassRule; 039import org.junit.Test; 040import org.junit.experimental.categories.Category; 041 042/** 043 * Test DataBlockEncodingTool. 044 */ 045@Category({MiscTests.class, SmallTests.class}) 046public class TestDataBlockEncodingTool { 047 048 @ClassRule 049 public static final HBaseClassTestRule CLASS_RULE = 050 HBaseClassTestRule.forClass(TestDataBlockEncodingTool.class); 051 052 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 053 private static final String ROOT_DIR = 054 TEST_UTIL.getDataTestDir("TestDataBlockEncodingTool").toString(); 055 private static final Configuration conf = TEST_UTIL.getConfiguration(); 056 private static FileSystem fs; 057 private static StoreFileWriter sfw; 058 059 @Before 060 public void setUp() throws IOException { 061 fs = TEST_UTIL.getTestFileSystem(); 062 } 063 064 private void testHFile(String fileName, boolean useTags, boolean allTags) throws IOException { 065 Path path = new Path(ROOT_DIR, fileName); 066 try { 067 createHFileWithTags(path, useTags, allTags); 068 testDataBlockingTool(path); 069 } finally { 070 if (fs.exists(path)) { 071 fs.delete(path, false); 072 } 073 } 074 } 075 076 private void createHFileWithTags(Path path, boolean useTags, boolean allTags) throws IOException { 077 HFileContext meta = new HFileContextBuilder() 078 .withBlockSize(64 * 1024) 079 .withIncludesTags(useTags).build(); 080 sfw = 081 new StoreFileWriter.Builder(conf, fs) 082 .withFilePath(path) 083 .withFileContext(meta).build(); 084 long now = System.currentTimeMillis(); 085 byte[] FAMILY = Bytes.toBytes("cf"); 086 byte[] QUALIFIER = Bytes.toBytes("q"); 087 try { 088 for (char d = 'a'; d <= 'z'; d++) { 089 for (char e = 'a'; e <= 'z'; e++) { 090 byte[] b = new byte[] { (byte) d, (byte) e }; 091 KeyValue kv; 092 if (useTags) { 093 if (allTags) { 094 // Write cells with tags to HFile. 095 Tag[] tags = new Tag[]{ 096 new ArrayBackedTag((byte) 0, Bytes.toString(b)), 097 new ArrayBackedTag((byte) 0, Bytes.toString(b))}; 098 kv = new KeyValue(b, FAMILY, QUALIFIER, now, b, tags); 099 } else { 100 // Write half cells with tags and half without tags to HFile. 101 if ((e - 'a') % 2 == 0) { 102 kv = new KeyValue(b, FAMILY, QUALIFIER, now, b); 103 } else { 104 Tag[] tags = new Tag[]{ 105 new ArrayBackedTag((byte) 0, Bytes.toString(b)), 106 new ArrayBackedTag((byte) 0, Bytes.toString(b))}; 107 kv = new KeyValue(b, FAMILY, QUALIFIER, now, b, tags); 108 } 109 } 110 } else { 111 // Write cells without tags to HFile. 112 kv = new KeyValue(b, FAMILY, QUALIFIER, now, b); 113 } 114 sfw.append(kv); 115 } 116 } 117 sfw.appendMetadata(0, false); 118 } finally { 119 sfw.close(); 120 } 121 } 122 123 private static void testDataBlockingTool(Path path) throws IOException { 124 Configuration conf = HBaseConfiguration.create(); 125 int maxKV = Integer.MAX_VALUE; 126 boolean doVerify = true; 127 boolean doBenchmark = true; 128 String testHFilePath = path.toString(); 129 DataBlockEncodingTool.testCodecs(conf, maxKV, testHFilePath, 130 Compression.Algorithm.GZ.getName(), doBenchmark, doVerify); 131 } 132 133 @Test 134 public void testHFileAllCellsWithTags() throws IOException { 135 testHFile("1234567890", true, true); 136 } 137 138 @Test 139 public void testHFileAllCellsWithoutTags() throws IOException { 140 testHFile("1234567089", false, false); 141 } 142 143 @Test 144 public void testHFileHalfCellsWithTags() throws IOException { 145 testHFile("1234560789", true, false); 146 } 147}