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)
071        .withOutputDir(testDir).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)
100        .withFileContext(meta)
101        .build();
102    MobTestUtil.writeStoreFile(writer2, caseName);
103    CachedMobFile cachedMobFile2 = CachedMobFile.create(fs, writer2.getPath(), conf, cacheConf);
104    cachedMobFile1.access(1);
105    cachedMobFile2.access(2);
106    assertEquals(1, cachedMobFile1.compareTo(cachedMobFile2));
107    assertEquals(-1, cachedMobFile2.compareTo(cachedMobFile1));
108    assertEquals(0, cachedMobFile1.compareTo(cachedMobFile1));
109  }
110
111  @Test
112  public void testReadKeyValue() throws Exception {
113    Path testDir = TEST_UTIL.getDataTestDir();
114    FileSystem fs = testDir.getFileSystem(conf);
115    HFileContext meta = new HFileContextBuilder().withBlockSize(8 * 1024).build();
116    StoreFileWriter writer = new StoreFileWriter.Builder(conf, cacheConf, fs)
117        .withOutputDir(testDir).withFileContext(meta).build();
118    String caseName = testName.getMethodName();
119    MobTestUtil.writeStoreFile(writer, caseName);
120    CachedMobFile cachedMobFile = CachedMobFile.create(fs, writer.getPath(), conf, cacheConf);
121    byte[] family = Bytes.toBytes(caseName);
122    byte[] qualify = Bytes.toBytes(caseName);
123    // Test the start key
124    byte[] startKey = Bytes.toBytes("aa");  // The start key bytes
125    KeyValue expectedKey =
126        new KeyValue(startKey, family, qualify, Long.MAX_VALUE, Type.Put, startKey);
127    KeyValue seekKey = expectedKey.createKeyOnly(false);
128    Cell cell = cachedMobFile.readCell(seekKey, false);
129    MobTestUtil.assertCellEquals(expectedKey, cell);
130
131    // Test the end key
132    byte[] endKey = Bytes.toBytes("zz");  // The end key bytes
133    expectedKey = new KeyValue(endKey, family, qualify, Long.MAX_VALUE, Type.Put, endKey);
134    seekKey = expectedKey.createKeyOnly(false);
135    cell = cachedMobFile.readCell(seekKey, false);
136    MobTestUtil.assertCellEquals(expectedKey, cell);
137
138    // Test the random key
139    byte[] randomKey = Bytes.toBytes(MobTestUtil.generateRandomString(2));
140    expectedKey = new KeyValue(randomKey, family, qualify, Long.MAX_VALUE, Type.Put, randomKey);
141    seekKey = expectedKey.createKeyOnly(false);
142    cell = cachedMobFile.readCell(seekKey, false);
143    MobTestUtil.assertCellEquals(expectedKey, cell);
144
145    // Test the key which is less than the start key
146    byte[] lowerKey = Bytes.toBytes("a1"); // Smaller than "aa"
147    expectedKey = new KeyValue(startKey, family, qualify, Long.MAX_VALUE, Type.Put, startKey);
148    seekKey = new KeyValue(lowerKey, family, qualify, Long.MAX_VALUE, Type.Put, lowerKey);
149    cell = cachedMobFile.readCell(seekKey, false);
150    MobTestUtil.assertCellEquals(expectedKey, cell);
151
152    // Test the key which is more than the end key
153    byte[] upperKey = Bytes.toBytes("z{"); // Bigger than "zz"
154    seekKey = new KeyValue(upperKey, family, qualify, Long.MAX_VALUE, Type.Put, upperKey);
155    cell = cachedMobFile.readCell(seekKey, false);
156    Assert.assertNull(cell);
157  }
158}