Repository navigation
Conversation
878b4b2 to
4347478
Compare
|
What's the status here? |
Still in progress. While fixing it, I encountered another issue, most likely on the USearch side: unum-cloud/USearch#697. I am trying to workaround the USearch problem and hope to have something ready tomorrow. |
73cf7fa to
7a04caf
Compare
f95792c to
b82c54a
Compare
|
The problem of concurrent adds for the same key (2. from cover letter) to be fixed in the scope of: https://scylladb.atlassian.net/browse/VECTOR-463. |
There was a problem hiding this comment.
Pull request overview
This PR fixes a race condition in the vector store that could cause panics during concurrent replacement operations. The fix decouples the atomic add_or_replace operation into separate remove and add operations, with the remove being serialized using an exclusive write lock to address thread-safety issues in the underlying usearch library.
Changes:
- Refactored the replacement logic to send separate Remove and Add index operations when updating existing embeddings
- Updated the usearch backend to handle Remove operations with exclusive locking to prevent concurrent access issues
- Renamed the
AddOrReplaceenum variant and related methods toAddto reflect the new separation of concerns
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
crates/vector-store/src/monitor_items.rs |
Modified to check if a key exists and send Remove before Add for updates; updated tests to expect separate Remove/Add operations |
crates/vector-store/src/index/usearch.rs |
Refactored add_or_replace into separate add and remove functions; added exclusive lock handling for Remove operations in dispatch_task; updated tests |
crates/vector-store/src/index/opensearch.rs |
Updated enum pattern matching from AddOrReplace to Add (internal implementation still uses add_or_replace function) |
crates/vector-store/src/index/actor.rs |
Renamed AddOrReplace enum variant to Add and updated the IndexExt trait method accordingly |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Concurrent replace operations could lead to a panic on `unwrap`. This race occurs if a key is removed by another thread between the existence check and the unwrap. Investigation revealed two underlying thread-safety issues in the `usearch` library's replace operation (see unum-cloud/USearch#697): 1. The `remove` operation is not safe to be called concurrently with other operations. 2. Concurrent `add` and `remove` operations on the same key are not thread-safe. This change fixes the panic and the non-exclusive remove by decoupling `add_or_replace` into separate `remove` and `add` operations within `monitor_items`. This serializes the operations, preventing the race. The second issue (concurrent add/remove on the same key) is not addressed in this change. It should be guaranteed by `monitor_items` that updates for the same primary key are processed serially to ensure the final state of the index matches the database. Fixes: VECTOR-464
The test was flaky because it assumed that a search would always succeed. However, since the add operation is concurrent, there is no guarantee that the search will find the newly added item immediately. This fix relaxes the assertion to account for this race condition, allowing the search to return no results.
b82c54a to
a9809b2
Compare
|
ci failed similar to known issue: VECTOR-440 |
While testing we can see seldom crash in usearch running concurrent_add_and_search test. The crash is in the search path. According to scylladb#330 and unum-cloud/USearch#697 we can run concurrently only one family of operations: add/insert is the first, search/count is the second. This patch introduces two family operations: insert and search. Each operation increases counter for operations as a flag that some task is running and decreases it at finish. Fixes: VECTOR-474
While testing we can see seldom crashes in usearch running concurrent_add_and_search test. The crash is in the search path. According to scylladb#330 and unum-cloud/USearch#697 we can run concurrently only one family of operations: add/insert is the first, search/count is the second. This patch introduces two family operations: insert and search. The commit introduces Operation type to synchronise between them. Each operation needs to get permit to proceed, and getting the permit is an async task (it could wait if the operation is different than requested). The Permit contains counter and notifier - in Drop if counter decreses to 0 the listener will be notified. Fixes: VECTOR-474
While testing we can see seldom crash in usearch running concurrent_add_and_search test. The crash is in the search path. According to #330 and unum-cloud/USearch#697 we can run concurrently only one family of operations: add/insert is the first, search/count is the second. This patch introduces two family operations: insert and search. Each operation increases counter for operations as a flag that some task is running and decreases it at finish. I've run 4000 times the concurrent_add_and_search test without crash. Fixes: VECTOR-474
While testing we can see seldom crashes in usearch running concurrent_add_and_search test. The crash is in the search path. According to scylladb#330 and unum-cloud/USearch#697 we can run concurrently only one family of operations: add/insert is the first, search/count is the second. This patch introduces two family operations: insert and search. The commit introduces Operation type to synchronise between them. Each operation needs to get permit to proceed, and getting the permit is an async task (it could wait if the operation is different than requested). The Permit contains counter and notifier - in Drop if counter decreses to 0 the listener will be notified. Fixes: VECTOR-474
Concurrent replace operations could lead to a panic on
unwrap. This raceoccurs if a key is removed by another thread between the existence check
and the unwrap.
Investigation revealed two underlying thread-safety issues in the
usearchlibrary's replace operation (see unum-cloud/USearch#697):
removeoperation is not safe to be called concurrently with other operations.addandremoveoperations on the same key are not thread-safe.This change fixes the panic and the non-exclusive remove by decoupling
add_or_replaceinto separateremoveandaddoperations withinmonitor_items. This serializes the operations, preventing the race.The second issue (concurrent add/remove on the same key) is not addressed
in this change. It should be guaranteed by
monitor_itemsthat updatesfor the same primary key are processed serially to ensure the final
state of the index matches the database.
Fixes: VECTOR-464