|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hbase.wal; |
| 19 | + |
| 20 | +import static org.junit.Assert.assertThrows; |
| 21 | + |
| 22 | +import java.io.IOException; |
| 23 | +import java.io.InterruptedIOException; |
| 24 | +import org.apache.hadoop.conf.Configuration; |
| 25 | +import org.apache.hadoop.fs.FileSystem; |
| 26 | +import org.apache.hadoop.fs.Path; |
| 27 | +import org.apache.hadoop.hbase.HBaseClassTestRule; |
| 28 | +import org.apache.hadoop.hbase.HBaseTestingUtil; |
| 29 | +import org.apache.hadoop.hbase.testclassification.RegionServerTests; |
| 30 | +import org.apache.hadoop.hbase.testclassification.SmallTests; |
| 31 | +import org.apache.hadoop.hbase.util.CommonFSUtils; |
| 32 | +import org.junit.AfterClass; |
| 33 | +import org.junit.BeforeClass; |
| 34 | +import org.junit.ClassRule; |
| 35 | +import org.junit.Test; |
| 36 | +import org.junit.experimental.categories.Category; |
| 37 | +import org.mockito.Mockito; |
| 38 | + |
| 39 | +@Category({ RegionServerTests.class, SmallTests.class }) |
| 40 | +public class TestRecoveredEditsOutputSink { |
| 41 | + |
| 42 | + @ClassRule |
| 43 | + public static final HBaseClassTestRule CLASS_RULE = |
| 44 | + HBaseClassTestRule.forClass(TestRecoveredEditsOutputSink.class); |
| 45 | + |
| 46 | + private static WALFactory wals; |
| 47 | + private static FileSystem fs; |
| 48 | + private static Path rootDir; |
| 49 | + private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); |
| 50 | + |
| 51 | + private static RecoveredEditsOutputSink outputSink; |
| 52 | + |
| 53 | + @BeforeClass |
| 54 | + public static void setUpBeforeClass() throws Exception { |
| 55 | + Configuration conf = TEST_UTIL.getConfiguration(); |
| 56 | + conf.set(WALFactory.WAL_PROVIDER, "filesystem"); |
| 57 | + rootDir = TEST_UTIL.createRootDir(); |
| 58 | + fs = CommonFSUtils.getRootDirFileSystem(conf); |
| 59 | + wals = new WALFactory(conf, "testRecoveredEditsOutputSinkWALFactory"); |
| 60 | + WALSplitter splitter = new WALSplitter(wals, conf, rootDir, fs, rootDir, fs); |
| 61 | + WALSplitter.PipelineController pipelineController = new WALSplitter.PipelineController(); |
| 62 | + EntryBuffers sink = new EntryBuffers(pipelineController, 1024 * 1024); |
| 63 | + outputSink = new RecoveredEditsOutputSink(splitter, pipelineController, sink, 3); |
| 64 | + } |
| 65 | + |
| 66 | + @AfterClass |
| 67 | + public static void tearDownAfterClass() throws Exception { |
| 68 | + wals.close(); |
| 69 | + fs.delete(rootDir, true); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testCloseSuccess() throws IOException { |
| 74 | + RecoveredEditsOutputSink spyOutputSink = Mockito.spy(outputSink); |
| 75 | + spyOutputSink.close(); |
| 76 | + Mockito.verify(spyOutputSink, Mockito.times(1)).finishWriterThreads(); |
| 77 | + Mockito.verify(spyOutputSink, Mockito.times(1)).closeWriters(true); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * When a WAL split is interrupted (ex. by a RegionServer abort), the thread join in |
| 82 | + * finishWriterThreads() will get interrupted, rethrowing the exception without stopping the |
| 83 | + * writer threads. Test to ensure that when this happens, RecoveredEditsOutputSink.close() does |
| 84 | + * not rename the recoveredEdits WAL files as this can cause corruption. Please see HBASE-28569. |
| 85 | + * However, the writers must still be closed. |
| 86 | + */ |
| 87 | + @Test |
| 88 | + public void testCloseWALSplitInterrupted() throws IOException { |
| 89 | + RecoveredEditsOutputSink spyOutputSink = Mockito.spy(outputSink); |
| 90 | + // The race condition will lead to an InterruptedException to be caught by finishWriterThreads() |
| 91 | + // which is then rethrown as an InterruptedIOException. |
| 92 | + Mockito.doThrow(new InterruptedIOException()).when(spyOutputSink).finishWriterThreads(); |
| 93 | + assertThrows(InterruptedIOException.class, spyOutputSink::close); |
| 94 | + Mockito.verify(spyOutputSink, Mockito.times(1)).finishWriterThreads(); |
| 95 | + Mockito.verify(spyOutputSink, Mockito.times(1)).closeWriters(false); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * 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 | +} |
0 commit comments