Skip to content

Commit 522e75c

Browse files
author
Anthony Romano
committed
mvcc: use GaugeFunc metric to load db size when requested
Relying on mvcc to set the db size metric can cause it to miss size changes when a txn commits after the last write completes before a quiescent period. Instead, load the db size on demand. Fixes #8146
1 parent 4e6c771 commit 522e75c

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

mvcc/kvstore.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,11 @@ func (s *store) Restore(b backend.Backend) error {
251251
}
252252

253253
func (s *store) restore() error {
254+
reportDbTotalSizeInBytesMu.Lock()
255+
b := s.b
256+
reportDbTotalSizeInBytes = func() float64 { return float64(b.Size()) }
257+
reportDbTotalSizeInBytesMu.Unlock()
258+
254259
min, max := newRevBytes(), newRevBytes()
255260
revToBytes(revision{main: 1}, min)
256261
revToBytes(revision{main: math.MaxInt64, sub: math.MaxInt64}, max)
@@ -261,8 +266,6 @@ func (s *store) restore() error {
261266
tx := s.b.BatchTx()
262267
tx.Lock()
263268

264-
dbTotalSize.Set(float64(s.b.Size()))
265-
266269
_, finishedCompactBytes := tx.UnsafeRange(metaBucketName, finishedCompactKeyName, nil, 0)
267270
if len(finishedCompactBytes) != 0 {
268271
s.compactMainRev = bytesToRev(finishedCompactBytes[0]).main

mvcc/kvstore_txn.go

-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ func (tw *storeTxnWrite) End() {
105105
if len(tw.changes) != 0 {
106106
tw.s.revMu.Unlock()
107107
}
108-
dbTotalSize.Set(float64(tw.s.b.Size()))
109108
tw.s.mu.RUnlock()
110109
}
111110

mvcc/metrics.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
package mvcc
1616

1717
import (
18+
"sync"
19+
1820
"github.com/prometheus/client_golang/prometheus"
1921
)
2022

@@ -129,12 +131,21 @@ var (
129131
Buckets: prometheus.ExponentialBuckets(100, 2, 14),
130132
})
131133

132-
dbTotalSize = prometheus.NewGauge(prometheus.GaugeOpts{
134+
dbTotalSize = prometheus.NewGaugeFunc(prometheus.GaugeOpts{
133135
Namespace: "etcd_debugging",
134136
Subsystem: "mvcc",
135137
Name: "db_total_size_in_bytes",
136138
Help: "Total size of the underlying database in bytes.",
137-
})
139+
},
140+
func() float64 {
141+
reportDbTotalSizeInBytesMu.RLock()
142+
defer reportDbTotalSizeInBytesMu.RUnlock()
143+
return reportDbTotalSizeInBytes()
144+
},
145+
)
146+
// overridden by mvcc initialization
147+
reportDbTotalSizeInBytesMu sync.RWMutex
148+
reportDbTotalSizeInBytes func() float64 = func() float64 { return 0 }
138149
)
139150

140151
func init() {

0 commit comments

Comments
 (0)