Skip to content

Commit 40c62f7

Browse files
authored
Merge pull request #1757 from prometheus/revert-121cas
Revert "exponential backoff for CAS operations on floats" and cut 1.21.1
2 parents eaf03ef + 689f590 commit 40c62f7

8 files changed

+41
-237
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## Unreleased
22

3+
## 1.21.1 / 2025-03-04
4+
5+
* [BUGFIX] prometheus: Revert of `Inc`, `Add` and `Observe` cumulative metric CAS optimizations (#1661), causing regressions on low contention cases.
6+
37
## 1.21.0 / 2025-02-17
48

59
:warning: This release contains potential breaking change if you upgrade `github.com/prometheus/common` to 0.62+ together with client_golang. :warning:

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.21.0
1+
1.21.1

prometheus/atomic_update.go

-50
This file was deleted.

prometheus/atomic_update_test.go

-167
This file was deleted.

prometheus/counter.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,13 @@ func (c *counter) Add(v float64) {
134134
return
135135
}
136136

137-
atomicUpdateFloat(&c.valBits, func(oldVal float64) float64 {
138-
return oldVal + v
139-
})
137+
for {
138+
oldBits := atomic.LoadUint64(&c.valBits)
139+
newBits := math.Float64bits(math.Float64frombits(oldBits) + v)
140+
if atomic.CompareAndSwapUint64(&c.valBits, oldBits, newBits) {
141+
return
142+
}
143+
}
140144
}
141145

142146
func (c *counter) AddWithExemplar(v float64, e Labels) {

prometheus/gauge.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,13 @@ func (g *gauge) Dec() {
120120
}
121121

122122
func (g *gauge) Add(val float64) {
123-
atomicUpdateFloat(&g.valBits, func(oldVal float64) float64 {
124-
return oldVal + val
125-
})
123+
for {
124+
oldBits := atomic.LoadUint64(&g.valBits)
125+
newBits := math.Float64bits(math.Float64frombits(oldBits) + val)
126+
if atomic.CompareAndSwapUint64(&g.valBits, oldBits, newBits) {
127+
return
128+
}
129+
}
126130
}
127131

128132
func (g *gauge) Sub(val float64) {

prometheus/histogram.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -1647,9 +1647,13 @@ func waitForCooldown(count uint64, counts *histogramCounts) {
16471647
// atomicAddFloat adds the provided float atomically to another float
16481648
// represented by the bit pattern the bits pointer is pointing to.
16491649
func atomicAddFloat(bits *uint64, v float64) {
1650-
atomicUpdateFloat(bits, func(oldVal float64) float64 {
1651-
return oldVal + v
1652-
})
1650+
for {
1651+
loadedBits := atomic.LoadUint64(bits)
1652+
newBits := math.Float64bits(math.Float64frombits(loadedBits) + v)
1653+
if atomic.CompareAndSwapUint64(bits, loadedBits, newBits) {
1654+
break
1655+
}
1656+
}
16531657
}
16541658

16551659
// atomicDecUint32 atomically decrements the uint32 p points to. See

prometheus/summary.go

+15-10
Original file line numberDiff line numberDiff line change
@@ -471,9 +471,13 @@ func (s *noObjectivesSummary) Observe(v float64) {
471471
n := atomic.AddUint64(&s.countAndHotIdx, 1)
472472
hotCounts := s.counts[n>>63]
473473

474-
atomicUpdateFloat(&hotCounts.sumBits, func(oldVal float64) float64 {
475-
return oldVal + v
476-
})
474+
for {
475+
oldBits := atomic.LoadUint64(&hotCounts.sumBits)
476+
newBits := math.Float64bits(math.Float64frombits(oldBits) + v)
477+
if atomic.CompareAndSwapUint64(&hotCounts.sumBits, oldBits, newBits) {
478+
break
479+
}
480+
}
477481
// Increment count last as we take it as a signal that the observation
478482
// is complete.
479483
atomic.AddUint64(&hotCounts.count, 1)
@@ -515,13 +519,14 @@ func (s *noObjectivesSummary) Write(out *dto.Metric) error {
515519
// Finally add all the cold counts to the new hot counts and reset the cold counts.
516520
atomic.AddUint64(&hotCounts.count, count)
517521
atomic.StoreUint64(&coldCounts.count, 0)
518-
519-
// Use atomicUpdateFloat to update hotCounts.sumBits atomically.
520-
atomicUpdateFloat(&hotCounts.sumBits, func(oldVal float64) float64 {
521-
return oldVal + sum.GetSampleSum()
522-
})
523-
atomic.StoreUint64(&coldCounts.sumBits, 0)
524-
522+
for {
523+
oldBits := atomic.LoadUint64(&hotCounts.sumBits)
524+
newBits := math.Float64bits(math.Float64frombits(oldBits) + sum.GetSampleSum())
525+
if atomic.CompareAndSwapUint64(&hotCounts.sumBits, oldBits, newBits) {
526+
atomic.StoreUint64(&coldCounts.sumBits, 0)
527+
break
528+
}
529+
}
525530
return nil
526531
}
527532

0 commit comments

Comments
 (0)