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