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.apache.hadoop.hbase.mob.MobConstants.MOB_CLEANER_BATCH_SIZE_UPPER_BOUND;
021import static org.junit.Assert.assertEquals;
022
023import org.apache.hadoop.fs.FileStatus;
024import org.apache.hadoop.fs.Path;
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseTestingUtility;
027import org.apache.hadoop.hbase.HColumnDescriptor;
028import org.apache.hadoop.hbase.HTableDescriptor;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.client.Admin;
031import org.apache.hadoop.hbase.client.BufferedMutator;
032import org.apache.hadoop.hbase.client.ConnectionFactory;
033import org.apache.hadoop.hbase.client.Put;
034import org.apache.hadoop.hbase.testclassification.MediumTests;
035import org.apache.hadoop.hbase.util.Bytes;
036import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
037import org.apache.hadoop.util.ToolRunner;
038import org.junit.After;
039import org.junit.AfterClass;
040import org.junit.Before;
041import org.junit.BeforeClass;
042import org.junit.ClassRule;
043import org.junit.Test;
044import org.junit.experimental.categories.Category;
045
046@Category(MediumTests.class)
047public class TestExpiredMobFileCleaner {
048
049  @ClassRule
050  public static final HBaseClassTestRule CLASS_RULE =
051    HBaseClassTestRule.forClass(TestExpiredMobFileCleaner.class);
052
053  private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
054  private final static TableName tableName = TableName.valueOf("TestExpiredMobFileCleaner");
055  private final static String family = "family";
056  private final static byte[] row1 = Bytes.toBytes("row1");
057  private final static byte[] row2 = Bytes.toBytes("row2");
058  private final static byte[] row3 = Bytes.toBytes("row3");
059  private final static byte[] qf = Bytes.toBytes("qf");
060
061  private static BufferedMutator table;
062  private static Admin admin;
063
064  @BeforeClass
065  public static void setUpBeforeClass() throws Exception {
066    TEST_UTIL.getConfiguration().setInt("hfile.format.version", 3);
067    TEST_UTIL.getConfiguration().setInt(MOB_CLEANER_BATCH_SIZE_UPPER_BOUND, 2);
068  }
069
070  @AfterClass
071  public static void tearDownAfterClass() throws Exception {
072
073  }
074
075  @Before
076  public void setUp() throws Exception {
077    TEST_UTIL.startMiniCluster(1);
078  }
079
080  @After
081  public void tearDown() throws Exception {
082    admin.disableTable(tableName);
083    admin.deleteTable(tableName);
084    admin.close();
085    TEST_UTIL.shutdownMiniCluster();
086    TEST_UTIL.getTestFileSystem().delete(TEST_UTIL.getDataTestDir(), true);
087  }
088
089  private void init() throws Exception {
090    HTableDescriptor desc = new HTableDescriptor(tableName);
091    HColumnDescriptor hcd = new HColumnDescriptor(family);
092    hcd.setMobEnabled(true);
093    hcd.setMobThreshold(3L);
094    hcd.setMaxVersions(4);
095    desc.addFamily(hcd);
096
097    admin = TEST_UTIL.getAdmin();
098    admin.createTable(desc);
099    table = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration())
100      .getBufferedMutator(tableName);
101  }
102
103  private void modifyColumnExpiryDays(int expireDays) throws Exception {
104    HColumnDescriptor hcd = new HColumnDescriptor(family);
105    hcd.setMobEnabled(true);
106    hcd.setMobThreshold(3L);
107    // change ttl as expire days to make some row expired
108    int timeToLive = expireDays * secondsOfDay();
109    hcd.setTimeToLive(timeToLive);
110
111    admin.modifyColumnFamily(tableName, hcd);
112  }
113
114  private void putKVAndFlush(BufferedMutator table, byte[] row, byte[] value, long ts)
115    throws Exception {
116
117    Put put = new Put(row, ts);
118    put.addColumn(Bytes.toBytes(family), qf, value);
119    table.mutate(put);
120
121    table.flush();
122    admin.flush(tableName);
123  }
124
125  /**
126   * Creates a 3 day old hfile and an 1 day old hfile then sets expiry to 2 days. Verifies that the
127   * 3 day old hfile is removed but the 1 day one is still present after the expiry based cleaner is
128   * run.
129   */
130  @Test
131  public void testCleaner() throws Exception {
132    init();
133
134    Path mobDirPath = MobUtils.getMobFamilyPath(TEST_UTIL.getConfiguration(), tableName, family);
135
136    byte[] dummyData = makeDummyData(600);
137    long ts = EnvironmentEdgeManager.currentTime() - 3 * secondsOfDay() * 1000; // 3 days before
138    putKVAndFlush(table, row1, dummyData, ts);
139    FileStatus[] firstFiles = TEST_UTIL.getTestFileSystem().listStatus(mobDirPath);
140    // the first mob file
141    assertEquals("Before cleanup without delay 1", 1, firstFiles.length);
142    String firstFile = firstFiles[0].getPath().getName();
143
144    // 1.5 day before
145    ts = (long) (EnvironmentEdgeManager.currentTime() - 1.5 * secondsOfDay() * 1000);
146    putKVAndFlush(table, row2, dummyData, ts);
147    FileStatus[] secondFiles = TEST_UTIL.getTestFileSystem().listStatus(mobDirPath);
148    // now there are 2 mob files
149    assertEquals("Before cleanup without delay 2", 2, secondFiles.length);
150    String f1 = secondFiles[0].getPath().getName();
151    String f2 = secondFiles[1].getPath().getName();
152    String secondFile = f1.equals(firstFile) ? f2 : f1;
153
154    ts = EnvironmentEdgeManager.currentTime() - 4 * secondsOfDay() * 1000; // 4 days before
155    putKVAndFlush(table, row3, dummyData, ts);
156    ts = EnvironmentEdgeManager.currentTime() - 4 * secondsOfDay() * 1000; // 4 days before
157    putKVAndFlush(table, row3, dummyData, ts);
158    FileStatus[] thirdFiles = TEST_UTIL.getTestFileSystem().listStatus(mobDirPath);
159    // now there are 4 mob files
160    assertEquals("Before cleanup without delay 3", 4, thirdFiles.length);
161
162    modifyColumnExpiryDays(2); // ttl = 2, make the first row expired
163
164    // run the cleaner
165    String[] args = new String[2];
166    args[0] = tableName.getNameAsString();
167    args[1] = family;
168    ToolRunner.run(TEST_UTIL.getConfiguration(), new ExpiredMobFileCleaner(), args);
169
170    FileStatus[] filesAfterClean = TEST_UTIL.getTestFileSystem().listStatus(mobDirPath);
171    String lastFile = filesAfterClean[0].getPath().getName();
172    // there are 4 mob files in total, but only 3 need to be cleaned
173    assertEquals("After cleanup without delay 1", 1, filesAfterClean.length);
174    assertEquals("After cleanup without delay 2", secondFile, lastFile);
175  }
176
177  private int secondsOfDay() {
178    return 24 * 3600;
179  }
180
181  private byte[] makeDummyData(int size) {
182    byte[] dummyData = new byte[size];
183    Bytes.random(dummyData);
184    return dummyData;
185  }
186}