Skip to content

Conversation

@knowack1
Copy link
Collaborator

@knowack1 knowack1 commented Nov 27, 2025

Usearch's API uses global locks. Operations like add and
remove can hold these locks for extended periods.

When these operations are executed on the Tokio runtime, they can block
worker threads. This leads to thread starvation, effectively halting all
asynchronous tasks, including handling HTTP requests, processing CDC
data, and serving metrics.

This commit moves the blocking add and remove calls to a dedicated
Rayon thread pool. By isolating these operations, the Tokio runtime
remains unblocked and responsive, ensuring
the application's I/O continues to function correctly.

Fixes: VECTOR-339

@knowack1 knowack1 marked this pull request as draft November 27, 2025 14:09
@knowack1 knowack1 force-pushed the vector-339-vector-store-blocked-during-fullscan branch 2 times, most recently from 7878e6a to 4d97b34 Compare December 4, 2025 13:24
@knowack1 knowack1 changed the title WIP vector-store: Move Usearch operations to a dedicated tasks Dec 4, 2025
.take(limit.0.get())
.collect();

sim.wait_search(start);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should measure time of preparing response also, so wait_search should be after preparing keys vector.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change reverted.
I was changing simulator because I tokio was handing for some reason - I though it was due to locks.
But in fact to was due to dropped tx channel for config in tests: https://github.com/scylladb/vector-store/pull/294/files#diff-cb2126eb464b326d11b2dcfbd02661e90d1a34a8735bd45a911c0e2bfee2607dL79. This caused that rx side was in the "busy loop".

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not reverted. The commit is still there.

Comment on lines 329 to 331
let keys = sim.keys.read().unwrap();
let len = keys.len() as u64;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need to lock sim.keys for all time of finding random keys.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.
Now keys are locked temporary only.

@knowack1 knowack1 force-pushed the vector-339-vector-store-blocked-during-fullscan branch from 4d97b34 to 50ab25b Compare December 5, 2025 14:49
The simulator crashes if a search was performed on an empty index
(i.e. before any keys were indexed).
@knowack1 knowack1 force-pushed the vector-339-vector-store-blocked-during-fullscan branch from 50ab25b to e056901 Compare December 5, 2025 15:41
@knowack1 knowack1 marked this pull request as ready for review December 5, 2025 15:46
@knowack1 knowack1 marked this pull request as draft December 5, 2025 15:46
@github-actions github-actions bot added the P2 label Dec 5, 2025
Usearch's API uses global locks. Operations like `add` and
`remove` can hold these locks for extended periods.

When these operations are executed on the Tokio runtime, they can block
worker threads. This leads to thread starvation, effectively halting all
asynchronous tasks, including handling HTTP requests, processing CDC
data, and serving metrics.

This commit moves the blocking `add` and `remove` calls to a dedicated
Rayon thread pool. By isolating these  operations, the Tokio runtime
remains unblocked and responsive, ensuring
the application's I/O continues to function correctly.
@knowack1 knowack1 force-pushed the vector-339-vector-store-blocked-during-fullscan branch from e056901 to f56877a Compare December 5, 2025 15:49
@knowack1 knowack1 marked this pull request as ready for review December 5, 2025 15:50
@github-actions github-actions bot added P2 and removed P2 labels Dec 5, 2025
@knowack1 knowack1 requested a review from ewienik December 5, 2025 15:50
@github-actions github-actions bot added P2 and removed P2 labels Dec 5, 2025
Copy link
Collaborator

@ewienik ewienik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a need for limiting rayon background threads to the same number as tokio threads.

} => {
add_or_replace(idx, keys, usearch_key, primary_key, embedding);
rayon::spawn(move || {
add_or_replace(idx, keys, usearch_key, primary_key, embedding);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently idx is based on RwLock, so there will be still blocking of thread. Better would be to replace the RwLock with a semaphore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants