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.hbase.util.HFileArchiveUtil; 039import org.apache.hadoop.util.ToolRunner; 040import org.junit.jupiter.api.AfterAll; 041import org.junit.jupiter.api.AfterEach; 042import org.junit.jupiter.api.BeforeAll; 043import org.junit.jupiter.api.BeforeEach; 044import org.junit.jupiter.api.Tag; 045import org.junit.jupiter.api.Test; 046import org.slf4j.Logger; 047import org.slf4j.LoggerFactory; 048 049@Tag(MediumTests.TAG) 050public class TestExpiredMobFileCleaner { 051 052 private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 053 private final static TableName tableName = TableName.valueOf("TestExpiredMobFileCleaner"); 054 private final static String family = "family"; 055 private final static byte[] row1 = Bytes.toBytes("row1"); 056 private final static byte[] row2 = Bytes.toBytes("row2"); 057 private final static byte[] row3 = Bytes.toBytes("row3"); 058 private final static byte[] qf = Bytes.toBytes("qf"); 059 private static final Logger LOG = LoggerFactory.getLogger(TestExpiredMobFileCleaner.class); 060 061 private static BufferedMutator table; 062 private static Admin admin; 063 064 @BeforeAll 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 @AfterAll 071 public static void tearDownAfterClass() throws Exception { 072 073 } 074 075 @BeforeEach 076 public void setUp() throws Exception { 077 TEST_UTIL.startMiniCluster(1); 078 } 079 080 @AfterEach 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 TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName); 091 ColumnFamilyDescriptor columnFamilyDescriptor = 092 ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(family)).setMobEnabled(true) 093 .setMobThreshold(3L).setMaxVersions(4).build(); 094 tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor); 095 096 admin = TEST_UTIL.getAdmin(); 097 admin.createTable(tableDescriptorBuilder.build()); 098 table = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration()) 099 .getBufferedMutator(tableName); 100 } 101 102 private void modifyColumnExpiryDays(int expireDays) throws Exception { 103 ColumnFamilyDescriptorBuilder columnFamilyDescriptorBuilder = ColumnFamilyDescriptorBuilder 104 .newBuilder(Bytes.toBytes(family)).setMobEnabled(true).setMobThreshold(3L); 105 // change ttl as expire days to make some row expired 106 int timeToLive = expireDays * secondsOfDay(); 107 columnFamilyDescriptorBuilder.setTimeToLive(timeToLive); 108 109 admin.modifyColumnFamily(tableName, columnFamilyDescriptorBuilder.build()); 110 } 111 112 private void putKVAndFlush(BufferedMutator table, byte[] row, byte[] value, long ts) 113 throws Exception { 114 115 Put put = new Put(row, ts); 116 put.addColumn(Bytes.toBytes(family), qf, value); 117 table.mutate(put); 118 119 table.flush(); 120 admin.flush(tableName); 121 } 122 123 /** 124 * Creates a 3 day old hfile and an 1 day old hfile then sets expiry to 2 days. Verifies that the 125 * 3 day old hfile is removed but the 1 day one is still present after the expiry based cleaner is 126 * run. 127 */ 128 @Test 129 public void testCleaner() throws Exception { 130 init(); 131 132 Path mobDirPath = MobUtils.getMobFamilyPath(TEST_UTIL.getConfiguration(), tableName, family); 133 134 byte[] dummyData = makeDummyData(600); 135 long ts = EnvironmentEdgeManager.currentTime() - 3 * secondsOfDay() * 1000; // 3 days before 136 putKVAndFlush(table, row1, dummyData, ts); 137 LOG.info("test log to be deleted, tablename is " + tableName); 138 CommonFSUtils.logFileSystemState(TEST_UTIL.getTestFileSystem(), 139 TEST_UTIL.getDefaultRootDirPath(), LOG); 140 FileStatus[] firstFiles = TEST_UTIL.getTestFileSystem().listStatus(mobDirPath); 141 // the first mob file 142 assertEquals(1, firstFiles.length, "Before cleanup without delay 1"); 143 String firstFile = firstFiles[0].getPath().getName(); 144 145 // 1.5 day before 146 ts = (long) (EnvironmentEdgeManager.currentTime() - 1.5 * secondsOfDay() * 1000); 147 putKVAndFlush(table, row2, dummyData, ts); 148 FileStatus[] secondFiles = TEST_UTIL.getTestFileSystem().listStatus(mobDirPath); 149 // now there are 2 mob files 150 assertEquals(2, secondFiles.length, "Before cleanup without delay 2"); 151 String f1 = secondFiles[0].getPath().getName(); 152 String f2 = secondFiles[1].getPath().getName(); 153 String secondFile = f1.equals(firstFile) ? f2 : f1; 154 155 ts = EnvironmentEdgeManager.currentTime() - 4 * secondsOfDay() * 1000; // 4 days before 156 putKVAndFlush(table, row3, dummyData, ts); 157 ts = EnvironmentEdgeManager.currentTime() - 4 * secondsOfDay() * 1000; // 4 days before 158 putKVAndFlush(table, row3, dummyData, ts); 159 FileStatus[] thirdFiles = TEST_UTIL.getTestFileSystem().listStatus(mobDirPath); 160 // now there are 4 mob files 161 assertEquals(4, thirdFiles.length, "Before cleanup without delay 3"); 162 163 modifyColumnExpiryDays(2); // ttl = 2, make the first row expired 164 165 // run the cleaner 166 String[] args = new String[2]; 167 args[0] = tableName.getNameAsString(); 168 args[1] = family; 169 ToolRunner.run(TEST_UTIL.getConfiguration(), new ExpiredMobFileCleaner(), args); 170 171 FileStatus[] filesAfterClean = TEST_UTIL.getTestFileSystem().listStatus(mobDirPath); 172 String lastFile = filesAfterClean[0].getPath().getName(); 173 // there are 4 mob files in total, but only 3 need to be cleaned 174 assertEquals(1, filesAfterClean.length, "After cleanup without delay 1"); 175 assertEquals(secondFile, lastFile, "After cleanup without delay 2"); 176 177 // The 3 expired files must have been archived (renamed) into the mob region's archive 178 // directory, not merely deleted. 179 Path mobTableDir = 180 CommonFSUtils.getTableDir(MobUtils.getMobHome(TEST_UTIL.getConfiguration()), tableName); 181 Path archiveDir = HFileArchiveUtil.getStoreArchivePath(TEST_UTIL.getConfiguration(), 182 MobUtils.getMobRegionInfo(tableName), mobTableDir, Bytes.toBytes(family)); 183 FileStatus[] archivedFiles = TEST_UTIL.getTestFileSystem().listStatus(archiveDir); 184 assertEquals(3, archivedFiles.length, "Expired mob files should be archived"); 185 } 186 187 private int secondsOfDay() { 188 return 24 * 3600; 189 } 190 191 private byte[] makeDummyData(int size) { 192 byte[] dummyData = new byte[size]; 193 Bytes.random(dummyData); 194 return dummyData; 195 } 196}