|
| 1 | +// Copyright 2019 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package infoschema |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + |
| 19 | + "github.com/pingcap/errors" |
| 20 | + "github.com/pingcap/parser/model" |
| 21 | + "github.com/pingcap/parser/mysql" |
| 22 | + "github.com/pingcap/tidb/kv" |
| 23 | + "github.com/pingcap/tidb/meta/autoid" |
| 24 | + "github.com/pingcap/tidb/sessionctx" |
| 25 | + "github.com/pingcap/tidb/sessionctx/variable" |
| 26 | + "github.com/pingcap/tidb/table" |
| 27 | + "github.com/pingcap/tidb/util" |
| 28 | +) |
| 29 | + |
| 30 | +// The `inspection_schema` is used to provide a consistent view of `information_schema` tables, |
| 31 | +// so the related table should have the same table name within `information_schema`. |
| 32 | +// The data will be obtained lazily from `information_schema` and cache in `SessionVars`, and |
| 33 | +// the cached data will be cleared at `InspectionExec` closing. |
| 34 | +var inspectionTables = map[string][]columnInfo{ |
| 35 | + tableClusterInfo: tableClusterInfoCols, |
| 36 | + TableClusterConfig: tableClusterConfigCols, |
| 37 | + TableClusterLoad: tableClusterLoadCols, |
| 38 | + TableClusterHardware: tableClusterHardwareCols, |
| 39 | + TableClusterSystemInfo: tableClusterSystemInfoCols, |
| 40 | +} |
| 41 | + |
| 42 | +type inspectionSchemaTable struct { |
| 43 | + infoschemaTable |
| 44 | +} |
| 45 | + |
| 46 | +// IterRecords implements table.Table IterRecords interface. |
| 47 | +func (it *inspectionSchemaTable) IterRecords(ctx sessionctx.Context, startKey kv.Key, cols []*table.Column, |
| 48 | + fn table.RecordIterFunc) error { |
| 49 | + sessionVars := ctx.GetSessionVars() |
| 50 | + // The `InspectionTableCache` will be assigned in `InspectionExec.Open` and be |
| 51 | + // cleaned at `InspectionExec.Close`, so nil represents currently in non-inspection mode. |
| 52 | + if sessionVars.InspectionTableCache == nil { |
| 53 | + return errors.New("not currently in inspection mode") |
| 54 | + } |
| 55 | + |
| 56 | + if len(startKey) != 0 { |
| 57 | + return table.ErrUnsupportedOp |
| 58 | + } |
| 59 | + |
| 60 | + // Obtain data from cache first. |
| 61 | + cached, found := sessionVars.InspectionTableCache[it.meta.Name.L] |
| 62 | + if !found { |
| 63 | + // We retrieve data from `information_schema` if can found in cache. |
| 64 | + rows, err := it.getRows(ctx, cols) |
| 65 | + cached = variable.TableSnapshot{ |
| 66 | + Rows: rows, |
| 67 | + Err: err, |
| 68 | + } |
| 69 | + sessionVars.InspectionTableCache[it.meta.Name.L] = cached |
| 70 | + } |
| 71 | + if cached.Err != nil { |
| 72 | + return cached.Err |
| 73 | + } |
| 74 | + |
| 75 | + for i, row := range cached.Rows { |
| 76 | + more, err := fn(int64(i), row, cols) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + if !more { |
| 81 | + break |
| 82 | + } |
| 83 | + } |
| 84 | + return nil |
| 85 | +} |
| 86 | + |
| 87 | +func init() { |
| 88 | + // Initialize the inspection schema database and register the driver to `drivers`. |
| 89 | + dbID := autoid.InspectionSchemaDBID |
| 90 | + tables := make([]*model.TableInfo, 0, len(inspectionTables)) |
| 91 | + for name, cols := range inspectionTables { |
| 92 | + tableInfo := buildTableMeta(name, cols) |
| 93 | + tables = append(tables, tableInfo) |
| 94 | + var ok bool |
| 95 | + tid, ok := tableIDMap[tableInfo.Name.O] |
| 96 | + if !ok { |
| 97 | + panic(fmt.Sprintf("get inspection_schema table id failed, unknown system table `%v`", tableInfo.Name.O)) |
| 98 | + } |
| 99 | + // Reuse information_schema table id serial number. |
| 100 | + tableInfo.ID = tid - autoid.InformationSchemaDBID + autoid.InspectionSchemaDBID |
| 101 | + for i, c := range tableInfo.Columns { |
| 102 | + c.ID = int64(i) + 1 |
| 103 | + } |
| 104 | + } |
| 105 | + inspectionSchema := &model.DBInfo{ |
| 106 | + ID: dbID, |
| 107 | + Name: util.InspectionSchemaName, |
| 108 | + Charset: mysql.DefaultCharset, |
| 109 | + Collate: mysql.DefaultCollationName, |
| 110 | + Tables: tables, |
| 111 | + } |
| 112 | + builder := func(_ autoid.Allocators, meta *model.TableInfo) (table.Table, error) { |
| 113 | + columns := make([]*table.Column, len(meta.Columns)) |
| 114 | + for i, col := range meta.Columns { |
| 115 | + columns[i] = table.ToColumn(col) |
| 116 | + } |
| 117 | + tbl := &inspectionSchemaTable{ |
| 118 | + infoschemaTable{ |
| 119 | + meta: meta, |
| 120 | + cols: columns, |
| 121 | + tp: table.VirtualTable, |
| 122 | + }, |
| 123 | + } |
| 124 | + return tbl, nil |
| 125 | + } |
| 126 | + RegisterVirtualTable(inspectionSchema, builder) |
| 127 | +} |
0 commit comments