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