-
Notifications
You must be signed in to change notification settings - Fork 432
fix: route insert-runner thread safety through a client capability #849
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jamesgao-jpg
merged 5 commits into
zilliztech:main
from
serhiizghama:fix/rate-runner-thread-safe-capability
Aug 27, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
94e8105
refactor: route insert-runner thread safety through a client capability
serhiizghama 838ffa2
fix: give non-thread-safe clients their own copy_for_thread
serhiizghama 8b68c6e
test: cover insert-runner thread-safety routing
serhiizghama 019c0c0
fix: give psycopg-backed clients a shallow copy_for_thread
serhiizghama 8544074
test: use a rate/batch_size pair valid under the new insert-batch val…
serhiizghama File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| """Unit tests for RatedMultiThreadingInsertRunner.send_insert_task routing. | ||
|
|
||
| These exercise the thread-safety routing without a live database: a thread-safe | ||
| client inserts through the shared object, while a non-thread-safe client is routed | ||
| through copy_for_thread() + init() so each worker owns its own connection. | ||
| """ | ||
|
|
||
| from contextlib import contextmanager | ||
| from copy import deepcopy | ||
|
|
||
| from vectordb_bench.backend.clients.api import VectorDB | ||
| from vectordb_bench.backend.runner.rate_runner import RatedMultiThreadingInsertRunner | ||
|
|
||
|
|
||
| class FakeDB(VectorDB): | ||
| def __init__(self, thread_safe: bool = True, name: str = "Fake"): | ||
| self.thread_safe = thread_safe | ||
| self.name = name | ||
| self.init_calls = 0 | ||
| self.inserted: list[int] = [] | ||
|
|
||
| @contextmanager | ||
| def init(self): | ||
| self.init_calls += 1 | ||
| yield | ||
|
|
||
| def insert_embeddings(self, embeddings: list, metadata: list, **kwargs): | ||
| self.inserted.append(len(embeddings)) | ||
| return len(embeddings), None | ||
|
|
||
| def search_embedding(self, *args, **kwargs): | ||
| return [] | ||
|
|
||
| def optimize(self, data_size: int | None = None): | ||
| return | ||
|
|
||
|
|
||
| class CapturingNonThreadSafeDB(FakeDB): | ||
| """Records the per-thread copy so the test can assert against it.""" | ||
|
|
||
| def __init__(self): | ||
| super().__init__(thread_safe=False, name="Capturing") | ||
| self.thread_copy: CapturingNonThreadSafeDB | None = None | ||
|
|
||
| def copy_for_thread(self) -> "VectorDB": | ||
| c = deepcopy(self) | ||
| self.thread_copy = c | ||
| return c | ||
|
|
||
|
|
||
| def _runner(db: VectorDB): | ||
| return RatedMultiThreadingInsertRunner(rate=10, db=db, dataset_iter=None, batch_size=10) | ||
|
|
||
|
|
||
| def test_thread_safe_client_uses_shared_object(): | ||
| db = FakeDB(thread_safe=True) | ||
| _runner(db).send_insert_task(db, [[0.1], [0.2]], ["a", "b"]) | ||
|
|
||
| # Shared client inserts directly, no per-thread copy/init from the runner. | ||
| assert db.inserted == [2] | ||
| assert db.init_calls == 0 | ||
|
|
||
|
|
||
| def test_non_thread_safe_client_routes_through_copy(): | ||
| db = CapturingNonThreadSafeDB() | ||
| _runner(db).send_insert_task(db, [[0.1], [0.2]], ["a", "b"]) | ||
|
|
||
| copy = db.thread_copy | ||
| assert copy is not None | ||
| assert copy is not db | ||
| # Insert + init happen on the thread-local copy, never the parent. | ||
| assert copy.init_calls == 1 | ||
| assert copy.inserted == [2] | ||
| assert db.inserted == [] | ||
| assert db.init_calls == 0 | ||
|
|
||
|
|
||
| def test_default_copy_for_thread_is_a_distinct_deep_copy(): | ||
| db = FakeDB(thread_safe=False) | ||
| copy = db.copy_for_thread() | ||
|
|
||
| assert copy is not db | ||
| assert isinstance(copy, FakeDB) | ||
| copy.inserted.append(1) | ||
| assert db.inserted == [] |
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
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
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
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
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
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Avoid deep-copying live Psycopg handles
Adbpg and VectorChord both declare thread_safe=False but do not override copy_for_thread. run_with_rate keeps the parent init context open while these workers run, so the default deepcopy(self) receives a live psycopg.Connection. I reproduced this with Psycopg 3.3.4: deepcopy(connection) raises TypeError: no default reduce due to non-trivial cinit. As a result, the first fixed-rate insert task for either backend fails before the copy reaches init().
Please give both clients a shallow-copy override that clears conn and cursor before worker init reopens them; PgVector uses the same handle pattern and is worth covering too. The fake regression client only contains copyable lists, so it does not exercise this boundary.