-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
pool.go
419 lines (374 loc) · 11.4 KB
/
pool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
Copyright 2019 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package connpool
import (
"context"
"fmt"
"net"
"strings"
"sync"
"sync/atomic"
"time"
"vitess.io/vitess/go/netutil"
"vitess.io/vitess/go/pools"
"vitess.io/vitess/go/trace"
"vitess.io/vitess/go/vt/callerid"
"vitess.io/vitess/go/vt/dbconfigs"
"vitess.io/vitess/go/vt/dbconnpool"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/mysqlctl"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
)
// ErrConnPoolClosed is returned when the connection pool is closed.
var ErrConnPoolClosed = vterrors.New(vtrpcpb.Code_INTERNAL, "internal error: unexpected: conn pool is closed")
const (
getWithoutS = "GetWithoutSettings"
getWithS = "GetWithSettings"
)
// Pool implements a custom connection pool for tabletserver.
// It's similar to dbconnpool.ConnPool, but the connections it creates
// come with built-in ability to kill in-flight queries. These connections
// also trigger a CheckMySQL call if we fail to connect to MySQL.
// Other than the connection type, ConnPool maintains an additional
// pool of dba connections that are used to kill connections.
type Pool struct {
env tabletenv.Env
name string
mu sync.Mutex
connections pools.IResourcePool
capacity int
timeout time.Duration
idleTimeout time.Duration
maxLifetime time.Duration
waiterCap int64
waiterCount atomic.Int64
waiterQueueFull atomic.Int64
dbaPool *dbconnpool.ConnectionPool
appDebugParams dbconfigs.Connector
getConnTime *servenv.TimingsWrapper
}
// NewPool creates a new Pool. The name is used
// to publish stats only.
func NewPool(env tabletenv.Env, name string, cfg tabletenv.ConnPoolConfig) *Pool {
idleTimeout := cfg.IdleTimeoutSeconds.Get()
maxLifetime := cfg.MaxLifetimeSeconds.Get()
cp := &Pool{
env: env,
name: name,
capacity: cfg.Size,
timeout: cfg.TimeoutSeconds.Get(),
idleTimeout: idleTimeout,
maxLifetime: maxLifetime,
waiterCap: int64(cfg.MaxWaiters),
dbaPool: dbconnpool.NewConnectionPool("", 1, idleTimeout, maxLifetime, 0),
}
if name == "" {
return cp
}
env.Exporter().NewGaugeFunc(name+"Capacity", "Tablet server conn pool capacity", cp.Capacity)
env.Exporter().NewGaugeFunc(name+"Available", "Tablet server conn pool available", cp.Available)
env.Exporter().NewGaugeFunc(name+"Active", "Tablet server conn pool active", cp.Active)
env.Exporter().NewGaugeFunc(name+"InUse", "Tablet server conn pool in use", cp.InUse)
env.Exporter().NewGaugeFunc(name+"MaxCap", "Tablet server conn pool max cap", cp.MaxCap)
env.Exporter().NewCounterFunc(name+"WaitCount", "Tablet server conn pool wait count", cp.WaitCount)
env.Exporter().NewCounterDurationFunc(name+"WaitTime", "Tablet server wait time", cp.WaitTime)
env.Exporter().NewGaugeDurationFunc(name+"IdleTimeout", "Tablet server idle timeout", cp.IdleTimeout)
env.Exporter().NewCounterFunc(name+"IdleClosed", "Tablet server conn pool idle closed", cp.IdleClosed)
env.Exporter().NewCounterFunc(name+"MaxLifetimeClosed", "Tablet server conn pool refresh closed", cp.MaxLifetimeClosed)
env.Exporter().NewCounterFunc(name+"Exhausted", "Number of times pool had zero available slots", cp.Exhausted)
env.Exporter().NewCounterFunc(name+"WaiterQueueFull", "Number of times the waiter queue was full", cp.waiterQueueFull.Load)
env.Exporter().NewCounterFunc(name+"Get", "Tablet server conn pool get count", cp.GetCount)
env.Exporter().NewCounterFunc(name+"GetSetting", "Tablet server conn pool get with setting count", cp.GetSettingCount)
env.Exporter().NewCounterFunc(name+"DiffSetting", "Number of times pool applied different setting", cp.DiffSettingCount)
env.Exporter().NewCounterFunc(name+"ResetSetting", "Number of times pool reset the setting", cp.ResetSettingCount)
cp.getConnTime = env.Exporter().NewTimings(name+"GetConnTime", "Tracks the amount of time it takes to get a connection", "Settings")
return cp
}
func (cp *Pool) pool() (p pools.IResourcePool) {
cp.mu.Lock()
p = cp.connections
cp.mu.Unlock()
return p
}
// Open must be called before starting to use the pool.
func (cp *Pool) Open(appParams, dbaParams, appDebugParams dbconfigs.Connector) {
cp.mu.Lock()
defer cp.mu.Unlock()
f := func(ctx context.Context) (pools.Resource, error) {
return NewDBConn(ctx, cp, appParams)
}
var refreshCheck pools.RefreshCheck
if net.ParseIP(appParams.Host()) == nil {
refreshCheck = netutil.DNSTracker(appParams.Host())
}
cp.connections = pools.NewResourcePool(f, cp.capacity, cp.capacity, cp.idleTimeout, cp.maxLifetime, cp.getLogWaitCallback(), refreshCheck, mysqlctl.PoolDynamicHostnameResolution)
cp.appDebugParams = appDebugParams
cp.dbaPool.Open(dbaParams)
}
func (cp *Pool) getLogWaitCallback() func(time.Time) {
if cp.name == "" {
return func(start time.Time) {} // no op
}
return func(start time.Time) {
cp.env.Stats().WaitTimings.Record(cp.name+"ResourceWaitTime", start)
}
}
// Close will close the pool and wait for connections to be returned before
// exiting.
func (cp *Pool) Close() {
log.Infof("connpool - started execution of Close")
p := cp.pool()
log.Infof("connpool - found the pool")
if p == nil {
log.Infof("connpool - pool is empty")
return
}
// We should not hold the lock while calling Close
// because it waits for connections to be returned.
log.Infof("connpool - calling close on the pool")
p.Close()
log.Infof("connpool - acquiring lock")
cp.mu.Lock()
log.Infof("connpool - acquired lock")
cp.connections.Close()
cp.connections = nil
cp.mu.Unlock()
log.Infof("connpool - closing dbaPool")
cp.dbaPool.Close()
log.Infof("connpool - finished execution of Close")
}
// Get returns a connection.
// You must call Recycle on DBConn once done.
func (cp *Pool) Get(ctx context.Context, setting *pools.Setting) (*DBConn, error) {
span, ctx := trace.NewSpan(ctx, "Pool.Get")
defer span.Finish()
if cp.waiterCap > 0 {
waiterCount := cp.waiterCount.Add(1)
defer cp.waiterCount.Add(-1)
if waiterCount > cp.waiterCap {
cp.waiterQueueFull.Add(1)
return nil, vterrors.Errorf(vtrpcpb.Code_RESOURCE_EXHAUSTED, "pool %s waiter count exceeded", cp.name)
}
}
if cp.isCallerIDAppDebug(ctx) {
return NewDBConnNoPool(ctx, cp.appDebugParams, cp.dbaPool, setting)
}
p := cp.pool()
if p == nil {
return nil, ErrConnPoolClosed
}
span.Annotate("capacity", p.Capacity())
span.Annotate("in_use", p.InUse())
span.Annotate("available", p.Available())
span.Annotate("active", p.Active())
if cp.timeout != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, cp.timeout)
defer cancel()
}
start := time.Now()
r, err := p.Get(ctx, setting)
if err != nil {
return nil, err
}
if cp.getConnTime != nil {
if setting == nil {
cp.getConnTime.Record(getWithoutS, start)
} else {
cp.getConnTime.Record(getWithS, start)
}
}
return r.(*DBConn), nil
}
// Put puts a connection into the pool.
func (cp *Pool) Put(conn *DBConn) {
p := cp.pool()
if p == nil {
panic(ErrConnPoolClosed)
}
if conn == nil {
p.Put(nil)
} else {
p.Put(conn)
}
}
// SetCapacity alters the size of the pool at runtime.
func (cp *Pool) SetCapacity(capacity int) (err error) {
cp.mu.Lock()
defer cp.mu.Unlock()
if cp.connections != nil {
err = cp.connections.SetCapacity(capacity)
if err != nil {
return err
}
}
cp.capacity = capacity
return nil
}
// SetIdleTimeout sets the idleTimeout on the pool.
func (cp *Pool) SetIdleTimeout(idleTimeout time.Duration) {
cp.mu.Lock()
defer cp.mu.Unlock()
if cp.connections != nil {
cp.connections.SetIdleTimeout(idleTimeout)
}
cp.dbaPool.SetIdleTimeout(idleTimeout)
cp.idleTimeout = idleTimeout
}
// StatsJSON returns the pool stats as a JSON object.
func (cp *Pool) StatsJSON() string {
p := cp.pool()
if p == nil {
return "{}"
}
res := p.StatsJSON()
closingBraceIndex := strings.LastIndex(res, "}")
if closingBraceIndex == -1 { // unexpected...
return res
}
return fmt.Sprintf(`%s, "WaiterQueueFull": %v}`, res[:closingBraceIndex], cp.waiterQueueFull.Load())
}
// Capacity returns the pool capacity.
func (cp *Pool) Capacity() int64 {
p := cp.pool()
if p == nil {
return 0
}
return p.Capacity()
}
// Available returns the number of available connections in the pool
func (cp *Pool) Available() int64 {
p := cp.pool()
if p == nil {
return 0
}
return p.Available()
}
// Active returns the number of active connections in the pool
func (cp *Pool) Active() int64 {
p := cp.pool()
if p == nil {
return 0
}
return p.Active()
}
// InUse returns the number of in-use connections in the pool
func (cp *Pool) InUse() int64 {
p := cp.pool()
if p == nil {
return 0
}
return p.InUse()
}
// MaxCap returns the maximum size of the pool
func (cp *Pool) MaxCap() int64 {
p := cp.pool()
if p == nil {
return 0
}
return p.MaxCap()
}
// WaitCount returns how many clients are waiting for a connection
func (cp *Pool) WaitCount() int64 {
p := cp.pool()
if p == nil {
return 0
}
return p.WaitCount()
}
// WaitTime return the pool WaitTime.
func (cp *Pool) WaitTime() time.Duration {
p := cp.pool()
if p == nil {
return 0
}
return p.WaitTime()
}
// IdleTimeout returns the idle timeout for the pool.
func (cp *Pool) IdleTimeout() time.Duration {
p := cp.pool()
if p == nil {
return 0
}
return p.IdleTimeout()
}
// IdleClosed returns the number of closed connections for the pool.
func (cp *Pool) IdleClosed() int64 {
p := cp.pool()
if p == nil {
return 0
}
return p.IdleClosed()
}
// MaxLifetimeClosed returns the number of connections closed to refresh timeout for the pool.
func (cp *Pool) MaxLifetimeClosed() int64 {
p := cp.pool()
if p == nil {
return 0
}
return p.MaxLifetimeClosed()
}
// Exhausted returns the number of times available went to zero for the pool.
func (cp *Pool) Exhausted() int64 {
p := cp.pool()
if p == nil {
return 0
}
return p.Exhausted()
}
// GetCount returns the number of times get was called
func (cp *Pool) GetCount() int64 {
p := cp.pool()
if p == nil {
return 0
}
return p.GetCount()
}
// GetSettingCount returns the number of times getWithSettings was called
func (cp *Pool) GetSettingCount() int64 {
p := cp.pool()
if p == nil {
return 0
}
return p.GetSettingCount()
}
// DiffSettingCount returns the number of times different settings were applied on the resource.
func (cp *Pool) DiffSettingCount() int64 {
p := cp.pool()
if p == nil {
return 0
}
return p.DiffSettingCount()
}
// ResetSettingCount returns the number of times settings were reset on the resource.
func (cp *Pool) ResetSettingCount() int64 {
p := cp.pool()
if p == nil {
return 0
}
return p.ResetSettingCount()
}
func (cp *Pool) isCallerIDAppDebug(ctx context.Context) bool {
params, err := cp.appDebugParams.MysqlParams()
if err != nil {
return false
}
if params == nil || params.Uname == "" {
return false
}
callerID := callerid.ImmediateCallerIDFromContext(ctx)
return callerID != nil && callerID.Username == params.Uname
}