Skip to content

Commit

Permalink
infoschema: add virtual table cluster config
Browse files Browse the repository at this point in the history
Signed-off-by: Lonng <heng@lonng.org>
  • Loading branch information
lonng committed Oct 23, 2019
1 parent adaea5c commit 456ab97
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
github.com/gorilla/websocket v1.4.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4
github.com/grpc-ecosystem/grpc-gateway v1.5.1 // indirect
github.com/jeremywohl/flatten v0.0.0-20190921043622-d936035e55cf
github.com/json-iterator/go v1.1.6 // indirect
github.com/klauspost/cpuid v0.0.0-20170728055534-ae7887de9fa5
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jeremywohl/flatten v0.0.0-20190921043622-d936035e55cf h1:Ut4tTtPNmInWiEWJRernsWm688R0RN6PFO8sZhwI0sk=
github.com/jeremywohl/flatten v0.0.0-20190921043622-d936035e55cf/go.mod h1:4AmD/VxjWcI5SRB0n6szE2A6s2fsNHDLO0nAlMHgfLQ=
github.com/jonboulle/clockwork v0.1.0 h1:VKV+ZcuP6l3yW9doeqz6ziZGgcynBVQO+obU0+0hcPo=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs=
Expand Down
42 changes: 42 additions & 0 deletions infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ import (
"context"
"encoding/json"
"fmt"
"net/http"
"sort"
"strings"
"sync"
"time"

"github.com/jeremywohl/flatten"
"github.com/pingcap/errors"
"github.com/pingcap/parser/charset"
"github.com/pingcap/parser/model"
Expand Down Expand Up @@ -87,6 +89,7 @@ const (
tableTiKVRegionPeers = "TIKV_REGION_PEERS"
tableTiDBServersInfo = "TIDB_SERVERS_INFO"
tableTiDBClusterInfo = "TIDB_CLUSTER_INFO"
tableTiDBClusterConfig = "TIDB_CLUSTER_CONFIG"
)

type columnInfo struct {
Expand Down Expand Up @@ -672,6 +675,13 @@ var tableTiDBClusterInfoCols = []columnInfo{
{"GIT_HASH", mysql.TypeVarchar, 64, 0, nil, nil},
}

var tableTiDBClusterConfigCols = []columnInfo{
{"SERVER_TYPE", mysql.TypeVarchar, 8, 0, nil, nil},
{"INSTANCE", mysql.TypeVarchar, 64, 0, nil, nil},
{"NAME", mysql.TypeVarchar, 256, 0, nil, nil},
{"VALUE", mysql.TypeVarchar, 128, 0, nil, nil},
}

func dataForTiKVRegionStatus(ctx sessionctx.Context) (records [][]types.Datum, err error) {
tikvStore, ok := ctx.GetStore().(tikv.Storage)
if !ok {
Expand Down Expand Up @@ -1939,7 +1949,36 @@ func dataForTiDBClusterInfo(ctx sessionctx.Context) ([][]types.Datum, error) {
rows = append(rows, row)
idx++
}
return rows, nil
}

// TODO: poc for config flatten, fetch all servers
func dataForClusterConfig() ([][]types.Datum, error) {
resp, err := http.Get("http://127.0.0.1:49904/config")
if err != nil {
return nil, err
}
defer resp.Body.Close()

var nested map[string]interface{}
if err = json.NewDecoder(resp.Body).Decode(&nested); err != nil {
return nil, err
}
data, err := flatten.Flatten(nested, "", flatten.DotStyle)
if err != nil {
return nil, err
}
var rows [][]types.Datum
instance := types.NewStringDatum("127.0.0.1:49904")
serverTp := types.NewStringDatum("TiKV")
for key, val := range data {
rows = append(rows, []types.Datum{
serverTp,
instance,
types.NewStringDatum(key),
types.NewStringDatum(fmt.Sprintf("%v", val)),
})
}
return rows, nil
}

Expand Down Expand Up @@ -1985,6 +2024,7 @@ var tableNameToColumns = map[string][]columnInfo{
tableTiKVRegionPeers: tableTiKVRegionPeersCols,
tableTiDBServersInfo: tableTiDBServersInfoCols,
tableTiDBClusterInfo: tableTiDBClusterInfoCols,
tableTiDBClusterConfig: tableTiDBClusterConfigCols,
}

func createInfoSchemaTable(handle *Handle, meta *model.TableInfo) *infoschemaTable {
Expand Down Expand Up @@ -2092,6 +2132,8 @@ func (it *infoschemaTable) getRows(ctx sessionctx.Context, cols []*table.Column)
fullRows, err = dataForServersInfo()
case tableTiDBClusterInfo:
fullRows, err = dataForTiDBClusterInfo(ctx)
case tableTiDBClusterConfig:
fullRows, err = dataForClusterConfig()
}
if err != nil {
return nil, err
Expand Down

0 comments on commit 456ab97

Please sign in to comment.