Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion go/vt/dbconnpool/connection_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,16 @@ import (
// PooledDBConnection objects.
type ConnectionPool struct {
*smartconnpool.ConnPool[*DBConnection]

name string
}

// usedNames is for preventing expvar from panicking. Tests
// create pool objects multiple time. If a name was previously
// used, expvar initialization is skipped.
// through non-test code.
var usedNames = make(map[string]bool)

// NewConnectionPool creates a new ConnectionPool. The name is used
// to publish stats only.
func NewConnectionPool(name string, stats *servenv.Exporter, capacity int, idleTimeout time.Duration, maxLifetime time.Duration, dnsResolutionFrequency time.Duration) *ConnectionPool {
Expand All @@ -47,7 +55,18 @@ func NewConnectionPool(name string, stats *servenv.Exporter, capacity int, idleT
MaxLifetime: maxLifetime,
RefreshInterval: dnsResolutionFrequency,
}
return &ConnectionPool{ConnPool: smartconnpool.NewPool(&config)}
cp := &ConnectionPool{ConnPool: smartconnpool.NewPool(&config), name: name}
if name == "" || usedNames[name] {
return cp
}
usedNames[name] = true

if stats == nil {
// This is unnamed exported so it will use the stats functions directly when adding to the expvar.
stats = servenv.NewExporter("", "")
}
cp.ConnPool.RegisterStats(stats, name)
return cp
}

// Open must be called before starting to use the pool.
Expand Down
23 changes: 23 additions & 0 deletions go/vt/vttablet/endtoend/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,29 @@ func TestQueryTimeout(t *testing.T) {
compareIntDiff(t, vend, "Kills/Connections", vstart, 1)
}

// TestHeartbeatMetric validates the heartbeat metrics exists from the connection pool.
func TestHeartbeatMetric(t *testing.T) {
tcases := []struct {
metricName string
exp any
}{{
metricName: "HeartbeatWriteAppPoolCapacity",
exp: 2,
}, {
metricName: "HeartbeatWriteAllPrivsPoolCapacity",
exp: 2,
}}

metrics := framework.DebugVars()
for _, tcase := range tcases {
t.Run(tcase.metricName, func(t *testing.T) {
mValue, exists := metrics[tcase.metricName]
require.True(t, exists, "metric %s not found", tcase.metricName)
require.EqualValues(t, tcase.exp, mValue, "metric %s value is %d, want %d", tcase.metricName, mValue, tcase.exp)
})
}
}

func changeVar(t *testing.T, name, value string) (revert func()) {
t.Helper()

Expand Down
3 changes: 3 additions & 0 deletions go/vt/vttablet/endtoend/framework/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ func StartServer(ctx context.Context, connParams, connAppDebugParams mysql.ConnP
config.EnableViews = true
config.QueryCacheDoorkeeper = false
config.SchemaReloadInterval = 5 * time.Second
config.ReplicationTracker.Mode = tabletenv.Heartbeat
config.ReplicationTracker.HeartbeatOnDemand = 1 * time.Second
config.ReplicationTracker.HeartbeatInterval = 1 * time.Second
gotBytes, _ := yaml2.Marshal(config)
log.Infof("Config:\n%s", gotBytes)
return StartCustomServer(ctx, connParams, connAppDebugParams, dbName, config)
Expand Down
Loading