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