Skip to content

Extract Vector Set cleanup work channel and work set - #2020

Merged
Tiago Nápoli (tiagonapoli) merged 4 commits into
microsoft:mainfrom
tiagonapoli:tiagonapoli/vector-set-cleanup-primitives
Aug 5, 2026
Merged

Tiago Nápoli (tiagonapoli) merged 4 commits into
microsoft:mainfrom
tiagonapoli:tiagonapoli/vector-set-cleanup-primitives

Conversation

@tiagonapoli

@tiagonapoli Tiago Nápoli (tiagonapoli) commented Aug 4, 2026 •

Copy link
Copy Markdown
Collaborator

Pulls the ad-hoc Channel and ConcurrentDictionary plumbing in the Vector Set cleanup pipeline behind two small types, and uses them in VectorManager. Behaviour is unchanged.

VectorSetCleanupWorkChannel<T>

Replaces all three queues. They differ only in how the consumer reads:

queue payload read shape
requestDropTaskChannel none - a wake-up drains and discards the backlog
requestCleanupTaskChannel (ulong Context, TaskCompletionSource) consumes the backlog as one batch
cleanupTaskChannel none, but one item = one scan one item per iteration

The drop queue's items are pure wake-ups - the work lives in requestedDrops, and one pass services all of it - so the backlog genuinely collapses and

// Drain all wake up signals
while (requestDropTaskChannel.Reader.TryRead(out _)) { }

gets a home as requestDropTaskChannel.DrainPending().

The cleanup-request queue also reads everything available, but for the opposite reason: each item is distinct work (a context to mark, and a TaskCompletionSource a caller is waiting on), and draining it in one go lets the pass mark every context and then persist UpdateContextMetadata once rather than once per item.

The last queue acts on the item itself, so it takes one per iteration.

Two of the queues carry no payload, because the work they refer to lives elsewhere - in requestedDrops for the drop queue, and in the marked contexts for the cleanup queue. Both DrainPending() and the payload-free TryPublish() are extension methods on VectorSetCleanupWorkChannel<object> rather than members, so neither can be called on requestCleanupTaskChannel: publishing a default tuple there would queue a null TaskCompletionSource, and discarding its backlog would drop contexts that need marking and strand every waiter on it.

The type also folds the three-step shutdown

requestDropTaskChannel.Writer.Complete();
AsyncUtils.BlockingWait(requestDropTaskChannel.Reader.Completion);
AsyncUtils.BlockingWait(requestDropTask);

into requestDropTaskChannel.CompleteAndWaitForConsumerTask(requestDropTask), and does not expose the underlying ChannelReader/ChannelWriter.

VectorSetCleanupWorkSet<TValue>

Wraps requestedDrops, moving the NET9_0_OR_GREATER alternate-lookup branching and the poll-until-absent loop out of VectorManager. DropRequested and WaitForDiskANNIndexDrop become one-liners over Contains / WaitForCompletion.

Tests

Unit tests over both types.

Copilot AI lite review requested due to automatic review settings August 4, 2026 21:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Refactors the Vector Set cleanup pipeline by encapsulating the existing Channel-based signaling and ConcurrentDictionary-based “requested drops” tracking into two dedicated helper types, and wires these into VectorManager while keeping overall behavior intended to remain unchanged.

Changes:

  • Introduces VectorSetCleanupWorkChannel<T> to centralize the single-consumer queue/wake-up patterns used by Vector Set cleanup tasks.
  • Introduces VectorSetCleanupWorkSet<TValue> to wrap the “requested drops” dictionary and the NET9 alternate-lookup branching.
  • Adds unit tests covering both helper types.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
test/standalone/Garnet.test.vectorset/VectorSetCleanupWorkSetTests.cs Adds unit tests for VectorSetCleanupWorkSet<TValue> behavior (add/contains/complete/enumerate/wait).
test/standalone/Garnet.test.vectorset/VectorSetCleanupWorkChannelTests.cs Adds unit tests for publish/read/completion semantics of VectorSetCleanupWorkChannel<T>.
libs/server/Resp/Vector/VectorManager.cs Switches cleanup/request/drop queues and requested-drop tracking to the new helper types.
libs/server/Resp/Vector/VectorManager.Cleanup.cs Updates cleanup/drop worker loops to use VectorSetCleanupWorkChannel<T> + VectorSetCleanupWorkSet<TValue>.
libs/server/Resp/Vector/Cleanup/VectorSetCleanupWorkSet.cs New work-set wrapper over the “requested drops” dictionary + alternate lookup.
libs/server/Resp/Vector/Cleanup/VectorSetCleanupWorkChannel.cs New work-channel wrapper + payload-free extensions (TryPublish, DrainPending).

Comment thread libs/server/Resp/Vector/Cleanup/VectorSetCleanupWorkSet.cs
Pull the ad-hoc Channel and ConcurrentDictionary plumbing in the Vector Set
cleanup pipeline behind two small types.

VectorSetCleanupWorkChannel replaces all three queues. They differ only in how
the consumer reads: the drop and mark tasks service all outstanding work in one
pass, so they take the whole backlog per iteration and a redundant item costs
nothing, while the cleanup scan acts on the item itself and takes one per
iteration. The drop queue's payload stays null - it is a wake-up, and the work
it refers to lives in requestedDrops. The type folds the three-step "complete
the writer, wait for the reader, wait for the consumer" shutdown into
CompleteAndWaitForConsumerTask, and does not expose the underlying reader or
writer.

VectorSetCleanupWorkSet wraps the keyed set of outstanding drops, moving the
NET9_0_OR_GREATER alternate-lookup branching and the poll-until-absent loop out
of VectorManager.

Behaviour is unchanged.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: d251734c-427c-4331-aebd-471b12411de1
@tiagonapoli
Tiago Nápoli (tiagonapoli) force-pushed the tiagonapoli/vector-set-cleanup-primitives branch from 7711248 to 5ba2f32 Compare August 4, 2026 21:23
@tiagonapoli
Tiago Nápoli (tiagonapoli) merged commit bda83e9 into microsoft:main Aug 5, 2026
316 of 317 checks passed
@tiagonapoli
Tiago Nápoli (tiagonapoli) deleted the tiagonapoli/vector-set-cleanup-primitives branch August 5, 2026 17:17
@github-actions github-actions Bot locked and limited conversation to collaborators Oct 5, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants