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