-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
query_engine.go
678 lines (585 loc) · 22.4 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
/*
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 tabletserver
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"sync"
"sync/atomic"
"time"
"vitess.io/vitess/go/acl"
"vitess.io/vitess/go/cache/theine"
"vitess.io/vitess/go/mysql/sqlerror"
"vitess.io/vitess/go/pools"
"vitess.io/vitess/go/stats"
"vitess.io/vitess/go/streamlog"
"vitess.io/vitess/go/sync2"
"vitess.io/vitess/go/trace"
"vitess.io/vitess/go/vt/dbconnpool"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logutil"
"vitess.io/vitess/go/vt/proto/topodata"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/tableacl"
tacl "vitess.io/vitess/go/vt/tableacl/acl"
"vitess.io/vitess/go/vt/vterrors"
"vitess.io/vitess/go/vt/vthash"
"vitess.io/vitess/go/vt/vttablet/tabletserver/connpool"
"vitess.io/vitess/go/vt/vttablet/tabletserver/planbuilder"
"vitess.io/vitess/go/vt/vttablet/tabletserver/rules"
"vitess.io/vitess/go/vt/vttablet/tabletserver/schema"
"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
"vitess.io/vitess/go/vt/vttablet/tabletserver/txserializer"
)
// _______________________________________________
// TabletPlan wraps the planbuilder's exec plan to enforce additional rules
// and track stats.
type TabletPlan struct {
*planbuilder.Plan
Original string
Rules *rules.Rules
Authorized []*tableacl.ACLResult
QueryCount uint64
Time uint64
MysqlTime uint64
RowsAffected uint64
RowsReturned uint64
ErrorCount uint64
}
// AddStats updates the stats for the current TabletPlan.
func (ep *TabletPlan) AddStats(queryCount uint64, duration, mysqlTime time.Duration, rowsAffected, rowsReturned, errorCount uint64) {
atomic.AddUint64(&ep.QueryCount, queryCount)
atomic.AddUint64(&ep.Time, uint64(duration))
atomic.AddUint64(&ep.MysqlTime, uint64(mysqlTime))
atomic.AddUint64(&ep.RowsAffected, rowsAffected)
atomic.AddUint64(&ep.RowsReturned, rowsReturned)
atomic.AddUint64(&ep.ErrorCount, errorCount)
}
// Stats returns the current stats of TabletPlan.
func (ep *TabletPlan) Stats() (queryCount uint64, duration, mysqlTime time.Duration, rowsAffected, rowsReturned, errorCount uint64) {
queryCount = atomic.LoadUint64(&ep.QueryCount)
duration = time.Duration(atomic.LoadUint64(&ep.Time))
mysqlTime = time.Duration(atomic.LoadUint64(&ep.MysqlTime))
rowsAffected = atomic.LoadUint64(&ep.RowsAffected)
rowsReturned = atomic.LoadUint64(&ep.RowsReturned)
errorCount = atomic.LoadUint64(&ep.ErrorCount)
return
}
// buildAuthorized builds 'Authorized', which is the runtime part for 'Permissions'.
func (ep *TabletPlan) buildAuthorized() {
ep.Authorized = make([]*tableacl.ACLResult, len(ep.Permissions))
for i, perm := range ep.Permissions {
ep.Authorized[i] = tableacl.Authorized(perm.TableName, perm.Role)
}
}
func (ep *TabletPlan) IsValid(hasReservedCon, hasSysSettings bool) error {
if !ep.NeedsReservedConn {
return nil
}
return isValid(ep.PlanID, hasReservedCon, hasSysSettings)
}
func isValid(planType planbuilder.PlanType, hasReservedCon bool, hasSysSettings bool) error {
switch planType {
case planbuilder.PlanSelectLockFunc, planbuilder.PlanDDL:
if hasReservedCon {
return nil
}
case planbuilder.PlanSet:
if hasReservedCon || hasSysSettings {
return nil
}
}
return vterrors.Errorf(vtrpcpb.Code_FAILED_PRECONDITION, "%s not allowed without reserved connection", planType.String())
}
// _______________________________________________
type PlanCacheKey = theine.StringKey
type PlanCache = theine.Store[PlanCacheKey, *TabletPlan]
type SettingsCacheKey = theine.HashKey256
type SettingsCache = theine.Store[SettingsCacheKey, *pools.Setting]
type currentSchema struct {
tables map[string]*schema.Table
epoch uint32
}
// QueryEngine implements the core functionality of tabletserver.
// It assumes that no requests will be sent to it before Open is
// called and succeeds.
// Shutdown is done in the following order:
//
// Close: There should be no more pending queries when this
// function is called.
type QueryEngine struct {
isOpen atomic.Bool
env tabletenv.Env
se *schema.Engine
// mu protects the following fields.
schemaMu sync.Mutex
epoch uint32
schema atomic.Pointer[currentSchema]
plans *PlanCache
settings *SettingsCache
queryRuleSources *rules.Map
// Pools
conns *connpool.Pool
streamConns *connpool.Pool
// Services
consolidator sync2.Consolidator
streamConsolidator *StreamConsolidator
// txSerializer protects vttablet from applications which try to concurrently
// UPDATE (or DELETE) a "hot" row (or range of rows).
// Such queries would be serialized by MySQL anyway. This serializer prevents
// that we start more than one transaction per hot row (range).
// For implementation details, please see BeginExecute() in tabletserver.go.
txSerializer *txserializer.TxSerializer
// Vars
maxResultSize atomic.Int64
warnResultSize atomic.Int64
streamBufferSize atomic.Int64
// tableaclExemptCount count the number of accesses allowed
// based on membership in the superuser ACL
tableaclExemptCount atomic.Int64
strictTableACL bool
enableTableACLDryRun bool
// TODO(sougou) There are two acl packages. Need to rename.
exemptACL tacl.ACL
strictTransTables bool
consolidatorMode atomic.Value
// stats
// Note: queryErrorCountsWithCode is similar to queryErrorCounts except it contains error code as an additional dimension
queryCounts, queryCountsWithTabletType, queryTimes, queryErrorCounts, queryErrorCountsWithCode, queryRowsAffected, queryRowsReturned *stats.CountersWithMultiLabels
// stats flags
enablePerWorkloadTableMetrics bool
// Loggers
accessCheckerLogger *logutil.ThrottledLogger
}
// NewQueryEngine creates a new QueryEngine.
// This is a singleton class.
// You must call this only once.
func NewQueryEngine(env tabletenv.Env, se *schema.Engine) *QueryEngine {
config := env.Config()
qe := &QueryEngine{
env: env,
se: se,
queryRuleSources: rules.NewMap(),
enablePerWorkloadTableMetrics: config.EnablePerWorkloadTableMetrics,
}
// Cache for query plans: user configured size with a doorkeeper by default to prevent one-off queries
// from thrashing the cache.
qe.plans = theine.NewStore[PlanCacheKey, *TabletPlan](config.QueryCacheMemory, config.QueryCacheDoorkeeper)
// cache for connection settings: default to 1/4th of the size for the query cache and do
// not use a doorkeeper because custom connection settings are rarely one-off and we always
// want to cache them
var settingsCacheMemory = config.QueryCacheMemory / 4
qe.settings = theine.NewStore[SettingsCacheKey, *pools.Setting](settingsCacheMemory, false)
qe.schema.Store(¤tSchema{
tables: make(map[string]*schema.Table),
epoch: 0,
})
qe.conns = connpool.NewPool(env, "ConnPool", config.OltpReadPool)
qe.streamConns = connpool.NewPool(env, "StreamConnPool", config.OlapReadPool)
qe.consolidatorMode.Store(config.Consolidator)
qe.consolidator = sync2.NewConsolidator()
if config.ConsolidatorStreamTotalSize > 0 && config.ConsolidatorStreamQuerySize > 0 {
log.Infof("Stream consolidator is enabled with query size set to %d and total size set to %d.",
config.ConsolidatorStreamQuerySize, config.ConsolidatorStreamTotalSize)
qe.streamConsolidator = NewStreamConsolidator(config.ConsolidatorStreamTotalSize, config.ConsolidatorStreamQuerySize, returnStreamResult)
} else {
log.Info("Stream consolidator is not enabled.")
}
qe.txSerializer = txserializer.New(env)
qe.strictTableACL = config.StrictTableACL
qe.enableTableACLDryRun = config.EnableTableACLDryRun
qe.strictTransTables = config.EnforceStrictTransTables
if config.TableACLExemptACL != "" {
if f, err := tableacl.GetCurrentACLFactory(); err == nil {
if exemptACL, err := f.New([]string{config.TableACLExemptACL}); err == nil {
log.Infof("Setting Table ACL exempt rule for %v", config.TableACLExemptACL)
qe.exemptACL = exemptACL
} else {
log.Infof("Cannot build exempt ACL for table ACL: %v", err)
}
} else {
log.Infof("Cannot get current ACL Factory: %v", err)
}
}
qe.maxResultSize.Store(int64(config.Oltp.MaxRows))
qe.warnResultSize.Store(int64(config.Oltp.WarnRows))
qe.streamBufferSize.Store(int64(config.StreamBufferSize))
planbuilder.PassthroughDMLs = config.PassthroughDML
qe.accessCheckerLogger = logutil.NewThrottledLogger("accessChecker", 1*time.Second)
env.Exporter().NewGaugeFunc("MaxResultSize", "Query engine max result size", qe.maxResultSize.Load)
env.Exporter().NewGaugeFunc("WarnResultSize", "Query engine warn result size", qe.warnResultSize.Load)
env.Exporter().NewGaugeFunc("StreamBufferSize", "Query engine stream buffer size", qe.streamBufferSize.Load)
env.Exporter().NewCounterFunc("TableACLExemptCount", "Query engine table ACL exempt count", qe.tableaclExemptCount.Load)
env.Exporter().NewGaugeFunc("QueryCacheLength", "Query engine query cache length", func() int64 {
return int64(qe.plans.Len())
})
env.Exporter().NewGaugeFunc("QueryCacheSize", "Query engine query cache size", func() int64 {
return int64(qe.plans.UsedCapacity())
})
env.Exporter().NewGaugeFunc("QueryCacheCapacity", "Query engine query cache capacity", func() int64 {
return int64(qe.plans.MaxCapacity())
})
env.Exporter().NewCounterFunc("QueryCacheEvictions", "Query engine query cache evictions", func() int64 {
return qe.plans.Metrics.Evicted()
})
labels := []string{"Table", "Plan"}
if config.EnablePerWorkloadTableMetrics {
labels = []string{"Table", "Plan", "Workload"}
}
qe.queryCounts = env.Exporter().NewCountersWithMultiLabels("QueryCounts", "query counts", labels)
qe.queryCountsWithTabletType = env.Exporter().NewCountersWithMultiLabels("QueryCountsWithTabletType", "query counts with tablet type labels", []string{"Table", "Plan", "TabletType"})
qe.queryTimes = env.Exporter().NewCountersWithMultiLabels("QueryTimesNs", "query times in ns", labels)
qe.queryRowsAffected = env.Exporter().NewCountersWithMultiLabels("QueryRowsAffected", "query rows affected", labels)
qe.queryRowsReturned = env.Exporter().NewCountersWithMultiLabels("QueryRowsReturned", "query rows returned", labels)
qe.queryErrorCounts = env.Exporter().NewCountersWithMultiLabels("QueryErrorCounts", "query error counts", labels)
qe.queryErrorCountsWithCode = env.Exporter().NewCountersWithMultiLabels("QueryErrorCountsWithCode", "query error counts with error code", []string{"Table", "Plan", "Code"})
env.Exporter().HandleFunc("/debug/hotrows", qe.txSerializer.ServeHTTP)
env.Exporter().HandleFunc("/debug/tablet_plans", qe.handleHTTPQueryPlans)
env.Exporter().HandleFunc("/debug/query_stats", qe.handleHTTPQueryStats)
env.Exporter().HandleFunc("/debug/query_rules", qe.handleHTTPQueryRules)
env.Exporter().HandleFunc("/debug/consolidations", qe.handleHTTPConsolidations)
env.Exporter().HandleFunc("/debug/acl", qe.handleHTTPAclJSON)
return qe
}
// Open must be called before sending requests to QueryEngine.
func (qe *QueryEngine) Open() error {
if qe.isOpen.Load() {
return nil
}
log.Info("Query Engine: opening")
config := qe.env.Config()
qe.conns.Open(config.DB.AppWithDB(), config.DB.DbaWithDB(), config.DB.AppDebugWithDB())
conn, err := qe.conns.Get(tabletenv.LocalContext(), nil)
if err != nil {
qe.conns.Close()
return err
}
err = conn.VerifyMode(qe.strictTransTables)
// Recycle needs to happen before error check.
// Otherwise, qe.conns.Close will hang.
conn.Recycle()
if err != nil {
qe.conns.Close()
return err
}
qe.streamConns.Open(config.DB.AppWithDB(), config.DB.DbaWithDB(), config.DB.AppDebugWithDB())
qe.se.RegisterNotifier("qe", qe.schemaChanged, true)
qe.plans.EnsureOpen()
qe.settings.EnsureOpen()
qe.isOpen.Store(true)
return nil
}
// Close must be called to shut down QueryEngine.
// You must ensure that no more queries will be sent
// before calling Close.
func (qe *QueryEngine) Close() {
if !qe.isOpen.Swap(false) {
return
}
// Close in reverse order of Open.
qe.se.UnregisterNotifier("qe")
qe.plans.Close()
qe.settings.Close()
qe.streamConns.Close()
qe.conns.Close()
log.Info("Query Engine: closed")
}
var errNoCache = errors.New("plan should not be cached")
func (qe *QueryEngine) getPlan(curSchema *currentSchema, sql string) (*TabletPlan, error) {
statement, err := sqlparser.Parse(sql)
if err != nil {
return nil, err
}
splan, err := planbuilder.Build(statement, curSchema.tables, qe.env.Config().DB.DBName, qe.env.Config().EnableViews)
if err != nil {
return nil, err
}
plan := &TabletPlan{Plan: splan, Original: sql}
plan.Rules = qe.queryRuleSources.FilterByPlan(sql, plan.PlanID, plan.TableNames()...)
plan.buildAuthorized()
if plan.PlanID == planbuilder.PlanDDL || plan.PlanID == planbuilder.PlanSet || sqlparser.SkipQueryPlanCacheDirective(statement) {
return plan, errNoCache
}
return plan, nil
}
// GetPlan returns the TabletPlan that for the query. Plans are cached in a cache.LRUCache.
func (qe *QueryEngine) GetPlan(ctx context.Context, logStats *tabletenv.LogStats, sql string, skipQueryPlanCache bool) (*TabletPlan, error) {
span, _ := trace.NewSpan(ctx, "QueryEngine.GetPlan")
defer span.Finish()
var plan *TabletPlan
var err error
curSchema := qe.schema.Load()
if skipQueryPlanCache {
plan, err = qe.getPlan(curSchema, sql)
} else {
plan, logStats.CachedPlan, err = qe.plans.GetOrLoad(PlanCacheKey(sql), curSchema.epoch, func() (*TabletPlan, error) {
return qe.getPlan(curSchema, sql)
})
}
if errors.Is(err, errNoCache) {
err = nil
}
return plan, err
}
// GetStreamPlan is similar to GetPlan, but doesn't use the cache
// and doesn't enforce a limit. It just returns the parsed query.
func (qe *QueryEngine) GetStreamPlan(sql string) (*TabletPlan, error) {
splan, err := planbuilder.BuildStreaming(sql, qe.schema.Load().tables)
if err != nil {
return nil, err
}
plan := &TabletPlan{Plan: splan, Original: sql}
plan.Rules = qe.queryRuleSources.FilterByPlan(sql, plan.PlanID, plan.TableName().String())
plan.buildAuthorized()
return plan, nil
}
// GetMessageStreamPlan builds a plan for Message streaming.
func (qe *QueryEngine) GetMessageStreamPlan(name string) (*TabletPlan, error) {
splan, err := planbuilder.BuildMessageStreaming(name, qe.schema.Load().tables)
if err != nil {
return nil, err
}
plan := &TabletPlan{Plan: splan}
plan.Rules = qe.queryRuleSources.FilterByPlan("stream from "+name, plan.PlanID, plan.TableName().String())
plan.buildAuthorized()
return plan, nil
}
// GetConnSetting returns system settings for the connection.
func (qe *QueryEngine) GetConnSetting(ctx context.Context, settings []string) (*pools.Setting, error) {
span, _ := trace.NewSpan(ctx, "QueryEngine.GetConnSetting")
defer span.Finish()
hasher := vthash.New256()
for _, q := range settings {
_, _ = hasher.WriteString(q)
}
var cacheKey SettingsCacheKey
hasher.Sum(cacheKey[:0])
connSetting, _, err := qe.settings.GetOrLoad(cacheKey, 0, func() (*pools.Setting, error) {
// build the setting queries
query, resetQuery, err := planbuilder.BuildSettingQuery(settings)
if err != nil {
return nil, err
}
return pools.NewSetting(query, resetQuery), nil
})
return connSetting, err
}
// ClearQueryPlanCache should be called if query plan cache is potentially obsolete
func (qe *QueryEngine) ClearQueryPlanCache() {
qe.schemaMu.Lock()
defer qe.schemaMu.Unlock()
qe.epoch++
current := qe.schema.Load()
qe.schema.Store(¤tSchema{
tables: current.tables,
epoch: qe.epoch,
})
}
func (qe *QueryEngine) ForEachPlan(each func(plan *TabletPlan) bool) {
curSchema := qe.schema.Load()
qe.plans.Range(curSchema.epoch, func(_ PlanCacheKey, plan *TabletPlan) bool {
return each(plan)
})
}
// IsMySQLReachable returns an error if it cannot connect to MySQL.
// This can be called before opening the QueryEngine.
func (qe *QueryEngine) IsMySQLReachable() error {
conn, err := dbconnpool.NewDBConnection(context.TODO(), qe.env.Config().DB.AppWithDB())
if err != nil {
if sqlerror.IsTooManyConnectionsErr(err) {
return nil
}
return err
}
conn.Close()
return nil
}
func (qe *QueryEngine) schemaChanged(tables map[string]*schema.Table, created, altered, dropped []*schema.Table) {
qe.schemaMu.Lock()
defer qe.schemaMu.Unlock()
if len(altered) != 0 || len(dropped) != 0 {
qe.epoch++
}
qe.schema.Store(¤tSchema{
tables: tables,
epoch: qe.epoch,
})
}
// QueryPlanCacheCap returns the capacity of the query cache.
func (qe *QueryEngine) QueryPlanCacheCap() int {
return qe.plans.MaxCapacity()
}
// QueryPlanCacheLen returns the length (size in entries) of the query cache
func (qe *QueryEngine) QueryPlanCacheLen() (count int) {
qe.ForEachPlan(func(plan *TabletPlan) bool {
count++
return true
})
return
}
// AddStats adds the given stats for the planName.tableName
func (qe *QueryEngine) AddStats(planType planbuilder.PlanType, tableName, workload string, tabletType topodata.TabletType, queryCount int64, duration, mysqlTime time.Duration, rowsAffected, rowsReturned, errorCount int64, errorCode string) {
// table names can contain "." characters, replace them!
keys := []string{tableName, planType.String()}
// Only use the workload as a label if that's enabled in the configuration.
if qe.enablePerWorkloadTableMetrics {
keys = append(keys, workload)
}
qe.queryCounts.Add(keys, queryCount)
qe.queryTimes.Add(keys, int64(duration))
qe.queryErrorCounts.Add(keys, errorCount)
qe.queryCountsWithTabletType.Add([]string{tableName, planType.String(), tabletType.String()}, queryCount)
// queryErrorCountsWithCode is similar to queryErrorCounts except we have an additional dimension
// of error code.
if errorCount > 0 {
errorKeys := []string{tableName, planType.String(), errorCode}
qe.queryErrorCountsWithCode.Add(errorKeys, errorCount)
}
// For certain plan types like select, we only want to add their metrics to rows returned
// But there are special cases like `SELECT ... INTO OUTFILE ''` which return positive rows affected
// So we check if it is positive and add that too.
switch planType {
case planbuilder.PlanSelect, planbuilder.PlanSelectStream, planbuilder.PlanSelectImpossible, planbuilder.PlanShow, planbuilder.PlanOtherRead:
qe.queryRowsReturned.Add(keys, rowsReturned)
if rowsAffected > 0 {
qe.queryRowsAffected.Add(keys, rowsAffected)
}
default:
qe.queryRowsAffected.Add(keys, rowsAffected)
if rowsReturned > 0 {
qe.queryRowsReturned.Add(keys, rowsReturned)
}
}
}
type perQueryStats struct {
Query string
Table string
Plan planbuilder.PlanType
QueryCount uint64
Time time.Duration
MysqlTime time.Duration
RowsAffected uint64
RowsReturned uint64
ErrorCount uint64
}
func (qe *QueryEngine) handleHTTPQueryPlans(response http.ResponseWriter, request *http.Request) {
if err := acl.CheckAccessHTTP(request, acl.DEBUGGING); err != nil {
acl.SendError(response, err)
return
}
response.Header().Set("Content-Type", "text/plain")
qe.ForEachPlan(func(plan *TabletPlan) bool {
response.Write([]byte(fmt.Sprintf("%#v\n", sqlparser.TruncateForUI(plan.Original))))
if b, err := json.MarshalIndent(plan.Plan, "", " "); err != nil {
response.Write([]byte(err.Error()))
} else {
response.Write(b)
}
response.Write(([]byte)("\n\n"))
return true
})
}
func (qe *QueryEngine) handleHTTPQueryStats(response http.ResponseWriter, request *http.Request) {
if err := acl.CheckAccessHTTP(request, acl.DEBUGGING); err != nil {
acl.SendError(response, err)
return
}
response.Header().Set("Content-Type", "application/json; charset=utf-8")
var qstats []perQueryStats
qe.ForEachPlan(func(plan *TabletPlan) bool {
var pqstats perQueryStats
pqstats.Query = unicoded(sqlparser.TruncateForUI(plan.Original))
pqstats.Table = plan.TableName().String()
pqstats.Plan = plan.PlanID
pqstats.QueryCount, pqstats.Time, pqstats.MysqlTime, pqstats.RowsAffected, pqstats.RowsReturned, pqstats.ErrorCount = plan.Stats()
qstats = append(qstats, pqstats)
return true
})
if b, err := json.MarshalIndent(qstats, "", " "); err != nil {
response.Write([]byte(err.Error()))
} else {
response.Write(b)
}
}
func (qe *QueryEngine) handleHTTPQueryRules(response http.ResponseWriter, request *http.Request) {
if err := acl.CheckAccessHTTP(request, acl.DEBUGGING); err != nil {
acl.SendError(response, err)
return
}
response.Header().Set("Content-Type", "application/json; charset=utf-8")
b, err := json.MarshalIndent(qe.queryRuleSources, "", " ")
if err != nil {
response.Write([]byte(err.Error()))
return
}
buf := bytes.NewBuffer(nil)
json.HTMLEscape(buf, b)
response.Write(buf.Bytes())
}
func (qe *QueryEngine) handleHTTPAclJSON(response http.ResponseWriter, request *http.Request) {
if err := acl.CheckAccessHTTP(request, acl.DEBUGGING); err != nil {
acl.SendError(response, err)
return
}
aclConfig := tableacl.GetCurrentConfig()
if aclConfig == nil {
response.WriteHeader(http.StatusNotFound)
return
}
response.Header().Set("Content-Type", "application/json; charset=utf-8")
b, err := json.MarshalIndent(aclConfig, "", " ")
if err != nil {
response.Write([]byte(err.Error()))
return
}
buf := bytes.NewBuffer(nil)
json.HTMLEscape(buf, b)
response.Write(buf.Bytes())
}
// ServeHTTP lists the most recent, cached queries and their count.
func (qe *QueryEngine) handleHTTPConsolidations(response http.ResponseWriter, request *http.Request) {
if err := acl.CheckAccessHTTP(request, acl.DEBUGGING); err != nil {
acl.SendError(response, err)
return
}
items := qe.consolidator.Items()
response.Header().Set("Content-Type", "text/plain")
if items == nil {
response.Write([]byte("empty\n"))
return
}
response.Write([]byte(fmt.Sprintf("Length: %d\n", len(items))))
for _, v := range items {
var query string
if streamlog.GetRedactDebugUIQueries() {
query, _ = sqlparser.RedactSQLQuery(v.Query)
} else {
query = v.Query
}
response.Write([]byte(fmt.Sprintf("%v: %s\n", v.Count, query)))
}
}
// unicoded returns a valid UTF-8 string that json won't reject
func unicoded(in string) (out string) {
for i, v := range in {
if v == 0xFFFD {
return in[:i]
}
}
return in
}