Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

collector: let plan replayer collector support dbvars and tiflash replica info #406

Merged
merged 1 commit into from
Aug 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions collector/plan_replayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"time"

"github.com/BurntSushi/toml"
"github.com/joomcode/errorx"
"github.com/pingcap/diag/pkg/models"
perrs "github.com/pingcap/errors"
Expand Down Expand Up @@ -135,9 +136,84 @@ func (c *PlanReplayerCollectorOptions) collectPlanReplayer(ctx context.Context,
if len(errs) > 0 {
logger.Warnf("error happened during explain sql, err:%v", strings.Join(errs, ","))
}
errs = c.collectDBVars(zw, db)
if len(errs) > 0 {
logger.Warnf("error happened during collect dbvars, err:%v", strings.Join(errs, ","))
}
errs = c.collectTiflashReplicas(zw, db)
if len(errs) > 0 {
logger.Warnf("error happened during collect tiflash replica info, err:%v", strings.Join(errs, ","))
}
return nil
}

/*
|-table_tiflash_replica.txt
*/
func (c *PlanReplayerCollectorOptions) collectTiflashReplicas(zw *zip.Writer, db *sql.DB) (errs []string) {
vf, err := zw.Create("table_tiflash_replica.txt")
if err != nil {
errs = append(errs, fmt.Errorf("create table_tiflash_replica.txt failed, err:%v", err).Error())
return errs
}
for table := range c.tables {
err := func() error {
sql := fmt.Sprintf("SELECT TABLE_SCHEMA,TABLE_NAME,REPLICA_COUNT FROM INFORMATION_SCHEMA.TIFLASH_REPLICA WHERE TABLE_SCHEMA='%s' AND TABLE_NAME ='%s' AND REPLICA_COUNT >0", table.dbName, table.tableName)
rows, err := db.Query(sql)
if err != nil {
return fmt.Errorf("failed to query tiflash replicas, err:%v", err)
}
for rows.Next() {
var dbName, tableName, count string
err = rows.Scan(&dbName, &tableName, &count)
r := []string{
dbName, tableName, count,
}
fmt.Fprintf(vf, "%s\n", strings.Join(r, "\t"))
}
return nil
}()
if err != nil {
errs = append(errs, err.Error())
}
}
return errs
}

/*
|-variables.toml
*/
func (c *PlanReplayerCollectorOptions) collectDBVars(zw *zip.Writer, db *sql.DB) (errs []string) {
err := func() error {
vf, err := zw.Create("variables.toml")
if err != nil {
return fmt.Errorf("create variables.toml failed, err:%v", err)
}
rows, err := db.Query("show variables")
if err != nil {
return fmt.Errorf("show variables failed, err:%v", err)
}
defer rows.Close()
varMap := make(map[string]string)
var name, value string
for rows.Next() {
err := rows.Scan(&name, &value)
if err != nil {
return fmt.Errorf("show variables failed, err:%v", err)
}
varMap[name] = value
}
if err := toml.NewEncoder(vf).Encode(varMap); err != nil {
return fmt.Errorf("dump varMap failed, err:%v", err)
}
return nil
}()
if err != nil {
errs = append(errs, err.Error())
}
return errs
}

/*
|-meta.txt
|-schema
Expand Down
1 change: 0 additions & 1 deletion configs/profiles/tidb-plan-replayer.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ maintainers = [
# list of data types to collect
collectors = [
"config",
"db_vars",
Copy link
Contributor Author

@Yisaer Yisaer Aug 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we still remain db_vars collector in code, and it won't be updated in expectation

"sql_bind",
"plan_replayer"
]
Expand Down