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

balancer/pickfirst: Add pick first metrics #7839

Merged
merged 10 commits into from
Nov 26, 2024
Merged

Conversation

zasweq
Copy link
Contributor

@zasweq zasweq commented Nov 13, 2024

This PR adds pick first metrics according to A78, and tests as well.

RELEASE NOTES:

  • balancer/pickfirst: Emit Metrics from pick_first load balancing policy

@zasweq zasweq requested a review from dfawley November 13, 2024 22:45
@zasweq zasweq added this to the 1.69 Release milestone Nov 13, 2024
@zasweq zasweq added the Type: Feature New features or improvements in behavior label Nov 13, 2024
@zasweq zasweq force-pushed the pf-metrics branch 2 times, most recently from 1cca48a to e214b5f Compare November 14, 2024 02:51
Copy link

codecov bot commented Nov 14, 2024

Codecov Report

Attention: Patch coverage is 70.00000% with 6 lines in your changes missing coverage. Please review.

Project coverage is 81.93%. Comparing base (7d53957) to head (f2d97e7).
Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
balancer/pickfirst/pickfirstleaf/pickfirstleaf.go 66.66% 5 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master    #7839   +/-   ##
=======================================
  Coverage   81.92%   81.93%           
=======================================
  Files         375      375           
  Lines       37979    38007   +28     
=======================================
+ Hits        31114    31140   +26     
- Misses       5572     5575    +3     
+ Partials     1293     1292    -1     
Files with missing lines Coverage Δ
internal/testutils/stats/test_metrics_recorder.go 76.42% <100.00%> (+1.06%) ⬆️
balancer/pickfirst/pickfirstleaf/pickfirstleaf.go 87.88% <66.66%> (-0.96%) ⬇️

... and 22 files with indirect coverage changes

---- 🚨 Try these New Features:

@dfawley
Copy link
Member

dfawley commented Nov 19, 2024

Would you mind reviewing this @arjan-bal?

balancer/pickfirst/pickfirstleaf/pickfirstleaf.go Outdated Show resolved Hide resolved
balancer/pickfirst/pickfirstleaf/pickfirstleaf.go Outdated Show resolved Hide resolved
balancer/pickfirst/pickfirstleaf/pickfirstleaf.go Outdated Show resolved Hide resolved
balancer/pickfirst/pickfirstleaf/pickfirstleaf.go Outdated Show resolved Hide resolved
balancer/pickfirst/pickfirstleaf/pickfirstleaf.go Outdated Show resolved Hide resolved
@zasweq
Copy link
Contributor Author

zasweq commented Nov 20, 2024

Thanks for the pass Easwar, I did this work with Doug who knows a lot more about the state transitions so I want to hear your input on Easwar's concerns @dfawley.

@zasweq zasweq assigned dfawley and unassigned zasweq Nov 20, 2024
@dfawley
Copy link
Member

dfawley commented Nov 20, 2024

I think it would be better for you to reply to them first @zasweq.

balancer/pickfirst/pickfirstleaf/metrics_test.go Outdated Show resolved Hide resolved
balancer/pickfirst/pickfirstleaf/metrics_test.go Outdated Show resolved Hide resolved
balancer/pickfirst/pickfirstleaf/metrics_test.go Outdated Show resolved Hide resolved
balancer/pickfirst/pickfirstleaf/metrics_test.go Outdated Show resolved Hide resolved
balancer/pickfirst/pickfirstleaf/metrics_test.go Outdated Show resolved Hide resolved
@arjan-bal arjan-bal assigned zasweq and unassigned arjan-bal Nov 20, 2024
balancer/pickfirst/pickfirstleaf/metrics_test.go Outdated Show resolved Hide resolved
}

ss.Stop()
if err = pollForDisconnectedMetrics(ctx, tmr); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

There is a single caller for this. Prefer getting rid of the function and inlining it 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.

The reason I had this in a separate function is I would have to do a bool local.

Switched to that. Let me know what you think.

Copy link
Contributor

Choose a reason for hiding this comment

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

You don't need a boolean. If you change the body of the for to return when you see grpc.lb.pick_first.disconnections (instead of breaking), then the only case where you will execute code below the for would be when the context expires. And therefore you can check for ctx.Err() != nil instead of checking for the boolean.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I return early I don't even need the ctx.Err check right? The only reason it would hit that would be for the ctx to expire without having found disconnection/returned right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So my failure mode is an uncondtional t.Fatalf("timeout waiting for grpc.lb.pick_first.disconnections metric").

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, with Arjan's point about await state I can use that to poll/sync and then the metrics test can be deterministic one time check. Thanks Arjan for suggestion.

balancer/pickfirst/pickfirstleaf/metrics_test.go Outdated Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should have tests for happy eyeballs cases. Did you consider enhancing existing tests to check for metric values at the end of the test? Going down that path would cover a lot of scenarios instead of very simple ones being tested 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.

