Skip to content

Commit 78ad09a

Browse files
[release-20.0] fix: App and Dba Pool metrics (vitessio#18048) (vitessio#18083)
Signed-off-by: Harshit Gangal <[email protected]> Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com> Co-authored-by: Harshit Gangal <[email protected]>
1 parent 5b77924 commit 78ad09a

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

go/vt/dbconnpool/connection_pool.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,16 @@ import (
3636
// PooledDBConnection objects.
3737
type ConnectionPool struct {
3838
*smartconnpool.ConnPool[*DBConnection]
39+
40+
name string
3941
}
4042

43+
// usedNames is for preventing expvar from panicking. Tests
44+
// create pool objects multiple time. If a name was previously
45+
// used, expvar initialization is skipped.
46+
// through non-test code.
47+
var usedNames = make(map[string]bool)
48+
4149
// NewConnectionPool creates a new ConnectionPool. The name is used
4250
// to publish stats only.
4351
func NewConnectionPool(name string, stats *servenv.Exporter, capacity int, idleTimeout time.Duration, maxLifetime time.Duration, dnsResolutionFrequency time.Duration) *ConnectionPool {
@@ -47,7 +55,18 @@ func NewConnectionPool(name string, stats *servenv.Exporter, capacity int, idleT
4755
MaxLifetime: maxLifetime,
4856
RefreshInterval: dnsResolutionFrequency,
4957
}
50-
return &ConnectionPool{ConnPool: smartconnpool.NewPool(&config)}
58+
cp := &ConnectionPool{ConnPool: smartconnpool.NewPool(&config), name: name}
59+
if name == "" || usedNames[name] {
60+
return cp
61+
}
62+
usedNames[name] = true
63+
64+
if stats == nil {
65+
// This is unnamed exported so it will use the stats functions directly when adding to the expvar.
66+
stats = servenv.NewExporter("", "")
67+
}
68+
cp.ConnPool.RegisterStats(stats, name)
69+
return cp
5170
}
5271

5372
// Open must be called before starting to use the pool.

go/vt/vttablet/endtoend/config_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,29 @@ func TestQueryTimeout(t *testing.T) {
279279
compareIntDiff(t, vend, "Kills/Connections", vstart, 1)
280280
}
281281

282+
// TestHeartbeatMetric validates the heartbeat metrics exists from the connection pool.
283+
func TestHeartbeatMetric(t *testing.T) {
284+
tcases := []struct {
285+
metricName string
286+
exp any
287+
}{{
288+
metricName: "HeartbeatWriteAppPoolCapacity",
289+
exp: 2,
290+
}, {
291+
metricName: "HeartbeatWriteAllPrivsPoolCapacity",
292+
exp: 2,
293+
}}
294+
295+
metrics := framework.DebugVars()
296+
for _, tcase := range tcases {
297+
t.Run(tcase.metricName, func(t *testing.T) {
298+
mValue, exists := metrics[tcase.metricName]
299+
require.True(t, exists, "metric %s not found", tcase.metricName)
300+
require.EqualValues(t, tcase.exp, mValue, "metric %s value is %d, want %d", tcase.metricName, mValue, tcase.exp)
301+
})
302+
}
303+
}
304+
282305
func changeVar(t *testing.T, name, value string) (revert func()) {
283306
t.Helper()
284307

go/vt/vttablet/endtoend/framework/server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ func StartServer(ctx context.Context, connParams, connAppDebugParams mysql.ConnP
129129
config.EnableViews = true
130130
config.QueryCacheDoorkeeper = false
131131
config.SchemaReloadInterval = 5 * time.Second
132+
config.ReplicationTracker.Mode = tabletenv.Heartbeat
133+
config.ReplicationTracker.HeartbeatOnDemand = 1 * time.Second
134+
config.ReplicationTracker.HeartbeatInterval = 1 * time.Second
132135
gotBytes, _ := yaml2.Marshal(config)
133136
log.Infof("Config:\n%s", gotBytes)
134137
return StartCustomServer(ctx, connParams, connAppDebugParams, dbName, config)

0 commit comments

Comments
 (0)