Skip to content

Commit 42ed2a6

Browse files
Replace the pool when reopening.
Signed-off-by: Arthur Schreiber <[email protected]>
1 parent 7dd3c6b commit 42ed2a6

File tree

3 files changed

+114
-10
lines changed

3 files changed

+114
-10
lines changed

go/pools/smartconnpool/pool.go

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -803,58 +803,141 @@ func (pool *ConnPool[C]) StatsJSON() map[string]any {
803803
}
804804
}
805805

806-
// RegisterStats registers this pool's metrics into a stats Exporter
807-
func (pool *ConnPool[C]) RegisterStats(stats *servenv.Exporter, name string) {
808-
if stats == nil || name == "" {
809-
return
810-
}
806+
type StatsExporter[C Connection] struct {
807+
// The Pool for which this exporter is exporting stats.
808+
// It is an atomic pointer so that it can be updated safely.
809+
// The pointer is nil if the pool has not been registered yet.
810+
pool atomic.Pointer[ConnPool[C]]
811+
}
811812

812-
pool.Name = name
813+
func NewStatsExporter[C Connection](stats *servenv.Exporter, name string) *StatsExporter[C] {
814+
se := &StatsExporter[C]{}
813815

814816
stats.NewGaugeFunc(name+"Capacity", "Tablet server conn pool capacity", func() int64 {
817+
pool := se.pool.Load()
818+
if pool == nil {
819+
return 0
820+
}
821+
815822
return pool.Capacity()
816823
})
817824
stats.NewGaugeFunc(name+"Available", "Tablet server conn pool available", func() int64 {
825+
pool := se.pool.Load()
826+
if pool == nil {
827+
return 0
828+
}
829+
818830
return pool.Available()
819831
})
820832
stats.NewGaugeFunc(name+"Active", "Tablet server conn pool active", func() int64 {
833+
pool := se.pool.Load()
834+
if pool == nil {
835+
return 0
836+
}
837+
821838
return pool.Active()
822839
})
823840
stats.NewGaugeFunc(name+"InUse", "Tablet server conn pool in use", func() int64 {
841+
pool := se.pool.Load()
842+
if pool == nil {
843+
return 0
844+
}
845+
824846
return pool.InUse()
825847
})
826848
stats.NewGaugeFunc(name+"MaxCap", "Tablet server conn pool max cap", func() int64 {
849+
pool := se.pool.Load()
850+
if pool == nil {
851+
return 0
852+
}
853+
827854
// the smartconnpool doesn't have a maximum capacity
828855
return pool.Capacity()
829856
})
830857
stats.NewGaugeFunc(name+"IdleAllowed", "Tablet server conn pool idle allowed limit", func() int64 {
858+
pool := se.pool.Load()
859+
if pool == nil {
860+
return 0
861+
}
862+
831863
return pool.IdleCount()
832864
})
833865
stats.NewCounterFunc(name+"WaitCount", "Tablet server conn pool wait count", func() int64 {
866+
pool := se.pool.Load()
867+
if pool == nil {
868+
return 0
869+
}
870+
834871
return pool.Metrics.WaitCount()
835872
})
836873
stats.NewCounterDurationFunc(name+"WaitTime", "Tablet server wait time", func() time.Duration {
874+
pool := se.pool.Load()
875+
if pool == nil {
876+
return 0
877+
}
878+
837879
return pool.Metrics.WaitTime()
838880
})
839881
stats.NewGaugeDurationFunc(name+"IdleTimeout", "Tablet server idle timeout", func() time.Duration {
882+
pool := se.pool.Load()
883+
if pool == nil {
884+
return 0
885+
}
886+
840887
return pool.IdleTimeout()
841888
})
842889
stats.NewCounterFunc(name+"IdleClosed", "Tablet server conn pool idle closed", func() int64 {
890+
pool := se.pool.Load()
891+
if pool == nil {
892+
return 0
893+
}
894+
843895
return pool.Metrics.IdleClosed()
844896
})
845897
stats.NewCounterFunc(name+"MaxLifetimeClosed", "Tablet server conn pool refresh closed", func() int64 {
898+
pool := se.pool.Load()
899+
if pool == nil {
900+
return 0
901+
}
902+
846903
return pool.Metrics.MaxLifetimeClosed()
847904
})
848905
stats.NewCounterFunc(name+"Get", "Tablet server conn pool get count", func() int64 {
906+
pool := se.pool.Load()
907+
if pool == nil {
908+
return 0
909+
}
910+
849911
return pool.Metrics.GetCount()
850912
})
851913
stats.NewCounterFunc(name+"GetSetting", "Tablet server conn pool get with setting count", func() int64 {
914+
pool := se.pool.Load()
915+
if pool == nil {
916+
return 0
917+
}
918+
852919
return pool.Metrics.GetSettingCount()
853920
})
854921
stats.NewCounterFunc(name+"DiffSetting", "Number of times pool applied different setting", func() int64 {
922+
pool := se.pool.Load()
923+
if pool == nil {
924+
return 0
925+
}
926+
855927
return pool.Metrics.DiffSettingCount()
856928
})
857929
stats.NewCounterFunc(name+"ResetSetting", "Number of times pool reset the setting", func() int64 {
930+
pool := se.pool.Load()
931+
if pool == nil {
932+
return 0
933+
}
934+
858935
return pool.Metrics.ResetSettingCount()
859936
})
937+
938+
return se
939+
}
940+
941+
func (se *StatsExporter[C]) SetPool(pool *ConnPool[C]) {
942+
se.pool.Store(pool)
860943
}

go/vt/dbconnpool/connection_pool.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ import (
3737
type ConnectionPool struct {
3838
*smartconnpool.ConnPool[*DBConnection]
3939

40-
name string
40+
config smartconnpool.Config[*DBConnection]
41+
statsExporter *smartconnpool.StatsExporter[*DBConnection]
42+
name string
4143
}
4244

4345
// usedNames is for preventing expvar from panicking. Tests
@@ -55,7 +57,7 @@ func NewConnectionPool(name string, stats *servenv.Exporter, capacity int, idleT
5557
MaxLifetime: maxLifetime,
5658
RefreshInterval: dnsResolutionFrequency,
5759
}
58-
cp := &ConnectionPool{ConnPool: smartconnpool.NewPool(&config), name: name}
60+
cp := &ConnectionPool{ConnPool: smartconnpool.NewPool(&config), config: config, name: name}
5961
if name == "" || usedNames[name] {
6062
return cp
6163
}
@@ -65,7 +67,10 @@ func NewConnectionPool(name string, stats *servenv.Exporter, capacity int, idleT
6567
// This is unnamed exported so it will use the stats functions directly when adding to the expvar.
6668
stats = servenv.NewExporter("", "")
6769
}
68-
cp.ConnPool.RegisterStats(stats, name)
70+
71+
cp.statsExporter = smartconnpool.NewStatsExporter[*DBConnection](stats, name)
72+
73+
// cp.ConnPool.RegisterStats(stats, name)
6974
return cp
7075
}
7176

@@ -88,6 +93,12 @@ func (cp *ConnectionPool) Open(info dbconfigs.Connector) {
8893
}
8994

9095
cp.ConnPool.Open(connect, refresh)
96+
cp.statsExporter.SetPool(cp.ConnPool)
97+
}
98+
99+
func (cp *ConnectionPool) Close() {
100+
cp.ConnPool.Close()
101+
cp.ConnPool = smartconnpool.NewPool(&cp.config)
91102
}
92103

