fix(collection): allow writes and reads to proceed during Optimize - #614
Open
YongqiYin wants to merge 3 commits into
Open
fix(collection): allow writes and reads to proceed during Optimize#614YongqiYin wants to merge 3 commits into
YongqiYin wants to merge 3 commits into
Conversation
Optimize() held schema_handle_mtx_ exclusively for its whole duration, so Insert/Query/Fetch/Delete (which take it in shared mode) were blocked until the long-running compact finished. - Downgrade Optimize's schema lock to shared mode; writes and reads now proceed during the compact and only wait on the short write_mtx_ critical sections (flush and version commit). Schema operations and close/destroy still take the lock exclusively and remain mutually exclusive with Optimize. - Add optimize_mtx_ to keep concurrent Optimize calls serialized, preserving the previous queuing semantics. - Make SegmentManager internally thread-safe with a shared_mutex: readers (get_segments) can now run concurrently with the segment replacement in Optimize's commit phase. - Add a regression test that asserts inserts and fetches make progress while a background Optimize is running. Fixes alibaba#553
egolearner
previously approved these changes
Jul 24, 2026
egolearner
left a comment
Collaborator
There was a problem hiding this comment.
基本ok,ut中验证optimize和写、fetch、query并发会更加完备
YongqiYin
force-pushed
the
fix/optimize-blocking-writes
branch
from
July 24, 2026 09:25
a478c5c to
63d26e0
Compare
Strengthening the UT to run Insert, Fetch and Query concurrently with a background Optimize (review feedback) exposed a use-after-free: the commit phase called reload_vector_index() on a live segment, destroying vector indexers and mutating block metas that concurrent readers were still using (readers only hold the shared schema lock). - Commit the new segment set by reopening segments from their new metas and swapping instances, never mutating live segments in place; in-flight readers keep replaced instances alive via shared_ptr. - Add SegmentManager::replace_segments() to apply the whole swap under one lock (validate first, then mutate), so readers never observe a partially committed segment set (no transient duplicate or missing segments), and drop the now-unused destroy_segment(). - Extend the regression test: writer, fetcher and querier threads now run concurrently with Optimize; worker failures are collected and asserted on the main thread. Query errors caused by the pre-existing Insert/Query visibility race in the writing segment (reproducible without Optimize) are tolerated and documented; a quiescent full-topk query is asserted after all threads join.
With Optimize committing via segment reopen-and-swap, the pre-index vector files of a replaced segment instance (e.g. the flat file that a newly built HNSW index supersedes) were no longer deleted and lingered on disk until the segment was compacted away. Reclaim them with the same mark-and-defer model segments already use: - VectorColumnIndexer: add MarkDestroyOnRelease(); a marked indexer closes and removes its index file in the destructor, i.e. only after the last reference is released, so in-flight readers of the replaced segment instance keep the file alive until they finish. - Segment: add mark_vector_index_files_for_removal(). Base and quantized indexers are marked independently: when a quantized index is built on top of a reused base file (see create_vector_index), only the old quantized file is superseded and the base file is still referenced by the new segment meta, so it must not be marked. - Optimize: after the new segment set is committed, mark the replaced instances' superseded indexers. - Add a regression test asserting exactly one index file remains per indexed column after Optimize and that a fresh open reads all docs.
Collaborator
Author
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Optimize() held schema_handle_mtx_ exclusively for its whole duration, so Insert/Query/Fetch/Delete (which take it in shared mode) were blocked until the long-running compact finished.
Fixes #553