Simplify TranslogWriter#closeWithTragicEvent (#29412)

This commit simplifies the exception handling in
TranslogWriter#closeWithTragicEvent. When invoking this method, the
inner close method could throw an exception which we always catch and
suppress into the exception that led us to tragically close. This commit
moves that repeated logic into closeWithTragicException and now callers
simply need to catch, invoke closeWithTragicException, and rethrow.
This commit is contained in:
Jason Tedor 2018-04-10 10:15:54 -04:00 committed by GitHub
parent 0f40199d10
commit bca192a327
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 23 additions and 43 deletions

View File

@ -164,16 +164,20 @@ public class TranslogWriter extends BaseTranslogReader implements Closeable {
return tragedy; return tragedy;
} }
private synchronized void closeWithTragicEvent(Exception exception) throws IOException { private synchronized void closeWithTragicEvent(final Exception ex) {
assert exception != null; assert ex != null;
if (tragedy == null) { if (tragedy == null) {
tragedy = exception; tragedy = ex;
} else if (tragedy != exception) { } else if (tragedy != ex) {
// it should be safe to call closeWithTragicEvents on multiple layers without // it should be safe to call closeWithTragicEvents on multiple layers without
// worrying about self suppression. // worrying about self suppression.
tragedy.addSuppressed(exception); tragedy.addSuppressed(ex);
} }
try {
close(); close();
} catch (final IOException | RuntimeException e) {
ex.addSuppressed(e);
}
} }
/** /**
@ -194,11 +198,7 @@ public class TranslogWriter extends BaseTranslogReader implements Closeable {
try { try {
data.writeTo(outputStream); data.writeTo(outputStream);
} catch (final Exception ex) { } catch (final Exception ex) {
try {
closeWithTragicEvent(ex); closeWithTragicEvent(ex);
} catch (final Exception inner) {
ex.addSuppressed(inner);
}
throw ex; throw ex;
} }
totalOffset += data.length(); totalOffset += data.length();
@ -290,13 +290,9 @@ public class TranslogWriter extends BaseTranslogReader implements Closeable {
synchronized (this) { synchronized (this) {
try { try {
sync(); // sync before we close.. sync(); // sync before we close..
} catch (IOException e) { } catch (final Exception ex) {
try { closeWithTragicEvent(ex);
closeWithTragicEvent(e); throw ex;
} catch (Exception inner) {
e.addSuppressed(inner);
}
throw e;
} }
if (closed.compareAndSet(false, true)) { if (closed.compareAndSet(false, true)) {
return new TranslogReader(getLastSyncedCheckpoint(), channel, path, getFirstOperationOffset()); return new TranslogReader(getLastSyncedCheckpoint(), channel, path, getFirstOperationOffset());
@ -346,12 +342,8 @@ public class TranslogWriter extends BaseTranslogReader implements Closeable {
try { try {
outputStream.flush(); outputStream.flush();
checkpointToSync = getCheckpoint(); checkpointToSync = getCheckpoint();
} catch (Exception ex) { } catch (final Exception ex) {
try {
closeWithTragicEvent(ex); closeWithTragicEvent(ex);
} catch (Exception inner) {
ex.addSuppressed(inner);
}
throw ex; throw ex;
} }
} }
@ -360,12 +352,8 @@ public class TranslogWriter extends BaseTranslogReader implements Closeable {
try { try {
channel.force(false); channel.force(false);
writeCheckpoint(channelFactory, path.getParent(), checkpointToSync); writeCheckpoint(channelFactory, path.getParent(), checkpointToSync);
} catch (Exception ex) { } catch (final Exception ex) {
try {
closeWithTragicEvent(ex); closeWithTragicEvent(ex);
} catch (Exception inner) {
ex.addSuppressed(inner);
}
throw ex; throw ex;
} }
assert lastSyncedCheckpoint.offset <= checkpointToSync.offset : assert lastSyncedCheckpoint.offset <= checkpointToSync.offset :
@ -392,13 +380,9 @@ public class TranslogWriter extends BaseTranslogReader implements Closeable {
} }
} }
} }
} catch (final IOException e) { } catch (final Exception ex) {
try { closeWithTragicEvent(ex);
closeWithTragicEvent(e); throw ex;
} catch (final IOException inner) {
e.addSuppressed(inner);
}
throw e;
} }
// we don't have to have a lock here because we only write ahead to the file, so all writes has been complete // 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. // for the requested location.
@ -451,12 +435,8 @@ public class TranslogWriter extends BaseTranslogReader implements Closeable {
try { try {
ensureOpen(); ensureOpen();
super.flush(); super.flush();
} catch (Exception ex) { } catch (final Exception ex) {
try {
closeWithTragicEvent(ex); closeWithTragicEvent(ex);
} catch (Exception inner) {
ex.addSuppressed(inner);
}
throw ex; throw ex;
} }
} }