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

Fix metrics AlreadyRegisteredError on TestSampler unit test #105886

Closed
wants to merge 11 commits into from
Closed

Fix metrics AlreadyRegisteredError on TestSampler unit test #105886

wants to merge 11 commits into from

Conversation

CatherineF-dev
Copy link
Contributor

@CatherineF-dev CatherineF-dev commented Oct 25, 2021

Fixes #104940

Test:

make test KUBE_RACE=-race KUBE_TIMEOUT=--timeout=600s GOFLAGS=-count=10 WHAT=./staging/src/k8s.io/apiserver/pkg/util/flowcontrol/metrics/

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/
```
@k8s-ci-robot
Copy link
Contributor

@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.

@k8s-ci-robot k8s-ci-robot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/needs-kind Indicates a PR lacks a `kind/foo` label and requires one. labels Oct 25, 2021
@k8s-ci-robot
Copy link
Contributor

Welcome @CatherineF-dev!

It looks like this is your first PR to kubernetes/kubernetes 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/kubernetes has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. labels Oct 25, 2021
@k8s-ci-robot
Copy link
Contributor

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 /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

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.

@k8s-ci-robot k8s-ci-robot added needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Oct 25, 2021
@k8s-ci-robot k8s-ci-robot added area/apiserver sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. and removed do-not-merge/needs-sig Indicates an issue or PR lacks a `sig/foo` label and requires one. labels Oct 25, 2021
@k8s-ci-robot k8s-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. sig/architecture Categorizes an issue or PR as relevant to SIG Architecture. sig/cluster-lifecycle Categorizes an issue or PR as relevant to SIG Cluster Lifecycle. sig/instrumentation Categorizes an issue or PR as relevant to SIG Instrumentation. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Oct 25, 2021
@CatherineF-dev
Copy link
Contributor Author

/assign @logicalhan

@MikeSpreitzer
Copy link
Member

@MikeSpreitzer

@@ -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() {
Copy link
Member

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 vars, and make the test func Reset the metrics and set the fake clock. The file-level vars could be initialized with init() (erm, maybe there's a reason not to use this for tests, but I forget) or test-suite initialization.

Copy link
Member

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
Copy link
Member

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.

@k8s-ci-robot k8s-ci-robot added size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Oct 29, 2021
@MikeSpreitzer
Copy link
Member

The latest revision retreats to just ignoring the errors rather than fixing the problem.

@MikeSpreitzer
Copy link
Member

The latest revision does not fix the problem. See the evidence in https://gist.github.com/MikeSpreitzer/0549a42e51c4bcd83587a7dcf4d50d5a

@CatherineF-dev
Copy link
Contributor Author

CatherineF-dev commented Oct 29, 2021

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 duplicate metrics collector registration attempted was solved.

Will understand test logics and solve it also.

@MikeSpreitzer
Copy link
Member

The problem is that the accumulators are not reset to zero between tests.

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Nov 1, 2021
@CatherineF-dev
Copy link
Contributor Author

/retest

@CatherineF-dev
Copy link
Contributor Author

CatherineF-dev commented Nov 1, 2021

The latest revision does not fix the problem. See the evidence in https://gist.github.com/MikeSpreitzer/0549a42e51c4bcd83587a7dcf4d50d5a

Hi Mike, could you help review again? Thanks!
@MikeSpreitzer

Copy link
Member

@MikeSpreitzer MikeSpreitzer left a 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.

@MikeSpreitzer
Copy link
Member

The revised staging/src/k8s.io/component-base/metrics/testutil/metrics_test.go also works with repeated invocations in the same process --- but not for good reasons. Note the call to legacyregistry.Register with the freshly constructed HistogramVec. In the first repetition, this succeeds. In later repetitions, I figure this returns an error. The calls to Observe update the current HistogramVec, but the call to GetHistogramVecFromGatherer always examines the HistogramVec from the first repetition.

Copy link
Member

@MikeSpreitzer MikeSpreitzer left a 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()
Copy link
Member

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.

@MikeSpreitzer
Copy link
Member

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.

Comment on lines +38 to +47
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
)
Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

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.

@CatherineF-dev CatherineF-dev changed the title Fix metrics AlreadyRegisteredErrors in tests Fix metrics AlreadyRegisteredError on TestSampler unit test Nov 10, 2021
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: CatherineF-dev
To complete the pull request process, please ask for approval from mikespreitzer after the PR has been reviewed.

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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot
Copy link
Contributor

@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.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 16, 2021
@CatherineF-dev
Copy link
Contributor Author

It's solved in #103526

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/apiserver cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. kind/flake Categorizes issue or PR as related to a flaky test. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. priority/important-longterm Important over the long term, but may not be staffed and/or may need multiple releases to complete. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/architecture Categorizes an issue or PR as relevant to SIG Architecture. sig/cluster-lifecycle Categorizes an issue or PR as relevant to SIG Cluster Lifecycle. sig/instrumentation Categorizes an issue or PR as relevant to SIG Instrumentation. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. triage/accepted Indicates an issue or PR is ready to be actively worked on.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UT failure: panic: duplicate metrics collector registration attempted
6 participants