Apache Lucene open-source search software
Fix race condition on flush for DWPT seqNo generation (#13627)
Benjamin Trentd26b1521170f
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex bdc5ce0796e..572d2c6ff67 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -360,6 +360,8 @@ Bug Fixes* GITHUB#13615: Correct scalar quantization when used in conjunction with COSINE similarity. Vectors are normalizedbefore quantization to ensure the cosine similarity is correctly calculated. (Ben Trent)+* GITHUB#13627: Fix race condition on flush for DWPT seqNo generation. (Ben Trent, Ao Li)+Other--------------------(No changes)diff --git a/lucene/core/src/java/org/apache/lucene/index/DocumentsWriter.java b/lucene/core/src/java/org/apache/lucene/index/DocumentsWriter.javaindex 7955df5630e..e32c8b20c04 100644--- a/lucene/core/src/java/org/apache/lucene/index/DocumentsWriter.java+++ b/lucene/core/src/java/org/apache/lucene/index/DocumentsWriter.java@@ -430,10 +430,16 @@ final class DocumentsWriter implements Closeable, Accountable {}flushingDWPT = flushControl.doAfterDocument(dwpt);} finally {- if (dwpt.isFlushPending() || dwpt.isAborted()) {- dwpt.unlock();- } else {- perThreadPool.marksAsFreeAndUnlock(dwpt);+ // If a flush is occurring, we don't want to allow this dwpt to be reused+ // If it is aborted, we shouldn't allow it to be reused+ // If the deleteQueue is advanced, this means the maximum seqNo has been set and it cannot be+ // reused+ synchronized (flushControl) {+ if (dwpt.isFlushPending() || dwpt.isAborted() || dwpt.isQueueAdvanced()) {+ dwpt.unlock();+ } else {+ perThreadPool.marksAsFreeAndUnlock(dwpt);+ }}assert dwpt.isHeldByCurrentThread() == false : "we didn't release the dwpt even on abort";}diff --git a/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterDeleteQueue.java b/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterDeleteQueue.javaindex 2cea3a6db0d..34c858eaf5d 100644--- a/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterDeleteQueue.java+++ b/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterDeleteQueue.java@@ -636,7 +636,7 @@ final class DocumentsWriterDeleteQueue implements Accountable, Closeable {}/** Returns <code>true</code> if it was advanced. */- boolean isAdvanced() {+ synchronized boolean isAdvanced() {return advanced;}}diff --git a/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterPerThread.java b/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterPerThread.javaindex 3fe76a6338f..d472302f7e8 100644--- a/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterPerThread.java+++ b/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterPerThread.java@@ -718,6 +718,10 @@ final class DocumentsWriterPerThread implements Accountable, Lock {return flushPending.get() == Boolean.TRUE;}+ boolean isQueueAdvanced() {+ return deleteQueue.isAdvanced();+ }+/** Sets this DWPT as flush pending. This can only be set once. */void setFlushPending() {flushPending.set(Boolean.TRUE);diff --git a/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterPerThreadPool.java b/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterPerThreadPool.javaindex d69a71bfea5..93276c7ced4 100644--- a/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterPerThreadPool.java+++ b/lucene/core/src/java/org/apache/lucene/index/DocumentsWriterPerThreadPool.java@@ -138,6 +138,15 @@ final class DocumentsWriterPerThreadPool implements Iterable<DocumentsWriterPerTvoid marksAsFreeAndUnlock(DocumentsWriterPerThread state) {final long ramBytesUsed = state.ramBytesUsed();+ assert state.isFlushPending() == false+ && state.isAborted() == false+ && state.isQueueAdvanced() == false+ : "DWPT has pending flush: "+ + state.isFlushPending()+ + " aborted="+ + state.isAborted()+ + " queueAdvanced="+ + state.isQueueAdvanced();assert contains(state): "we tried to add a DWPT back to the pool but the pool doesn't know about this DWPT";freeList.addAndUnlock(state, ramBytesUsed);