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