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