Skip to content

Commit bca192a

Browse files
authored
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.
1 parent 0f40199 commit bca192a

File tree

1 file changed

+23
-43
lines changed

1 file changed

+23
-43
lines changed

server/src/main/java/org/elasticsearch/index/translog/TranslogWriter.java

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -164,16 +164,20 @@ public Exception getTragicException() {
164164
return tragedy;
165165
}
166166

167-
private synchronized void closeWithTragicEvent(Exception exception) throws IOException {
168-
assert exception != null;
167+
private synchronized void closeWithTragicEvent(final Exception ex) {
168+
assert ex != null;
169169
if (tragedy == null) {
170-
tragedy = exception;
171-
} else if (tragedy != exception) {
170+
tragedy = ex;
171+
} else if (tragedy != ex) {
172172
// it should be safe to call closeWithTragicEvents on multiple layers without
173173
// worrying about self suppression.
174-
tragedy.addSuppressed(exception);
174+
tragedy.addSuppressed(ex);
175+
}
176+
try {
177+
close();
178+
} catch (final IOException | RuntimeException e) {
179+
ex.addSuppressed(e);
175180
}
176-
close();
177181
}
178182

179183
/**
@@ -194,11 +198,7 @@ public synchronized Translog.Location add(final BytesReference data, final long
194198
try {
195199
data.writeTo(outputStream);
196200
} catch (final Exception ex) {
197-
try {
198-
closeWithTragicEvent(ex);
199-
} catch (final Exception inner) {
200-
ex.addSuppressed(inner);
201-
}
201+
closeWithTragicEvent(ex);
202202
throw ex;
203203
}
204204
totalOffset += data.length();
@@ -290,13 +290,9 @@ public TranslogReader closeIntoReader() throws IOException {
290290
synchronized (this) {
291291
try {
292292
sync(); // sync before we close..
293-
} catch (IOException e) {
294-
try {
295-
closeWithTragicEvent(e);
296-
} catch (Exception inner) {
297-
e.addSuppressed(inner);
298-
}
299-
throw e;
293+
} catch (final Exception ex) {
294+
closeWithTragicEvent(ex);
295+
throw ex;
300296
}
301297
if (closed.compareAndSet(false, true)) {
302298
return new TranslogReader(getLastSyncedCheckpoint(), channel, path, getFirstOperationOffset());
@@ -346,12 +342,8 @@ public boolean syncUpTo(long offset) throws IOException {
346342
try {
347343
outputStream.flush();
348344
checkpointToSync = getCheckpoint();
349-
} catch (Exception ex) {
350-
try {
351-
closeWithTragicEvent(ex);
352-
} catch (Exception inner) {
353-
ex.addSuppressed(inner);
354-
}
345+
} catch (final Exception ex) {
346+
closeWithTragicEvent(ex);
355347
throw ex;
356348
}
357349
}
@@ -360,12 +352,8 @@ public boolean syncUpTo(long offset) throws IOException {
360352
try {
361353
channel.force(false);
362354
writeCheckpoint(channelFactory, path.getParent(), checkpointToSync);
363-
} catch (Exception ex) {
364-
try {
365-
closeWithTragicEvent(ex);
366-
} catch (Exception inner) {
367-
ex.addSuppressed(inner);
368-
}
355+
} catch (final Exception ex) {
356+
closeWithTragicEvent(ex);
369357
throw ex;
370358
}
371359
assert lastSyncedCheckpoint.offset <= checkpointToSync.offset :
@@ -392,13 +380,9 @@ protected void readBytes(ByteBuffer targetBuffer, long position) throws IOExcept
392380
}
393381
}
394382
}
395-
} catch (final IOException e) {
396-
try {
397-
closeWithTragicEvent(e);
398-
} catch (final IOException inner) {
399-
e.addSuppressed(inner);
400-
}
401-
throw e;
383+
} catch (final Exception ex) {
384+
closeWithTragicEvent(ex);
385+
throw ex;
402386
}
403387
// we don't have to have a lock here because we only write ahead to the file, so all writes has been complete
404388
// for the requested location.
@@ -451,12 +435,8 @@ public synchronized void flush() throws IOException {
451435
try {
452436
ensureOpen();
453437
super.flush();
454-
} catch (Exception ex) {
455-
try {
456-
closeWithTragicEvent(ex);
457-
} catch (Exception inner) {
458-
ex.addSuppressed(inner);
459-
}
438+
} catch (final Exception ex) {
439+
closeWithTragicEvent(ex);
460440
throw ex;
461441
}
462442
}

0 commit comments

Comments
 (0)