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 org.apache.hadoop.hbase.HBaseTestingUtil;
021import org.apache.hadoop.hbase.TableName;
022import org.apache.hadoop.hbase.client.Admin;
023import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
024import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
025import org.apache.hadoop.hbase.client.ConnectionFactory;
026import org.apache.hadoop.hbase.client.Put;
027import org.apache.hadoop.hbase.client.Scan;
028import org.apache.hadoop.hbase.client.Table;
029import org.apache.hadoop.hbase.client.TableDescriptor;
030import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
031import org.apache.hadoop.hbase.io.encoding.DataBlockEncoding;
032import org.apache.hadoop.hbase.testclassification.MediumTests;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
035import org.junit.jupiter.api.AfterAll;
036import org.junit.jupiter.api.BeforeAll;
037import org.junit.jupiter.api.Tag;
038import org.junit.jupiter.api.Test;
039
040@Tag(MediumTests.TAG)
041public class TestMobDataBlockEncoding {
042
043  private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
044  private final static byte[] row1 = Bytes.toBytes("row1");
045  private final static byte[] family = Bytes.toBytes("family");
046  private final static byte[] qf1 = Bytes.toBytes("qualifier1");
047  private final static byte[] qf2 = Bytes.toBytes("qualifier2");
048  protected final byte[] qf3 = Bytes.toBytes("qualifier3");
049  private static Table table;
050  private static Admin admin;
051  private static ColumnFamilyDescriptor columnFamilyDescriptor;
052  private static TableDescriptor tableDescriptor;
053  private static long defaultThreshold = 10;
054
055  @BeforeAll
056  public static void setUpBeforeClass() throws Exception {
057    TEST_UTIL.startMiniCluster(1);
058  }
059
060  @AfterAll
061  public static void tearDownAfterClass() throws Exception {
062    TEST_UTIL.shutdownMiniCluster();
063  }
064
065  public void setUp(long threshold, String TN, DataBlockEncoding encoding) throws Exception {
066    columnFamilyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(family).setMobEnabled(true)
067      .setMobThreshold(threshold).setMaxVersions(4).setDataBlockEncoding(encoding).build();
068    tableDescriptor = TableDescriptorBuilder.newBuilder(TableName.valueOf(TN))
069      .setColumnFamily(columnFamilyDescriptor).build();
070    admin = TEST_UTIL.getAdmin();
071    admin.createTable(tableDescriptor);
072    table = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration())
073      .getTable(TableName.valueOf(TN));
074  }
075
076  /**
077   * Generate the mob value.
078   * @param size the size of the value
079   * @return the mob value generated
080   */
081  private static byte[] generateMobValue(int size) {
082    byte[] mobVal = new byte[size];
083    Bytes.random(mobVal);
084    return mobVal;
085  }
086
087  @Test
088  public void testDataBlockEncoding() throws Exception {
089    for (DataBlockEncoding encoding : DataBlockEncoding.values()) {
090      testDataBlockEncoding(encoding);
091    }
092  }
093
094  public void testDataBlockEncoding(DataBlockEncoding encoding) throws Exception {
095    String TN = "testDataBlockEncoding" + encoding;
096    setUp(defaultThreshold, TN, encoding);
097    long ts1 = EnvironmentEdgeManager.currentTime();
098    long ts2 = ts1 + 1;
099    long ts3 = ts1 + 2;
100    byte[] value = generateMobValue((int) defaultThreshold + 1);
101
102    Put put1 = new Put(row1);
103    put1.addColumn(family, qf1, ts3, value);
104    put1.addColumn(family, qf2, ts2, value);
105    put1.addColumn(family, qf3, ts1, value);
106    table.put(put1);
107    admin.flush(TableName.valueOf(TN));
108
109    Scan scan = new Scan();
110    scan.readVersions(4);
111    MobTestUtil.assertCellsValue(table, scan, value, 3);
112  }
113}