93104
func (cp *ConnectionPool) Get(ctx context.Context) (*PooledDBConnection, error) {

go/vt/vttablet/tabletserver/connpool/pool.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ type PooledConn = smartconnpool.Pooled[*Conn]
4949
// pool of dba connections that are used to kill connections.
5050
type Pool struct {
5151
*smartconnpool.ConnPool[*Conn]
52+
53+
config smartconnpool.Config[*Conn]
54+
statsExporter *smartconnpool.StatsExporter[*Conn]
55+
5256
dbaPool *dbconnpool.ConnectionPool
5357

5458
timeout time.Duration
@@ -82,8 +86,10 @@ func NewPool(env tabletenv.Env, name string, cfg tabletenv.ConnPoolConfig) *Pool
8286
cp.getConnTime = env.Exporter().NewTimings(name+"GetConnTime", "Tracks the amount of time it takes to get a connection", "Settings")
8387
}
8488

89+
cp.config = config
90+
8591
cp.ConnPool = smartconnpool.NewPool(&config)
86-
cp.ConnPool.RegisterStats(env.Exporter(), name)
92+
cp.statsExporter = smartconnpool.NewStatsExporter[*Conn](env.Exporter(), name)
8793

8894
cp.dbaPool = dbconnpool.NewConnectionPool("", env.Exporter(), 1, config.IdleTimeout, config.MaxLifetime, 0)
8995

@@ -104,13 +110,17 @@ func (cp *Pool) Open(appParams, dbaParams, appDebugParams dbconfigs.Connector) {
104110
}
105111

106112
cp.ConnPool.Open(connect, refresh)
113+
cp.statsExporter.SetPool(cp.ConnPool)
114+
107115
cp.dbaPool.Open(dbaParams)
108116
}
109117

110118
// Close will close the pool and wait for connections to be returned before
111119
// exiting.
112120
func (cp *Pool) Close() {
113121
cp.ConnPool.Close()
122+
cp.ConnPool = smartconnpool.NewPool(&cp.config)
123+
114124
cp.dbaPool.Close()
115125
}
116126

0 commit comments

Comments
 (0)