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

insights: call ingester methods by reference #86146

Merged
merged 1 commit into from
Aug 16, 2022
Merged

insights: call ingester methods by reference #86146

merged 1 commit into from
Aug 16, 2022

Conversation

matthewtodd
Copy link
Contributor

@matthewtodd matthewtodd commented Aug 15, 2022

Fixes the data race observed in #85988. Passing this data structure by
value meant that the fullHandler was working with a different object
than the external callers.

Release justification: Category 3: Fixes for high-priority or
high-severity bugs in existing functionality

Release note: None

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@matthewtodd matthewtodd requested a review from a team August 15, 2022 15:13
@matthewtodd matthewtodd marked this pull request as ready for review August 15, 2022 15:13
Copy link
Contributor

@maryliag maryliag left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @maryliag and @matthewtodd)


-- commits line 4 at r1:
nit: can you add a little more explanation about what this change does?

Copy link
Contributor

@maryliag maryliag left a comment

Choose a reason for hiding this comment

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

Just a small nit, otherwise :lgtm:

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @maryliag and @matthewtodd)

Copy link
Contributor

@j82w j82w left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @j82w, @maryliag, and @matthewtodd)


pkg/sql/sqlstats/insights/ingester.go line 58 at r1 (raw file):

}

func (i *concurrentBufferIngester) Start(ctx context.Context, stopper *stop.Stopper) {

Should a test be added to avoid possible regressions?

Copy link
Contributor Author

@matthewtodd matthewtodd left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @j82w and @maryliag)


-- commits line 4 at r1:

Previously, maryliag (Marylia Gutierrez) wrote…

nit: can you add a little more explanation about what this change does?

Done.


pkg/sql/sqlstats/insights/ingester.go line 58 at r1 (raw file):

Previously, j82w wrote…

Should a test be added to avoid possible regressions?

Good q. I'm wondering what that test would look like. I think we could come up with something, but I also think it's unlikely we'll revert to passing by value, so 🤷 Perhaps the data race detector is our friend here. WDYT?

Copy link
Contributor

@maryliag maryliag left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @j82w and @maryliag)


pkg/sql/sqlstats/insights/ingester.go line 58 at r1 (raw file):

Previously, matthewtodd (Matthew Todd) wrote…

Good q. I'm wondering what that test would look like. I think we could come up with something, but I also think it's unlikely we'll revert to passing by value, so 🤷 Perhaps the data race detector is our friend here. WDYT?

The previous behaviour was never part of a release, so this change won't affect any "old" behaviour

@xinhaoz
Copy link
Member

xinhaoz commented Aug 15, 2022

pkg/sql/sqlstats/insights/insights.go line 141 at r2 (raw file):

// New builds a new Registry.
func New(st *cluster.Settings, metrics Metrics) Registry {
	return newConcurrentBufferIngester(newRegistry(st, metrics))

Just wondering for learning's sake, are there cases when we want to return an interface in the constructor over the exported struct directly? In the past I recall this was discouraged.

Copy link
Contributor Author

@matthewtodd matthewtodd left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @j82w, @maryliag, and @xinhaoz)


pkg/sql/sqlstats/insights/insights.go line 141 at r2 (raw file):

Previously, xinhaoz (Xin Hao Zhang) wrote…

Just wondering for learning's sake, are there cases when we want to return an interface in the constructor over the exported struct directly? In the past I recall this was discouraged.

Oh, that's interesting! Now that you mention it, I kind of recall those conversations, but I don't really remember the reasons. I'll ask in #go-learners.

Copy link
Contributor Author

@matthewtodd matthewtodd left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @j82w, @maryliag, and @xinhaoz)


pkg/sql/sqlstats/insights/insights.go line 141 at r2 (raw file):

Previously, matthewtodd (Matthew Todd) wrote…

Oh, that's interesting! Now that you mention it, I kind of recall those conversations, but I don't really remember the reasons. I'll ask in #go-learners.

Googling first, this result doesn't seem particularly Go-specific; you could just as easily balance the same forces the other way, as is done in, say, Java, where returning interfaces is the norm. I wonder if the difference is purely cultural, or if there's some technical benefit?

Copy link
Contributor Author

@matthewtodd matthewtodd left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @j82w, @maryliag, and @xinhaoz)


pkg/sql/sqlstats/insights/insights.go line 141 at r2 (raw file):

Previously, matthewtodd (Matthew Todd) wrote…

Googling first, this result doesn't seem particularly Go-specific; you could just as easily balance the same forces the other way, as is done in, say, Java, where returning interfaces is the norm. I wonder if the difference is purely cultural, or if there's some technical benefit?

From what I've digested, I think it probably does make sense to return a struct here. (Though the interface was maybe slightly helpful tactically in the move to wrap the ingester around the registry.)

In general, I've been feeling like the Registry interface is too big: the needs of the ingester (Start) are completely separate from the needs of the registry (enabled), and there's a reader/writer distinction just waiting to be made. I'll take a little time to play with it on Friday to see if something simpler shakes out.

Fixes the data race observed in #85988. Passing this data structure by
value meant that the `fullHandler` was working with a different object
than the external callers.

Release justification: Category 3: Fixes for high-priority or
high-severity bugs in existing functionality

Release note: None
@matthewtodd
Copy link
Contributor Author

bors r+

@ajwerner
Copy link
Contributor

A great rule of thumb for Go is accept interfaces, return structs.

https://medium.com/@cep21/preemptive-interface-anti-pattern-in-go-54c18ac0668a

@craig
Copy link
Contributor

craig bot commented Aug 16, 2022

Build succeeded:

@craig craig bot merged commit f4042d4 into cockroachdb:master Aug 16, 2022
@matthewtodd matthewtodd deleted the insights-ingester-by-reference branch August 16, 2022 10:08
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.

6 participants