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: big memory overflow #2540

Merged
merged 7 commits into from
Nov 5, 2024

Conversation

leiwingqueen
Copy link
Contributor

@leiwingqueen leiwingqueen commented Nov 1, 2024

What this PR does / why we need it:

I have a machine with big memory (about 10000TB size) will get a negative number in metrics. I found that the node metrics value do the convert as float64(val.MilliValue()) / 1000 before return. It may overflow when the value is bigger than maxInt64/1000.

How does this change affect the cardinality of KSM: (increases, decreases or does not change cardinality)

does not change cardinality

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes # #2541

Copy link

linux-foundation-easycla bot commented Nov 1, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

@k8s-ci-robot k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Nov 1, 2024
@k8s-ci-robot
Copy link
Contributor

This issue is currently awaiting triage.

If kube-state-metrics contributors determine this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

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-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. label Nov 1, 2024
@k8s-ci-robot
Copy link
Contributor

Welcome @leiwingqueen!

It looks like this is your first PR to kubernetes/kube-state-metrics 🎉. 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/kube-state-metrics 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 size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Nov 1, 2024
@leiwingqueen leiwingqueen changed the title fix big memory overflow fix: big memory overflow Nov 1, 2024
internal/store/node.go Outdated Show resolved Hide resolved
internal/store/node.go Outdated Show resolved Hide resolved
@mrueg
Copy link
Member

mrueg commented Nov 3, 2024

Checking the code for MilliValue, it seems like we're using it in other occasions as well. Do you mind moving the convertValueToFloat64 func into internal/store/utils.go and apply them there as well?

./internal/store/horizontalpodautoscaler.go:                                    metricMap[value] = float64(metricTarget.Value.MilliValue()) / 1000
./internal/store/horizontalpodautoscaler.go:                                    metricMap[average] = float64(metricTarget.AverageValue.MilliValue()) / 1000
./internal/store/horizontalpodautoscaler.go:                                    metricMap[value] = float64(currentMetric.Value.MilliValue()) / 1000
./internal/store/horizontalpodautoscaler.go:                                    metricMap[average] = float64(currentMetric.AverageValue.MilliValue()) / 1000
./internal/store/node.go:                                               Value: float64(val.MilliValue()) / 1000,
./internal/store/node.go:                                               Value: float64(val.MilliValue()) / 1000,
./internal/store/node.go:                                               Value: float64(val.MilliValue()) / 1000,
./internal/store/node.go:                                                       Value: float64(val.MilliValue()) / 1000,
./internal/store/node.go:                                                       Value: float64(val.MilliValue()) / 1000,
./internal/store/node.go:                                                       Value: float64(val.MilliValue()) / 1000,
./internal/store/node.go:                                               Value: float64(val.MilliValue()) / 1000,
./internal/store/node.go:                                               Value: float64(val.MilliValue()) / 1000,
./internal/store/node.go:                                               Value: float64(val.MilliValue()) / 1000,
./internal/store/node.go:                                                       Value: float64(val.MilliValue()) / 1000,
./internal/store/node.go:                                                       Value: float64(val.MilliValue()) / 1000,
./internal/store/node.go:                                                       Value: float64(val.MilliValue()) / 1000,
./internal/store/pod.go:                                                        Value:       float64(val.MilliValue()) / 1000,
./internal/store/pod.go:                                                        Value:       float64(val.MilliValue()) / 1000,
./internal/store/pod.go:                                                        Value:       float64(val.MilliValue()) / 1000,
./internal/store/pod.go:                                                        Value:       float64(val.MilliValue()) / 1000,
./internal/store/pod.go:                                                        Value: float64(val.MilliValue()) / 1000,
./internal/store/limitrange.go:                                                 Value:       float64(min.MilliValue()) / 1000,
./internal/store/limitrange.go:                                                 Value:       float64(max.MilliValue()) / 1000,
./internal/store/limitrange.go:                                                 Value:       float64(df.MilliValue()) / 1000,
./internal/store/limitrange.go:                                                 Value:       float64(dfR.MilliValue()) / 1000,
./internal/store/limitrange.go:                                                 Value:       float64(mLR.MilliValue()) / 1000,
./internal/store/resourcequota.go:                                              Value:       float64(qty.MilliValue()) / 1000,
./internal/store/resourcequota.go:                                              Value:       float64(qty.MilliValue()) / 1000,

@mrueg mrueg closed this Nov 3, 2024
@mrueg mrueg reopened this Nov 3, 2024
@mrueg
Copy link
Member

