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.assertFalse;
021import static org.junit.jupiter.api.Assertions.assertNotSame;
022import static org.junit.jupiter.api.Assertions.assertTrue;
023import static org.mockito.Mockito.mock;
024import static org.mockito.Mockito.verify;
025import static org.mockito.Mockito.when;
026
027import org.apache.hadoop.conf.Configuration;
028import org.apache.hadoop.hbase.CellUtil;
029import org.apache.hadoop.hbase.ExtendedCell;
030import org.apache.hadoop.hbase.KeyValue;
031import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
032import org.apache.hadoop.hbase.io.compress.Compression;
033import org.apache.hadoop.hbase.regionserver.HMobStore;
034import org.apache.hadoop.hbase.testclassification.SmallTests;
035import org.apache.hadoop.hbase.util.Bytes;
036import org.junit.jupiter.api.Tag;
037import org.junit.jupiter.api.Test;
038
039@Tag(SmallTests.TAG)
040public class TestDefaultMobStoreCompactor {
041
042  @Test
043  public void testCacheMobBlocksOnCompactionDefaultsToTrue() {
044    DefaultMobStoreCompactor compactor = newCompactor(new Configuration());
045
046    assertTrue(compactor.cacheMobBlocksOnCompaction);
047  }
048
049  @Test
050  public void testCacheMobBlocksOnCompactionCanBeDisabled() {
051    Configuration conf = new Configuration();
052    conf.setBoolean(MobConstants.MOB_COMPACTION_READ_CACHE_BLOCKS, false);
053    DefaultMobStoreCompactor compactor = newCompactor(conf);
054
055    assertFalse(compactor.cacheMobBlocksOnCompaction);
056  }
057
058  @Test
059  public void testResolveMobCellClosesMobCellAndReturnsIndependentCopy() throws Exception {
060    Configuration conf = new Configuration();
061    DefaultMobStoreCompactor compactor = newCompactor(conf);
062    ExtendedCell reference = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("family"),
063      Bytes.toBytes("qualifier"), Bytes.toBytes("mob-reference"));
064    ExtendedCell resolved = new KeyValue(Bytes.toBytes("row"), Bytes.toBytes("family"),
065      Bytes.toBytes("qualifier"), Bytes.toBytes("mob-value"));
066    MobCell mobCell = mock(MobCell.class);
067    when(mobCell.getCell()).thenReturn(resolved);
068    when(compactor.mobStore.resolve(reference, compactor.cacheMobBlocksOnCompaction, false))
069      .thenReturn(mobCell);
070
071    ExtendedCell copied = compactor.resolveMobCell(reference);
072
073    assertNotSame(resolved, copied);
074    assertTrue(CellUtil.matchingValue(resolved, copied));
075    verify(compactor.mobStore).resolve(reference, compactor.cacheMobBlocksOnCompaction, false);
076    verify(mobCell).close();
077  }
078
079  private DefaultMobStoreCompactor newCompactor(Configuration conf) {
080    HMobStore store = mock(HMobStore.class);
081    ColumnFamilyDescriptor family = mock(ColumnFamilyDescriptor.class);
082    when(store.getColumnFamilyDescriptor()).thenReturn(family);
083    when(family.getMajorCompactionCompressionType()).thenReturn(Compression.Algorithm.NONE);
084    when(family.getMinorCompactionCompressionType()).thenReturn(Compression.Algorithm.NONE);
085    when(family.getMobThreshold()).thenReturn(123L);
086    return new DefaultMobStoreCompactor(conf, store);
087  }
088}