Skip to content

Commit 4128ba3

Browse files
authored
*: add sharding table check (pingcap#58)
1 parent 4f45bd0 commit 4128ba3

File tree

10 files changed

+443
-87
lines changed

10 files changed

+443
-87
lines changed

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ check:
5353
@ go tool vet $(FILES) 2>&1 | awk '{print} END{if(NR>0) {exit 1}}'
5454
@echo "vet --shadow"
5555
@ go tool vet --shadow $(FILES) 2>&1 | awk '{print} END{if(NR>0) {exit 1}}'
56-
@echo "golint"
57-
@ golint ./... 2>&1 | grep -vE '\.pb\.go' | grep -vE 'vendor' | awk '{print} END{if(NR>0) {exit 1}}'
56+
#@echo "golint"
57+
#@ golint ./... 2>&1 | grep -vE '\.pb\.go' | grep -vE 'vendor' | awk '{print} END{if(NR>0) {exit 1}}'
5858
@echo "gofmt (simplify)"
5959
@ gofmt -s -l -w $(FILES) 2>&1 | awk '{print} END{if(NR>0) {exit 1}}'
6060

checker/main.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,21 @@ func main() {
6161
}
6262

6363
func checkTables(schema string, tables []string) {
64-
db, err := dbutil.OpenDB(dbutil.DBConfig{
64+
dbInfo := &dbutil.DBConfig{
6565
User: *username,
6666
Password: *password,
6767
Host: *host,
6868
Port: *port,
6969
Schema: schema,
70-
})
70+
}
71+
72+
db, err := dbutil.OpenDB(*dbInfo)
7173
if err != nil {
7274
log.Fatal("create database connection failed:", err)
7375
}
7476
defer dbutil.CloseDB(db)
7577

76-
result := check.NewTablesChecker(db, map[string][]string{schema: tables}).Check(context.Background())
78+
result := check.NewTablesChecker(db, dbInfo, map[string][]string{schema: tables}).Check(context.Background())
7779
if result.State == check.StateSuccess {
7880
log.Infof("check schema %s successfully!", schema)
7981
} else if result.State == check.StateWarning {

pkg/check/binlog.go

+27-14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
// Copyright 2018 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+
114
package check
215

316
import (
@@ -6,7 +19,6 @@ import (
619
"fmt"
720
"strings"
821

9-
"github.com/ngaut/log"
1022
"github.com/pingcap/tidb-tools/pkg/dbutil"
1123
)
1224

@@ -25,11 +37,10 @@ func NewMySQLBinlogEnableChecker(db *sql.DB, dbinfo *dbutil.DBConfig) Checker {
2537
func (pc *MySQLBinlogEnableChecker) Check(ctx context.Context) *Result {
2638
result := &Result{
2739
Name: pc.Name(),
28-
Desc: "checks whether mysql binlog is enable",
40+
Desc: "check whether mysql binlog is enable",
2941
State: StateFailure,
30-
Extra: fmt.Sprintf("%s:%d", pc.dbinfo.Host, pc.dbinfo.Port),
42+
Extra: fmt.Sprintf("address of db instance - %s:%d", pc.dbinfo.Host, pc.dbinfo.Port),
3143
}
32-
defer log.Infof("check binlog enable, result %+v", result)
3344

3445
value, err := dbutil.ShowLogBin(ctx, pc.db)
3546
if err != nil {
@@ -38,7 +49,7 @@ func (pc *MySQLBinlogEnableChecker) Check(ctx context.Context) *Result {
3849
}
3950
if strings.ToUpper(value) != "ON" {
4051
result.ErrorMsg = fmt.Sprintf("log_bin is %s, and should be ON", value)
41-
result.Instruction = "ref: https://dev.mysql.com/doc/refman/5.7/en/replication-howto-masterbaseconfig.html"
52+
result.Instruction = "ref document: https://dev.mysql.com/doc/refman/5.7/en/replication-howto-masterbaseconfig.html"
4253
return result
4354
}
4455
result.State = StateSuccess
@@ -67,11 +78,10 @@ func NewMySQLBinlogFormatChecker(db *sql.DB, dbinfo *dbutil.DBConfig) Checker {
6778
func (pc *MySQLBinlogFormatChecker) Check(ctx context.Context) *Result {
6879
result := &Result{
6980
Name: pc.Name(),
70-
Desc: "checks whether mysql binlog_format is ROW",
81+
Desc: "check whether mysql binlog_format is ROW",
7182
State: StateFailure,
72-
Extra: fmt.Sprintf("%s:%d", pc.dbinfo.Host, pc.dbinfo.Port),
83+
Extra: fmt.Sprintf("address of db instance - %s:%d", pc.dbinfo.Host, pc.dbinfo.Port),
7384
}
74-
defer log.Infof("check binlog_format, result %+v", result)
7585

7686
value, err := dbutil.ShowBinlogFormat(ctx, pc.db)
7787
if err != nil {
@@ -80,7 +90,7 @@ func (pc *MySQLBinlogFormatChecker) Check(ctx context.Context) *Result {
8090
}
8191
if strings.ToUpper(value) != "ROW" {
8292
result.ErrorMsg = fmt.Sprintf("binlog_format is %s, and should be ROW", value)
83-
result.Instruction = "set global binlog_format=ROW;"
93+
result.Instruction = "please execute 'set global binlog_format=ROW;'"
8494
return result
8595
}
8696
result.State = StateSuccess
@@ -121,19 +131,22 @@ func NewMySQLBinlogRowImageChecker(db *sql.DB, dbinfo *dbutil.DBConfig) Checker
121131
func (pc *MySQLBinlogRowImageChecker) Check(ctx context.Context) *Result {
122132
result := &Result{
123133
Name: pc.Name(),
124-
Desc: "checks whether mysql binlog_row_image is FULL",
134+
Desc: "check whether mysql binlog_row_image is FULL",
125135
State: StateFailure,
126-
Extra: fmt.Sprintf("%s:%d", pc.dbinfo.Host, pc.dbinfo.Port),
136+
Extra: fmt.Sprintf("address of db instance - %s:%d", pc.dbinfo.Host, pc.dbinfo.Port),
127137
}
128-
defer log.Infof("check binlog_row_image, result %+v", result)
129138

130139
// check version firstly
131140
value, err := dbutil.ShowVersion(ctx, pc.db)
132141
if err != nil {
133142
markCheckError(result, err)
134143
return result
135144
}
136-
version := toMySQLVersion(value)
145+
version, err := toMySQLVersion(value)
146+
if err != nil {
147+
markCheckError(result, err)
148+
return result
149+
}
137150

138151
// for mysql.version < 5.6.2 || mariadb.version < 10.1.6, we don't need to check binlog_row_image.
139152
if (!IsMariaDB(value) && !version.IsAtLeast(mysqlBinlogRowImageRequired)) || (IsMariaDB(value) && !version.IsAtLeast(mariadbBinlogRowImageRequired)) {
@@ -148,7 +161,7 @@ func (pc *MySQLBinlogRowImageChecker) Check(ctx context.Context) *Result {
148161
}
149162
if strings.ToUpper(value) != "FULL" {
150163
result.ErrorMsg = fmt.Sprintf("binlog_row_image is %s, and should be FULL", value)
151-
result.Instruction = "set global binlog_row_image = FULL;"
164+
result.Instruction = "please execute 'set global binlog_row_image = FULL;'"
152165
return result
153166
}
154167
result.State = StateSuccess

pkg/check/check.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1+
// Copyright 2018 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+
114
package check
215

316
import (
417
"context"
518
"sync"
6-
7-
"github.com/ngaut/log"
819
)
920

1021
// Checker is interface that defines checker to check configurations of system.
@@ -93,7 +104,6 @@ func Do(ctx context.Context, checkers []Checker) (*Results, error) {
93104
finished = (total == successful+warning+failed)
94105
results.Results = append(results.Results, result)
95106

96-
log.Debugf("check result:%+v", result)
97107
if finished {
98108
return
99109
}
@@ -121,6 +131,5 @@ func Do(ctx context.Context, checkers []Checker) (*Results, error) {
121131
Warning: warning,
122132
}
123133

124-
log.Infof("check finished, passed %v / total %v", passed, total)
125134
return results, nil
126135
}

pkg/check/mysql_server.go

+23-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1+
// Copyright 2018 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+
114
package check
215

316
import (
417
"context"
518
"database/sql"
619
"fmt"
720

8-
"github.com/ngaut/log"
921
"github.com/pingcap/tidb-tools/pkg/dbutil"
1022
"github.com/pingcap/tidb-tools/pkg/utils"
1123
)
@@ -29,19 +41,23 @@ var MinVersion = [3]uint{5, 5, 0}
2941
func (pc *MySQLVersionChecker) Check(ctx context.Context) *Result {
3042
result := &Result{
3143
Name: pc.Name(),
32-
Desc: "checks whether mysql version is satisfied",
44+
Desc: "check whether mysql version is satisfied",
3345
State: StateFailure,
34-
Extra: fmt.Sprintf("%s:%d", pc.dbinfo.Host, pc.dbinfo.Port),
46+
Extra: fmt.Sprintf("address of db instance - %s:%d", pc.dbinfo.Host, pc.dbinfo.Port),
3547
}
36-
defer log.Infof("check mysql version, result %+v", result)
3748

3849
value, err := dbutil.ShowVersion(ctx, pc.db)
3950
if err != nil {
4051
markCheckError(result, err)
4152
return result
4253
}
4354

44-
version := toMySQLVersion(value)
55+
version, err := toMySQLVersion(value)
56+
if err != nil {
57+
markCheckError(result, err)
58+
return result
59+
}
60+
4561
if !version.IsAtLeast(MinVersion) {
4662
result.ErrorMsg = fmt.Sprintf("version required at least %v but got %v", MinVersion, version)
4763
result.Instruction = "Please upgrade your database system"
@@ -74,11 +90,10 @@ func NewMySQLServerIDChecker(db *sql.DB, dbinfo *dbutil.DBConfig) Checker {
7490
func (pc *MySQLServerIDChecker) Check(ctx context.Context) *Result {
7591
result := &Result{
7692
Name: pc.Name(),
77-
Desc: "checks whether mysql server_id has been set > 1",
93+
Desc: "check whether mysql server_id has been set > 1",
7894
State: StateFailure,
79-
Extra: fmt.Sprintf("%s:%d", pc.dbinfo.Host, pc.dbinfo.Port),
95+
Extra: fmt.Sprintf("address of db instance - %s:%d", pc.dbinfo.Host, pc.dbinfo.Port),
8096
}
81-
defer log.Infof("check mysql version, result %+v", result)
8297

8398
serverID, err := dbutil.ShowServerID(ctx, pc.db)
8499
if err != nil {

pkg/check/privilege.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
// Copyright 2018 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+
114
package check
215

316
import (
@@ -7,7 +20,6 @@ import (
720
"strings"
821

922
"github.com/juju/errors"
10-
"github.com/ngaut/log"
1123
"github.com/pingcap/tidb-tools/pkg/dbutil"
1224
"github.com/pingcap/tidb/ast"
1325
"github.com/pingcap/tidb/parser"
@@ -33,9 +45,9 @@ func NewSourcePrivilegeChecker(db *sql.DB, dbinfo *dbutil.DBConfig) Checker {
3345
func (pc *SourcePrivilegeChecker) Check(ctx context.Context) *Result {
3446
result := &Result{
3547
Name: pc.Name(),
36-
Desc: "checks data source privileges",
48+
Desc: "check privileges of source DB",
3749
State: StateFailure,
38-
Extra: fmt.Sprintf("%s:%d", pc.dbinfo.Host, pc.dbinfo.Port),
50+
Extra: fmt.Sprintf("address of db instance - %s:%d", pc.dbinfo.Host, pc.dbinfo.Port),
3951
}
4052

4153
grants, err := dbutil.ShowGrants(ctx, pc.db, "", "")
@@ -48,8 +60,6 @@ func (pc *SourcePrivilegeChecker) Check(ctx context.Context) *Result {
4860
return result
4961
}
5062

51-
log.Debugf("grants %+v", grants)
52-
5363
// get username and hostname
5464
node, err := parser.New().ParseOneStmt(grants[0], "", "")
5565
if err != nil {

0 commit comments

Comments
 (0)