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;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021
022import java.io.IOException;
023import org.apache.hadoop.fs.FSDataOutputStream;
024import org.apache.hadoop.fs.Path;
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.master.assignment.MockMasterServices;
027import org.apache.hadoop.hbase.testclassification.MasterTests;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.junit.jupiter.api.AfterEach;
030import org.junit.jupiter.api.BeforeEach;
031import org.junit.jupiter.api.Tag;
032import org.junit.jupiter.api.Test;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036/**
037 * Tests for OldWALsDirSizeChore Here we are using the {@link MockMasterServices} to mock the Hbase
038 * Master. Chore's won't be running automatically; we need to run every time.
039 */
040@Tag(MasterTests.TAG)
041@Tag(SmallTests.TAG)
042public class TestOldWALsDirSizeChore {
043
044  private static final Logger LOG = LoggerFactory.getLogger(TestOldWALsDirSizeChore.class);
045
046  private MockMasterServices master;
047
048  private static final HBaseTestingUtil HBASE_TESTING_UTILITY = new HBaseTestingUtil();
049
050  @BeforeEach
051  public void setUp() throws Exception {
052    master = new MockMasterServices(HBASE_TESTING_UTILITY.getConfiguration());
053    master.start(10, null);
054  }
055
056  @AfterEach
057  public void tearDown() throws Exception {
058    master.stop("tearDown");
059  }
060
061  @Test
062  public void testOldWALsDirSizeChore() throws IOException {
063    // Assume the OldWALs directory size is initially zero as the chore hasn't run yet
064    long currentOldWALsDirSize = master.getMasterWalManager().getOldWALsDirSize();
065    assertEquals(0, currentOldWALsDirSize,
066      "Initial OldWALs directory size should be zero before running the chore");
067
068    int dummyFileSize = 50 * 1024 * 1024; // 50MB
069    byte[] dummyData = new byte[dummyFileSize];
070
071    // Create a dummy file in the OldWALs directory
072    Path dummyFileInOldWALsDir = new Path(master.getMasterWalManager().getOldLogDir(), "dummy.txt");
073    try (FSDataOutputStream outputStream =
074      master.getMasterWalManager().getFileSystem().create(dummyFileInOldWALsDir)) {
075      outputStream.write(dummyData);
076    }
077
078    // Run the OldWALsDirSizeChore to update the directory size
079    OldWALsDirSizeChore oldWALsDirSizeChore = new OldWALsDirSizeChore(master);
080    oldWALsDirSizeChore.chore();
081
082    // Verify that the OldWALs directory size has increased by the file size
083    assertEquals(dummyFileSize, master.getMasterWalManager().getOldWALsDirSize(),
084      "OldWALs directory size after chore should be as expected");
085  }
086}