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