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 */
018
019package org.apache.hadoop.hbase.mob;
020
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.hbase.Cell;
023import org.apache.hadoop.hbase.CellUtil;
024import org.apache.hadoop.hbase.HBaseClassTestRule;
025import org.apache.hadoop.hbase.HBaseTestingUtility;
026import org.apache.hadoop.hbase.HConstants;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.client.Admin;
029import org.apache.hadoop.hbase.client.Result;
030import org.apache.hadoop.hbase.client.ResultScanner;
031import org.apache.hadoop.hbase.client.Scan;
032import org.apache.hadoop.hbase.client.Table;
033import org.apache.hadoop.hbase.io.ByteBuffAllocator;
034import org.apache.hadoop.hbase.snapshot.MobSnapshotTestingUtils;
035import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
036import org.apache.hadoop.hbase.testclassification.MediumTests;
037import org.apache.hadoop.hbase.util.Bytes;
038import org.junit.AfterClass;
039import org.junit.Assert;
040import org.junit.BeforeClass;
041import org.junit.ClassRule;
042import org.junit.Test;
043import org.junit.experimental.categories.Category;
044import org.slf4j.Logger;
045import org.slf4j.LoggerFactory;
046
047/**
048 * Test the MOB feature when enable RPC ByteBuffAllocator (HBASE-22122)
049 */
050@Category({ MediumTests.class })
051public class TestMobWithByteBuffAllocator {
052
053  @ClassRule
054  public static final HBaseClassTestRule CLASS_RULE =
055      HBaseClassTestRule.forClass(TestMobWithByteBuffAllocator.class);
056
057  private static final String TABLE_NAME = "TestMobWithByteBuffAllocator";
058  private static final Logger LOG = LoggerFactory.getLogger(TestMobWithByteBuffAllocator.class);
059
060  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
061  private static final Configuration CONF = UTIL.getConfiguration();
062  private static final byte[] FAMILY = Bytes.toBytes("f");
063
064  @BeforeClass
065  public static void setUp() throws Exception {
066    // Must use the ByteBuffAllocator here
067    CONF.setBoolean(ByteBuffAllocator.ALLOCATOR_POOL_ENABLED_KEY, true);
068    // Must use OFF-HEAP BucketCache here.
069    CONF.setFloat(HConstants.HFILE_BLOCK_CACHE_SIZE_KEY, 0.1f);
070    CONF.set(HConstants.BUCKET_CACHE_IOENGINE_KEY, "offheap");
071    // 32MB for BucketCache.
072    CONF.setFloat(HConstants.BUCKET_CACHE_SIZE_KEY, 32);
073    CONF.setInt(MobConstants.MOB_FILE_CACHE_SIZE_KEY, 0);
074    UTIL.startMiniCluster();
075  }
076
077  @AfterClass
078  public static void tearDown() throws Exception {
079    UTIL.shutdownMiniCluster();
080  }
081
082  @Test
083  public void testReadingCellsFromHFile() throws Exception {
084    TableName tableName = TableName.valueOf(TABLE_NAME);
085    MobSnapshotTestingUtils.createMobTable(UTIL, tableName, 1, FAMILY);
086    LOG.info("Create an mob table {} successfully.", tableName);
087
088    int expectedRows = 500;
089    SnapshotTestingUtils.loadData(UTIL, tableName, expectedRows, FAMILY);
090    LOG.info("Load 500 rows data into table {} successfully.", tableName);
091
092    // Flush all the data into HFiles.
093    try (Admin admin = UTIL.getConnection().getAdmin()) {
094      admin.flush(tableName);
095    }
096
097    // Scan the rows
098    MobSnapshotTestingUtils.verifyMobRowCount(UTIL, tableName, expectedRows);
099
100    // Reversed scan the rows
101    int rows = 0;
102    try (Table table = UTIL.getConnection().getTable(tableName)) {
103      try (ResultScanner scanner = table.getScanner(new Scan().setReversed(true))) {
104        Result res = scanner.next();
105        while (res != null) {
106          rows++;
107          for (Cell cell : res.listCells()) {
108            Assert.assertTrue(CellUtil.cloneValue(cell).length > 0);
109          }
110          res = scanner.next();
111        }
112      }
113    }
114    Assert.assertEquals(expectedRows, rows);
115  }
116}