forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.go
285 lines (252 loc) · 7.17 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
/*
Copyright 2017 Google Inc.
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 (
"sync"
"time"
"golang.org/x/net/context"
"vitess.io/vitess/go/mysql"
"vitess.io/vitess/go/pools"
"vitess.io/vitess/go/stats"
"vitess.io/vitess/go/vt/callerid"
"vitess.io/vitess/go/vt/dbconnpool"
"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")
// usedNames is for preventing expvar from panicking. Tests
// create pool objects multiple time. If a name was previously
// used, expvar initialization is skipped.
// TODO(sougou): Find a way to still crash if this happened
// through non-test code.
var usedNames = make(map[string]bool)
// MySQLChecker defines the CheckMySQL interface that lower
// level objects can use to call back into TabletServer.
type MySQLChecker interface {
CheckMySQL()
}
// 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 {
mu sync.Mutex
connections *pools.ResourcePool
capacity int
idleTimeout time.Duration
dbaPool *dbconnpool.ConnectionPool
checker MySQLChecker
appDebugParams *mysql.ConnParams
}
// New creates a new Pool. The name is used
// to publish stats only.
func New(
name string,
capacity int,
idleTimeout time.Duration,
checker MySQLChecker) *Pool {
cp := &Pool{
capacity: capacity,
idleTimeout: idleTimeout,
dbaPool: dbconnpool.NewConnectionPool("", 1, idleTimeout),
checker: checker,
}
if name == "" || usedNames[name] {
return cp
}
usedNames[name] = true
stats.NewGaugeFunc(name+"Capacity", "Tablet server conn pool capacity", cp.Capacity)
stats.NewGaugeFunc(name+"Available", "Tablet server conn pool available", cp.Available)
stats.NewGaugeFunc(name+"Active", "Tablet server conn pool active", cp.Active)
stats.NewGaugeFunc(name+"InUse", "Tablet server conn pool in use", cp.InUse)
stats.NewGaugeFunc(name+"MaxCap", "Tablet server conn pool max cap", cp.MaxCap)
stats.NewCounterFunc(name+"WaitCount", "Tablet server conn pool wait count", cp.WaitCount)
stats.NewCounterDurationFunc(name+"WaitTime", "Tablet server wait time", cp.WaitTime)
stats.NewGaugeDurationFunc(name+"IdleTimeout", "Tablet server idle timeout", cp.IdleTimeout)
stats.NewCounterFunc(name+"IdleClosed", "Tablet server conn pool idle closed", cp.IdleClosed)
return cp
}
func (cp *Pool) pool() (p *pools.ResourcePool) {
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 *mysql.ConnParams) {
cp.mu.Lock()
defer cp.mu.Unlock()
f := func() (pools.Resource, error) {
return NewDBConn(cp, appParams)
}
cp.connections = pools.NewResourcePool(f, cp.capacity, cp.capacity, cp.idleTimeout)
cp.appDebugParams = appDebugParams
cp.dbaPool.Open(dbaParams, tabletenv.MySQLStats)
}
// Close will close the pool and wait for connections to be returned before
// exiting.
func (cp *Pool) Close() {
p := cp.pool()
if p == nil {
return
}
// We should not hold the lock while calling Close
// because it waits for connections to be returned.
p.Close()
cp.mu.Lock()
cp.connections = nil
cp.mu.Unlock()
cp.dbaPool.Close()
}
// Get returns a connection.
// You must call Recycle on DBConn once done.
func (cp *Pool) Get(ctx context.Context) (*DBConn, error) {
if cp.isCallerIDAppDebug(ctx) {
return NewDBConnNoPool(cp.appDebugParams, cp.dbaPool)
}
p := cp.pool()
if p == nil {
return nil, ErrConnPoolClosed
}
r, err := p.Get(ctx)
if err != nil {
return nil, err
}
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 "{}"
}
return p.StatsJSON()
}
// 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()
}
func (cp *Pool) isCallerIDAppDebug(ctx context.Context) bool {
callerID := callerid.ImmediateCallerIDFromContext(ctx)
if cp.appDebugParams.Uname == "" {
return false
}
return callerID != nil && callerID.Username == cp.appDebugParams.Uname
}