Skip to content

[ENH] GetCollectionSize on SysDB read replica #3503

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
merged 5 commits into from
Jan 18, 2025

Conversation

drewkim
Copy link
Contributor

@drewkim drewkim commented Jan 17, 2025

Description of changes

Summarize the changes made by this PR.

  • Improvements & Bug fixes
    • None
  • New functionality
    • Hooks up SysDB Go coordinator to a DB read replica
    • Adds a new RPC, GetCollectionSize, to return the total records in a collection, pulling from the reader DB

Test plan

How are these changes tested?

  • Tests pass locally with pytest for python, yarn test for js, cargo test for rust

Documentation Changes

Are all docstrings for user-facing APIs updated if required? Do we need to make documentation changes in the docs repository?

Copy link

Reviewer Checklist

Please leverage this checklist to ensure your code review is thorough before approving

Testing, Bugs, Errors, Logs, Documentation

  • Can you think of any use case in which the code does not behave as intended? Have they been tested?
  • Can you think of any inputs or external events that could break the code? Is user input validated and safe? Have they been tested?
  • If appropriate, are there adequate property based tests?
  • If appropriate, are there adequate unit tests?
  • Should any logging, debugging, tracing information be added or removed?
  • Are error messages user-friendly?
  • Have all documentation changes needed been made?
  • Have all non-obvious changes been commented?

System Compatibility

  • Are there any potential impacts on other parts of the system or backward compatibility?
  • Does this change intersect with any items on our roadmap, and if so, is there a plan for fitting them together?

Quality

  • Is this code of a unexpectedly high quality (Readability, Modularity, Intuitiveness)

Copy link
Contributor Author

drewkim commented Jan 17, 2025

This stack of pull requests is managed by Graphite. Learn more about stacking.

@@ -33,7 +32,7 @@ type Coordinator struct {
deleteMode DeleteMode
}

func NewCoordinator(ctx context.Context, db *gorm.DB, deleteMode DeleteMode) (*Coordinator, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A bit of clean up—we don't actually use the db in the coordinator since we access the globalDB.

@@ -142,6 +143,31 @@ func (s *collectionDb) getCollections(id *string, name *string, tenantID string,
return
}

func (s *collectionDb) GetCollectionSize(id string) (uint64, error) {
query := s.read_db.Table("collections").
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note the use of read_db here.

@drewkim drewkim marked this pull request as ready for review January 17, 2025 17:54
@drewkim drewkim force-pushed the drew/_enh_getcollectionsize_on_sysdb_read_replica branch from 0e9a6c3 to d0e4a2f Compare January 17, 2025 19:31
@drewkim drewkim requested a review from codetheweb January 17, 2025 21:46
@@ -25,6 +25,7 @@ import (
type APIsTestSuite struct {
suite.Suite
db *gorm.DB
read_db *gorm.DB
Copy link
Contributor

Choose a reason for hiding this comment

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

do you think it's worth using gorm's DBResolver or is that too much framework overhead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't want to cutover all of our reads to the read replica, which it seems DBResolver does automatically. Probably not worth at this point

Copy link
Contributor

Choose a reason for hiding this comment

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

it looked like you could configure it to not use the replica by default and then explicitly use the replica with

tx := db.Clauses(dbresolver.Use("secondary"), dbresolver.Write).Begin()

but not strongly advocating for it

@@ -384,4 +392,5 @@ service SysDB {
rpc FlushCollectionCompaction(FlushCollectionCompactionRequest) returns (FlushCollectionCompactionResponse) {}
rpc RestoreCollection(RestoreCollectionRequest) returns (RestoreCollectionResponse) {}
rpc ListCollectionVersions(ListCollectionVersionsRequest) returns (ListCollectionVersionsResponse) {}
rpc GetCollectionSize(GetCollectionSizeRequest) returns (GetCollectionSizeResponse) {}
Copy link
Contributor

Choose a reason for hiding this comment

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

what do you think about calling this

Suggested change
rpc GetCollectionSize(GetCollectionSizeRequest) returns (GetCollectionSizeResponse) {}
rpc GetApproximateCollectionSize(GetCollectionSizeRequest) returns (GetCollectionSizeResponse) {}

since this is reading from the replica and it may lag behind the leader?

Copy link
Contributor

@codetheweb codetheweb left a comment

Choose a reason for hiding this comment

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

do we need to update go/pkg/log/store/schema/collection.sql?

@drewkim drewkim requested a review from eculver January 17, 2025 22:45
@@ -142,6 +143,29 @@ func (s *collectionDb) getCollections(id *string, name *string, tenantID string,
return
}

func (s *collectionDb) GetCollectionSize(id string) (uint64, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there some magical reason why some of these methods take a *string versus ones like this that take a string? I don't have enough context on why one would make sense over the other but it would be nice to be consistent. I feel like string should be the way to go here, but the callsite for this is using an interface which requires a deref, which is not great.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the call site to not require a deref. I think the use of *string in some places is because it isn't a required param.

defer span.End()
}

total_records_post_compaction, err := tc.metaDomain.CollectionDb(ctx).GetCollectionSize(*types.FromUniqueID(collectionID))
Copy link
Contributor

Choose a reason for hiding this comment

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

The *types.FromUniqueID(collectionID) part is a little strange. I guess it's fine if we're already doing it all over, but technically this could blow up if this returned nil.

Copy link
Contributor Author

@drewkim drewkim Jan 17, 2025

Choose a reason for hiding this comment

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

Updated to use .String() instead of a deref

Copy link
Contributor

@eculver eculver left a comment

Choose a reason for hiding this comment

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

I don't have anything blocking, but would be good to get @sanketkedia and @rohitcpbot to have a look before approving.

Copy link
Contributor

@eculver eculver left a comment

Choose a reason for hiding this comment

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

I'm going to go ahead and approve. I think it's pretty straightforward as is and all the testing looks good. We can adjust if we need to but this LGTM.

@drewkim drewkim merged commit fe90bdf into main Jan 18, 2025
79 checks passed
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