Skip to content

Commit

Permalink
cache: add a metric for the region cache size (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
dethi authored Nov 17, 2023
1 parent e40c231 commit 642db04
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
15 changes: 12 additions & 3 deletions caches.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,15 @@ func (krc *keyRegionCache) getOverlaps(reg hrpc.RegionInfo) []hrpc.RegionInfo {
// passed region was put in the cache.
func (krc *keyRegionCache) put(reg hrpc.RegionInfo) (overlaps []hrpc.RegionInfo, replaced bool) {
krc.m.Lock()
defer krc.m.Unlock()

// Update region cache metric
beforeLen := krc.regions.Len()
defer func() {
afterLen := krc.regions.Len()
cachedRegionTotal.Add(float64(afterLen - beforeLen))
}()

krc.regions.Put(reg.Name(), func(v hrpc.RegionInfo, exists bool) (hrpc.RegionInfo, bool) {
if exists {
// region is already in cache,
Expand All @@ -285,8 +294,6 @@ func (krc *keyRegionCache) put(reg hrpc.RegionInfo) (overlaps []hrpc.RegionInfo,
return reg, true
})
if !replaced {
krc.m.Unlock()

log.WithFields(log.Fields{
"region": reg,
"overlaps": overlaps,
Expand All @@ -302,7 +309,6 @@ func (krc *keyRegionCache) put(reg hrpc.RegionInfo) (overlaps []hrpc.RegionInfo,
// let region establishers know that they can give up
o.MarkDead()
}
krc.m.Unlock()

log.WithFields(log.Fields{
"region": reg,
Expand All @@ -319,6 +325,9 @@ func (krc *keyRegionCache) del(reg hrpc.RegionInfo) bool {
// let region establishers know that they can give up
reg.MarkDead()

if success {
cachedRegionTotal.Dec()
}
log.WithFields(log.Fields{
"region": reg,
}).Debug("removed region")
Expand Down
7 changes: 7 additions & 0 deletions prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,11 @@ var (
Buckets: prometheus.ExponentialBuckets(1, 2, 10),
},
)

cachedRegionTotal = promauto.NewGauge(prometheus.GaugeOpts{
Namespace: "gohbase",
Subsystem: "cache",
Name: "regions_total",
Help: "Total number of regions in the cache",
})
)

0 comments on commit 642db04

Please sign in to comment.