diwu-sf opened a new pull request, #17006:
URL: https://github.com/apache/lucene/pull/17006

   ### Description
   
   With soft deletes, `IndexWriter#getDocStats().numDocs` and 
`IndexWriter#hasDeletions()` can be wrong:
   
   1. A soft delete or soft update resolved by an auto flush isn't counted. 
`numDocs` is too high, and `hasDeletions()` can return false. A hard delete in 
the same spot is counted.
   2. An undelete with `tryUpdateDocValue` isn't counted, so `numDocs` is too 
low.
   3. When a merge commits, soft deletes made while the merge ran stop being 
counted, so `numDocs` goes up.
   4. `hasDeletions()` returns false for a segment that only has soft deletes, 
when the writer has no reader on it.
   
   1 to 3 last until the next NRT reopen, commit or explicit flush. Readers and 
commits are not affected.
   
   **Cause.** For 1 to 3, a soft delete only counts once its doc values update 
is written, but an auto flush, `tryUpdateDocValue` and a merge commit only 
queue the update. For 4, `SegmentCommitInfo#hasDeletions()` only checks for a 
live docs file, and only hard deletes write one.
   
   **Fix.** `IndexWriter#numDeletedDocs(SegmentCommitInfo)`, which 
`getDocStats` uses, now also counts the queued soft deletes updates. It applies 
them with `PendingSoftDeletes#applySoftDeletes` to a copy of the live docs, so 
the live docs that readers see don't change. `hasDeletions()` now checks 
`numDeletedDocs` for each segment, which also counts soft deletes on segments 
the writer has no reader for. `numDeletesToMerge` is unchanged, because 
`SoftDeletesRetentionMergePolicy` counts deletes from the reader, which can't 
see queued updates.
   
   For each segment with queued soft deletes updates, the count copies the live 
docs and walks the updates, under the writer lock. With 500,000 queued soft 
deletes in one segment, `getDocStats()` took about 7.5 ms.
   
   **Tests.** In `TestIndexWriter`, `testSoftDeleteCountsAfterAutoFlush` and 
`testSoftUpdateCountsAfterAutoFlush` cover 1, with 
`testHardDeleteCountsAfterAutoFlush` as a control. 
`testSoftUndeleteCountsBeforeWrite` covers 2 and 
`testHasDeletionsWithOnlySoftDeletes` covers 4. 
`TestIndexWriterMergePolicy#testSoftDeletesDuringMergeCountAfterMergeCommits` 
covers 3. The soft deletes tests fail without the fix and pass with it. 
`./gradlew :lucene:core:test` and `:lucene:core:check` pass.
   
   Related: because of 3, the randomized test in #16996 only checks 
`getDocStats` right after opening a writer. With this fix and #16996, checking 
`getDocStats` against an NRT reader at every step of that test passed 300 runs. 
Once #17003 is merged, the count should pass the hard live docs to 
`applySoftDeletes`, like `onDocValuesUpdate` does there.
   
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to