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; 021import static org.junit.Assert.assertFalse; 022import static org.junit.Assert.assertTrue; 023 024import java.io.IOException; 025import java.util.Arrays; 026import org.apache.hadoop.conf.Configuration; 027import org.apache.hadoop.fs.FileStatus; 028import org.apache.hadoop.fs.FileSystem; 029import org.apache.hadoop.fs.Path; 030import org.apache.hadoop.hbase.HBaseClassTestRule; 031import org.apache.hadoop.hbase.HBaseTestingUtil; 032import org.apache.hadoop.hbase.TableName; 033import org.apache.hadoop.hbase.client.Admin; 034import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor; 035import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 036import org.apache.hadoop.hbase.client.CompactionState; 037import org.apache.hadoop.hbase.client.Put; 038import org.apache.hadoop.hbase.client.Result; 039import org.apache.hadoop.hbase.client.ResultScanner; 040import org.apache.hadoop.hbase.client.Table; 041import org.apache.hadoop.hbase.client.TableDescriptor; 042import org.apache.hadoop.hbase.master.cleaner.TimeToLiveHFileCleaner; 043import org.apache.hadoop.hbase.testclassification.MediumTests; 044import org.apache.hadoop.hbase.util.Bytes; 045import org.junit.After; 046import org.junit.Before; 047import org.junit.ClassRule; 048import org.junit.Test; 049import org.junit.experimental.categories.Category; 050import org.slf4j.Logger; 051import org.slf4j.LoggerFactory; 052 053/** 054 * Mob file cleaner chore test. 1. Creates MOB table 2. Load MOB data and flushes it N times 3. Runs 055 * major MOB compaction (N MOB files go to archive) 4. Verifies that number of MOB files in a mob 056 * directory is N+1 5. Waits for a period of time larger than minimum age to archive 6. Runs Mob 057 * cleaner chore 7 Verifies that number of MOB files in a mob directory is 1. 058 */ 059@Category(MediumTests.class) 060public class TestMobFileCleanerChore { 061 private static final Logger LOG = LoggerFactory.getLogger(TestMobFileCleanerChore.class); 062 @ClassRule 063 public static final HBaseClassTestRule CLASS_RULE = 064 HBaseClassTestRule.forClass(TestMobFileCleanerChore.class); 065 066 private HBaseTestingUtil HTU; 067 068 private final static String famStr = "f1"; 069 private final static byte[] fam = Bytes.toBytes(famStr); 070 private final static byte[] qualifier = Bytes.toBytes("q1"); 071 private final static long mobLen = 10; 072 private final static byte[] mobVal = Bytes 073 .toBytes("01234567890123456789012345678901234567890123456789012345678901234567890123456789"); 074 075 private Configuration conf; 076 private TableDescriptor tableDescriptor; 077 private ColumnFamilyDescriptor familyDescriptor; 078 private Admin admin; 079 private Table table = null; 080 private MobFileCleanerChore chore; 081 private long minAgeToArchive = 10000; 082 083 public TestMobFileCleanerChore() { 084 } 085 086 @Before 087 public void setUp() throws Exception { 088 HTU = new HBaseTestingUtil(); 089 conf = HTU.getConfiguration(); 090 091 initConf(); 092 093 HTU.startMiniCluster(); 094 admin = HTU.getAdmin(); 095 chore = new MobFileCleanerChore(); 096 familyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(fam).setMobEnabled(true) 097 .setMobThreshold(mobLen).setMaxVersions(1).build(); 098 tableDescriptor = HTU.createModifyableTableDescriptor("testMobCompactTable") 099 .setColumnFamily(familyDescriptor).build(); 100 table = HTU.createTable(tableDescriptor, null); 101 } 102 103 private void initConf() { 104 105 conf.setInt("hfile.format.version", 3); 106 conf.setLong(TimeToLiveHFileCleaner.TTL_CONF_KEY, 0); 107 conf.setInt("hbase.client.retries.number", 100); 108 conf.setInt("hbase.hregion.max.filesize", 200000000); 109 conf.setInt("hbase.hregion.memstore.flush.size", 800000); 110 conf.setInt("hbase.hstore.blockingStoreFiles", 150); 111 conf.setInt("hbase.hstore.compaction.throughput.lower.bound", 52428800); 112 conf.setInt("hbase.hstore.compaction.throughput.higher.bound", 2 * 52428800); 113 // conf.set(MobStoreEngine.DEFAULT_MOB_COMPACTOR_CLASS_KEY, 114 // FaultyMobStoreCompactor.class.getName()); 115 // Disable automatic MOB compaction 116 conf.setLong(MobConstants.MOB_COMPACTION_CHORE_PERIOD, 0); 117 // Disable automatic MOB file cleaner chore 118 conf.setLong(MobConstants.MOB_CLEANER_PERIOD, 0); 119 // Set minimum age to archive to 10 sec 120 conf.setLong(MobConstants.MIN_AGE_TO_ARCHIVE_KEY, minAgeToArchive); 121 // Set compacted file discharger interval to a half minAgeToArchive 122 conf.setLong("hbase.hfile.compaction.discharger.interval", minAgeToArchive / 2); 123 } 124 125 private void loadData(int start, int num) { 126 try { 127 128 for (int i = 0; i < num; i++) { 129 Put p = new Put(Bytes.toBytes(start + i)); 130 p.addColumn(fam, qualifier, mobVal); 131 table.put(p); 132 } 133 admin.flush(table.getName()); 134 } catch (Exception e) { 135 LOG.error("MOB file cleaner chore test FAILED", e); 136 assertTrue(false); 137 } 138 } 139 140 @After 141 public void tearDown() throws Exception { 142 admin.disableTable(tableDescriptor.getTableName()); 143 admin.deleteTable(tableDescriptor.getTableName()); 144 HTU.shutdownMiniCluster(); 145 } 146 147 @Test 148 public void testMobFileCleanerChore() throws InterruptedException, IOException { 149 150 loadData(0, 10); 151 loadData(10, 10); 152 loadData(20, 10); 153 long num = getNumberOfMobFiles(conf, table.getName(), new String(fam)); 154 assertEquals(3, num); 155 // Major compact 156 admin.majorCompact(tableDescriptor.getTableName(), fam); 157 // wait until compaction is complete 158 while (admin.getCompactionState(tableDescriptor.getTableName()) != CompactionState.NONE) { 159 Thread.sleep(100); 160 } 161 162 num = getNumberOfMobFiles(conf, table.getName(), new String(fam)); 163 assertEquals(4, num); 164 // We have guarantee, that compcated file discharger will run during this pause 165 // because it has interval less than this wait time 166 LOG.info("Waiting for {}ms", minAgeToArchive + 1000); 167 168 Thread.sleep(minAgeToArchive + 1000); 169 LOG.info("Cleaning up MOB files"); 170 // Cleanup 171 chore.cleanupObsoleteMobFiles(conf, table.getName()); 172 173 // verify that nothing have happened 174 num = getNumberOfMobFiles(conf, table.getName(), new String(fam)); 175 assertEquals(4, num); 176 177 long scanned = scanTable(); 178 assertEquals(30, scanned); 179 180 // add a MOB file to with a name refering to a non-existing region 181 Path extraMOBFile = MobTestUtil.generateMOBFileForRegion(conf, table.getName(), 182 familyDescriptor, "nonExistentRegion"); 183 num = getNumberOfMobFiles(conf, table.getName(), new String(fam)); 184 assertEquals(5, num); 185 186 LOG.info("Waiting for {}ms", minAgeToArchive + 1000); 187 188 Thread.sleep(minAgeToArchive + 1000); 189 LOG.info("Cleaning up MOB files"); 190 chore.cleanupObsoleteMobFiles(conf, table.getName()); 191 192 // check that the extra file got deleted 193 num = getNumberOfMobFiles(conf, table.getName(), new String(fam)); 194 assertEquals(4, num); 195 196 FileSystem fs = FileSystem.get(conf); 197 assertFalse(fs.exists(extraMOBFile)); 198 199 scanned = scanTable(); 200 assertEquals(30, scanned); 201 202 } 203 204 private long getNumberOfMobFiles(Configuration conf, TableName tableName, String family) 205 throws IOException { 206 FileSystem fs = FileSystem.get(conf); 207 Path dir = MobUtils.getMobFamilyPath(conf, tableName, family); 208 FileStatus[] stat = fs.listStatus(dir); 209 for (FileStatus st : stat) { 210 LOG.debug("DDDD MOB Directory content: {} size={}", st.getPath(), st.getLen()); 211 } 212 LOG.debug("MOB Directory content total files: {}", stat.length); 213 214 return stat.length; 215 } 216 217 private long scanTable() { 218 try { 219 220 Result result; 221 ResultScanner scanner = table.getScanner(fam); 222 long counter = 0; 223 while ((result = scanner.next()) != null) { 224 assertTrue(Arrays.equals(result.getValue(fam, qualifier), mobVal)); 225 counter++; 226 } 227 return counter; 228 } catch (Exception e) { 229 e.printStackTrace(); 230 LOG.error("MOB file cleaner chore test FAILED"); 231 if (HTU != null) { 232 assertTrue(false); 233 } else { 234 System.exit(-1); 235 } 236 } 237 return 0; 238 } 239}