Skip to content

fix(server): Use BucketSet directly in OnChange#7095

Merged
dranikpg merged 3 commits into
dragonflydb:mainfrom
dranikpg:use-bucket-set
Apr 14, 2026
Merged

fix(server): Use BucketSet directly in OnChange#7095
dranikpg merged 3 commits into
dragonflydb:mainfrom
dranikpg:use-bucket-set

Conversation

@dranikpg
Copy link
Copy Markdown
Contributor

@dranikpg dranikpg commented Apr 8, 2026

Compute bucket set once in DbSlice and pass it on to OnChange callbacks as it is

Continuation of #7068

@dranikpg
Copy link
Copy Markdown
Contributor Author

dranikpg commented Apr 9, 2026

augment review

@augmentcode
Copy link
Copy Markdown

augmentcode Bot commented Apr 9, 2026

🤖 Augment PR Summary

Summary: This PR refactors insert change notifications to pass a precomputed PrimeTable::BucketSet directly to OnChange listeners.

Changes:

  • DbSlice::AddOrFindInternal computes CVCUponInsert() once and forwards the resulting bucket set through CallChangeCallbacks.
  • ChangeReq is simplified to std::variant<PrimeTable::bucket_iterator, PrimeTable::BucketSet>, and listeners are updated to use variant accessors.
  • SerializerBase insert handling now consumes a BucketSet directly instead of recomputing from the key.
  • DashTable::BucketSet adds operator== to debug-verify that bucket selection remains stable across potentially blocking callbacks.

🤖 Was this summary useful? React with 👍 or 👎

Copy link
Copy Markdown

@augmentcode augmentcode Bot left a comment

Choose a reason for hiding this comment

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

Review completed. 2 suggestions posted.

Fix All in Augment

Comment augment review to trigger a new review at any time.

Comment thread src/server/serializer_base.cc Outdated
void SerializerBase::OnChangeBlocking(DbIndex db_index, std::string_view key) {
void SerializerBase::OnChangeBlocking(DbIndex db_index, const PrimeTable::BucketSet& buckets) {
// We must acquire the mutex ahead and process all buckets under the same lock.
// This ensures that CVCUponInsert and the table insertion that invoked this callback
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The comment here still says CVCUponInsert is executed in this function, but the insert path now receives a precomputed PrimeTable::BucketSet, so the wording about state alignment/linear ordering seems outdated. Consider updating the comment (and the related header comment) so it matches the new data flow.

Severity: low

Other Locations
  • src/server/serializer_base.h:95

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed

Comment thread src/server/table.h
}
};
// ChangeReq - describes the change to the table: either single bucket or whole bucket set.
using ChangeReq = std::variant<PrimeTable::bucket_iterator, PrimeTable::BucketSet>;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Now that ChangeReq is a std::variant<PrimeTable::bucket_iterator, PrimeTable::BucketSet>, the replication/serialization docs that describe ChangeReq::update() and req.change appear stale and could mislead future work. Consider updating docs/shard-serialization.md to reflect the new variant-based API.

Severity: low

Other Locations
  • docs/shard-serialization.md:169

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

@dranikpg dranikpg marked this pull request as ready for review April 9, 2026 07:39
Copilot AI review requested due to automatic review settings April 9, 2026 07:39
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the DbSlice on-change notification path to compute the DashTable BucketSet once (at the mutation site) and pass it through callbacks, instead of recomputing it inside listeners. This continues the snapshot/OnChange correctness work from #7068 by reducing redundant work and avoiding passing transient insertion data.

Changes:

  • Replace ChangeReq with a std::variant<bucket_iterator, BucketSet> to carry either a single bucket or a precomputed insertion bucket set.
  • Update SerializerBase OnChange handling to accept a BucketSet directly for insertion notifications.
  • Compute BucketSet once in DbSlice::AddOrFindInternal and pass it to CallChangeCallbacks; add BucketSet::operator== for invariants/debugging.

Reviewed changes

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

Show a summary per file
File Description
src/server/table.h Simplifies change notification payload to a variant including BucketSet.
src/server/serializer_base.h Updates insertion OnChange API to take BucketSet instead of a key.
src/server/serializer_base.cc Dispatches variant directly and processes passed BucketSet buckets.
src/server/db_slice.cc Computes BucketSet once for insertions and updates callback consumers accordingly.
src/core/dash.h Adds equality operator for BucketSet to support comparisons/assertions.

Comment thread src/server/db_slice.cc Outdated
Comment on lines +685 to +686
// Set of possible insertion buckets must be the same after possibly blocking call
DCHECK(bucket_set == db.prime.CVCUponInsert(key));
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

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

The new DCHECK(bucket_set == db.prime.CVCUponInsert(key)) can be violated if CallChangeCallbacks yields and other fibers mutate the table in-between, causing CVCUponInsert to return a different BucketSet for the same key. This risks flaky DCHECK failures in debug/testing; consider removing this assertion or enforcing a real exclusion if you need this invariant.

Suggested change
// Set of possible insertion buckets must be the same after possibly blocking call
DCHECK(bucket_set == db.prime.CVCUponInsert(key));
// Do not re-check CVCUponInsert(key) after callbacks: they may yield and allow other fibers
// to mutate the table, which can legitimately change the candidate bucket set.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That is true and exactly why I have it do see if it's really happens in practise

Copy link
Copy Markdown
Collaborator

@romange romange Apr 9, 2026

Choose a reason for hiding this comment

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

then make it clear in the comments that if this breaks it's not a bug, but something we do not handle yet in the implementation.

Copy link
Copy Markdown

@augmentcode augmentcode Bot left a comment

Choose a reason for hiding this comment

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

Review completed. No suggestions at this time.

Comment augment review to trigger a new review at any time.

Comment thread src/server/db_slice.cc Outdated

// It's a new entry.
CallChangeCallbacks(cntx.db_index, ChangeReq{key});
auto bucket_set = db.prime.CVCUponInsert(key);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

there is some performance impact as we do it now even when there are no registered callbacks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

fixed

@dranikpg dranikpg requested a review from romange April 14, 2026 10:13
@dranikpg dranikpg merged commit 4c43566 into dragonflydb:main Apr 14, 2026
13 checks passed
@dranikpg dranikpg deleted the use-bucket-set branch May 20, 2026 15:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants