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