From c6253d23647eb9a3292ad8f47a9dee15be381df4 Mon Sep 17 00:00:00 2001 From: dengweisysu Date: Thu, 10 Oct 2019 05:15:15 +0800 Subject: [PATCH] Sync translog without lock before trim unreferenced readers (#47790) This commit is similar to the optimization made in #45765. With this change, we fsync most of the data of the current generation without holding writeLock when trimming unreferenced readers. Relates #45765 --- .../main/java/org/elasticsearch/index/translog/Translog.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/server/src/main/java/org/elasticsearch/index/translog/Translog.java b/server/src/main/java/org/elasticsearch/index/translog/Translog.java index 5f7f834c5e292..63dfc0459e07c 100644 --- a/server/src/main/java/org/elasticsearch/index/translog/Translog.java +++ b/server/src/main/java/org/elasticsearch/index/translog/Translog.java @@ -1678,6 +1678,8 @@ public void rollGeneration() throws IOException { * required generation */ public void trimUnreferencedReaders() throws IOException { + // move most of the data to disk to reduce the time the lock is held + sync(); try (ReleasableLock ignored = writeLock.acquire()) { if (closed.get()) { // we're shutdown potentially on some tragic event, don't delete anything @@ -1705,6 +1707,7 @@ public void trimUnreferencedReaders() throws IOException { // We now update the checkpoint to ignore the file we are going to remove. // Note that there is a provision in recoverFromFiles to allow for the case where we synced the checkpoint // but crashed before we could delete the file. + // sync at once to make sure that there's at most one unreferenced generation. current.sync(); deleteReaderFiles(reader); }