View Javadoc

1   /**
2   *
3   * Licensed to the Apache Software Foundation (ASF) under one
4   * or more contributor license agreements.  See the NOTICE file
5   * distributed with this work for additional information
6   * regarding copyright ownership.  The ASF licenses this file
7   * to you under the Apache License, Version 2.0 (the
8   * "License"); you may not use this file except in compliance
9   * with the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19  package org.apache.hadoop.hbase.mob;
20  
21  import java.util.List;
22  import java.util.Random;
23  
24  import org.apache.hadoop.hbase.Cell;
25  import org.apache.hadoop.hbase.CellUtil;
26  import org.apache.hadoop.hbase.HBaseTestingUtility;
27  import org.apache.hadoop.hbase.HColumnDescriptor;
28  import org.apache.hadoop.hbase.HTableDescriptor;
29  import org.apache.hadoop.hbase.testclassification.MediumTests;
30  import org.apache.hadoop.hbase.TableName;
31  import org.apache.hadoop.hbase.client.HBaseAdmin;
32  import org.apache.hadoop.hbase.client.HTable;
33  import org.apache.hadoop.hbase.client.Put;
34  import org.apache.hadoop.hbase.client.Result;
35  import org.apache.hadoop.hbase.client.ResultScanner;
36  import org.apache.hadoop.hbase.client.Scan;
37  import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
38  import org.apache.hadoop.hbase.util.Bytes;
39  import org.junit.AfterClass;
40  import org.junit.Assert;
41  import org.junit.BeforeClass;
42  import org.junit.Test;
43  import org.junit.experimental.categories.Category;
44  
45  @Category(MediumTests.class)
46  public class TestMobDataBlockEncoding {
47  
48    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
49    private final static byte [] row1 = Bytes.toBytes("row1");
50    private final static byte [] family = Bytes.toBytes("family");
51    private final static byte [] qf1 = Bytes.toBytes("qualifier1");
52    private final static byte [] qf2 = Bytes.toBytes("qualifier2");
53    protected final byte[] qf3 = Bytes.toBytes("qualifier3");
54    private static HTable table;
55    private static HBaseAdmin admin;
56    private static HColumnDescriptor hcd;
57    private static HTableDescriptor desc;
58    private static Random random = new Random();
59    private static long defaultThreshold = 10;
60  
61    @BeforeClass
62    public static void setUpBeforeClass() throws Exception {
63      TEST_UTIL.getConfiguration().setInt("hfile.format.version", 3);
64      TEST_UTIL.getConfiguration().setInt("hbase.master.info.port", 0);
65      TEST_UTIL.getConfiguration().setBoolean("hbase.regionserver.info.port.auto", true);
66  
67      TEST_UTIL.startMiniCluster(1);
68    }
69  
70    @AfterClass
71    public static void tearDownAfterClass() throws Exception {
72      TEST_UTIL.shutdownMiniCluster();
73    }
74  
75    public void setUp(long threshold, String TN, DataBlockEncoding encoding)
76        throws Exception {
77      desc = new HTableDescriptor(TableName.valueOf(TN));
78      hcd = new HColumnDescriptor(family);
79      hcd.setMobEnabled(true);
80      hcd.setMobThreshold(threshold);
81      hcd.setMaxVersions(4);
82      hcd.setDataBlockEncoding(encoding);
83      desc.addFamily(hcd);
84      admin = new HBaseAdmin(TEST_UTIL.getConfiguration());
85      admin.createTable(desc);
86      table = new HTable(TEST_UTIL.getConfiguration(), TN);
87    }
88  
89    /**
90     * Generate the mob value.
91     *
92     * @param size the size of the value
93     * @return the mob value generated
94     */
95    private static byte[] generateMobValue(int size) {
96      byte[] mobVal = new byte[size];
97      random.nextBytes(mobVal);
98      return mobVal;
99    }
100 
101   @Test
102   public void testDataBlockEncoding() throws Exception {
103     for (DataBlockEncoding encoding : DataBlockEncoding.values()) {
104       testDataBlockEncoding(encoding);
105     }
106   }
107 
108   public void testDataBlockEncoding(DataBlockEncoding encoding) throws Exception {
109     String TN = "testDataBlockEncoding" + encoding;
110     setUp(defaultThreshold, TN, encoding);
111     long ts1 = System.currentTimeMillis();
112     long ts2 = ts1 + 1;
113     long ts3 = ts1 + 2;
114     byte[] value = generateMobValue((int) defaultThreshold + 1);
115 
116     Put put1 = new Put(row1);
117     put1.add(family, qf1, ts3, value);
118     put1.add(family, qf2, ts2, value);
119     put1.add(family, qf3, ts1, value);
120     table.put(put1);
121 
122     table.flushCommits();
123     admin.flush(TN);
124 
125     Scan scan = new Scan();
126     scan.setMaxVersions(4);
127 
128     ResultScanner results = table.getScanner(scan);
129     int count = 0;
130     for (Result res : results) {
131       List<Cell> cells = res.listCells();
132       for(Cell cell : cells) {
133         // Verify the value
134         Assert.assertEquals(Bytes.toString(value),
135             Bytes.toString(CellUtil.cloneValue(cell)));
136         count++;
137       }
138     }
139     results.close();
140     Assert.assertEquals(3, count);
141   }
142 }