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.jupiter.api.Assertions.assertEquals; 022 023import org.apache.hadoop.fs.FileStatus; 024import org.apache.hadoop.fs.Path; 025import org.apache.hadoop.hbase.HBaseTestingUtil; 026import org.apache.hadoop.hbase.TableName; 027import org.apache.hadoop.hbase.client.Admin; 028import org.apache.hadoop.hbase.client.BufferedMutator; 029import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor; 030import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 031import org.apache.hadoop.hbase.client.ConnectionFactory; 032import org.apache.hadoop.hbase.client.Put; 033import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 034import org.apache.hadoop.hbase.testclassification.MediumTests; 035import org.apache.hadoop.hbase.util.Bytes; 036import org.apache.hadoop.hbase.util.CommonFSUtils; 037import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 038import org.apache.hadoop.util.ToolRunner; 039import org.junit.jupiter.api.AfterAll; 040import org.junit.jupiter.api.AfterEach; 041import org.junit.jupiter.api.BeforeAll; 042import org.junit.jupiter.api.BeforeEach; 043import org.junit.jupiter.api.Tag; 044import org.junit.jupiter.api.Test; 045import org.slf4j.Logger; 046import org.slf4j.LoggerFactory; 047 048@Tag(MediumTests.TAG) 049public class TestExpiredMobFileCleaner { 050 051 private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 052 private final static TableName tableName = TableName.valueOf("TestExpiredMobFileCleaner"); 053 private final static String family = "family"; 054 private final static byte[] row1 = Bytes.toBytes("row1"); 055 private final static byte[] row2 = Bytes.toBytes("row2"); 056 private final static byte[] row3 = Bytes.toBytes("row3"); 057 private final static byte[] qf = Bytes.toBytes("qf"); 058 private static final Logger LOG = LoggerFactory.getLogger(TestExpiredMobFileCleaner.class); 059 060 private static BufferedMutator table; 061 private static Admin admin; 062 063 @BeforeAll 064 public static void setUpBeforeClass() throws Exception { 065 TEST_UTIL.getConfiguration().setInt("hfile.format.version", 3); 066 TEST_UTIL.getConfiguration().setInt(MOB_CLEANER_BATCH_SIZE_UPPER_BOUND, 2); 067 } 068 069 @AfterAll 070 public static void tearDownAfterClass() throws Exception { 071 072 } 073 074 @BeforeEach 075 public void setUp() throws Exception { 076 TEST_UTIL.startMiniCluster(1); 077 } 078 079 @AfterEach 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 = TableDescriptorBuilder.newBuilder(tableName); 090 ColumnFamilyDescriptor columnFamilyDescriptor = 091 ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(family)).setMobEnabled(true) 092 .setMobThreshold(3L).setMaxVersions(4).build(); 093 tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor); 094 095 admin = TEST_UTIL.getAdmin(); 096 admin.createTable(tableDescriptorBuilder.build()); 097 table = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration()) 098 .getBufferedMutator(tableName); 099 } 100 101 private void modifyColumnExpiryDays(int expireDays) throws Exception { 102 ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder 103 .newBuilder(Bytes.toBytes(family)).setMobEnabled(true).setMobThreshold(3L); 104 // change ttl as expire days to make some row expired 105 int timeToLive = expireDays * secondsOfDay(); 106 columnFamilyDescriptorBuilder.setTimeToLive(timeToLive); 107 108 admin.modifyColumnFamily(tableName, columnFamilyDescriptorBuilder.build()); 109 } 110 111 private void putKVAndFlush(BufferedMutator table, byte[] row, byte[] value, long ts) 112 throws Exception { 113 114 Put put = new Put(row, ts); 115 put.addColumn(Bytes.toBytes(family), qf, value); 116 table.mutate(put); 117 118 table.flush(); 119 admin.flush(tableName); 120 } 121 122 /** 123 * Creates a 3 day old hfile and an 1 day old hfile then sets expiry to 2 days. Verifies that the 124 * 3 day old hfile is removed but the 1 day one is still present after the expiry based cleaner is 125 * run. 126 */ 127 @Test 128 public void testCleaner() throws Exception { 129 init(); 130 131 Path mobDirPath = MobUtils.getMobFamilyPath(TEST_UTIL.getConfiguration(), tableName, family); 132 133 byte[] dummyData = makeDummyData(600); 134 long ts = EnvironmentEdgeManager.currentTime() - 3 * secondsOfDay() * 1000; // 3 days before 135 putKVAndFlush(table, row1, dummyData, ts); 136 LOG.info("test log to be deleted, tablename is " + tableName); 137 CommonFSUtils.logFileSystemState(TEST_UTIL.getTestFileSystem(), 138 TEST_UTIL.getDefaultRootDirPath(), LOG); 139 FileStatus[] firstFiles = TEST_UTIL.getTestFileSystem().listStatus(mobDirPath); 140 // the first mob file 141 assertEquals(1, firstFiles.length, "Before cleanup without delay 1"); 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(2, secondFiles.length, "Before cleanup without delay 2"); 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(4, thirdFiles.length, "Before cleanup without delay 3"); 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(1, filesAfterClean.length, "After cleanup without delay 1"); 174 assertEquals(secondFile, lastFile, "After cleanup without delay 2"); 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}