forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlquery.go
418 lines (377 loc) · 12.5 KB
/
sqlquery.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
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tabletserver
import (
"bytes"
"fmt"
"math/rand"
"strings"
"sync"
"time"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/mysql"
mproto "github.com/youtube/vitess/go/mysql/proto"
rpcproto "github.com/youtube/vitess/go/rpcwrap/proto"
"github.com/youtube/vitess/go/stats"
"github.com/youtube/vitess/go/sync2"
"github.com/youtube/vitess/go/tb"
"github.com/youtube/vitess/go/vt/dbconfigs"
"github.com/youtube/vitess/go/vt/tabletserver/proto"
)
// exclusive transitions can be executed without a lock
// NOT_SERVING -> CONNECTING
// CONNECTING -> ABORT -> NOT_SERVING
// CONNECTING -> INITIALIZING -> SERVING/NOT_SERVING
// SERVING -> SHUTTING_DOWN -> NOT_SERVING
const (
NOT_SERVING = iota
CONNECTING
ABORT
INITIALIZING
SERVING
SHUTTING_DOWN
)
var stateName = map[int64]string{
NOT_SERVING: "NOT_SERVING",
CONNECTING: "CONNECTING",
ABORT: "ABORT",
INITIALIZING: "INITIALIZING",
SERVING: "SERVING",
SHUTTING_DOWN: "SHUTTING_DOWN",
}
//-----------------------------------------------
// RPC API
type SqlQuery struct {
// We use a hybrid locking scheme to control state transitions. This is
// optimal for frequent reads and infrequent state changes.
// You can use atomic lockless reads if you don't care about any state
// changes after you've read the variable. This is the common use case.
// You can use atomic lockless writes if you don't care about, or already
// know, the previous value of the object. This is true for exclusive
// transitions as documented above.
// You should use the statemu lock if you want to execute a transition
// where you don't want the state to change from the time you've read it.
statemu sync.Mutex
state sync2.AtomicInt64
qe *QueryEngine
sessionId int64
dbconfig dbconfigs.DBConfig
}
func NewSqlQuery(config Config) *SqlQuery {
sq := &SqlQuery{}
sq.qe = NewQueryEngine(config)
stats.PublishJSONFunc("Voltron", sq.statsJSON)
stats.Publish("TabletState", stats.IntFunc(sq.state.Get))
stats.Publish("TabletStateName", stats.StringFunc(sq.GetState))
return sq
}
// GetState returns the name of the current SqlQuery state (which is
// read atomically).
func (sq *SqlQuery) GetState() string {
return stateName[sq.state.Get()]
}
func (sq *SqlQuery) setState(state int64) {
log.Infof("SqlQuery state: %v -> %v", stateName[sq.state.Get()], stateName[state])
sq.state.Set(state)
}
func (sq *SqlQuery) allowQueries(dbconfig dbconfigs.DBConfig, schemaOverrides []SchemaOverride, qrs *QueryRules) {
sq.statemu.Lock()
v := sq.state.Get()
switch v {
case CONNECTING, ABORT, SERVING:
sq.statemu.Unlock()
log.Infof("Ignoring allowQueries request, current state: %v", v)
return
case INITIALIZING, SHUTTING_DOWN:
panic("unreachable")
}
// state is NOT_SERVING
sq.setState(CONNECTING)
sq.statemu.Unlock()
// Try connecting. disallowQueries can change the state to ABORT during this time.
waitTime := time.Second
for {
c, err := mysql.Connect(dbconfig.MysqlParams())
if err == nil {
c.Close()
break
}
log.Errorf("%v", err)
time.Sleep(waitTime)
// Cap at 32 seconds
if waitTime < 30*time.Second {
waitTime = waitTime * 2
}
if sq.state.Get() == ABORT {
// Exclusive transition. No need to lock statemu.
sq.setState(NOT_SERVING)
log.Infof("allowQueries aborting")
return
}
}
// Connection successful. Keep statemu locked.
sq.statemu.Lock()
defer sq.statemu.Unlock()
if sq.state.Get() == ABORT {
sq.setState(NOT_SERVING)
log.Infof("allowQueries aborting")
return
}
sq.setState(INITIALIZING)
defer func() {
if x := recover(); x != nil {
log.Errorf("%s", x.(*TabletError).Message)
sq.setState(NOT_SERVING)
return
}
sq.setState(SERVING)
}()
sq.qe.Open(dbconfig, schemaOverrides, qrs)
sq.dbconfig = dbconfig
sq.sessionId = Rand()
log.Infof("Session id: %d", sq.sessionId)
}
func (sq *SqlQuery) disallowQueries() {
sq.statemu.Lock()
defer sq.statemu.Unlock()
switch sq.state.Get() {
case CONNECTING:
sq.setState(ABORT)
return
case NOT_SERVING, ABORT:
return
case INITIALIZING, SHUTTING_DOWN:
panic("unreachable")
}
// state is SERVING
sq.setState(SHUTTING_DOWN)
defer func() {
sq.setState(NOT_SERVING)
}()
log.Infof("Stopping query service: %d", sq.sessionId)
sq.qe.Close()
sq.sessionId = 0
sq.dbconfig = dbconfigs.DBConfig{}
}
// checkState checks if we can serve queries. If not, it causes an
// error whose category is state dependent:
// SERVING: Everything is allowed.
// SHUTTING_DOWN:
// SELECT & BEGIN: RETRY errors
// DMLs & COMMITS: Allowed
// NOT_SERVING: RETRY for all.
func (sq *SqlQuery) checkState(sessionId int64, allowShutdown bool) {
switch sq.state.Get() {
case NOT_SERVING:
panic(NewTabletError(RETRY, "not serving"))
case CONNECTING, ABORT, INITIALIZING:
panic(NewTabletError(RETRY, "initalizing"))
case SHUTTING_DOWN:
if !allowShutdown {
panic(NewTabletError(RETRY, "unavailable"))
}
}
// state is SERVING
if sessionId == 0 || sessionId != sq.sessionId {
panic(NewTabletError(RETRY, "Invalid session Id %v", sessionId))
}
}
func (sq *SqlQuery) GetSessionId(sessionParams *proto.SessionParams, sessionInfo *proto.SessionInfo) error {
if sq.state.Get() != SERVING {
return NewTabletError(RETRY, "Query server is in %s state", stateName[sq.state.Get()])
}
if sessionParams.Keyspace != sq.dbconfig.Keyspace {
return NewTabletError(FATAL, "Keyspace mismatch, expecting %v, received %v", sq.dbconfig.Keyspace, sessionParams.Keyspace)
}
if sessionParams.Shard != sq.dbconfig.Shard {
return NewTabletError(FATAL, "Shard mismatch, expecting %v, received %v", sq.dbconfig.Shard, sessionParams.Shard)
}
sessionInfo.SessionId = sq.sessionId
return nil
}
func (sq *SqlQuery) Begin(context *rpcproto.Context, session *proto.Session, txInfo *proto.TransactionInfo) (err error) {
logStats := newSqlQueryStats("Begin", context)
logStats.OriginalSql = "begin"
defer handleError(&err, logStats)
sq.checkState(session.SessionId, false)
txInfo.TransactionId = sq.qe.Begin(logStats, session.ConnectionId)
return nil
}
func (sq *SqlQuery) Commit(context *rpcproto.Context, session *proto.Session, noOutput *string) (err error) {
logStats := newSqlQueryStats("Commit", context)
logStats.OriginalSql = "commit"
defer handleError(&err, logStats)
sq.checkState(session.SessionId, true)
sq.qe.Commit(logStats, session.TransactionId)
return nil
}
func (sq *SqlQuery) Rollback(context *rpcproto.Context, session *proto.Session, noOutput *string) (err error) {
logStats := newSqlQueryStats("Rollback", context)
logStats.OriginalSql = "rollback"
defer handleError(&err, logStats)
sq.checkState(session.SessionId, true)
sq.qe.Rollback(logStats, session.TransactionId)
return nil
}
func (sq *SqlQuery) CreateReserved(session *proto.Session, connectionInfo *proto.ConnectionInfo) (err error) {
defer handleError(&err, nil)
sq.checkState(session.SessionId, false)
connectionInfo.ConnectionId = sq.qe.CreateReserved()
return nil
}
func (sq *SqlQuery) CloseReserved(session *proto.Session, noOutput *string) (err error) {
defer handleError(&err, nil)
sq.checkState(session.SessionId, false)
sq.qe.CloseReserved(session.ConnectionId)
return nil
}
func (sq *SqlQuery) invalidateForDml(dml *proto.DmlType) {
if sq.state.Get() != SERVING {
return
}
sq.qe.InvalidateForDml(dml)
}
func (sq *SqlQuery) invalidateForDDL(ddlInvalidate *proto.DDLInvalidate) {
if sq.state.Get() != SERVING {
return
}
sq.qe.InvalidateForDDL(ddlInvalidate)
}
func handleExecError(query *proto.Query, err *error, logStats *sqlQueryStats) {
if logStats != nil {
logStats.Send()
}
if x := recover(); x != nil {
terr, ok := x.(*TabletError)
if !ok {
log.Errorf("Uncaught panic for %v:\n%v\n%s", query, x, tb.Stack(4))
*err = NewTabletError(FAIL, "%v: uncaught panic for %v", x, query)
errorStats.Add("Panic", 1)
return
}
*err = terr
terr.RecordStats()
// suppress these errors in logs
if terr.ErrorType == RETRY || terr.ErrorType == TX_POOL_FULL || terr.SqlError == mysql.DUP_ENTRY {
return
}
log.Errorf("%s: %v", terr.Message, query)
}
}
func (sq *SqlQuery) Execute(context *rpcproto.Context, query *proto.Query, reply *mproto.QueryResult) (err error) {
logStats := newSqlQueryStats("Execute", context)
defer handleExecError(query, &err, logStats)
// allow shutdown state if we're in a transaction
allowShutdown := (query.TransactionId != 0)
sq.checkState(query.SessionId, allowShutdown)
*reply = *sq.qe.Execute(logStats, query)
return nil
}
// the first QueryResult will have Fields set (and Rows nil)
// the subsequent QueryResult will have Rows set (and Fields nil)
func (sq *SqlQuery) StreamExecute(context *rpcproto.Context, query *proto.Query, sendReply func(reply interface{}) error) (err error) {
logStats := newSqlQueryStats("StreamExecute", context)
defer handleExecError(query, &err, logStats)
// check cases we don't handle yet
if query.TransactionId != 0 {
return NewTabletError(FAIL, "Transactions not supported with streaming")
}
if query.ConnectionId != 0 {
return NewTabletError(FAIL, "Persistent connections not supported with streaming")
}
sq.checkState(query.SessionId, false)
sq.qe.StreamExecute(logStats, query, func(reply interface{}) error {
if sq.state.Get() != SERVING {
return NewTabletError(FAIL, "Query server is in %s state", stateName[sq.state.Get()])
}
return sendReply(reply)
})
return nil
}
func (sq *SqlQuery) ExecuteBatch(context *rpcproto.Context, queryList *proto.QueryList, reply *proto.QueryResultList) (err error) {
defer handleError(&err, nil)
if len(queryList.Queries) == 0 {
panic(NewTabletError(FAIL, "Empty query list"))
}
sq.checkState(queryList.SessionId, false)
begin_called := false
var noOutput string
session := proto.Session{
TransactionId: queryList.TransactionId,
ConnectionId: queryList.ConnectionId,
SessionId: queryList.SessionId,
}
reply.List = make([]mproto.QueryResult, 0, len(queryList.Queries))
for _, bound := range queryList.Queries {
trimmed := strings.ToLower(strings.Trim(bound.Sql, " \t\r\n"))
switch trimmed {
case "begin":
if session.TransactionId != 0 {
panic(NewTabletError(FAIL, "Nested transactions disallowed"))
}
var txInfo proto.TransactionInfo
if err = sq.Begin(context, &session, &txInfo); err != nil {
return err
}
session.TransactionId = txInfo.TransactionId
begin_called = true
reply.List = append(reply.List, mproto.QueryResult{})
case "commit":
if !begin_called {
panic(NewTabletError(FAIL, "Cannot commit without begin"))
}
if err = sq.Commit(context, &session, &noOutput); err != nil {
return err
}
session.TransactionId = 0
begin_called = false
reply.List = append(reply.List, mproto.QueryResult{})
default:
query := proto.Query{
Sql: bound.Sql,
BindVariables: bound.BindVariables,
TransactionId: session.TransactionId,
ConnectionId: session.ConnectionId,
SessionId: session.SessionId,
}
var localReply mproto.QueryResult
if err = sq.Execute(context, &query, &localReply); err != nil {
if begin_called {
sq.Rollback(context, &session, &noOutput)
}
return err
}
reply.List = append(reply.List, localReply)
}
}
if begin_called {
sq.Rollback(context, &session, &noOutput)
panic(NewTabletError(FAIL, "begin called with no commit"))
}
return nil
}
func (sq *SqlQuery) statsJSON() string {
buf := bytes.NewBuffer(make([]byte, 0, 128))
fmt.Fprintf(buf, "{")
fmt.Fprintf(buf, "\n \"State\": \"%v\",", stateName[sq.state.Get()])
fmt.Fprintf(buf, "\n \"CachePool\": %v,", sq.qe.cachePool.StatsJSON())
fmt.Fprintf(buf, "\n \"QueryCache\": %v,", sq.qe.schemaInfo.queries.StatsJSON())
fmt.Fprintf(buf, "\n \"SchemaReloadTime\": %v,", int64(sq.qe.schemaInfo.reloadTime))
fmt.Fprintf(buf, "\n \"ConnPool\": %v,", sq.qe.connPool.StatsJSON())
fmt.Fprintf(buf, "\n \"StreamConnPool\": %v,", sq.qe.streamConnPool.StatsJSON())
fmt.Fprintf(buf, "\n \"TxPool\": %v,", sq.qe.txPool.StatsJSON())
fmt.Fprintf(buf, "\n \"ActiveTxPool\": %v,", sq.qe.activeTxPool.StatsJSON())
fmt.Fprintf(buf, "\n \"ActivePool\": %v,", sq.qe.activePool.StatsJSON())
fmt.Fprintf(buf, "\n \"MaxResultSize\": %v,", sq.qe.maxResultSize.Get())
fmt.Fprintf(buf, "\n \"StreamBufferSize\": %v,", sq.qe.streamBufferSize.Get())
fmt.Fprintf(buf, "\n \"ReservedPool\": %v", sq.qe.reservedPool.StatsJSON())
fmt.Fprintf(buf, "\n}")
return buf.String()
}
func init() {
rand.Seed(time.Now().UnixNano())
}
func Rand() int64 {
return rand.Int63()
}