From 642db041784ff431171ee980a0bdfe0ece97403c Mon Sep 17 00:00:00 2001 From: Thibault Deutsch Date: Fri, 17 Nov 2023 18:14:17 +0000 Subject: [PATCH] cache: add a metric for the region cache size (#242) --- caches.go | 15 ++++++++++++--- prometheus.go | 7 +++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/caches.go b/caches.go index 2d44042f..2b45adc0 100644 --- a/caches.go +++ b/caches.go @@ -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, @@ -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, @@ -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, @@ -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") diff --git a/prometheus.go b/prometheus.go index 4ef24bca..d7a1c271 100644 --- a/prometheus.go +++ b/prometheus.go @@ -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", + }) )