I never considered that. Would you rather me write my own happy eyeballs or scale up the existing? I know there was lots of previous contention about adding too many assertions to tests.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's a good idea. I would suggest adding assertions to the same test because asserting both behaviours in the same test would give a better indication that the features work together. If this severely hampers readability, we can have separate tests.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree with @arjan-bal.

I'm OK with it being a separate PR 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.

Scaled up two basic happy eyeballs tests (in a separate commit).

Note that there is a limitation in our test metrics recorder that it only persist the most recently emitted metric value for a given metric name. The reason for this is vast non determinism in other unit tests that use this component, where the most recent was deterministic but not the total metric. It's too far down the rabbit hole now to change I think, but let me know if y'all feel strongly about trying to add a summation assertion/if you want me to add metrics assertions to more happy eyeballs tests/what you think about the additional assertions in general.

I'll be happy to add more, remove the commit, leave as is, etc.

Copy link
Contributor Author

@zasweq zasweq Nov 22, 2024

Choose a reason for hiding this comment

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

I added a test to the other test that test with a ClientConn, so I think that should be good enough outside the summation thing, which I don't think should be addressed in this PR if at all unless found assertions inadequate.

I saw other tests test interleaving addresses by sending TF, but figured this coverage should be good enough. Thanks for suggestion.

balancer/pickfirst/pickfirstleaf/metrics_test.go Outdated Show resolved Hide resolved
balancer/pickfirst/pickfirstleaf/pickfirstleaf.go Outdated Show resolved Hide resolved
@zasweq zasweq removed their assignment Nov 20, 2024
@zasweq
Copy link
Contributor Author

zasweq commented Nov 20, 2024

Thanks for the comments y'all. Got to all comments (split out implementation and testing changes in separate commits).

@@ -57,7 +58,28 @@ var (
// Name is the name of the pick_first_leaf balancer.
// It is changed to "pick_first" in init() if this balancer is to be
// registered as the default pickfirst.
Name = "pick_first_leaf"
Name = "pick_first_leaf"
disconnectionsMetric = estats.RegisterInt64Count(estats.MetricDescriptor{
Copy link
Member

Choose a reason for hiding this comment

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

This one is suffixed with Metric and the others are not. Let's be consistent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Done. The RLS/WRR metrics are also suffixed with Metric, just checked.

cc: cc,
cc: cc,
target: bo.Target.String(),
metricsRecorder: bo.MetricsRecorder, // ClientConn will always create a Metrics Recorder so guaranteed to be non nil.
Copy link
Member

Choose a reason for hiding this comment

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

This comment seems like it's unnecessary or in the wrong place? It's not being dereferenced 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.

This was Easwar's suggestion after our back and forth: #7839 (comment). What do you think about this. I'll drop "so guaranteed to be non nil" from this one because it's documented on the field.

@@ -575,6 +607,12 @@ func (b *pickfirstBalancer) updateSubConnState(sd *scData, newState balancer.Sub
// the first address when the picker is used.
b.shutdownRemainingLocked(sd)
b.state = connectivity.Idle
// READY SubConn interspliced in between CONNECTING and IDLE, need to
// account for that.
if oldState == connectivity.Connecting && newState.ConnectivityState == connectivity.Idle {
Copy link
Member

Choose a reason for hiding this comment

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

+1 to something like:

if oldState == Connecting {
  // A known issue causes a race that prevents the READY state change notification.  This works around it.

Also it would be great if we could create an issue for that problem and link it here so we know to come back and delete this one it's resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed second conditional. Created a Github issue and linked to it.

balancer/pickfirst/pickfirstleaf/metrics_test.go Outdated Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

That's a good idea. I would suggest adding assertions to the same test because asserting both behaviours in the same test would give a better indication that the features work together. If this severely hampers readability, we can have separate tests.

@arjan-bal arjan-bal assigned zasweq and unassigned arjan-bal Nov 21, 2024
@easwars easwars removed their assignment Nov 21, 2024
@zasweq zasweq assigned easwars and arjan-bal and unassigned zasweq Nov 22, 2024
@zasweq
Copy link
Contributor Author

zasweq commented Nov 22, 2024

Thanks for the passes. Got to all comments and scaled up two very basic happy eyeballs tests. Let me know what you think.

@arjan-bal arjan-bal assigned zasweq and unassigned arjan-bal Nov 25, 2024
@zasweq
Copy link
Contributor Author

zasweq commented Nov 25, 2024

Thanks for the comments. Great suggestions.

@zasweq zasweq assigned arjan-bal and unassigned zasweq Nov 25, 2024
Copy link
Contributor

@arjan-bal arjan-bal left a comment

Choose a reason for hiding this comment

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

LGTM

@arjan-bal arjan-bal removed their assignment Nov 26, 2024
@zasweq zasweq merged commit 967ba46 into grpc:master Nov 26, 2024
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature New features or improvements in behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants