001/*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.mob;
020import static org.junit.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.io.IOException;
024import java.util.Arrays;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.fs.FileStatus;
027import org.apache.hadoop.fs.FileSystem;
028import org.apache.hadoop.fs.Path;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.HBaseTestingUtil;
031import org.apache.hadoop.hbase.TableName;
032import org.apache.hadoop.hbase.client.Admin;
033import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
034import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
035import org.apache.hadoop.hbase.client.CompactionState;
036import org.apache.hadoop.hbase.client.Put;
037import org.apache.hadoop.hbase.client.Result;
038import org.apache.hadoop.hbase.client.ResultScanner;
039import org.apache.hadoop.hbase.client.Table;
040import org.apache.hadoop.hbase.client.TableDescriptor;
041import org.apache.hadoop.hbase.master.cleaner.TimeToLiveHFileCleaner;
042import org.apache.hadoop.hbase.testclassification.MediumTests;
043import org.apache.hadoop.hbase.util.Bytes;
044import org.junit.After;
045import org.junit.Before;
046import org.junit.ClassRule;
047import org.junit.Test;
048import org.junit.experimental.categories.Category;
049import org.slf4j.Logger;
050import org.slf4j.LoggerFactory;
051
052/**
053  * Mob file cleaner chore test.
054  * 1. Creates MOB table
055  * 2. Load MOB data and flushes it N times
056  * 3. Runs major MOB compaction (N MOB files go to archive)
057  * 4. Verifies that number of MOB files in a mob directory is N+1
058  * 5. Waits for a period of time larger than minimum age to archive
059  * 6. Runs Mob cleaner chore
060  * 7 Verifies that number of MOB files in a mob directory is 1.
061 */
062@Category(MediumTests.class)
063public class TestMobFileCleanerChore {
064  private static final Logger LOG = LoggerFactory.getLogger(TestMobFileCleanerChore.class);
065  @ClassRule
066  public static final HBaseClassTestRule CLASS_RULE =
067      HBaseClassTestRule.forClass(TestMobFileCleanerChore.class);
068
069  private HBaseTestingUtil HTU;
070
071  private final static String famStr = "f1";
072  private final static byte[] fam = Bytes.toBytes(famStr);
073  private final static byte[] qualifier = Bytes.toBytes("q1");
074  private final static long mobLen = 10;
075  private final static byte[] mobVal = Bytes
076      .toBytes("01234567890123456789012345678901234567890123456789012345678901234567890123456789");
077
078  private Configuration conf;
079  private TableDescriptor tableDescriptor;
080  private ColumnFamilyDescriptor familyDescriptor;
081  private Admin admin;
082  private Table table = null;
083  private MobFileCleanerChore chore;
084  private long minAgeToArchive = 10000;
085
086  public TestMobFileCleanerChore() {
087  }
088
089  @Before
090  public void setUp() throws Exception {
091    HTU = new HBaseTestingUtil();
092    conf = HTU.getConfiguration();
093
094    initConf();
095
096    HTU.startMiniCluster();
097    admin = HTU.getAdmin();
098    chore = new MobFileCleanerChore();
099    familyDescriptor = ColumnFamilyDescriptorBuilder.newBuilder(fam).setMobEnabled(true)
100      .setMobThreshold(mobLen).setMaxVersions(1).build();
101    tableDescriptor = HTU.createModifyableTableDescriptor("testMobCompactTable")
102      .setColumnFamily(familyDescriptor).build();
103    table = HTU.createTable(tableDescriptor, null);
104  }
105
106  private void initConf() {
107
108    conf.setInt("hfile.format.version", 3);
109    conf.setLong(TimeToLiveHFileCleaner.TTL_CONF_KEY, 0);
110    conf.setInt("hbase.client.retries.number", 100);
111    conf.setInt("hbase.hregion.max.filesize", 200000000);
112    conf.setInt("hbase.hregion.memstore.flush.size", 800000);
113    conf.setInt("hbase.hstore.blockingStoreFiles", 150);
114    conf.setInt("hbase.hstore.compaction.throughput.lower.bound", 52428800);
115    conf.setInt("hbase.hstore.compaction.throughput.higher.bound", 2 * 52428800);
116    //conf.set(MobStoreEngine.DEFAULT_MOB_COMPACTOR_CLASS_KEY,
117    //  FaultyMobStoreCompactor.class.getName());
118    // Disable automatic MOB compaction
119    conf.setLong(MobConstants.MOB_COMPACTION_CHORE_PERIOD, 0);
120    // Disable automatic MOB file cleaner chore
121    conf.setLong(MobConstants.MOB_CLEANER_PERIOD, 0);
122    // Set minimum age to archive to 10 sec
123    conf.setLong(MobConstants.MIN_AGE_TO_ARCHIVE_KEY, minAgeToArchive);
124    // Set compacted file discharger interval to a half minAgeToArchive
125    conf.setLong("hbase.hfile.compaction.discharger.interval", minAgeToArchive/2);
126  }
127
128  private void loadData(int start, int num) {
129    try {
130
131      for (int i = 0; i < num; i++) {
132        Put p = new Put(Bytes.toBytes(start + i));
133        p.addColumn(fam, qualifier, mobVal);
134        table.put(p);
135      }
136      admin.flush(table.getName());
137    } catch (Exception e) {
138      LOG.error("MOB file cleaner chore test FAILED", e);
139      assertTrue(false);
140    }
141  }
142
143  @After
144  public void tearDown() throws Exception {
145    admin.disableTable(tableDescriptor.getTableName());
146    admin.deleteTable(tableDescriptor.getTableName());
147    HTU.shutdownMiniCluster();
148  }
149
150  @Test
151  public void testMobFileCleanerChore() throws InterruptedException, IOException {
152
153    loadData(0, 10);
154    loadData(10, 10);
155    loadData(20, 10);
156    long num = getNumberOfMobFiles(conf, table.getName(), new String(fam));
157    assertEquals(3, num);
158    // Major compact
159    admin.majorCompact(tableDescriptor.getTableName(), fam);
160    // wait until compaction is complete
161    while (admin.getCompactionState(tableDescriptor.getTableName()) != CompactionState.NONE) {
162      Thread.sleep(100);
163    }
164
165    num = getNumberOfMobFiles(conf, table.getName(), new String(fam));
166    assertEquals(4, num);
167    // We have guarantee, that compcated file discharger will run during this pause
168    // because it has interval less than this wait time
169    LOG.info("Waiting for {}ms", minAgeToArchive + 1000);
170
171    Thread.sleep(minAgeToArchive + 1000);
172    LOG.info("Cleaning up MOB files");
173    // Cleanup again
174    chore.cleanupObsoleteMobFiles(conf, table.getName());
175
176    num = getNumberOfMobFiles(conf, table.getName(), new String(fam));
177    assertEquals(1, num);
178
179    long scanned = scanTable();
180    assertEquals(30, scanned);
181  }
182
183  private  long getNumberOfMobFiles(Configuration conf, TableName tableName, String family)
184      throws IOException {
185    FileSystem fs = FileSystem.get(conf);
186    Path dir = MobUtils.getMobFamilyPath(conf, tableName, family);
187    FileStatus[] stat = fs.listStatus(dir);
188    for (FileStatus st : stat) {
189      LOG.debug("DDDD MOB Directory content: {} size={}", st.getPath(), st.getLen());
190    }
191    LOG.debug("MOB Directory content total files: {}", stat.length);
192
193    return stat.length;
194  }
195
196
197  private long scanTable() {
198    try {
199
200      Result result;
201      ResultScanner scanner = table.getScanner(fam);
202      long counter = 0;
203      while ((result = scanner.next()) != null) {
204        assertTrue(Arrays.equals(result.getValue(fam, qualifier), mobVal));
205        counter++;
206      }
207      return counter;
208    } catch (Exception e) {
209      e.printStackTrace();
210      LOG.error("MOB file cleaner chore test FAILED");
211      if (HTU != null) {
212        assertTrue(false);
213      } else {
214        System.exit(-1);
215      }
216    }
217    return 0;
218  }
219}