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