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