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.mob; 019 020import static org.junit.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertNull; 022 023import org.apache.hadoop.conf.Configuration; 024import org.apache.hadoop.fs.FileSystem; 025import org.apache.hadoop.fs.Path; 026import org.apache.hadoop.hbase.Cell; 027import org.apache.hadoop.hbase.HBaseTestingUtil; 028import org.apache.hadoop.hbase.KeyValue; 029import org.apache.hadoop.hbase.KeyValue.Type; 030import org.apache.hadoop.hbase.io.hfile.CacheConfig; 031import org.apache.hadoop.hbase.io.hfile.HFileContext; 032import org.apache.hadoop.hbase.io.hfile.HFileContextBuilder; 033import org.apache.hadoop.hbase.regionserver.BloomType; 034import org.apache.hadoop.hbase.regionserver.HStoreFile; 035import org.apache.hadoop.hbase.regionserver.StoreFileInfo; 036import org.apache.hadoop.hbase.regionserver.StoreFileWriter; 037import org.apache.hadoop.hbase.testclassification.SmallTests; 038import org.apache.hadoop.hbase.util.Bytes; 039import org.junit.jupiter.api.Tag; 040import org.junit.jupiter.api.Test; 041import org.junit.jupiter.api.TestInfo; 042import org.slf4j.Logger; 043import org.slf4j.LoggerFactory; 044 045@Tag(SmallTests.TAG) 046public class TestCachedMobFile { 047 048 static final Logger LOG = LoggerFactory.getLogger(TestCachedMobFile.class); 049 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 050 private Configuration conf = TEST_UTIL.getConfiguration(); 051 private CacheConfig cacheConf = new CacheConfig(conf); 052 private static final String FAMILY1 = "familyName1"; 053 private static final String FAMILY2 = "familyName2"; 054 private static final long EXPECTED_REFERENCE_ZERO = 0; 055 private static final long EXPECTED_REFERENCE_ONE = 1; 056 private static final long EXPECTED_REFERENCE_TWO = 2; 057 058 @Test 059 public void testOpenClose(TestInfo testInfo) throws Exception { 060 String caseName = testInfo.getTestMethod().get().getName(); 061 Path testDir = TEST_UTIL.getDataTestDir(); 062 FileSystem fs = testDir.getFileSystem(conf); 063 HFileContext meta = new HFileContextBuilder().withBlockSize(8 * 1024).build(); 064 StoreFileWriter writer = new StoreFileWriter.Builder(conf, cacheConf, fs).withOutputDir(testDir) 065 .withFileContext(meta).build(); 066 StoreFileInfo storeFileInfo = 067 StoreFileInfo.createStoreFileInfoForHFile(conf, fs, writer.getPath(), true); 068 MobTestUtil.writeStoreFile(writer, caseName); 069 CachedMobFile cachedMobFile = 070 new CachedMobFile(new HStoreFile(storeFileInfo, BloomType.NONE, cacheConf)); 071 assertEquals(EXPECTED_REFERENCE_ZERO, cachedMobFile.getReferenceCount()); 072 cachedMobFile.open(); 073 assertEquals(EXPECTED_REFERENCE_ONE, cachedMobFile.getReferenceCount()); 074 cachedMobFile.open(); 075 assertEquals(EXPECTED_REFERENCE_TWO, cachedMobFile.getReferenceCount()); 076 cachedMobFile.close(); 077 assertEquals(EXPECTED_REFERENCE_ONE, cachedMobFile.getReferenceCount()); 078 cachedMobFile.close(); 079 assertEquals(EXPECTED_REFERENCE_ZERO, cachedMobFile.getReferenceCount()); 080 } 081 082 @SuppressWarnings("SelfComparison") 083 @Test 084 public void testCompare(TestInfo testInfo) throws Exception { 085 String caseName = testInfo.getTestMethod().get().getName(); 086 Path testDir = TEST_UTIL.getDataTestDir(); 087 FileSystem fs = testDir.getFileSystem(conf); 088 Path outputDir1 = new Path(testDir, FAMILY1); 089 HFileContext meta = new HFileContextBuilder().withBlockSize(8 * 1024).build(); 090 StoreFileWriter writer1 = new StoreFileWriter.Builder(conf, cacheConf, fs) 091 .withOutputDir(outputDir1).withFileContext(meta).build(); 092 MobTestUtil.writeStoreFile(writer1, caseName); 093 StoreFileInfo storeFileInfo1 = 094 StoreFileInfo.createStoreFileInfoForHFile(conf, fs, writer1.getPath(), true); 095 CachedMobFile cachedMobFile1 = 096 new CachedMobFile(new HStoreFile(storeFileInfo1, BloomType.NONE, cacheConf)); 097 Path outputDir2 = new Path(testDir, FAMILY2); 098 StoreFileWriter writer2 = new StoreFileWriter.Builder(conf, cacheConf, fs) 099 .withOutputDir(outputDir2).withFileContext(meta).build(); 100 MobTestUtil.writeStoreFile(writer2, caseName); 101 StoreFileInfo storeFileInfo2 = 102 StoreFileInfo.createStoreFileInfoForHFile(conf, fs, writer2.getPath(), true); 103 CachedMobFile cachedMobFile2 = 104 new CachedMobFile(new HStoreFile(storeFileInfo2, BloomType.NONE, cacheConf)); 105 cachedMobFile1.access(1); 106 cachedMobFile2.access(2); 107 assertEquals(1, cachedMobFile1.compareTo(cachedMobFile2)); 108 assertEquals(-1, cachedMobFile2.compareTo(cachedMobFile1)); 109 assertEquals(0, cachedMobFile1.compareTo(cachedMobFile1)); 110 } 111 112 @Test 113 public void testReadKeyValue(TestInfo testInfo) throws Exception { 114 Path testDir = TEST_UTIL.getDataTestDir(); 115 FileSystem fs = testDir.getFileSystem(conf); 116 HFileContext meta = new HFileContextBuilder().withBlockSize(8 * 1024).build(); 117 StoreFileWriter writer = new StoreFileWriter.Builder(conf, cacheConf, fs).withOutputDir(testDir) 118 .withFileContext(meta).build(); 119 String caseName = testInfo.getTestMethod().get().getName(); 120 MobTestUtil.writeStoreFile(writer, caseName); 121 StoreFileInfo storeFileInfo = 122 StoreFileInfo.createStoreFileInfoForHFile(conf, fs, writer.getPath(), true); 123 CachedMobFile cachedMobFile = 124 new CachedMobFile(new HStoreFile(storeFileInfo, BloomType.NONE, cacheConf)); 125 byte[] family = Bytes.toBytes(caseName); 126 byte[] qualify = Bytes.toBytes(caseName); 127 // Test the start key 128 byte[] startKey = Bytes.toBytes("aa"); // The start key bytes 129 KeyValue expectedKey = 130 new KeyValue(startKey, family, qualify, Long.MAX_VALUE, Type.Put, startKey); 131 KeyValue seekKey = expectedKey.createKeyOnly(false); 132 Cell cell = cachedMobFile.readCell(seekKey, false).getCell(); 133 MobTestUtil.assertCellEquals(expectedKey, cell); 134 135 // Test the end key 136 byte[] endKey = Bytes.toBytes("zz"); // The end key bytes 137 expectedKey = new KeyValue(endKey, family, qualify, Long.MAX_VALUE, Type.Put, endKey); 138 seekKey = expectedKey.createKeyOnly(false); 139 cell = cachedMobFile.readCell(seekKey, false).getCell(); 140 MobTestUtil.assertCellEquals(expectedKey, cell); 141 142 // Test the random key 143 byte[] randomKey = Bytes.toBytes(MobTestUtil.generateRandomString(2)); 144 expectedKey = new KeyValue(randomKey, family, qualify, Long.MAX_VALUE, Type.Put, randomKey); 145 seekKey = expectedKey.createKeyOnly(false); 146 cell = cachedMobFile.readCell(seekKey, false).getCell(); 147 MobTestUtil.assertCellEquals(expectedKey, cell); 148 149 // Test the key which is less than the start key 150 byte[] lowerKey = Bytes.toBytes("a1"); // Smaller than "aa" 151 expectedKey = new KeyValue(startKey, family, qualify, Long.MAX_VALUE, Type.Put, startKey); 152 seekKey = new KeyValue(lowerKey, family, qualify, Long.MAX_VALUE, Type.Put, lowerKey); 153 cell = cachedMobFile.readCell(seekKey, false).getCell(); 154 MobTestUtil.assertCellEquals(expectedKey, cell); 155 156 // Test the key which is more than the end key 157 byte[] upperKey = Bytes.toBytes("z{"); // Bigger than "zz" 158 seekKey = new KeyValue(upperKey, family, qualify, Long.MAX_VALUE, Type.Put, upperKey); 159 assertNull(cachedMobFile.readCell(seekKey, false)); 160 } 161}