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