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.awaitility.Awaitility.await;
021import static org.junit.jupiter.api.Assertions.assertEquals;
022import static org.junit.jupiter.api.Assertions.assertFalse;
023import static org.junit.jupiter.api.Assertions.assertTrue;
024
025import java.io.IOException;
026import java.time.Duration;
027import org.apache.hadoop.conf.Configuration;
028import org.apache.hadoop.fs.FileStatus;
029import org.apache.hadoop.fs.FileSystem;
030import org.apache.hadoop.fs.Path;
031import org.apache.hadoop.hbase.HConstants;
032import org.apache.hadoop.hbase.Stoppable;
033import org.apache.hadoop.hbase.client.Put;
034import org.apache.hadoop.hbase.master.cleaner.LogCleaner;
035import org.apache.hadoop.hbase.master.cleaner.TimeToLiveMasterLocalStoreWALCleaner;
036import org.apache.hadoop.hbase.testclassification.MasterTests;
037import org.apache.hadoop.hbase.testclassification.MediumTests;
038import org.apache.hadoop.hbase.util.Bytes;
039import org.junit.jupiter.api.Tag;
040import org.junit.jupiter.api.Test;
041
042@Tag(MasterTests.TAG)
043@Tag(MediumTests.TAG)
044public class TestMasterRegionWALCleaner extends MasterRegionTestBase {
045
046  private static long TTL_MS = 5000;
047
048  private LogCleaner logCleaner;
049
050  private Path globalWALArchiveDir;
051
052  @Override
053  protected void postSetUp() throws IOException {
054    Configuration conf = htu.getConfiguration();
055    conf.setLong(TimeToLiveMasterLocalStoreWALCleaner.TTL_CONF_KEY, TTL_MS);
056    Path testDir = htu.getDataTestDir();
057    FileSystem fs = testDir.getFileSystem(conf);
058    globalWALArchiveDir = new Path(testDir, HConstants.HREGION_OLDLOGDIR_NAME);
059    logCleaner = new LogCleaner(1000, new Stoppable() {
060
061      private volatile boolean stopped = false;
062
063      @Override
064      public void stop(String why) {
065        stopped = true;
066      }
067
068      @Override
069      public boolean isStopped() {
070        return stopped;
071      }
072    }, conf, fs, globalWALArchiveDir, logCleanerPool, null);
073    choreService.scheduleChore(logCleaner);
074  }
075
076  @Test
077  public void test() throws IOException, InterruptedException {
078    region
079      .update(r -> r.put(new Put(Bytes.toBytes(1)).addColumn(CF1, QUALIFIER, Bytes.toBytes(1))));
080    region.flush(true);
081    Path testDir = htu.getDataTestDir();
082    FileSystem fs = testDir.getFileSystem(htu.getConfiguration());
083    // no archived wal files yet
084    assertFalse(fs.exists(globalWALArchiveDir));
085    region.requestRollAll();
086    region.waitUntilWalRollFinished();
087    // archiving wal is called in a background thread when rolling WALs, so it is possible that when
088    // waitUntilWalRollFinished returns, the archived WAL files have not been moved to the global
089    // archive directory yet, so here we need to wait for the directory to be created and the files
090    // to be moved.
091    await().atMost(Duration.ofSeconds(15)).untilAsserted(() -> {
092      assertTrue(fs.exists(globalWALArchiveDir));
093      assertEquals(1, fs.listStatus(globalWALArchiveDir).length);
094    });
095    FileStatus[] files = fs.listStatus(globalWALArchiveDir);
096    assertEquals(1, files.length);
097    // Rebase the WAL mtime so the following timing assertions are not affected by the wait above.
098    // Cleaner TTL is based on file mtime.
099    fs.setTimes(files[0].getPath(), System.currentTimeMillis(), -1);
100    Thread.sleep(2000);
101    // should still be there
102    assertTrue(fs.exists(files[0].getPath()));
103    // should have been cleaned
104    await().atMost(Duration.ofSeconds(15))
105      .untilAsserted(() -> assertEquals(0, fs.listStatus(globalWALArchiveDir).length));
106  }
107}