mrueg commented Nov 3, 2024

accidentally closed this, apologies.

@leiwingqueen
Copy link
Contributor Author

Checking the code for MilliValue, it seems like we're using it in other occasions as well. Do you mind moving the convertValueToFloat64 func into internal/store/utils.go and apply them there as well?

./internal/store/horizontalpodautoscaler.go:                                    metricMap[value] = float64(metricTarget.Value.MilliValue()) / 1000
./internal/store/horizontalpodautoscaler.go:                                    metricMap[average] = float64(metricTarget.AverageValue.MilliValue()) / 1000
./internal/store/horizontalpodautoscaler.go:                                    metricMap[value] = float64(currentMetric.Value.MilliValue()) / 1000
./internal/store/horizontalpodautoscaler.go:                                    metricMap[average] = float64(currentMetric.AverageValue.MilliValue()) / 1000
./internal/store/node.go:                                               Value: float64(val.MilliValue()) / 1000,
./internal/store/node.go:                                               Value: float64(val.MilliValue()) / 1000,
./internal/store/node.go:                                               Value: float64(val.MilliValue()) / 1000,
./internal/store/node.go:                                                       Value: float64(val.MilliValue()) / 1000,
./internal/store/node.go:                                                       Value: float64(val.MilliValue()) / 1000,
./internal/store/node.go:                                                       Value: float64(val.MilliValue()) / 1000,
./internal/store/node.go:                                               Value: float64(val.MilliValue()) / 1000,
./internal/store/node.go:                                               Value: float64(val.MilliValue()) / 1000,
./internal/store/node.go:                                               Value: float64(val.MilliValue()) / 1000,
./internal/store/node.go:                                                       Value: float64(val.MilliValue()) / 1000,
./internal/store/node.go:                                                       Value: float64(val.MilliValue()) / 1000,
./internal/store/node.go:                                                       Value: float64(val.MilliValue()) / 1000,
./internal/store/pod.go:                                                        Value:       float64(val.MilliValue()) / 1000,
./internal/store/pod.go:                                                        Value:       float64(val.MilliValue()) / 1000,
./internal/store/pod.go:                                                        Value:       float64(val.MilliValue()) / 1000,
./internal/store/pod.go:                                                        Value:       float64(val.MilliValue()) / 1000,
./internal/store/pod.go:                                                        Value: float64(val.MilliValue()) / 1000,
./internal/store/limitrange.go:                                                 Value:       float64(min.MilliValue()) / 1000,
./internal/store/limitrange.go:                                                 Value:       float64(max.MilliValue()) / 1000,
./internal/store/limitrange.go:                                                 Value:       float64(df.MilliValue()) / 1000,
./internal/store/limitrange.go:                                                 Value:       float64(dfR.MilliValue()) / 1000,
./internal/store/limitrange.go:                                                 Value:       float64(mLR.MilliValue()) / 1000,
./internal/store/resourcequota.go:                                              Value:       float64(qty.MilliValue()) / 1000,
./internal/store/resourcequota.go:                                              Value:       float64(qty.MilliValue()) / 1000,

copy that


// convertValueToFloat64 converts a resource.Quantity to a float64.
func convertValueToFloat64(q *resource.Quantity) float64 {
if q.Value() > resource.MaxMilliValue {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we remove if-else and use float64(q.MilliValue() / 1000)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

may be not. q.MilliValue() will cause overflow when q.Value() is larger than maxInt64/1000. for example, 1000TB will return a negative number.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ack.

@CatherineF-dev
Copy link
Contributor

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 4, 2024
@leiwingqueen
Copy link
Contributor Author

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: CatherineF-dev, leiwingqueen Once this PR has been reviewed and has the lgtm label, please assign rexagod for approval. For more information see the Kubernetes Code Review Process.

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

/assign @rexagod

@leiwingqueen
Copy link
Contributor Author

/assign @mrueg

internal/store/utils.go Outdated Show resolved Hide resolved
@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 5, 2024
@mrueg
Copy link
Member

mrueg commented Nov 5, 2024

I updated the comment in the convertValueToFloat64 func to make it clear that it's checking for an overflow.

/lgtm

Thanks for your contribution!

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Nov 5, 2024
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: CatherineF-dev, leiwingqueen, mrueg

The full list of commands accepted by this bot can be found here.

The pull request process is described 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 k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 5, 2024
@k8s-ci-robot k8s-ci-robot merged commit 61a1993 into kubernetes:main Nov 5, 2024
12 checks passed
@mrueg mrueg added this to the v2.14.0 milestone Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants