forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
schema_info.go
725 lines (653 loc) · 22 KB
/
schema_info.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
// 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"
"encoding/json"
"fmt"
"net/http"
"strconv"
"sync"
"time"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/acl"
"github.com/youtube/vitess/go/cache"
"github.com/youtube/vitess/go/sqldb"
"github.com/youtube/vitess/go/sqltypes"
"github.com/youtube/vitess/go/stats"
"github.com/youtube/vitess/go/timer"
"github.com/youtube/vitess/go/trace"
"github.com/youtube/vitess/go/vt/concurrency"
querypb "github.com/youtube/vitess/go/vt/proto/query"
vtrpcpb "github.com/youtube/vitess/go/vt/proto/vtrpc"
"github.com/youtube/vitess/go/vt/schema"
"github.com/youtube/vitess/go/vt/tableacl"
"github.com/youtube/vitess/go/vt/tabletserver/planbuilder"
"golang.org/x/net/context"
)
const baseShowTables = "SELECT table_name, table_type, unix_timestamp(create_time), table_comment, table_rows, data_length, index_length, data_free FROM information_schema.tables WHERE table_schema = database()"
const maxTableCount = 10000
const (
debugQueryPlansKey = "query_plans"
debugQueryStatsKey = "query_stats"
debugSchemaKey = "schema"
debugQueryRulesKey = "query_rules"
)
// ExecPlan wraps the planbuilder's exec plan to enforce additional rules
// and track stats.
type ExecPlan struct {
*planbuilder.ExecPlan
TableInfo *TableInfo
Fields []*querypb.Field
Rules *QueryRules
Authorized *tableacl.ACLResult
mu sync.Mutex
QueryCount int64
Time time.Duration
RowCount int64
ErrorCount int64
}
// Size allows ExecPlan to be in cache.LRUCache.
func (*ExecPlan) Size() int {
return 1
}
// AddStats updates the stats for the current ExecPlan.
func (ep *ExecPlan) AddStats(queryCount int64, duration time.Duration, rowCount, errorCount int64) {
ep.mu.Lock()
ep.QueryCount += queryCount
ep.Time += duration
ep.RowCount += rowCount
ep.ErrorCount += errorCount
ep.mu.Unlock()
}
// Stats returns the current stats of ExecPlan.
func (ep *ExecPlan) Stats() (queryCount int64, duration time.Duration, rowCount, errorCount int64) {
ep.mu.Lock()
queryCount = ep.QueryCount
duration = ep.Time
rowCount = ep.RowCount
errorCount = ep.ErrorCount
ep.mu.Unlock()
return
}
// SchemaInfo stores the schema info and performs operations that
// keep itself up-to-date.
type SchemaInfo struct {
mu sync.Mutex
tables map[string]*TableInfo
lastChange int64
reloadTime time.Duration
// actionMutex serializes all calls to state-altering methods:
// Open, Close, Reload, DropTable, CreateOrUpdateTable.
actionMutex sync.Mutex
// The following vars are either read-only or have
// their own synchronization.
queries *cache.LRUCache
connPool *ConnPool
ticks *timer.Timer
endpoints map[string]string
queryRuleSources *QueryRuleInfo
queryServiceStats *QueryServiceStats
}
// NewSchemaInfo creates a new SchemaInfo.
func NewSchemaInfo(
statsPrefix string,
checker MySQLChecker,
queryCacheSize int,
reloadTime time.Duration,
idleTimeout time.Duration,
endpoints map[string]string,
enablePublishStats bool,
queryServiceStats *QueryServiceStats) *SchemaInfo {
si := &SchemaInfo{
queries: cache.NewLRUCache(int64(queryCacheSize)),
connPool: NewConnPool("", 3, idleTimeout, enablePublishStats, queryServiceStats, checker),
ticks: timer.NewTimer(reloadTime),
endpoints: endpoints,
reloadTime: reloadTime,
queryRuleSources: NewQueryRuleInfo(),
queryServiceStats: queryServiceStats,
}
if enablePublishStats {
stats.Publish(statsPrefix+"QueryCacheLength", stats.IntFunc(si.queries.Length))
stats.Publish(statsPrefix+"QueryCacheSize", stats.IntFunc(si.queries.Size))
stats.Publish(statsPrefix+"QueryCacheCapacity", stats.IntFunc(si.queries.Capacity))
stats.Publish(statsPrefix+"QueryCacheOldest", stats.StringFunc(func() string {
return fmt.Sprintf("%v", si.queries.Oldest())
}))
stats.Publish(statsPrefix+"SchemaReloadTime", stats.DurationFunc(si.ticks.Interval))
_ = stats.NewMultiCountersFunc(statsPrefix+"QueryCounts", []string{"Table", "Plan"}, si.getQueryCount)
_ = stats.NewMultiCountersFunc(statsPrefix+"QueryTimesNs", []string{"Table", "Plan"}, si.getQueryTime)
_ = stats.NewMultiCountersFunc(statsPrefix+"QueryRowCounts", []string{"Table", "Plan"}, si.getQueryRowCount)
_ = stats.NewMultiCountersFunc(statsPrefix+"QueryErrorCounts", []string{"Table", "Plan"}, si.getQueryErrorCount)
_ = stats.NewMultiCountersFunc(statsPrefix+"TableRows", []string{"Table"}, si.getTableRows)
_ = stats.NewMultiCountersFunc(statsPrefix+"DataLength", []string{"Table"}, si.getDataLength)
_ = stats.NewMultiCountersFunc(statsPrefix+"IndexLength", []string{"Table"}, si.getIndexLength)
_ = stats.NewMultiCountersFunc(statsPrefix+"DataFree", []string{"Table"}, si.getDataFree)
}
for _, ep := range endpoints {
http.Handle(ep, si)
}
return si
}
// Open initializes the current SchemaInfo for service by loading the necessary info from the specified database.
func (si *SchemaInfo) Open(dbaParams *sqldb.ConnParams, strictMode bool) {
si.actionMutex.Lock()
defer si.actionMutex.Unlock()
ctx := context.Background()
si.connPool.Open(dbaParams, dbaParams)
// Get time first because it needs a connection from the pool.
curTime := si.mysqlTime(ctx)
conn := getOrPanic(ctx, si.connPool)
defer conn.Recycle()
if strictMode {
if err := conn.VerifyMode(); err != nil {
panic(NewTabletError(vtrpcpb.ErrorCode_INTERNAL_ERROR, err.Error()))
}
}
tableData, err := conn.Exec(ctx, baseShowTables, maxTableCount, false)
if err != nil {
panic(PrefixTabletError(vtrpcpb.ErrorCode_INTERNAL_ERROR, err, "Could not get table list: "))
}
tables := make(map[string]*TableInfo, len(tableData.Rows)+1)
tables["dual"] = &TableInfo{Table: schema.NewTable("dual")}
wg := sync.WaitGroup{}
mu := sync.Mutex{}
for _, row := range tableData.Rows {
wg.Add(1)
go func(row []sqltypes.Value) {
defer wg.Done()
conn := getOrPanic(ctx, si.connPool)
defer conn.Recycle()
tableName := row[0].String()
tableInfo, err := NewTableInfo(
conn,
tableName,
row[1].String(), // table_type
row[3].String(), // table_comment
)
if err != nil {
si.queryServiceStats.InternalErrors.Add("Schema", 1)
log.Errorf("SchemaInfo.Open: failed to create TableInfo for table %s: %v", tableName, err)
// Skip over the table that had an error and move on to the next one
return
}
tableInfo.SetMysqlStats(row[4], row[5], row[6], row[7])
mu.Lock()
tables[tableName] = tableInfo
mu.Unlock()
}(row)
}
wg.Wait()
// Fail if we can't load the schema for any tables, but we know that some tables exist. This points to a configuration problem.
if len(tableData.Rows) != 0 && len(tables) == 1 { // len(tables) is always at least 1 because of the "dual" table
panic(NewTabletError(vtrpcpb.ErrorCode_INTERNAL_ERROR, "could not get schema for any tables"))
}
func() {
si.mu.Lock()
defer si.mu.Unlock()
si.tables = tables
si.lastChange = curTime
}()
// Clear is not really needed. Doing it for good measure.
si.queries.Clear()
si.ticks.Start(func() {
if err := si.Reload(ctx); err != nil {
log.Errorf("periodic schema reload failed: %v", err)
}
})
}
// Close shuts down SchemaInfo. It can be re-opened after Close.
func (si *SchemaInfo) Close() {
// Make sure there are no reloads going on, as they depend on SchemaInfo
// staying open during the reload.
si.actionMutex.Lock()
defer si.actionMutex.Unlock()
si.ticks.Stop()
si.connPool.Close()
si.queries.Clear()
si.mu.Lock()
defer si.mu.Unlock()
si.tables = nil
}
// IsClosed returns true if the SchemaInfo is closed.
func (si *SchemaInfo) IsClosed() bool {
si.mu.Lock()
defer si.mu.Unlock()
return si.tables == nil
}
// Reload reloads the schema info from the db.
// Any tables that have changed since the last load are updated.
// This is a no-op if the SchemaInfo is closed.
func (si *SchemaInfo) Reload(ctx context.Context) error {
defer logError(si.queryServiceStats)
// Reload() gets called both from the ticker, and from external RPCs.
// We don't want them to race over writing data that was read concurrently.
si.actionMutex.Lock()
defer si.actionMutex.Unlock()
if si.IsClosed() {
return nil
}
// Get time first because it needs a connection from the pool.
curTime := si.mysqlTime(ctx)
var tableData *sqltypes.Result
var err error
func() {
conn := getOrPanic(ctx, si.connPool)
defer conn.Recycle()
tableData, err = conn.Exec(ctx, baseShowTables, maxTableCount, false)
}()
if err != nil {
return fmt.Errorf("could not get table list for reload: %v", err)
}
// Reload any tables that have changed. We try every table even if some fail,
// but we return success only if all tables succeed.
// The following section requires us to hold mu.
errs := &concurrency.AllErrorRecorder{}
si.mu.Lock()
defer si.mu.Unlock()
for _, row := range tableData.Rows {
tableName := row[0].String()
createTime, _ := row[2].ParseInt64()
// Check if we know about the table or it has been recreated.
if _, ok := si.tables[tableName]; !ok || createTime >= si.lastChange {
func() {
// Unlock so CreateOrUpdateTable can lock.
si.mu.Unlock()
defer si.mu.Lock()
log.Infof("Reloading schema for table: %s", tableName)
errs.RecordError(si.createOrUpdateTableLocked(ctx, tableName))
}()
continue
}
// Only update table_rows, data_length, index_length
si.tables[tableName].SetMysqlStats(row[4], row[5], row[6], row[7])
}
si.lastChange = curTime
return errs.Error()
}
func (si *SchemaInfo) mysqlTime(ctx context.Context) int64 {
conn := getOrPanic(ctx, si.connPool)
defer conn.Recycle()
tm, err := conn.Exec(ctx, "select unix_timestamp()", 1, false)
if err != nil {
panic(PrefixTabletError(vtrpcpb.ErrorCode_UNKNOWN_ERROR, err, "Could not get MySQL time: "))
}
if len(tm.Rows) != 1 || len(tm.Rows[0]) != 1 || tm.Rows[0][0].IsNull() {
panic(NewTabletError(vtrpcpb.ErrorCode_UNKNOWN_ERROR, "Unexpected result for MySQL time: %+v", tm.Rows))
}
t, err := strconv.ParseInt(tm.Rows[0][0].String(), 10, 64)
if err != nil {
panic(NewTabletError(vtrpcpb.ErrorCode_UNKNOWN_ERROR, "Could not parse time %+v: %v", tm, err))
}
return t
}
// ClearQueryPlanCache should be called if query plan cache is potentially obsolete
func (si *SchemaInfo) ClearQueryPlanCache() {
si.queries.Clear()
}
// CreateOrUpdateTable must be called if a DDL was applied to that table.
func (si *SchemaInfo) CreateOrUpdateTable(ctx context.Context, tableName string) error {
si.actionMutex.Lock()
defer si.actionMutex.Unlock()
return si.createOrUpdateTableLocked(ctx, tableName)
}
// createOrUpdateTableLocked must only be called while holding actionMutex.
func (si *SchemaInfo) createOrUpdateTableLocked(ctx context.Context, tableName string) error {
conn := getOrPanic(ctx, si.connPool)
defer conn.Recycle()
tableData, err := conn.Exec(ctx, fmt.Sprintf("%s and table_name = '%s'", baseShowTables, tableName), 1, false)
if err != nil {
si.queryServiceStats.InternalErrors.Add("Schema", 1)
return PrefixTabletError(vtrpcpb.ErrorCode_INTERNAL_ERROR, err,
fmt.Sprintf("CreateOrUpdateTable: information_schema query failed for table %s: ", tableName))
}
if len(tableData.Rows) != 1 {
// This can happen if DDLs race with each other.
return nil
}
row := tableData.Rows[0]
tableInfo, err := NewTableInfo(
conn,
tableName,
row[1].String(), // table_type
row[3].String(), // table_comment
)
if err != nil {
si.queryServiceStats.InternalErrors.Add("Schema", 1)
return PrefixTabletError(vtrpcpb.ErrorCode_INTERNAL_ERROR, err,
fmt.Sprintf("CreateOrUpdateTable: failed to create TableInfo for table %s: ", tableName))
}
// table_rows, data_length, index_length
tableInfo.SetMysqlStats(row[4], row[5], row[6], row[7])
// Need to acquire lock now.
si.mu.Lock()
defer si.mu.Unlock()
if _, ok := si.tables[tableName]; ok {
// If the table already exists, we overwrite it with the latest info.
// This also means that the query cache needs to be cleared.
// Otherwise, the query plans may not be in sync with the schema.
si.queries.Clear()
log.Infof("Updating table %s", tableName)
}
si.tables[tableName] = tableInfo
switch tableInfo.Type {
case schema.NoType:
log.Infof("Initialized table: %s", tableName)
case schema.Sequence:
log.Infof("Initialized sequence: %s", tableName)
}
return nil
}
// DropTable must be called if a table was dropped.
func (si *SchemaInfo) DropTable(tableName string) {
si.actionMutex.Lock()
defer si.actionMutex.Unlock()
si.mu.Lock()
defer si.mu.Unlock()
delete(si.tables, tableName)
si.queries.Clear()
log.Infof("Table %s forgotten", tableName)
}
// GetPlan returns the ExecPlan that for the query. Plans are cached in a cache.LRUCache.
func (si *SchemaInfo) GetPlan(ctx context.Context, logStats *LogStats, sql string) *ExecPlan {
span := trace.NewSpanFromContext(ctx)
span.StartLocal("SchemaInfo.GetPlan")
defer span.Finish()
// Fastpath if plan already exists.
if plan := si.getQuery(sql); plan != nil {
return plan
}
// TODO(sougou): It's not correct to hold this lock here because the code
// below runs queries against MySQL. But if we don't hold the lock, there
// are other race conditions where identical queries will end up building
// plans and compete with populating the query cache. In other words, we
// need a more elaborate scheme that blocks less, but still prevents these
// race conditions.
si.mu.Lock()
defer si.mu.Unlock()
// Recheck. A plan might have been built by someone else.
if plan := si.getQuery(sql); plan != nil {
return plan
}
var tableInfo *TableInfo
GetTable := func(tableName string) (table *schema.Table, ok bool) {
tableInfo, ok = si.tables[tableName]
if !ok {
return nil, false
}
return tableInfo.Table, true
}
splan, err := planbuilder.GetExecPlan(sql, GetTable)
if err != nil {
panic(PrefixTabletError(vtrpcpb.ErrorCode_UNKNOWN_ERROR, err, ""))
}
plan := &ExecPlan{ExecPlan: splan, TableInfo: tableInfo}
plan.Rules = si.queryRuleSources.filterByPlan(sql, plan.PlanID, plan.TableName)
plan.Authorized = tableacl.Authorized(plan.TableName, plan.PlanID.MinRole())
if plan.PlanID.IsSelect() {
if plan.FieldQuery == nil {
log.Warningf("Cannot cache field info: %s", sql)
} else {
conn := getOrPanic(ctx, si.connPool)
defer conn.Recycle()
sql := plan.FieldQuery.Query
start := time.Now()
r, err := conn.Exec(ctx, sql, 1, true)
logStats.AddRewrittenSQL(sql, start)
if err != nil {
panic(PrefixTabletError(vtrpcpb.ErrorCode_UNKNOWN_ERROR, err, "Error fetching fields: "))
}
plan.Fields = r.Fields
}
} else if plan.PlanID == planbuilder.PlanDDL || plan.PlanID == planbuilder.PlanSet {
return plan
}
si.queries.Set(sql, plan)
return plan
}
// GetStreamPlan is similar to GetPlan, but doesn't use the cache
// and doesn't enforce a limit. It just returns the parsed query.
func (si *SchemaInfo) GetStreamPlan(sql string) *ExecPlan {
var tableInfo *TableInfo
GetTable := func(tableName string) (table *schema.Table, ok bool) {
si.mu.Lock()
defer si.mu.Unlock()
tableInfo, ok = si.tables[tableName]
if !ok {
return nil, false
}
return tableInfo.Table, true
}
splan, err := planbuilder.GetStreamExecPlan(sql, GetTable)
if err != nil {
panic(PrefixTabletError(vtrpcpb.ErrorCode_UNKNOWN_ERROR, err, ""))
}
plan := &ExecPlan{ExecPlan: splan, TableInfo: tableInfo}
plan.Rules = si.queryRuleSources.filterByPlan(sql, plan.PlanID, plan.TableName)
plan.Authorized = tableacl.Authorized(plan.TableName, plan.PlanID.MinRole())
return plan
}
// GetTable returns the TableInfo for a table.
func (si *SchemaInfo) GetTable(tableName string) *TableInfo {
si.mu.Lock()
defer si.mu.Unlock()
return si.tables[tableName]
}
// GetSchema returns a copy of the schema.
func (si *SchemaInfo) GetSchema() []*schema.Table {
si.mu.Lock()
defer si.mu.Unlock()
tables := make([]*schema.Table, 0, len(si.tables))
for _, v := range si.tables {
tables = append(tables, v.Table)
}
return tables
}
// getQuery fetches the plan and makes it the most recent.
func (si *SchemaInfo) getQuery(sql string) *ExecPlan {
if cacheResult, ok := si.queries.Get(sql); ok {
return cacheResult.(*ExecPlan)
}
return nil
}
// peekQuery fetches the plan without changing the LRU order.
func (si *SchemaInfo) peekQuery(sql string) *ExecPlan {
if cacheResult, ok := si.queries.Peek(sql); ok {
return cacheResult.(*ExecPlan)
}
return nil
}
// SetQueryCacheCap sets the query cache capacity.
func (si *SchemaInfo) SetQueryCacheCap(size int) {
if size <= 0 {
panic(NewTabletError(vtrpcpb.ErrorCode_BAD_INPUT, "cache size %v out of range", size))
}
si.queries.SetCapacity(int64(size))
}
// QueryCacheCap returns the capacity of the query cache.
func (si *SchemaInfo) QueryCacheCap() int {
return int(si.queries.Capacity())
}
// SetReloadTime changes how often the schema is reloaded. This
// call also triggers an immediate reload.
func (si *SchemaInfo) SetReloadTime(reloadTime time.Duration) {
si.ticks.Trigger()
si.ticks.SetInterval(reloadTime)
si.mu.Lock()
defer si.mu.Unlock()
si.reloadTime = reloadTime
}
// ReloadTime returns schema info reload time.
func (si *SchemaInfo) ReloadTime() time.Duration {
si.mu.Lock()
defer si.mu.Unlock()
return si.reloadTime
}
func (si *SchemaInfo) getTableRows() map[string]int64 {
si.mu.Lock()
defer si.mu.Unlock()
tstats := make(map[string]int64)
for k, v := range si.tables {
tstats[k] = v.TableRows.Get()
}
return tstats
}
func (si *SchemaInfo) getDataLength() map[string]int64 {
si.mu.Lock()
defer si.mu.Unlock()
tstats := make(map[string]int64)
for k, v := range si.tables {
tstats[k] = v.DataLength.Get()
}
return tstats
}
func (si *SchemaInfo) getIndexLength() map[string]int64 {
si.mu.Lock()
defer si.mu.Unlock()
tstats := make(map[string]int64)
for k, v := range si.tables {
tstats[k] = v.IndexLength.Get()
}
return tstats
}
func (si *SchemaInfo) getDataFree() map[string]int64 {
si.mu.Lock()
defer si.mu.Unlock()
tstats := make(map[string]int64)
for k, v := range si.tables {
tstats[k] = v.DataFree.Get()
}
return tstats
}
func (si *SchemaInfo) getQueryCount() map[string]int64 {
f := func(plan *ExecPlan) int64 {
queryCount, _, _, _ := plan.Stats()
return queryCount
}
return si.getQueryStats(f)
}
func (si *SchemaInfo) getQueryTime() map[string]int64 {
f := func(plan *ExecPlan) int64 {
_, time, _, _ := plan.Stats()
return int64(time)
}
return si.getQueryStats(f)
}
func (si *SchemaInfo) getQueryRowCount() map[string]int64 {
f := func(plan *ExecPlan) int64 {
_, _, rowCount, _ := plan.Stats()
return rowCount
}
return si.getQueryStats(f)
}
func (si *SchemaInfo) getQueryErrorCount() map[string]int64 {
f := func(plan *ExecPlan) int64 {
_, _, _, errorCount := plan.Stats()
return errorCount
}
return si.getQueryStats(f)
}
type queryStatsFunc func(*ExecPlan) int64
func (si *SchemaInfo) getQueryStats(f queryStatsFunc) map[string]int64 {
keys := si.queries.Keys()
qstats := make(map[string]int64)
for _, v := range keys {
if plan := si.peekQuery(v); plan != nil {
table := plan.TableName
if table == "" {
table = "Join"
}
planType := plan.PlanID.String()
data := f(plan)
qstats[table+"."+planType] += data
}
}
return qstats
}
type perQueryStats struct {
Query string
Table string
Plan planbuilder.PlanType
QueryCount int64
Time time.Duration
RowCount int64
ErrorCount int64
}
func (si *SchemaInfo) ServeHTTP(response http.ResponseWriter, request *http.Request) {
if err := acl.CheckAccessHTTP(request, acl.DEBUGGING); err != nil {
acl.SendError(response, err)
return
}
if ep, ok := si.endpoints[debugQueryPlansKey]; ok && request.URL.Path == ep {
si.handleHTTPQueryPlans(response, request)
} else if ep, ok := si.endpoints[debugQueryStatsKey]; ok && request.URL.Path == ep {
si.handleHTTPQueryStats(response, request)
} else if ep, ok := si.endpoints[debugSchemaKey]; ok && request.URL.Path == ep {
si.handleHTTPSchema(response, request)
} else if ep, ok := si.endpoints[debugQueryRulesKey]; ok && request.URL.Path == ep {
si.handleHTTPQueryRules(response, request)
} else {
response.WriteHeader(http.StatusNotFound)
}
}
func (si *SchemaInfo) handleHTTPQueryPlans(response http.ResponseWriter, request *http.Request) {
keys := si.queries.Keys()
response.Header().Set("Content-Type", "text/plain")
response.Write([]byte(fmt.Sprintf("Length: %d\n", len(keys))))
for _, v := range keys {
response.Write([]byte(fmt.Sprintf("%#v\n", v)))
if plan := si.peekQuery(v); plan != nil {
if b, err := json.MarshalIndent(plan.ExecPlan, "", " "); err != nil {
response.Write([]byte(err.Error()))
} else {
response.Write(b)
}
response.Write(([]byte)("\n\n"))
}
}
}
func (si *SchemaInfo) handleHTTPQueryStats(response http.ResponseWriter, request *http.Request) {
keys := si.queries.Keys()
response.Header().Set("Content-Type", "application/json; charset=utf-8")
qstats := make([]perQueryStats, 0, len(keys))
for _, v := range keys {
if plan := si.peekQuery(v); plan != nil {
var pqstats perQueryStats
pqstats.Query = unicoded(v)
pqstats.Table = plan.TableName
pqstats.Plan = plan.PlanID
pqstats.QueryCount, pqstats.Time, pqstats.RowCount, pqstats.ErrorCount = plan.Stats()
qstats = append(qstats, pqstats)
}
}
if b, err := json.MarshalIndent(qstats, "", " "); err != nil {
response.Write([]byte(err.Error()))
} else {
response.Write(b)
}
}
func (si *SchemaInfo) handleHTTPSchema(response http.ResponseWriter, request *http.Request) {
response.Header().Set("Content-Type", "application/json; charset=utf-8")
tables := si.GetSchema()
b, err := json.MarshalIndent(tables, "", " ")
if err != nil {
response.Write([]byte(err.Error()))
return
}
buf := bytes.NewBuffer(nil)
json.HTMLEscape(buf, b)
response.Write(buf.Bytes())
}
func (si *SchemaInfo) handleHTTPQueryRules(response http.ResponseWriter, request *http.Request) {
response.Header().Set("Content-Type", "application/json; charset=utf-8")
b, err := json.MarshalIndent(si.queryRuleSources, "", " ")
if err != nil {
response.Write([]byte(err.Error()))
return
}
buf := bytes.NewBuffer(nil)
json.HTMLEscape(buf, b)
response.Write(buf.Bytes())
}