Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,27 @@ public synchronized void rollLog() throws IOException {
* @throws IOException
*/
public synchronized void close() throws IOException {
IOException exception = null;
if (logStream != null) {
logStream.close();
try {
logStream.close();
} catch (IOException e) {
exception = e;
}
}
for (FileOutputStream log : streamsToFlush) {
log.close();
try {
log.close();
} catch (IOException e) {
if (exception == null) {
exception = e;
} else if (exception != e) {
exception.addSuppressed(e);
}
}
}
if (exception != null) {
throw exception;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,24 @@
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import java.io.BufferedOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Queue;
import java.util.Random;
import java.util.stream.Collectors;
import org.apache.jute.Record;
Expand Down Expand Up @@ -64,6 +71,33 @@ public class FileTxnLogTest extends ZKTestCase {

private static final int KB = 1024;

@SuppressWarnings("unchecked")
@Test
public void testCloseAttemptsEveryStream(@TempDir File tmpDir) throws Exception {
FileTxnLog txnLog = new FileTxnLog(tmpDir);
BufferedOutputStream logStream = mock(BufferedOutputStream.class);
FileOutputStream firstStream = mock(FileOutputStream.class);
FileOutputStream secondStream = mock(FileOutputStream.class);
IOException logStreamFailure = new IOException("log stream");
IOException queuedStreamFailure = new IOException("queued stream");
doThrow(logStreamFailure).when(logStream).close();
doThrow(queuedStreamFailure).when(firstStream).close();
txnLog.logStream = logStream;

// Inject close failures directly because streamsToFlush is private.
Field streamsField = FileTxnLog.class.getDeclaredField("streamsToFlush");
streamsField.setAccessible(true);
Queue<FileOutputStream> streams = (Queue<FileOutputStream>) streamsField.get(txnLog);
streams.add(firstStream);
streams.add(secondStream);

IOException thrown = assertThrows(IOException.class, txnLog::close);

assertEquals(logStreamFailure, thrown);
assertArrayEquals(new Throwable[]{queuedStreamFailure}, thrown.getSuppressed());
verify(secondStream).close();
}

@Test
public void testInvalidPreallocSize() {
assertEquals(10 * KB, FilePadding.calculateFileSizeWithPadding(7 * KB, 10 * KB, 0),
Expand Down