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