Skip to content
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

feat(firestore): Query profiling #10164

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

bhshkh
Copy link
Contributor

@bhshkh bhshkh commented May 14, 2024

Design: go/cfdb-fs-go-query-profiling and go/cfdb-ds-go-query-profiling
Similar PR for Datastore #9200

@bhshkh bhshkh requested review from a team as code owners May 14, 2024 00:03
@product-auto-label product-auto-label bot added the api: firestore Issues related to the Firestore API. label May 14, 2024
@bhshkh bhshkh marked this pull request as draft May 14, 2024 00:06
@bhshkh bhshkh marked this pull request as ready for review May 14, 2024 21:37
@bhshkh bhshkh enabled auto-merge (squash) May 14, 2024 23:57
firestore/query.go Show resolved Hide resolved
firestore/query.go Outdated Show resolved Hide resolved
Comment on lines 205 to 207
// RunOption lets the user provide options while running a query
type RunOption interface {
apply(*runQuerySettings) error
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as above for this option as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved

@@ -475,6 +644,14 @@ func (q Query) fromProto(pbQuery *pb.RunQueryRequest) (Query, error) {
q.limit = limit
}

var err error
q.runQuerySettings, err = newRunQuerySettings(nil)
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 a case where this line will produce an error? Is there something in the proto that you wanted to extract here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It won't produce error now but I don't want to ignore the error since it may return some error in the future.
Things are getting extracted from proto in the next few lines.

@@ -1226,14 +1459,17 @@ type QuerySnapshot struct {

type btreeDocumentIterator btree.Iterator

func (it *btreeDocumentIterator) next() (*DocumentSnapshot, error) {
func (it *btreeDocumentIterator) next(_ ...RunOption) (*DocumentSnapshot, error) {
Copy link
Contributor

@gkevinzheng gkevinzheng Jul 10, 2024

Choose a reason for hiding this comment

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

I'm kinda curious, is there a reason why a RunOption would need to be passed in here despite the function not looking like it needs it? Is it to futureproof this call?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

type docIterator interface {
	next(opts ...RunOption) (*DocumentSnapshot, error)
	getExplainMetrics() *ExplainMetrics
	stop()
}

docIterator interface has been modified. RunOption is being passed here to ensure that the btreeDocumentIterator implements the docIterator interface.

Yes, it is not getting used currently but might get used later

firestore/query.go Outdated Show resolved Hide resolved
firestore/query.go Outdated Show resolved Hide resolved
// This can be used in combination with Deserialize to marshal Query objects.
// This could be useful, for instance, if executing a query formed in one
// process in another.
func (q Query) Serialize() ([]byte, 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 this new Serialize function required for query profiling?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was earlier at line 342. This is not new function. The diff is not getting rendered correctly.

// result, err := query.GetAllWithOptions(ExplainOptions{Analyze: true})
//
// It is not necessary to call Stop on the iterator after calling GetAllWithOptions.
func (it *DocumentIterator) GetAllWithOptions(opts ...RunOption) (*GetAllWithOptionsResult, 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 this new function required? It seems a little dangerous to give multiple places to set options like this. I could see that making maintenance/testing difficult in the future

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -1083,8 +1304,16 @@ func newQueryDocumentIterator(ctx context.Context, q *Query, tid []byte, rs *rea
}
}

func (it *queryDocumentIterator) next() (_ *DocumentSnapshot, err error) {
// opts override the options stored in it.q.runQuerySettings
func (it *queryDocumentIterator) next(opts ...RunOption) (_ *DocumentSnapshot, err error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this mean we support changing options mid-query?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is an unexported method. User cannot call this directly

But yes, user can definitely do this:
:

        // Analyze true
	docIter := client.Collection("cities").
		WithRunOptions(firestore.ExplainOptions{Analyze: true}).
		Documents(ctx)

         // Read single document
         docSnapshot, _ := docIter.Next()
         
         // Read rest of the documents with Analyze false
         res, _ := docIter.GetAllWithOptions(firestore.ExplainOptions{Analyze: true})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: firestore Issues related to the Firestore API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants