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.assertThrows;
021
022import java.io.IOException;
023import java.io.InterruptedIOException;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.fs.FileSystem;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.HBaseTestingUtil;
028import org.apache.hadoop.hbase.testclassification.RegionServerTests;
029import org.apache.hadoop.hbase.testclassification.SmallTests;
030import org.apache.hadoop.hbase.util.CommonFSUtils;
031import org.junit.jupiter.api.AfterAll;
032import org.junit.jupiter.api.BeforeAll;
033import org.junit.jupiter.api.Tag;
034import org.junit.jupiter.api.Test;
035import org.mockito.Mockito;
036
037@Tag(RegionServerTests.TAG)
038@Tag(SmallTests.TAG)
039public class TestRecoveredEditsOutputSink {
040
041  private static WALFactory wals;
042  private static FileSystem fs;
043  private static Path rootDir;
044  private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
045
046  private static RecoveredEditsOutputSink outputSink;
047
048  @BeforeAll
049  public static void setUpBeforeClass() throws Exception {
050    Configuration conf = TEST_UTIL.getConfiguration();
051    conf.set(WALFactory.WAL_PROVIDER, "filesystem");
052    rootDir = TEST_UTIL.createRootDir();
053    fs = CommonFSUtils.getRootDirFileSystem(conf);
054    wals = new WALFactory(conf, "testRecoveredEditsOutputSinkWALFactory");
055    WALSplitter splitter = new WALSplitter(wals, conf, rootDir, fs, rootDir, fs);
056    WALSplitter.PipelineController pipelineController = new WALSplitter.PipelineController();
057    EntryBuffers sink = new EntryBuffers(pipelineController, 1024 * 1024);
058    outputSink = new RecoveredEditsOutputSink(splitter, pipelineController, sink, 3);
059  }
060
061  @AfterAll
062  public static void tearDownAfterClass() throws Exception {
063    wals.close();
064    fs.delete(rootDir, true);
065  }
066
067  @Test
068  public void testCloseSuccess() throws IOException {
069    RecoveredEditsOutputSink spyOutputSink = Mockito.spy(outputSink);
070    spyOutputSink.close();
071    Mockito.verify(spyOutputSink, Mockito.times(1)).finishWriterThreads();
072    Mockito.verify(spyOutputSink, Mockito.times(1)).closeWriters(true);
073  }
074
075  /**
076   * When a WAL split is interrupted (ex. by a RegionServer abort), the thread join in
077   * finishWriterThreads() will get interrupted, rethrowing the exception without stopping the
078   * writer threads. Test to ensure that when this happens, RecoveredEditsOutputSink.close() does
079   * not rename the recoveredEdits WAL files as this can cause corruption. Please see HBASE-28569.
080   * However, the writers must still be closed.
081   */
082  @Test
083  public void testCloseWALSplitInterrupted() throws IOException {
084    RecoveredEditsOutputSink spyOutputSink = Mockito.spy(outputSink);
085    // The race condition will lead to an InterruptedException to be caught by finishWriterThreads()
086    // which is then rethrown as an InterruptedIOException.
087    Mockito.doThrow(new InterruptedIOException()).when(spyOutputSink).finishWriterThreads();
088    assertThrows(InterruptedIOException.class, spyOutputSink::close);
089    Mockito.verify(spyOutputSink, Mockito.times(1)).finishWriterThreads();
090    Mockito.verify(spyOutputSink, Mockito.times(1)).closeWriters(false);
091  }
092
093  /**
094   * When finishWriterThreads fails but does not throw an exception, ensure the writers are handled
095   * like in the exception case - the writers are closed but the recoveredEdits WAL files are not
096   * renamed.
097   */
098  @Test
099  public void testCloseWALFinishWriterThreadsFailed() throws IOException {
100    RecoveredEditsOutputSink spyOutputSink = Mockito.spy(outputSink);
101    Mockito.doReturn(false).when(spyOutputSink).finishWriterThreads();
102    spyOutputSink.close();
103    Mockito.verify(spyOutputSink, Mockito.times(1)).finishWriterThreads();
104    Mockito.verify(spyOutputSink, Mockito.times(1)).closeWriters(false);
105  }
106}