From f6020a6ff29824757855348157933e5cd90b49ba Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Mon, 4 Jan 2016 23:40:20 +0100 Subject: [PATCH] Close recovered translog readers if createWriter fails If we fail to create a writer all recovered translog readers are not closed today which causes all open files to leak. Closes #15754 --- .../index/translog/Translog.java | 15 ++++++++++-- .../index/translog/TranslogReader.java | 2 +- .../index/translog/TranslogTests.java | 23 +++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/index/translog/Translog.java b/core/src/main/java/org/elasticsearch/index/translog/Translog.java index c2cb3c19af8..3f8f0ab54df 100644 --- a/core/src/main/java/org/elasticsearch/index/translog/Translog.java +++ b/core/src/main/java/org/elasticsearch/index/translog/Translog.java @@ -167,8 +167,19 @@ public class Translog extends AbstractIndexShardComponent implements IndexShardC if (recoveredTranslogs.isEmpty()) { throw new IllegalStateException("at least one reader must be recovered"); } - current = createWriter(checkpoint.generation + 1); - this.lastCommittedTranslogFileGeneration = translogGeneration.translogFileGeneration; + boolean success = false; + try { + current = createWriter(checkpoint.generation + 1); + this.lastCommittedTranslogFileGeneration = translogGeneration.translogFileGeneration; + success = true; + } finally { + // we have to close all the recovered ones otherwise we leak file handles here + // for instance if we have a lot of tlog and we can't create the writer we keep on holding + // on to all the uncommitted tlog files if we don't close + if (success == false) { + IOUtils.closeWhileHandlingException(recoveredTranslogs); + } + } } else { this.recoveredTranslogs = Collections.emptyList(); IOUtils.rm(location); diff --git a/core/src/main/java/org/elasticsearch/index/translog/TranslogReader.java b/core/src/main/java/org/elasticsearch/index/translog/TranslogReader.java index d7077fd90ad..71dff6ec36e 100644 --- a/core/src/main/java/org/elasticsearch/index/translog/TranslogReader.java +++ b/core/src/main/java/org/elasticsearch/index/translog/TranslogReader.java @@ -138,7 +138,7 @@ public abstract class TranslogReader implements Closeable, Comparable