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.regionserver.storefiletracker;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021
022import java.io.IOException;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.HBaseTestingUtil;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
027import org.apache.hadoop.hbase.client.Get;
028import org.apache.hadoop.hbase.client.Put;
029import org.apache.hadoop.hbase.client.RegionInfo;
030import org.apache.hadoop.hbase.client.RegionInfoBuilder;
031import org.apache.hadoop.hbase.client.Result;
032import org.apache.hadoop.hbase.client.TableDescriptor;
033import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
034import org.apache.hadoop.hbase.regionserver.HRegion;
035import org.apache.hadoop.hbase.testclassification.MediumTests;
036import org.apache.hadoop.hbase.testclassification.RegionServerTests;
037import org.apache.hadoop.hbase.util.Bytes;
038import org.junit.jupiter.api.AfterEach;
039import org.junit.jupiter.api.BeforeEach;
040import org.junit.jupiter.api.Tag;
041import org.junit.jupiter.api.Test;
042import org.junit.jupiter.api.TestInfo;
043
044@Tag(RegionServerTests.TAG)
045@Tag(MediumTests.TAG)
046public class TestRegionWithFileBasedStoreFileTracker {
047
048  private static final HBaseTestingUtil UTIL = new HBaseTestingUtil();
049
050  private static final byte[] CF = Bytes.toBytes("cf");
051
052  private static final byte[] CQ = Bytes.toBytes("cq");
053
054  private static final TableDescriptor TD =
055    TableDescriptorBuilder.newBuilder(TableName.valueOf("file_based_tracker"))
056      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(CF)).build();
057
058  private static final RegionInfo RI = RegionInfoBuilder.newBuilder(TD.getTableName()).build();
059
060  private String name;
061
062  private HRegion region;
063
064  @BeforeEach
065  public void setUp(TestInfo testInfo) throws IOException {
066    name = testInfo.getTestMethod().get().getName();
067    Configuration conf = new Configuration(UTIL.getConfiguration());
068    conf.set(StoreFileTrackerFactory.TRACKER_IMPL, StoreFileTrackerFactory.Trackers.FILE.name());
069    region = HBaseTestingUtil.createRegionAndWAL(RI, UTIL.getDataTestDir(name), conf, TD);
070  }
071
072  @AfterEach
073  public void tearDown() throws IOException {
074    if (region != null) {
075      HBaseTestingUtil.closeRegionAndWAL(region);
076    }
077    UTIL.cleanupTestDir();
078  }
079
080  @Test
081  public void testFlushAndCompaction() throws IOException {
082    for (int i = 0; i < 10; i++) {
083      for (int j = 0; j < 10; j++) {
084        int v = i * 10 + j;
085        region.put(new Put(Bytes.toBytes(v)).addColumn(CF, CQ, Bytes.toBytes(v)));
086      }
087      region.flush(true);
088      if (i % 3 == 2) {
089        region.compact(true);
090      }
091    }
092    // reopen the region, make sure the store file tracker works, i.e, we can get all the records
093    // back
094    region.close();
095    region = HRegion.openHRegion(region, null);
096    for (int i = 0; i < 100; i++) {
097      Result result = region.get(new Get(Bytes.toBytes(i)));
098      assertEquals(i, Bytes.toInt(result.getValue(CF, CQ)));
099    }
100  }
101}