1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.mob;
20
21 import junit.framework.TestCase;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.fs.FileSystem;
27 import org.apache.hadoop.fs.Path;
28 import org.apache.hadoop.hbase.Cell;
29 import org.apache.hadoop.hbase.HBaseConfiguration;
30 import org.apache.hadoop.hbase.KeyValue;
31 import org.apache.hadoop.hbase.KeyValue.Type;
32 import org.apache.hadoop.hbase.io.hfile.CacheConfig;
33 import org.apache.hadoop.hbase.io.hfile.HFileContext;
34 import org.apache.hadoop.hbase.io.hfile.HFileContextBuilder;
35 import org.apache.hadoop.hbase.regionserver.StoreFile;
36 import org.apache.hadoop.hbase.testclassification.SmallTests;
37 import org.apache.hadoop.hbase.util.Bytes;
38 import org.apache.hadoop.hbase.util.FSUtils;
39 import org.junit.Assert;
40 import org.junit.Test;
41 import org.junit.experimental.categories.Category;
42
43 @Category(SmallTests.class)
44 public class TestCachedMobFile extends TestCase{
45 static final Log LOG = LogFactory.getLog(TestCachedMobFile.class);
46 private Configuration conf = HBaseConfiguration.create();
47 private CacheConfig cacheConf = new CacheConfig(conf);
48 private static final String TABLE = "tableName";
49 private static final String FAMILY = "familyName";
50 private static final String FAMILY1 = "familyName1";
51 private static final String FAMILY2 = "familyName2";
52 private static final long EXPECTED_REFERENCE_ZERO = 0;
53 private static final long EXPECTED_REFERENCE_ONE = 1;
54 private static final long EXPECTED_REFERENCE_TWO = 2;
55
56 @Test
57 public void testOpenClose() throws Exception {
58 String caseName = getName();
59 FileSystem fs = FileSystem.get(conf);
60 Path testDir = FSUtils.getRootDir(conf);
61 Path outputDir = new Path(new Path(testDir, TABLE),
62 FAMILY);
63 HFileContext meta = new HFileContextBuilder().withBlockSize(8*1024).build();
64 StoreFile.Writer writer = new StoreFile.WriterBuilder(conf, cacheConf, fs)
65 .withOutputDir(outputDir).withFileContext(meta).build();
66 MobTestUtil.writeStoreFile(writer, caseName);
67 CachedMobFile cachedMobFile = CachedMobFile.create(fs, writer.getPath(), conf, cacheConf);
68 Assert.assertEquals(EXPECTED_REFERENCE_ZERO, cachedMobFile.getReferenceCount());
69 cachedMobFile.open();
70 Assert.assertEquals(EXPECTED_REFERENCE_ONE, cachedMobFile.getReferenceCount());
71 cachedMobFile.open();
72 Assert.assertEquals(EXPECTED_REFERENCE_TWO, cachedMobFile.getReferenceCount());
73 cachedMobFile.close();
74 Assert.assertEquals(EXPECTED_REFERENCE_ONE, cachedMobFile.getReferenceCount());
75 cachedMobFile.close();
76 Assert.assertEquals(EXPECTED_REFERENCE_ZERO, cachedMobFile.getReferenceCount());
77 }
78
79 @Test
80 public void testCompare() throws Exception {
81 String caseName = getName();
82 FileSystem fs = FileSystem.get(conf);
83 Path testDir = FSUtils.getRootDir(conf);
84 Path outputDir1 = new Path(new Path(testDir, TABLE),
85 FAMILY1);
86 HFileContext meta = new HFileContextBuilder().withBlockSize(8 * 1024).build();
87 StoreFile.Writer writer1 = new StoreFile.WriterBuilder(conf, cacheConf, fs)
88 .withOutputDir(outputDir1).withFileContext(meta).build();
89 MobTestUtil.writeStoreFile(writer1, caseName);
90 CachedMobFile cachedMobFile1 = CachedMobFile.create(fs, writer1.getPath(), conf, cacheConf);
91 Path outputDir2 = new Path(new Path(testDir, TABLE),
92 FAMILY2);
93 StoreFile.Writer writer2 = new StoreFile.WriterBuilder(conf, cacheConf, fs)
94 .withOutputDir(outputDir2)
95 .withFileContext(meta)
96 .build();
97 MobTestUtil.writeStoreFile(writer2, caseName);
98 CachedMobFile cachedMobFile2 = CachedMobFile.create(fs, writer2.getPath(), conf, cacheConf);
99 cachedMobFile1.access(1);
100 cachedMobFile2.access(2);
101 Assert.assertEquals(cachedMobFile1.compareTo(cachedMobFile2), 1);
102 Assert.assertEquals(cachedMobFile2.compareTo(cachedMobFile1), -1);
103 Assert.assertEquals(cachedMobFile1.compareTo(cachedMobFile1), 0);
104 }
105
106 @Test
107 public void testReadKeyValue() throws Exception {
108 FileSystem fs = FileSystem.get(conf);
109 Path testDir = FSUtils.getRootDir(conf);
110 Path outputDir = new Path(new Path(testDir, TABLE), "familyname");
111 HFileContext meta = new HFileContextBuilder().withBlockSize(8 * 1024).build();
112 StoreFile.Writer writer = new StoreFile.WriterBuilder(conf, cacheConf, fs)
113 .withOutputDir(outputDir).withFileContext(meta).build();
114 String caseName = getName();
115 MobTestUtil.writeStoreFile(writer, caseName);
116 CachedMobFile cachedMobFile = CachedMobFile.create(fs, writer.getPath(), conf, cacheConf);
117 byte[] family = Bytes.toBytes(caseName);
118 byte[] qualify = Bytes.toBytes(caseName);
119
120 byte[] startKey = Bytes.toBytes("aa");
121 KeyValue expectedKey =
122 new KeyValue(startKey, family, qualify, Long.MAX_VALUE, Type.Put, startKey);
123 KeyValue seekKey = expectedKey.createKeyOnly(false);
124 Cell cell = cachedMobFile.readCell(seekKey, false);
125 MobTestUtil.assertCellEquals(expectedKey, cell);
126
127
128 byte[] endKey = Bytes.toBytes("zz");
129 expectedKey = new KeyValue(endKey, family, qualify, Long.MAX_VALUE, Type.Put, endKey);
130 seekKey = expectedKey.createKeyOnly(false);
131 cell = cachedMobFile.readCell(seekKey, false);
132 MobTestUtil.assertCellEquals(expectedKey, cell);
133
134
135 byte[] randomKey = Bytes.toBytes(MobTestUtil.generateRandomString(2));
136 expectedKey = new KeyValue(randomKey, family, qualify, Long.MAX_VALUE, Type.Put, randomKey);
137 seekKey = expectedKey.createKeyOnly(false);
138 cell = cachedMobFile.readCell(seekKey, false);
139 MobTestUtil.assertCellEquals(expectedKey, cell);
140
141
142 byte[] lowerKey = Bytes.toBytes("a1");
143 expectedKey = new KeyValue(startKey, family, qualify, Long.MAX_VALUE, Type.Put, startKey);
144 seekKey = new KeyValue(lowerKey, family, qualify, Long.MAX_VALUE, Type.Put, lowerKey);
145 cell = cachedMobFile.readCell(seekKey, false);
146 MobTestUtil.assertCellEquals(expectedKey, cell);
147
148
149 byte[] upperKey = Bytes.toBytes("z{");
150 seekKey = new KeyValue(upperKey, family, qualify, Long.MAX_VALUE, Type.Put, upperKey);
151 cell = cachedMobFile.readCell(seekKey, false);
152 Assert.assertNull(cell);
153 }
154 }