From 40060f8b7080d06a218518445a0a1dfc520c812a Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Mon, 8 Jan 2024 13:23:05 +0100 Subject: [PATCH] Reduce contention on flushControl.isFullFlush(). (#12958) `flushControl.isFullFlush()` is a surprising source of contention with documents that are cheap to index and many indexing threads. If I slightly modify luceneutil's `IndexGeoNames` benchmark to configure a 4GB indexing buffer and disable `TextField` fields, which are more costly to index than `KeywordField` or `IntField` fields, this brings the time to load all the dataset in the `IndexWriter` buffers from 8.0s to 7.0s. --- .../src/java/org/apache/lucene/index/DocumentsWriter.java | 6 +++++- .../apache/lucene/index/DocumentsWriterFlushControl.java | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lucene/core/src/java/org/apache/lucene/index/DocumentsWriter.java b/lucene/core/src/java/org/apache/lucene/index/DocumentsWriter.java index 37f2539d0f0..ec6ced68002 100644 --- a/lucene/core/src/java/org/apache/lucene/index/DocumentsWriter.java +++ b/lucene/core/src/java/org/apache/lucene/index/DocumentsWriter.java @@ -167,7 +167,11 @@ final class DocumentsWriter implements Closeable, Accountable { private boolean applyAllDeletes() throws IOException { final DocumentsWriterDeleteQueue deleteQueue = this.deleteQueue; - if (flushControl.isFullFlush() == false + // Check the applyAllDeletes flag first. This helps exit early most of the time without checking + // isFullFlush(), which takes a lock and introduces contention on small documents that are quick + // to index. + if (flushControl.getApplyAllDeletes() + && flushControl.isFullFlush() == false // never apply deletes during full flush this breaks happens before relationship. && deleteQueue.isOpen() // if it's closed then it's already fully applied and we have a new delete queue diff --git a/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterFlushControl.java b/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterFlushControl.java index 64acae6ec7b..9021a588f10 100644 --- a/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterFlushControl.java +++ b/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterFlushControl.java @@ -509,6 +509,14 @@ final class DocumentsWriterFlushControl implements Accountable, Closeable { return flushDeletes.getAndSet(false); } + /** + * Check whether deletes need to be applied. This can be used as a pre-flight check before calling + * {@link #getAndResetApplyAllDeletes()} to make sure that a single thread applies deletes. + */ + public boolean getApplyAllDeletes() { + return flushDeletes.get(); + } + public void setApplyAllDeletes() { flushDeletes.set(true); }