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.wal;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.fs.FileStatus;
024import org.apache.hadoop.fs.FileSystem;
025import org.apache.hadoop.fs.Path;
026import org.apache.hadoop.hbase.HBaseTestingUtil;
027import org.apache.hadoop.hbase.testclassification.MediumTests;
028import org.apache.hadoop.hbase.testclassification.RegionServerTests;
029import org.junit.jupiter.api.AfterAll;
030import org.junit.jupiter.api.BeforeAll;
031import org.junit.jupiter.api.BeforeEach;
032import org.junit.jupiter.api.Tag;
033import org.junit.jupiter.api.Test;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037/**
038 * Tests for TestFSHLogProvider which use WALPerformanceEvaluation for WAL data creation. This class
039 * was created as part of refactoring for hbase-diagnostics module creation in HBASE-28432 to break
040 * cyclic dependency.
041 */
042@Tag(RegionServerTests.TAG)
043@Tag(MediumTests.TAG)
044public class TestFSHLogProviderWithConcurrentWrites {
045
046  private static final Logger LOG =
047    LoggerFactory.getLogger(TestFSHLogProviderWithConcurrentWrites.class);
048
049  private static FileSystem fs;
050  private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
051
052  @BeforeEach
053  public void setUp() throws Exception {
054    FileStatus[] entries = fs.listStatus(new Path("/"));
055    for (FileStatus dir : entries) {
056      fs.delete(dir.getPath(), true);
057    }
058  }
059
060  @BeforeAll
061  public static void setUpBeforeClass() throws Exception {
062    // Make block sizes small.
063    TEST_UTIL.getConfiguration().setInt("dfs.blocksize", 1024 * 1024);
064    // quicker heartbeat interval for faster DN death notification
065    TEST_UTIL.getConfiguration().setInt("dfs.namenode.heartbeat.recheck-interval", 5000);
066    TEST_UTIL.getConfiguration().setInt("dfs.heartbeat.interval", 1);
067    TEST_UTIL.getConfiguration().setInt("dfs.client.socket-timeout", 5000);
068
069    // faster failover with cluster.shutdown();fs.close() idiom
070    TEST_UTIL.getConfiguration().setInt("hbase.ipc.client.connect.max.retries", 1);
071    TEST_UTIL.getConfiguration().setInt("dfs.client.block.recovery.retries", 1);
072    TEST_UTIL.getConfiguration().setInt("hbase.ipc.client.connection.maxidletime", 500);
073    TEST_UTIL.startMiniDFSCluster(3);
074
075    // Set up a working space for our tests.
076    TEST_UTIL.createRootDir();
077    fs = TEST_UTIL.getDFSCluster().getFileSystem();
078  }
079
080  @AfterAll
081  public static void tearDownAfterClass() throws Exception {
082    TEST_UTIL.shutdownMiniCluster();
083  }
084
085  /**
086   * Write to a log file with three concurrent threads and verifying all data is written.
087   */
088  @Test
089  public void testConcurrentWrites() throws Exception {
090    // Run the WPE tool with three threads writing 3000 edits each concurrently.
091    // When done, verify that all edits were written.
092    int errCode =
093      WALPerformanceEvaluation.innerMain(new Configuration(TEST_UTIL.getConfiguration()),
094        new String[] { "-threads", "3", "-verify", "-noclosefs", "-iterations", "3000" });
095    assertEquals(0, errCode);
096  }
097}