Skip to content

Revert "exponential backoff for CAS operations on floats" and cut 1.21.1 #1757

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

Merged
merged 2 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Unreleased

## 1.21.1 / 2025-03-04

* [BUGFIX] prometheus: Revert of `Inc`, `Add` and `Observe` cumulative metric CAS optimizations (#1661), causing regressions on low contention cases.

## 1.21.0 / 2025-02-17

:warning: This release contains potential breaking change if you upgrade `github.com/prometheus/common` to 0.62+ together with client_golang. :warning:
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.21.0
1.21.1
50 changes: 0 additions & 50 deletions prometheus/atomic_update.go

This file was deleted.

167 changes: 0 additions & 167 deletions prometheus/atomic_update_test.go

This file was deleted.

10 changes: 7 additions & 3 deletions prometheus/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,13 @@ func (c *counter) Add(v float64) {
return
}

atomicUpdateFloat(&c.valBits, func(oldVal float64) float64 {
return oldVal + v
})
for {
oldBits := atomic.LoadUint64(&c.valBits)
newBits := math.Float64bits(math.Float64frombits(oldBits) + v)
if atomic.CompareAndSwapUint64(&c.valBits, oldBits, newBits) {
return
}
}
}

func (c *counter) AddWithExemplar(v float64, e Labels) {
Expand Down
10 changes: 7 additions & 3 deletions prometheus/gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,13 @@ func (g *gauge) Dec() {
}

func (g *gauge) Add(val float64) {
atomicUpdateFloat(&g.valBits, func(oldVal float64) float64 {
return oldVal + val
})
for {
oldBits := atomic.LoadUint64(&g.valBits)
newBits := math.Float64bits(math.Float64frombits(oldBits) + val)
if atomic.CompareAndSwapUint64(&g.valBits, oldBits, newBits) {
return
}
}
}

func (g *gauge) Sub(val float64) {
Expand Down
10 changes: 7 additions & 3 deletions prometheus/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -1647,9 +1647,13 @@ func waitForCooldown(count uint64, counts *histogramCounts) {
// atomicAddFloat adds the provided float atomically to another float
// represented by the bit pattern the bits pointer is pointing to.
func atomicAddFloat(bits *uint64, v float64) {
atomicUpdateFloat(bits, func(oldVal float64) float64 {
return oldVal + v
})
for {
loadedBits := atomic.LoadUint64(bits)
newBits := math.Float64bits(math.Float64frombits(loadedBits) + v)
if atomic.CompareAndSwapUint64(bits, loadedBits, newBits) {
break
}
}
}

// atomicDecUint32 atomically decrements the uint32 p points to. See
Expand Down
25 changes: 15 additions & 10 deletions prometheus/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,13 @@ func (s *noObjectivesSummary) Observe(v float64) {
n := atomic.AddUint64(&s.countAndHotIdx, 1)
hotCounts := s.counts[n>>63]

atomicUpdateFloat(&hotCounts.sumBits, func(oldVal float64) float64 {
return oldVal + v
})
for {
oldBits := atomic.LoadUint64(&hotCounts.sumBits)
newBits := math.Float64bits(math.Float64frombits(oldBits) + v)
if atomic.CompareAndSwapUint64(&hotCounts.sumBits, oldBits, newBits) {
break
}
}
// Increment count last as we take it as a signal that the observation
// is complete.
atomic.AddUint64(&hotCounts.count, 1)
Expand Down Expand Up @@ -515,13 +519,14 @@ func (s *noObjectivesSummary) Write(out *dto.Metric) error {
// Finally add all the cold counts to the new hot counts and reset the cold counts.
atomic.AddUint64(&hotCounts.count, count)
atomic.StoreUint64(&coldCounts.count, 0)

// Use atomicUpdateFloat to update hotCounts.sumBits atomically.
atomicUpdateFloat(&hotCounts.sumBits, func(oldVal float64) float64 {
return oldVal + sum.GetSampleSum()
})
atomic.StoreUint64(&coldCounts.sumBits, 0)

for {
oldBits := atomic.LoadUint64(&hotCounts.sumBits)
newBits := math.Float64bits(math.Float64frombits(oldBits) + sum.GetSampleSum())
if atomic.CompareAndSwapUint64(&hotCounts.sumBits, oldBits, newBits) {
atomic.StoreUint64(&coldCounts.sumBits, 0)
break
}
}
return nil
}

Expand Down