forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_engine.go
808 lines (723 loc) · 27.5 KB
/
query_engine.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
// 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 (
"sync"
"time"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/hack"
mproto "github.com/youtube/vitess/go/mysql/proto"
"github.com/youtube/vitess/go/sqltypes"
"github.com/youtube/vitess/go/stats"
"github.com/youtube/vitess/go/sync2"
"github.com/youtube/vitess/go/vt/dbconfigs"
"github.com/youtube/vitess/go/vt/schema"
"github.com/youtube/vitess/go/vt/sqlparser"
"github.com/youtube/vitess/go/vt/tabletserver/proto"
)
const (
MAX_RESULT_NAME = "_vtMaxResultSize"
ROWCACHE_INVALIDATION_POSITION = "ROWCACHE_INVALIDATION_POSITION"
// SPOT_CHECK_MULTIPLIER determines the precision of the
// spot check ratio: 1e6 == 6 digits
SPOT_CHECK_MULTIPLIER = 1e6
)
//-----------------------------------------------
type QueryEngine struct {
// Obtain read lock on mu to execute queries
// Obtain write lock to start/stop query service
mu sync.RWMutex
cachePool *CachePool
schemaInfo *SchemaInfo
connPool *ConnectionPool
streamConnPool *ConnectionPool
txPool *ConnectionPool
activeTxPool *ActiveTxPool
activePool *ActivePool
consolidator *Consolidator
spotCheckFreq sync2.AtomicInt64
maxResultSize sync2.AtomicInt64
streamBufferSize sync2.AtomicInt64
}
type CompiledPlan struct {
Query string
*ExecPlan
BindVars map[string]interface{}
TransactionId int64
}
// stats are globals to allow anybody to set them
var (
queryStats *stats.Timings
waitStats *stats.Timings
killStats *stats.Counters
infoErrors *stats.Counters
errorStats *stats.Counters
internalErrors *stats.Counters
resultStats *stats.Histogram
spotCheckCount *stats.Int
QPSRates *stats.Rates
)
var resultBuckets = []int64{0, 1, 5, 10, 50, 100, 500, 1000, 5000, 10000}
// CacheInvalidator provides the abstraction needed for an instant invalidation
// vs. delayed invalidation in the case of in-transaction dmls
type CacheInvalidator interface {
Delete(key string) bool
}
// NewQueryEngine creates a new QueryEngine.
// This is a singleton class.
// You must call this only once.
func NewQueryEngine(config Config) *QueryEngine {
qe := &QueryEngine{}
// services
qe.cachePool = NewCachePool("RowcachePool", config.RowCache, time.Duration(config.QueryTimeout*1e9), time.Duration(config.IdleTimeout*1e9))
qe.schemaInfo = NewSchemaInfo(config.QueryCacheSize, time.Duration(config.SchemaReloadTime*1e9), time.Duration(config.IdleTimeout*1e9), config.SensitiveMode)
qe.connPool = NewConnectionPool("ConnPool", config.PoolSize, time.Duration(config.IdleTimeout*1e9))
qe.streamConnPool = NewConnectionPool("StreamConnPool", config.StreamPoolSize, time.Duration(config.IdleTimeout*1e9))
qe.txPool = NewConnectionPool("TransactionPool", config.TransactionCap, time.Duration(config.IdleTimeout*1e9)) // connections in pool has to be > transactionCap
qe.activeTxPool = NewActiveTxPool("ActiveTransactionPool", time.Duration(config.TransactionTimeout*1e9))
qe.activePool = NewActivePool("ActivePool", time.Duration(config.QueryTimeout*1e9), time.Duration(config.IdleTimeout*1e9))
qe.consolidator = NewConsolidator()
// vars
qe.spotCheckFreq = sync2.AtomicInt64(config.SpotCheckRatio * SPOT_CHECK_MULTIPLIER)
qe.maxResultSize = sync2.AtomicInt64(config.MaxResultSize)
qe.streamBufferSize = sync2.AtomicInt64(config.StreamBufferSize)
// stats
stats.Publish("MaxResultSize", stats.IntFunc(qe.maxResultSize.Get))
stats.Publish("StreamBufferSize", stats.IntFunc(qe.streamBufferSize.Get))
queryStats = stats.NewTimings("Queries")
QPSRates = stats.NewRates("QPS", queryStats, 15, 60*time.Second)
waitStats = stats.NewTimings("Waits")
killStats = stats.NewCounters("Kills")
infoErrors = stats.NewCounters("InfoErrors")
errorStats = stats.NewCounters("Errors")
internalErrors = stats.NewCounters("InternalErrors")
resultStats = stats.NewHistogram("Results", resultBuckets)
stats.Publish("SpotCheckRatio", stats.FloatFunc(func() float64 {
return float64(qe.spotCheckFreq.Get()) / SPOT_CHECK_MULTIPLIER
}))
spotCheckCount = stats.NewInt("SpotCheckCount")
return qe
}
func (qe *QueryEngine) Open(dbconfig *dbconfigs.DBConfig, schemaOverrides []SchemaOverride, qrs *QueryRules) {
// Wait for Close, in case it's running
qe.mu.Lock()
defer qe.mu.Unlock()
connFactory := GenericConnectionCreator(&dbconfig.ConnectionParams)
if dbconfig.EnableRowcache {
qe.cachePool.Open()
} else {
log.Infof("rowcache not enabled")
}
start := time.Now()
qe.schemaInfo.Open(connFactory, schemaOverrides, qe.cachePool, qrs)
log.Infof("Time taken to load the schema: %v", time.Now().Sub(start))
qe.connPool.Open(connFactory)
qe.streamConnPool.Open(connFactory)
qe.txPool.Open(connFactory)
qe.activeTxPool.Open()
qe.activePool.Open(connFactory)
}
func (qe *QueryEngine) Close() {
qe.activeTxPool.WaitForEmpty()
// Ensure all read locks are released (no more queries being served)
qe.mu.Lock()
defer qe.mu.Unlock()
qe.activePool.Close()
qe.schemaInfo.Close()
qe.activeTxPool.Close()
qe.txPool.Close()
qe.streamConnPool.Close()
qe.connPool.Close()
qe.cachePool.Close()
}
func (qe *QueryEngine) Begin(logStats *sqlQueryStats) (transactionId int64) {
qe.mu.RLock()
defer qe.mu.RUnlock()
var conn PoolConnection
if conn = qe.txPool.TryGet(); conn == nil {
panic(NewTabletError(TX_POOL_FULL, "Transaction pool connection limit exceeded"))
}
transactionId, err := qe.activeTxPool.SafeBegin(conn)
if err != nil {
conn.Recycle()
panic(err)
}
return transactionId
}
func (qe *QueryEngine) Commit(logStats *sqlQueryStats, transactionId int64) {
qe.mu.RLock()
defer qe.mu.RUnlock()
dirtyTables, err := qe.activeTxPool.SafeCommit(transactionId)
qe.invalidateRows(logStats, dirtyTables)
if err != nil {
panic(err)
}
}
func (qe *QueryEngine) invalidateRows(logStats *sqlQueryStats, dirtyTables map[string]DirtyKeys) {
for tableName, invalidList := range dirtyTables {
tableInfo := qe.schemaInfo.GetTable(tableName)
if tableInfo == nil {
continue
}
invalidations := int64(0)
for key := range invalidList {
tableInfo.Cache.Delete(key)
invalidations++
}
logStats.CacheInvalidations += invalidations
tableInfo.invalidations.Add(invalidations)
}
}
func (qe *QueryEngine) Rollback(logStats *sqlQueryStats, transactionId int64) {
qe.mu.RLock()
defer qe.mu.RUnlock()
qe.activeTxPool.Rollback(transactionId)
}
func (qe *QueryEngine) Execute(logStats *sqlQueryStats, query *proto.Query) (reply *mproto.QueryResult) {
qe.mu.RLock()
defer qe.mu.RUnlock()
if query.BindVariables == nil { // will help us avoid repeated nil checks
query.BindVariables = make(map[string]interface{})
}
logStats.BindVariables = query.BindVariables
// cheap hack: strip trailing comment into a special bind var
stripTrailing(query)
basePlan := qe.schemaInfo.GetPlan(logStats, query.Sql)
planName := basePlan.PlanId.String()
logStats.PlanType = planName
logStats.OriginalSql = basePlan.DisplayQuery
defer func(start time.Time) {
duration := time.Now().Sub(start)
queryStats.Add(planName, duration)
if reply == nil {
basePlan.AddStats(1, duration, 0, 1)
} else {
basePlan.AddStats(1, duration, int64(len(reply.Rows)), 0)
}
}(time.Now())
// Run it by the rules engine
action, desc := basePlan.Rules.getAction(logStats.RemoteAddr(), logStats.Username(), query.BindVariables)
if action == QR_FAIL_QUERY {
panic(NewTabletError(FAIL, "Query disallowed due to rule: %s", desc))
}
if basePlan.PlanId == sqlparser.PLAN_DDL {
return qe.execDDL(logStats, query.Sql)
}
plan := &CompiledPlan{
Query: query.Sql,
ExecPlan: basePlan,
BindVars: query.BindVariables,
TransactionId: query.TransactionId,
}
if query.TransactionId != 0 {
// Need upfront connection for DMLs and transactions
conn := qe.activeTxPool.Get(query.TransactionId)
defer conn.Recycle()
conn.RecordQuery(plan.DisplayQuery)
var invalidator CacheInvalidator
if plan.TableInfo != nil && plan.TableInfo.CacheType != schema.CACHE_NONE {
invalidator = conn.DirtyKeys(plan.TableName)
}
switch plan.PlanId {
case sqlparser.PLAN_PASS_DML:
// TODO(sougou): Delete code path that leads here.
// We need to permanently disallow this plan.
panic(NewTabletError(FAIL, "DML too complex"))
case sqlparser.PLAN_INSERT_PK:
reply = qe.execInsertPK(logStats, conn, plan, invalidator)
case sqlparser.PLAN_INSERT_SUBQUERY:
reply = qe.execInsertSubquery(logStats, conn, plan, invalidator)
case sqlparser.PLAN_DML_PK:
reply = qe.execDMLPK(logStats, conn, plan, invalidator)
case sqlparser.PLAN_DML_SUBQUERY:
reply = qe.execDMLSubquery(logStats, conn, plan, invalidator)
default: // select or set in a transaction, just count as select
reply = qe.execDirect(logStats, plan, conn)
}
} else {
switch plan.PlanId {
case sqlparser.PLAN_PASS_SELECT:
if plan.Reason == sqlparser.REASON_FOR_UPDATE {
panic(NewTabletError(FAIL, "Disallowed outside transaction"))
}
reply = qe.execSelect(logStats, plan)
case sqlparser.PLAN_PK_EQUAL:
reply = qe.execPKEqual(logStats, plan)
case sqlparser.PLAN_PK_IN:
reply = qe.execPKIN(logStats, plan)
case sqlparser.PLAN_SELECT_SUBQUERY:
reply = qe.execSubquery(logStats, plan)
case sqlparser.PLAN_SET:
waitingForConnectionStart := time.Now()
conn := qe.connPool.Get()
logStats.WaitingForConnection += time.Now().Sub(waitingForConnectionStart)
defer conn.Recycle()
reply = qe.execSet(logStats, conn, plan)
default:
panic(NewTabletError(NOT_IN_TX, "DMLs not allowed outside of transactions"))
}
}
if plan.PlanId.IsSelect() {
logStats.RowsAffected = int(reply.RowsAffected)
resultStats.Add(int64(reply.RowsAffected))
logStats.Rows = reply.Rows
}
return reply
}
// the first QueryResult will have Fields set (and Rows nil)
// the subsequent QueryResult will have Rows set (and Fields nil)
func (qe *QueryEngine) StreamExecute(logStats *sqlQueryStats, query *proto.Query, sendReply func(*mproto.QueryResult) error) {
qe.mu.RLock()
defer qe.mu.RUnlock()
if query.BindVariables == nil { // will help us avoid repeated nil checks
query.BindVariables = make(map[string]interface{})
}
logStats.BindVariables = query.BindVariables
// cheap hack: strip trailing comment into a special bind var
stripTrailing(query)
plan := qe.schemaInfo.GetStreamPlan(query.Sql)
logStats.PlanType = "SELECT_STREAM"
logStats.OriginalSql = plan.DisplayQuery
defer queryStats.Record("SELECT_STREAM", time.Now())
// does the real work: first get a connection
waitingForConnectionStart := time.Now()
conn := qe.streamConnPool.Get()
logStats.WaitingForConnection += time.Now().Sub(waitingForConnectionStart)
defer conn.Recycle()
// then let's stream!
qe.fullStreamFetch(logStats, conn, plan.FullQuery, query.BindVariables, nil, nil, sendReply)
}
func (qe *QueryEngine) InvalidateForDml(dml *proto.DmlType) {
if qe.cachePool.IsClosed() {
return
}
qe.mu.RLock()
defer qe.mu.RUnlock()
invalidations := int64(0)
tableInfo := qe.schemaInfo.GetTable(dml.Table)
if tableInfo == nil {
panic(NewTabletError(FAIL, "Table %s not found", dml.Table))
}
if tableInfo.CacheType == schema.CACHE_NONE {
return
}
for _, val := range dml.Keys {
newKey := validateKey(tableInfo, val)
if newKey != "" {
tableInfo.Cache.Delete(newKey)
}
invalidations++
}
tableInfo.invalidations.Add(invalidations)
}
func (qe *QueryEngine) InvalidateForDDL(ddlInvalidate *proto.DDLInvalidate) {
qe.mu.RLock()
defer qe.mu.RUnlock()
ddlPlan := sqlparser.DDLParse(ddlInvalidate.DDL)
if ddlPlan.Action == 0 {
panic(NewTabletError(FAIL, "DDL is not understood"))
}
qe.schemaInfo.DropTable(ddlPlan.TableName)
if ddlPlan.Action != sqlparser.DROP { // CREATE, ALTER, RENAME
qe.schemaInfo.CreateTable(ddlPlan.NewName)
}
}
//-----------------------------------------------
// DDL
func (qe *QueryEngine) execDDL(logStats *sqlQueryStats, ddl string) *mproto.QueryResult {
ddlPlan := sqlparser.DDLParse(ddl)
if ddlPlan.Action == 0 {
panic(NewTabletError(FAIL, "DDL is not understood"))
}
// Stolen from Begin
conn := qe.txPool.Get()
txid, err := qe.activeTxPool.SafeBegin(conn)
if err != nil {
conn.Recycle()
panic(err)
}
// Stolen from Commit
defer qe.activeTxPool.SafeCommit(txid)
// Stolen from Execute
conn = qe.activeTxPool.Get(txid)
defer conn.Recycle()
result, err := qe.executeSql(logStats, conn, ddl, false)
if err != nil {
panic(NewTabletErrorSql(FAIL, err))
}
qe.schemaInfo.DropTable(ddlPlan.TableName)
if ddlPlan.Action != sqlparser.DROP { // CREATE, ALTER, RENAME
qe.schemaInfo.CreateTable(ddlPlan.NewName)
}
return result
}
//-----------------------------------------------
// Execution
func (qe *QueryEngine) execPKEqual(logStats *sqlQueryStats, plan *CompiledPlan) (result *mproto.QueryResult) {
pkRows := buildValueList(plan.TableInfo, plan.PKValues, plan.BindVars)
if len(pkRows) != 1 || plan.Fields == nil {
panic("unexpected")
}
row := qe.fetchOne(logStats, plan, pkRows[0])
result = &mproto.QueryResult{}
result.Fields = plan.Fields
if row == nil {
return
}
result.Rows = make([][]sqltypes.Value, 1)
result.Rows[0] = applyFilter(plan.ColumnNumbers, row)
result.RowsAffected = 1
return
}
func (qe *QueryEngine) fetchOne(logStats *sqlQueryStats, plan *CompiledPlan, pk []sqltypes.Value) (row []sqltypes.Value) {
logStats.QuerySources |= QUERY_SOURCE_ROWCACHE
tableInfo := plan.TableInfo
keys := make([]string, 1)
keys[0] = buildKey(pk)
rcresults := tableInfo.Cache.Get(keys)
rcresult := rcresults[keys[0]]
if rcresult.Row != nil {
if qe.mustVerify() {
qe.spotCheck(logStats, plan, rcresult, pk)
}
logStats.CacheHits++
tableInfo.hits.Add(1)
return rcresult.Row
}
resultFromdb := qe.qFetch(logStats, plan.OuterQuery, plan.BindVars, pk)
if len(resultFromdb.Rows) == 0 {
logStats.CacheAbsent++
tableInfo.absent.Add(1)
return nil
}
row = resultFromdb.Rows[0]
tableInfo.Cache.Set(keys[0], row, rcresult.Cas)
logStats.CacheMisses++
tableInfo.misses.Add(1)
return row
}
func (qe *QueryEngine) execPKIN(logStats *sqlQueryStats, plan *CompiledPlan) (result *mproto.QueryResult) {
pkRows := buildINValueList(plan.TableInfo, plan.PKValues, plan.BindVars)
return qe.fetchMulti(logStats, plan, pkRows)
}
func (qe *QueryEngine) execSubquery(logStats *sqlQueryStats, plan *CompiledPlan) (result *mproto.QueryResult) {
innerResult := qe.qFetch(logStats, plan.Subquery, plan.BindVars, nil)
return qe.fetchMulti(logStats, plan, innerResult.Rows)
}
func (qe *QueryEngine) fetchMulti(logStats *sqlQueryStats, plan *CompiledPlan, pkRows [][]sqltypes.Value) (result *mproto.QueryResult) {
result = &mproto.QueryResult{}
if len(pkRows) == 0 {
return
}
if len(pkRows[0]) != 1 || plan.Fields == nil {
panic("unexpected")
}
tableInfo := plan.TableInfo
keys := make([]string, len(pkRows))
for i, pk := range pkRows {
keys[i] = buildKey(pk)
}
rcresults := tableInfo.Cache.Get(keys)
result.Fields = plan.Fields
rows := make([][]sqltypes.Value, 0, len(pkRows))
missingRows := make([]sqltypes.Value, 0, len(pkRows))
var hits, absent, misses int64
for i, pk := range pkRows {
rcresult := rcresults[keys[i]]
if rcresult.Row != nil {
if qe.mustVerify() {
qe.spotCheck(logStats, plan, rcresult, pk)
}
rows = append(rows, applyFilter(plan.ColumnNumbers, rcresult.Row))
hits++
} else {
missingRows = append(missingRows, pk[0])
}
}
if len(missingRows) != 0 {
resultFromdb := qe.qFetch(logStats, plan.OuterQuery, plan.BindVars, missingRows)
misses = int64(len(resultFromdb.Rows))
absent = int64(len(pkRows)) - hits - misses
for _, row := range resultFromdb.Rows {
rows = append(rows, applyFilter(plan.ColumnNumbers, row))
key := buildKey(applyFilter(plan.TableInfo.PKColumns, row))
tableInfo.Cache.Set(key, row, rcresults[key].Cas)
}
}
logStats.CacheHits = hits
logStats.CacheAbsent = absent
logStats.CacheMisses = misses
logStats.QuerySources |= QUERY_SOURCE_ROWCACHE
tableInfo.hits.Add(hits)
tableInfo.absent.Add(absent)
tableInfo.misses.Add(misses)
result.RowsAffected = uint64(len(rows))
result.Rows = rows
return result
}
func (qe *QueryEngine) mustVerify() bool {
return (Rand() % SPOT_CHECK_MULTIPLIER) < qe.spotCheckFreq.Get()
}
func (qe *QueryEngine) spotCheck(logStats *sqlQueryStats, plan *CompiledPlan, rcresult RCResult, pk []sqltypes.Value) {
spotCheckCount.Add(1)
resultFromdb := qe.qFetch(logStats, plan.OuterQuery, plan.BindVars, pk)
var dbrow []sqltypes.Value
if len(resultFromdb.Rows) != 0 {
dbrow = resultFromdb.Rows[0]
}
if dbrow == nil || !rowsAreEqual(rcresult.Row, dbrow) {
go qe.recheckLater(plan, rcresult, dbrow, pk)
}
}
func (qe *QueryEngine) recheckLater(plan *CompiledPlan, rcresult RCResult, dbrow []sqltypes.Value, pk []sqltypes.Value) {
// Read lock is needed because this runs as a separate goroutine.
// We also have to ensure that the server hasn't shut down by the time
// we got the lock.
qe.mu.RLock()
defer qe.mu.RUnlock()
if qe.cachePool.IsClosed() {
return
}
time.Sleep(10 * time.Second)
keys := make([]string, 1)
keys[0] = buildKey(pk)
reloaded := plan.TableInfo.Cache.Get(keys)[keys[0]]
// If reloaded row is absent or has changed, we're good
if reloaded.Row == nil || reloaded.Cas != rcresult.Cas {
return
}
log.Warningf("query: %v", plan.FullQuery)
log.Warningf("mismatch for: %v\ncache: %v\ndb: %v", pk, rcresult.Row, dbrow)
internalErrors.Add("Mismatch", 1)
}
// execDirect always sends the query to mysql
func (qe *QueryEngine) execDirect(logStats *sqlQueryStats, plan *CompiledPlan, conn PoolConnection) (result *mproto.QueryResult) {
if plan.Fields != nil {
result = qe.directFetch(logStats, conn, plan.FullQuery, plan.BindVars, nil, nil)
result.Fields = plan.Fields
return
}
result = qe.fullFetch(logStats, conn, plan.FullQuery, plan.BindVars, nil, nil)
return
}
// execSelect sends a query to mysql only if another identical query is not running. Otherwise, it waits and
// reuses the result. If the plan is missng field info, it sends the query to mysql requesting full info.
func (qe *QueryEngine) execSelect(logStats *sqlQueryStats, plan *CompiledPlan) (result *mproto.QueryResult) {
if plan.Fields != nil {
result = qe.qFetch(logStats, plan.FullQuery, plan.BindVars, nil)
result.Fields = plan.Fields
return
}
waitingForConnectionStart := time.Now()
conn := qe.connPool.Get()
logStats.WaitingForConnection += time.Now().Sub(waitingForConnectionStart)
defer conn.Recycle()
result = qe.fullFetch(logStats, conn, plan.FullQuery, plan.BindVars, nil, nil)
return
}
func (qe *QueryEngine) execInsertPK(logStats *sqlQueryStats, conn PoolConnection, plan *CompiledPlan, invalidator CacheInvalidator) (result *mproto.QueryResult) {
pkRows := buildValueList(plan.TableInfo, plan.PKValues, plan.BindVars)
return qe.execInsertPKRows(logStats, conn, plan, pkRows, invalidator)
}
func (qe *QueryEngine) execInsertSubquery(logStats *sqlQueryStats, conn PoolConnection, plan *CompiledPlan, invalidator CacheInvalidator) (result *mproto.QueryResult) {
innerResult := qe.directFetch(logStats, conn, plan.Subquery, plan.BindVars, nil, nil)
innerRows := innerResult.Rows
if len(innerRows) == 0 {
return &mproto.QueryResult{RowsAffected: 0}
}
if len(plan.ColumnNumbers) != len(innerRows[0]) {
panic(NewTabletError(FAIL, "Subquery length does not match column list"))
}
pkRows := make([][]sqltypes.Value, len(innerRows))
for i, innerRow := range innerRows {
pkRows[i] = applyFilterWithPKDefaults(plan.TableInfo, plan.SubqueryPKColumns, innerRow)
}
// Validating first row is sufficient
validateRow(plan.TableInfo, plan.TableInfo.PKColumns, pkRows[0])
plan.BindVars["_rowValues"] = innerRows
return qe.execInsertPKRows(logStats, conn, plan, pkRows, invalidator)
}
func (qe *QueryEngine) execInsertPKRows(logStats *sqlQueryStats, conn PoolConnection, plan *CompiledPlan, pkRows [][]sqltypes.Value, invalidator CacheInvalidator) (result *mproto.QueryResult) {
secondaryList := buildSecondaryList(plan.TableInfo, pkRows, plan.SecondaryPKValues, plan.BindVars)
bsc := buildStreamComment(plan.TableInfo, pkRows, secondaryList)
result = qe.directFetch(logStats, conn, plan.OuterQuery, plan.BindVars, nil, bsc)
return result
}
func (qe *QueryEngine) execDMLPK(logStats *sqlQueryStats, conn PoolConnection, plan *CompiledPlan, invalidator CacheInvalidator) (result *mproto.QueryResult) {
pkRows := buildValueList(plan.TableInfo, plan.PKValues, plan.BindVars)
secondaryList := buildSecondaryList(plan.TableInfo, pkRows, plan.SecondaryPKValues, plan.BindVars)
bsc := buildStreamComment(plan.TableInfo, pkRows, secondaryList)
result = qe.directFetch(logStats, conn, plan.OuterQuery, plan.BindVars, nil, bsc)
if invalidator != nil {
for _, pk := range pkRows {
key := buildKey(pk)
invalidator.Delete(key)
}
}
return result
}
func (qe *QueryEngine) execDMLSubquery(logStats *sqlQueryStats, conn PoolConnection, plan *CompiledPlan, invalidator CacheInvalidator) (result *mproto.QueryResult) {
innerResult := qe.directFetch(logStats, conn, plan.Subquery, plan.BindVars, nil, nil)
// no need to validate innerResult
return qe.execDMLPKRows(logStats, conn, plan, innerResult.Rows, invalidator)
}
func (qe *QueryEngine) execDMLPKRows(logStats *sqlQueryStats, conn PoolConnection, plan *CompiledPlan, pkRows [][]sqltypes.Value, invalidator CacheInvalidator) (result *mproto.QueryResult) {
if len(pkRows) == 0 {
return &mproto.QueryResult{RowsAffected: 0}
}
rowsAffected := uint64(0)
singleRow := make([][]sqltypes.Value, 1)
for _, pkRow := range pkRows {
singleRow[0] = pkRow
secondaryList := buildSecondaryList(plan.TableInfo, singleRow, plan.SecondaryPKValues, plan.BindVars)
bsc := buildStreamComment(plan.TableInfo, singleRow, secondaryList)
rowsAffected += qe.directFetch(logStats, conn, plan.OuterQuery, plan.BindVars, pkRow, bsc).RowsAffected
if invalidator != nil {
key := buildKey(pkRow)
invalidator.Delete(key)
}
}
return &mproto.QueryResult{RowsAffected: rowsAffected}
}
func (qe *QueryEngine) execSet(logStats *sqlQueryStats, conn PoolConnection, plan *CompiledPlan) (result *mproto.QueryResult) {
switch plan.SetKey {
case "vt_pool_size":
qe.connPool.SetCapacity(int(plan.SetValue.(float64)))
case "vt_stream_pool_size":
qe.streamConnPool.SetCapacity(int(plan.SetValue.(float64)))
case "vt_transaction_cap":
qe.txPool.SetCapacity(int(plan.SetValue.(float64)))
case "vt_transaction_timeout":
qe.activeTxPool.SetTimeout(time.Duration(plan.SetValue.(float64) * 1e9))
case "vt_schema_reload_time":
qe.schemaInfo.SetReloadTime(time.Duration(plan.SetValue.(float64) * 1e9))
case "vt_query_cache_size":
qe.schemaInfo.SetQueryCacheSize(int(plan.SetValue.(float64)))
case "vt_max_result_size":
val := int64(plan.SetValue.(float64))
if val < 1 {
panic(NewTabletError(FAIL, "max result size out of range %v", val))
}
qe.maxResultSize.Set(val)
case "vt_stream_buffer_size":
val := int64(plan.SetValue.(float64))
if val < 1024 {
panic(NewTabletError(FAIL, "stream buffer size out of range %v", val))
}
qe.streamBufferSize.Set(val)
case "vt_query_timeout":
qe.activePool.SetTimeout(time.Duration(plan.SetValue.(float64) * 1e9))
case "vt_idle_timeout":
t := plan.SetValue.(float64) * 1e9
qe.connPool.SetIdleTimeout(time.Duration(t))
qe.streamConnPool.SetIdleTimeout(time.Duration(t))
qe.txPool.SetIdleTimeout(time.Duration(t))
qe.activePool.SetIdleTimeout(time.Duration(t))
case "vt_spot_check_ratio":
qe.spotCheckFreq.Set(int64(plan.SetValue.(float64) * SPOT_CHECK_MULTIPLIER))
default:
return qe.directFetch(logStats, conn, plan.FullQuery, plan.BindVars, nil, nil)
}
return &mproto.QueryResult{}
}
func (qe *QueryEngine) qFetch(logStats *sqlQueryStats, parsed_query *sqlparser.ParsedQuery, bindVars map[string]interface{}, listVars []sqltypes.Value) (result *mproto.QueryResult) {
sql := qe.generateFinalSql(parsed_query, bindVars, listVars, nil)
q, ok := qe.consolidator.Create(string(sql))
if ok {
defer q.Broadcast()
waitingForConnectionStart := time.Now()
conn, err := qe.connPool.SafeGet()
logStats.WaitingForConnection += time.Now().Sub(waitingForConnectionStart)
if err != nil {
q.Err = NewTabletErrorSql(FATAL, err)
} else {
defer conn.Recycle()
q.Result, q.Err = qe.executeSql(logStats, conn, sql, false)
}
} else {
logStats.QuerySources |= QUERY_SOURCE_CONSOLIDATOR
q.Wait()
}
if q.Err != nil {
panic(q.Err)
}
return q.Result
}
func (qe *QueryEngine) directFetch(logStats *sqlQueryStats, conn PoolConnection, parsed_query *sqlparser.ParsedQuery, bindVars map[string]interface{}, listVars []sqltypes.Value, buildStreamComment []byte) (result *mproto.QueryResult) {
sql := qe.generateFinalSql(parsed_query, bindVars, listVars, buildStreamComment)
result, err := qe.executeSql(logStats, conn, sql, false)
if err != nil {
panic(err)
}
return result
}
// fullFetch also fetches field info
func (qe *QueryEngine) fullFetch(logStats *sqlQueryStats, conn PoolConnection, parsed_query *sqlparser.ParsedQuery, bindVars map[string]interface{}, listVars []sqltypes.Value, buildStreamComment []byte) (result *mproto.QueryResult) {
sql := qe.generateFinalSql(parsed_query, bindVars, listVars, buildStreamComment)
result, err := qe.executeSql(logStats, conn, sql, true)
if err != nil {
panic(err)
}
return result
}
func (qe *QueryEngine) fullStreamFetch(logStats *sqlQueryStats, conn PoolConnection, parsed_query *sqlparser.ParsedQuery, bindVars map[string]interface{}, listVars []sqltypes.Value, buildStreamComment []byte, callback func(*mproto.QueryResult) error) {
sql := qe.generateFinalSql(parsed_query, bindVars, listVars, buildStreamComment)
qe.executeStreamSql(logStats, conn, sql, callback)
}
func (qe *QueryEngine) generateFinalSql(parsed_query *sqlparser.ParsedQuery, bindVars map[string]interface{}, listVars []sqltypes.Value, buildStreamComment []byte) string {
bindVars[MAX_RESULT_NAME] = qe.maxResultSize.Get() + 1
sql, err := parsed_query.GenerateQuery(bindVars, listVars)
if err != nil {
panic(NewTabletError(FAIL, "%s", err))
}
if buildStreamComment != nil {
sql = append(sql, buildStreamComment...)
}
// undo hack done by stripTrailing
sql = restoreTrailing(sql, bindVars)
return hack.String(sql)
}
func (qe *QueryEngine) executeSql(logStats *sqlQueryStats, conn PoolConnection, sql string, wantfields bool) (*mproto.QueryResult, error) {
connid := conn.Id()
qe.activePool.Put(connid)
defer qe.activePool.Remove(connid)
logStats.QuerySources |= QUERY_SOURCE_MYSQL
logStats.NumberOfQueries += 1
logStats.AddRewrittenSql(sql)
// NOTE(szopa): I am not doing this measurement inside
// conn.ExecuteFetch because that would require changing the
// PoolConnection interface. Same applies to executeStreamSql.
fetchStart := time.Now()
result, err := conn.ExecuteFetch(sql, int(qe.maxResultSize.Get()), wantfields)
logStats.MysqlResponseTime += time.Now().Sub(fetchStart)
if err != nil {
return nil, NewTabletErrorSql(FAIL, err)
}
return result, nil
}
func (qe *QueryEngine) executeStreamSql(logStats *sqlQueryStats, conn PoolConnection, sql string, callback func(*mproto.QueryResult) error) {
logStats.QuerySources |= QUERY_SOURCE_MYSQL
logStats.NumberOfQueries += 1
logStats.AddRewrittenSql(sql)
fetchStart := time.Now()
err := conn.ExecuteStreamFetch(sql, callback, int(qe.streamBufferSize.Get()))
logStats.MysqlResponseTime += time.Now().Sub(fetchStart)
if err != nil {
panic(NewTabletErrorSql(FAIL, err))
}
}
func rowsAreEqual(row1, row2 []sqltypes.Value) bool {
if len(row1) != len(row2) {
return false
}
for i := 0; i < len(row1); i++ {
if row1[i].IsNull() && row2[i].IsNull() {
continue
}
if (row1[i].IsNull() && !row2[i].IsNull()) || (!row1[i].IsNull() && row2[i].IsNull()) || row1[i].String() != row2[i].String() {
return false
}
}
return true
}