Skip to content

Commit

Permalink
Merge pull request pingcap#11 from spongedu/tbssql2
Browse files Browse the repository at this point in the history
Support cmd: `admin do inspection`
  • Loading branch information
qiuyesuifeng authored Oct 20, 2019
2 parents 3fb5f65 + 527ff47 commit 02103de
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 1 deletion.
10 changes: 10 additions & 0 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ func (b *executorBuilder) build(p plannercore.Plan) Executor {
return b.buildShowDDLJobQueries(v)
case *plannercore.ShowSlow:
return b.buildShowSlow(v)
case *plannercore.TiDBInspection:
return b.buildTiDBInspection(v)
case *plannercore.PhysicalShow:
return b.buildShow(v)
case *plannercore.Simple:
Expand Down Expand Up @@ -320,6 +322,14 @@ func (b *executorBuilder) buildShowSlow(v *plannercore.ShowSlow) Executor {
return e
}

func (b *executorBuilder) buildTiDBInspection(v *plannercore.TiDBInspection) Executor {
e := &TiDBInspectionExec{
baseExecutor: newBaseExecutor(b.ctx, v.Schema(), v.ExplainID()),
done: false,
}
return e
}

func (b *executorBuilder) buildCheckIndex(v *plannercore.CheckIndex) Executor {
readerExec, err := buildNoRangeIndexLookUpReader(b, v.IndexLookUpReader)
if err != nil {
Expand Down
37 changes: 37 additions & 0 deletions executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -1604,3 +1604,40 @@ func ResetContextOfStmt(ctx sessionctx.Context, s ast.StmtNode) (err error) {
}
return
}

type TiDBInspectionExec struct {
baseExecutor
done bool
}

// Open implements the Executor Open interface.
func (e *TiDBInspectionExec) Open(ctx context.Context) error {
if err := e.baseExecutor.Open(ctx); err != nil {
return err
}

// dom := domain.GetDomain(e.ctx)
// e.result = dom.ShowSlowQuery(e.ShowSlow)
return nil
}

// Next implements the Executor Next interface.
func (e *TiDBInspectionExec) Next(ctx context.Context, req *chunk.Chunk) error {
req.Reset()
if e.done {
return nil
}

// Step 1. Create inspection db
inspectionDBName := fmt.Sprintf("%s_%s", "tidb_inspection",time.Now().Format("20060102150405"))
err := domain.GetDomain(e.ctx).DDL().CreateSchema(e.ctx, model.NewCIStr(inspectionDBName), nil)
req.AppendInt64(0, 0)
req.AppendString(1, "create inspection database")
if err != nil {
req.AppendString(2, err.Error())
} else {
req.AppendString(2, "finished")
}
e.done = true
return nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ require (
sourcegraph.com/sourcegraph/appdash-data v0.0.0-20151005221446-73f23eafcf67
)

replace github.com/pingcap/parser => github.com/qiuyesuifeng/parser v0.0.0-20191013064818-dec0e5f63128
replace github.com/pingcap/parser => github.com/spongedu/parser v0.0.0-20191017163538-da007a565e9b

replace github.com/google/pprof => github.com/lonng/pprof v0.0.0-20191012154247-04dfd648ce8d

Expand Down
24 changes: 24 additions & 0 deletions infoschema/inspection/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2016 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package inspection

var inspectionTables = []string{
tableGlobalStatus,
}

// tableGlobalStatus contains the column name definitions for table global_status, same as MySQL.
const tableGlobalStatus = "CREATE TABLE performance_schema.global_status(" +
"VARIABLE_NAME VARCHAR(64) not null," +
"VARIABLE_VALUE VARCHAR(1024));"

147 changes: 147 additions & 0 deletions infoschema/inspection/inspection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package inspection

import (
"github.com/pingcap/parser"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/tidb/ddl"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta/autoid"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/types"
)

func InitInspectionDB(name string) {
p := parser.New()
tbls := make([]*model.TableInfo, 0)
dbID := autoid.GenLocalSchemaID()

for _, sql := range inspectionTables {
stmt, err := p.ParseOneStmt(sql, "", "")
if err != nil {
panic(err)
}
meta, err := ddl.BuildTableInfoFromAST(stmt.(*ast.CreateTableStmt))
if err != nil {
panic(err)
}
tbls = append(tbls, meta)
meta.ID = autoid.GenLocalSchemaID()
for _, c := range meta.Columns {
c.ID = autoid.GenLocalSchemaID()
}
}
dbInfo := &model.DBInfo{
ID: dbID,
Name: model.NewCIStr(name),
Charset: mysql.DefaultCharset,
Collate: mysql.DefaultCollationName,
Tables: tbls,
}
infoschema.RegisterVirtualTable(dbInfo, tableFromMeta)
}

func tableFromMeta(alloc autoid.Allocator, meta *model.TableInfo) (table.Table, error) {
return createInspectionTable(meta), nil
}

// createPerfSchemaTable creates all perfSchemaTables
func createInspectionTable(meta *model.TableInfo) *inspectTable {
columns := make([]*table.Column, 0, len(meta.Columns))
for _, colInfo := range meta.Columns {
col := table.ToColumn(colInfo)
columns = append(columns, col)
}
t := &inspectTable{
meta: meta,
cols: columns,
}
return t
}

// inspectTable stands for the fake table all its data is in the memory.
type inspectTable struct {
infoschema.VirtualTable
meta *model.TableInfo
cols []*table.Column
}

// Cols implements table.Table Type interface.
func (vt *inspectTable) Cols() []*table.Column {
return vt.cols
}

// WritableCols implements table.Table Type interface.
func (vt *inspectTable) WritableCols() []*table.Column {
return vt.cols
}

// GetID implements table.Table GetID interface.
func (vt *inspectTable) GetPhysicalID() int64 {
return vt.meta.ID
}

// Meta implements table.Table Type interface.
func (vt *inspectTable) Meta() *model.TableInfo {
return vt.meta
}

func (vt *inspectTable) getRows(ctx sessionctx.Context, cols []*table.Column) (fullRows [][]types.Datum, err error) {
// switch vt.meta.Name.O {
// case tableNameEventsStatementsSummaryByDigest:
// fullRows = stmtsummary.StmtSummaryByDigestMap.ToDatum()
// case tableNameCpuProfile:
// fullRows, err = cpuProfileGraph()
// case tableNameMemoryProfile:
// fullRows, err = profileGraph("heap")
// case tableNameMutexProfile:
// fullRows, err = profileGraph("mutex")
// case tableNameAllocsProfile:
// fullRows, err = profileGraph("allocs")
// case tableNameBlockProfile:
// fullRows, err = profileGraph("block")
// case tableNameGoroutines:
// fullRows, err = goroutinesList()
// }
// if err != nil {
// return
// }
// if len(cols) == len(vt.cols) {
// return
// }
rows := make([][]types.Datum, len(fullRows))
for i, fullRow := range fullRows {
row := make([]types.Datum, len(cols))
for j, col := range cols {
row[j] = fullRow[col.Offset]
}
rows[i] = row
}
return rows, nil
}

// IterRecords implements table.Table IterRecords interface.
func (vt *inspectTable) IterRecords(ctx sessionctx.Context, startKey kv.Key, cols []*table.Column,
fn table.RecordIterFunc) error {
if len(startKey) != 0 {
return table.ErrUnsupportedOp
}
rows, err := vt.getRows(ctx, cols)
if err != nil {
return err
}
for i, row := range rows {
more, err := fn(int64(i), row, cols)
if err != nil {
return err
}
if !more {
break
}
}
return nil
}

5 changes: 5 additions & 0 deletions planner/core/common_plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ type ShowSlow struct {
*ast.ShowSlow
}

// TiDBInspection is for tidb inspection information.
type TiDBInspection struct {
baseSchemaProducer
}

// ShowDDLJobQueries is for showing DDL job queries sql.
type ShowDDLJobQueries struct {
baseSchemaProducer
Expand Down
13 changes: 13 additions & 0 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,10 @@ func (b *PlanBuilder) buildAdmin(ctx context.Context, as *ast.AdminStmt) (Plan,
p := &ShowDDL{}
p.SetSchema(buildShowDDLFields())
ret = p
case ast.AdminInspection:
p := &TiDBInspection{}
p.SetSchema(buildTiDBInspectionFields())
ret = p
case ast.AdminShowDDLJobs:
p := LogicalShowDDLJobs{JobNumber: as.JobNumber}.Init(b.ctx)
p.SetSchema(buildShowDDLJobsFields())
Expand Down Expand Up @@ -1267,6 +1271,15 @@ func buildShowDDLFields() *expression.Schema {
return schema
}

func buildTiDBInspectionFields() *expression.Schema {
schema := expression.NewSchema(make([]*expression.Column, 0, 3)...)
schema.Append(buildColumn("", "ID", mysql.TypeLonglong, 4))
schema.Append(buildColumn("", "MSG", mysql.TypeVarchar, 256))
schema.Append(buildColumn("", "STATUS", mysql.TypeVarchar, 256))

return schema
}

func buildRecoverIndexFields() *expression.Schema {
schema := expression.NewSchema(make([]*expression.Column, 0, 2)...)
schema.Append(buildColumn("", "ADDED_COUNT", mysql.TypeLonglong, 4))
Expand Down

0 comments on commit 02103de

Please sign in to comment.