Skip to content

Simplify TranslogWriter#closeWithTragicEvent #29412

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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 @@ -164,16 +164,20 @@ public Exception getTragicException() {
return tragedy;
}

private synchronized void closeWithTragicEvent(Exception exception) throws IOException {
assert exception != null;
private synchronized void closeWithTragicEvent(final Exception ex) {
assert ex != null;
if (tragedy == null) {
tragedy = exception;
} else if (tragedy != exception) {
tragedy = ex;
} else if (tragedy != ex) {
// it should be safe to call closeWithTragicEvents on multiple layers without
// worrying about self suppression.
tragedy.addSuppressed(exception);
tragedy.addSuppressed(ex);
}
try {
close();
} catch (final IOException | RuntimeException e) {
ex.addSuppressed(e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just catch (final Exception e) { here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want a compile-time error if a new checked exception crops here so that it's deliberately thought through.

}
close();
}

/**
Expand All @@ -194,11 +198,7 @@ public synchronized Translog.Location add(final BytesReference data, final long
try {
data.writeTo(outputStream);
} catch (final Exception ex) {
try {
closeWithTragicEvent(ex);
} catch (final Exception inner) {
ex.addSuppressed(inner);
}
closeWithTragicEvent(ex);
throw ex;
}
totalOffset += data.length();
Expand Down Expand Up @@ -290,13 +290,9 @@ public TranslogReader closeIntoReader() throws IOException {
synchronized (this) {
try {
sync(); // sync before we close..
} catch (IOException e) {
try {
closeWithTragicEvent(e);
} catch (Exception inner) {
e.addSuppressed(inner);
}
throw e;
} catch (final Exception ex) {
closeWithTragicEvent(ex);
throw ex;
}
if (closed.compareAndSet(false, true)) {
return new TranslogReader(getLastSyncedCheckpoint(), channel, path, getFirstOperationOffset());
Expand Down Expand Up @@ -346,12 +342,8 @@ public boolean syncUpTo(long offset) throws IOException {
try {
outputStream.flush();
checkpointToSync = getCheckpoint();
} catch (Exception ex) {
try {
closeWithTragicEvent(ex);
} catch (Exception inner) {
ex.addSuppressed(inner);
}
} catch (final Exception ex) {
closeWithTragicEvent(ex);
throw ex;
}
}
Expand All @@ -360,12 +352,8 @@ public boolean syncUpTo(long offset) throws IOException {
try {
channel.force(false);
writeCheckpoint(channelFactory, path.getParent(), checkpointToSync);
} catch (Exception ex) {
try {
closeWithTragicEvent(ex);
} catch (Exception inner) {
ex.addSuppressed(inner);
}
} catch (final Exception ex) {
closeWithTragicEvent(ex);
throw ex;
}
assert lastSyncedCheckpoint.offset <= checkpointToSync.offset :
Expand All @@ -392,13 +380,9 @@ protected void readBytes(ByteBuffer targetBuffer, long position) throws IOExcept
}
}
}
} catch (final IOException e) {
try {
closeWithTragicEvent(e);
} catch (final IOException inner) {
e.addSuppressed(inner);
}
throw e;
} catch (final Exception ex) {
closeWithTragicEvent(ex);
throw ex;
}
// we don't have to have a lock here because we only write ahead to the file, so all writes has been complete
// for the requested location.
Expand Down Expand Up @@ -451,12 +435,8 @@ public synchronized void flush() throws IOException {
try {
ensureOpen();
super.flush();
} catch (Exception ex) {
try {
closeWithTragicEvent(ex);
} catch (Exception inner) {
ex.addSuppressed(inner);
}
} catch (final Exception ex) {
closeWithTragicEvent(ex);
throw ex;
}
}
Expand Down