8000 using standard atomic instead of uber atomic by dvcanton · Pull Request #16394 · prometheus/prometheus · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

using standard atomic instead of uber atomic #16394

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

dvcanton
Copy link
@dvcanton dvcanton commented Apr 2, 2025

This solves issue #14866

Solved with @khanhtc1202 as part of the Contribfest Workshop at Kubecon Europe 2025.

Thanks to @ArthurSens @bwplotka @vesari @beorn7.

dvcanton added 2 commits April 2, 2025 13:01
Signed-off-by: Dave Canton <dvcanton7@gmail.com>
Signed-off-by: Dave Canton <dvcanton7@gmail.com>
@beorn7
Copy link
Member
beorn7 commented Apr 2, 2025

@bwplotka can you take this?

dvcanton added 2 commits April 3, 2025 10:20
Signed-off-by: Dave Canton <dvcanton7@gmail.com>
Signed-off-by: Dave <dvcanton7@gmail.com>
@dvcanton
Copy link
Author
dvcanton commented Apr 3, 2025

I have noticed some methods -- such as Inc, Dec, Sub -- are not available anymore in the standard atomic. I will fix this later (possibly after Kubecon).

dvcanton added 3 commits April 6, 2025 13:03
standard atomic types

Signed-off-by: Dave Canton <dvcanton7@gmail.com>
Signed-off-by: Dave Canton <dvcanton7@gmail.com>
Signed-off-by: Dave Canton <dvcanton7@gmail.com>
Copy link
Member
@bwplotka bwplotka left a comment

Choose a reason for hiding this comment

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

Epic work, thanks!

It looks solid, but we could make it more DRY and maintainable for everyone.

What about creating a new utils/atomicutil package that:

  • Contains generic atomic.Value[T] struct?
  • Have NewValue(obj T) *Value[T] that returns pointer.

This should make our life easier, what do you think?

For integer Inc, Dec, Sub I think it's ok to have a bit more complex use without wrapping.

Comment on lines -83 to -84
- pkg: "sync/atomic"
desc: "Use go.uber.org/atomic instead of sync/atomic"
Copy link
Member

Choose a reason for hiding this comment

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

Nice catch!

Can we actually NOT remove it but reverse the assertion?

Use sync/atomic instead of go.uber.org/atomic"

@@ -152,10 +152,24 @@ type AlertingRule struct {
func NewAlertingRule(
name string, vec parser.Expr, hold, keepFiringFor time.Duration,
labels, annotations, externalLabels labels.Labels, externalURL string,
restored bool, logger *slog.Logger,
_restored bool, logger *slog.Logger,
Copy link
Member

Choose a reason for hiding this comment

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

It's not idiomatic to start variable names with underscore only to avoid collisions. Let's use this opportunity to make it clear what all variable controls e.g. by renaming var restored atomic.Bool to restoredAtomic or this variable to nonAtomic...

Suggested change
_restored bool, logger *slog.Logger,
nonAtomicRestored bool, logger *slog.Logger,

8000
) *AlertingRule {
el := externalLabels.Map()

var restored atomic.Bool
Copy link
Member

Choose a reason for hiding this comment

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

This is great.

  1. I suggest we group all vars in multiline var ( ... ) statement, then restore things.
  2. If we see this pattern of taking pointers common, I think we could consider util/atomicutil package creation for our helpers?

Copy link
Member

Choose a reason for hiding this comment

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

Oh I definitely see this being pattern in other files 🙈

I would suggest doing (2).

@@ -302,7 +321,7 @@ func (r *AlertingRule) SetEvaluationTimestamp(ts time.Time) {

// GetEvaluationTimestamp returns the time the evaluation took place.
func (r *AlertingRule) GetEvaluationTimestamp() time.Time {
return r.evaluationTimestamp.Load()
return r.evaluationTimestamp.Load().(time.Time)
Copy link
Member

Choose a reason for hiding this comment

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

What about adding small helpers in util/atomicutil package for those type safe operations? Perhaps using generics?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0