diff --git a/server/src/main/java/org/elasticsearch/index/engine/InternalEngine.java b/server/src/main/java/org/elasticsearch/index/engine/InternalEngine.java index 7303fc0a5e0..a2d27e14983 100644 --- a/server/src/main/java/org/elasticsearch/index/engine/InternalEngine.java +++ b/server/src/main/java/org/elasticsearch/index/engine/InternalEngine.java @@ -2037,10 +2037,13 @@ public class InternalEngine extends Engine { private void releaseIndexCommit(IndexCommit snapshot) throws IOException { // Revisit the deletion policy if we can clean up the snapshotting commit. if (combinedDeletionPolicy.releaseCommit(snapshot)) { - ensureOpen(); - // Here we don't have to trim translog because snapshotting an index commit - // does not lock translog or prevents unreferenced files from trimming. - indexWriter.deleteUnusedFiles(); + try { + // Here we don't have to trim translog because snapshotting an index commit + // does not lock translog or prevents unreferenced files from trimming. + indexWriter.deleteUnusedFiles(); + } catch (AlreadyClosedException ignored) { + // That's ok, we'll clean up unused files the next time it's opened. + } } } diff --git a/server/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java b/server/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java index 36dbfa4b342..d096362edf0 100644 --- a/server/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java +++ b/server/src/test/java/org/elasticsearch/index/engine/InternalEngineTests.java @@ -4985,6 +4985,8 @@ public class InternalEngineTests extends EngineTestCase { IOUtils.close(engine, store); store = createStore(); final AtomicLong globalCheckpoint = new AtomicLong(SequenceNumbers.NO_OPS_PERFORMED); + final Engine.IndexCommitRef snapshot; + final boolean closeSnapshotBeforeEngine = randomBoolean(); try (InternalEngine engine = createEngine(store, createTempDir(), globalCheckpoint::get)) { int numDocs = between(1, 20); for (int i = 0; i < numDocs; i++) { @@ -4995,7 +4997,6 @@ public class InternalEngineTests extends EngineTestCase { } final boolean flushFirst = randomBoolean(); final boolean safeCommit = randomBoolean(); - final Engine.IndexCommitRef snapshot; if (safeCommit) { snapshot = engine.acquireSafeIndexCommit(); } else { @@ -5012,10 +5013,17 @@ public class InternalEngineTests extends EngineTestCase { assertThat(reader.numDocs(), equalTo(flushFirst && safeCommit == false ? numDocs : 0)); } assertThat(DirectoryReader.listCommits(engine.store.directory()), hasSize(2)); - snapshot.close(); - // check it's clean up - engine.flush(true, true); - assertThat(DirectoryReader.listCommits(engine.store.directory()), hasSize(1)); + + if (closeSnapshotBeforeEngine) { + snapshot.close(); + // check it's clean up + engine.flush(true, true); + assertThat(DirectoryReader.listCommits(engine.store.directory()), hasSize(1)); + } + } + + if (closeSnapshotBeforeEngine == false) { + snapshot.close(); // shouldn't throw AlreadyClosedException } }