-
Notifications
You must be signed in to change notification settings - Fork 38.7k
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
Fix metrics AlreadyRegisteredError on TestSampler unit test #105886
Fix metrics AlreadyRegisteredError on TestSampler unit test #105886
Conversation
Add to #105809 ```` make test KUBE_RACE=-race KUBE_TIMEOUT=--timeout=600s GOFLAGS=-count=1 WHAT=./staging/src/k8s.io/apiserver/pkg/util/flowcontrol/metrics/ ```
@CatherineF-dev: Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
Welcome @CatherineF-dev! |
Hi @CatherineF-dev. Thanks for your PR. I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
Change variable name once -> registerMetrics
/assign @logicalhan |
@@ -61,7 +64,9 @@ func TestSampler(t *testing.T) { | |||
saw := gen.Generate(0, 1, []string{}) | |||
regs := gen.metrics() | |||
for _, reg := range regs { | |||
legacyregistry.MustRegister(reg) | |||
registerMetrics.Do(func() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Surely the Do
should be outside the loop, rather than inside.
And don't forget the Reset. If I understand the problem, it is repeated runs of the same test.
I suggest putting the fake clock and SampleAndWatermarkObserverGenerator in file-level var
s, and make the test func Reset the metrics and set the fake clock. The file-level var
s could be initialized with init()
(erm, maybe there's a reason not to use this for tests, but I forget) or test-suite initialization.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand correctly, this PR is about making this code safe for the use case of running the same test func several times in series in the same process. That would be a surprise to the current version of the func, because it assumes that the values read from the metrics --- which are cumulative --- reflect only the activity of the current invocation of the func. So we need not only to register the metrics only once, we also need to reset their accumulators to zero at the start of the test func. If you look at the Prometheus client library, you will see that these metrics objects have a Reset()
method that does that.
@@ -30,6 +30,8 @@ import ( | |||
"k8s.io/utils/pointer" | |||
) | |||
|
|||
var registerMetrics sync.Once |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, you don't want the Do
inside the loop.
You want Reset
at the start of the test func.
The latest revision retreats to just ignoring the errors rather than fixing the problem. |
The latest revision does not fix the problem. See the evidence in https://gist.github.com/MikeSpreitzer/0549a42e51c4bcd83587a7dcf4d50d5a |
Yes. This testing has new errors after Will understand test logics and solve it also. |
The problem is that the accumulators are not reset to zero between tests. |
/retest |
Hi Mike, could you help review again? Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The change to sample_and_watermark_test.go
works when I test serial repetitions in the same process. The revised test func will not work if there are concurrent runs of the same test in the same process.
As noted in #105809 (comment), if this test func created and used its own private registry then concurrent invocations would also work.
The revised |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks correct. One small thing noted inline.
if diff := cmp.Diff(tt.wantVec, histogramVec); diff != "" { | ||
t.Errorf("Got unexpected HistogramVec (-want +got):\n%s", diff) | ||
} | ||
registry.Reset() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this Reset()
is not needed, because the registry involved is not used for anything after this statement.
I should also note that staging/src/k8s.io/apiserver/pkg/util/flowcontrol/metrics/sample_and_watermark_test.go could be changed to use a new private registry in each invocation of TestSampler, and that would make it work in the presence of concurrent invocations. |
var ( | ||
t0 = time.Now() | ||
clk = testclock.NewFakePassiveClock(t0) | ||
buckets = []float64{0, 1} | ||
gen = NewSampleAndWaterMarkHistogramsGenerator(clk, samplingPeriod, | ||
&compbasemetrics.HistogramOpts{Name: samplesHistName, Buckets: buckets}, | ||
&compbasemetrics.HistogramOpts{Name: "marks", Buckets: buckets}, | ||
[]string{}) | ||
dt time.Duration | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with a frame-local registry, these can remain frame-local too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, it would be much tidier to keep these local to the test func that uses them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remember I tried before. It didn't work.
https://gist.github.com/CatherineF-dev/ea06940aa9101f428685560f83c67cc2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That gist pointer only shows me some code, not testing results.
I tried testing a simple fix myself, and that revealed a bug in the test.
See #105886 for both a bug fix to the test and support for repeating the test.
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: CatherineF-dev The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
@CatherineF-dev: PR needs rebase. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
It's solved in #103526 |
Fixes #104940
Test: