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.master.region;
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 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.HConstants;
031import org.apache.hadoop.hbase.Stoppable;
032import org.apache.hadoop.hbase.client.Put;
033import org.apache.hadoop.hbase.master.cleaner.LogCleaner;
034import org.apache.hadoop.hbase.master.cleaner.TimeToLiveMasterLocalStoreWALCleaner;
035import org.apache.hadoop.hbase.testclassification.MasterTests;
036import org.apache.hadoop.hbase.testclassification.MediumTests;
037import org.apache.hadoop.hbase.util.Bytes;
038import org.junit.ClassRule;
039import org.junit.Test;
040import org.junit.experimental.categories.Category;
041
042@Category({ MasterTests.class, MediumTests.class })
043public class TestMasterRegionWALCleaner extends MasterRegionTestBase {
044
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE =
047    HBaseClassTestRule.forClass(TestMasterRegionWALCleaner.class);
048
049  private static long TTL_MS = 5000;
050
051  private LogCleaner logCleaner;
052
053  private Path globalWALArchiveDir;
054
055  @Override
056  protected void postSetUp() throws IOException {
057    Configuration conf = htu.getConfiguration();
058    conf.setLong(TimeToLiveMasterLocalStoreWALCleaner.TTL_CONF_KEY, TTL_MS);
059    Path testDir = htu.getDataTestDir();
060    FileSystem fs = testDir.getFileSystem(conf);
061    globalWALArchiveDir = new Path(testDir, HConstants.HREGION_OLDLOGDIR_NAME);
062    logCleaner = new LogCleaner(1000, new Stoppable() {
063
064      private volatile boolean stopped = false;
065
066      @Override
067      public void stop(String why) {
068        stopped = true;
069      }
070
071      @Override
072      public boolean isStopped() {
073        return stopped;
074      }
075    }, conf, fs, globalWALArchiveDir, logCleanerPool, null);
076    choreService.scheduleChore(logCleaner);
077  }
078
079  @Test
080  public void test() throws IOException, InterruptedException {
081    region
082      .update(r -> r.put(new Put(Bytes.toBytes(1)).addColumn(CF1, QUALIFIER, Bytes.toBytes(1))));
083    region.flush(true);
084    Path testDir = htu.getDataTestDir();
085    FileSystem fs = testDir.getFileSystem(htu.getConfiguration());
086    // no archived wal files yet
087    assertFalse(fs.exists(globalWALArchiveDir));
088    region.requestRollAll();
089    region.waitUntilWalRollFinished();
090    // should have one
091    FileStatus[] files = fs.listStatus(globalWALArchiveDir);
092    assertEquals(1, files.length);
093    Thread.sleep(2000);
094    // should still be there
095    assertTrue(fs.exists(files[0].getPath()));
096    Thread.sleep(6000);
097    // should have been cleaned
098    assertEquals(0, fs.listStatus(globalWALArchiveDir).length);
099  }
100}