Skip to content
This repository was archived by the owner on Nov 24, 2023. It is now read-only.

Commit 11861f7

Browse files
authored
cherrypick #2065 #2094 and #2133 to release-2.0.7 (#2143)
1 parent d2051ed commit 11861f7

File tree

21 files changed

+721
-744
lines changed

21 files changed

+721
-744
lines changed

.golangci.yml

+7
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ issues:
114114
- EXC0002 # golint
115115
- EXC0003
116116

117+
exclude-rules:
118+
# TODO Remove it.
119+
# Exclude some staticcheck messages
120+
- linters:
121+
- staticcheck
122+
text: "SA1019:"
123+
117124
# Fix found issues (if it's supported by the linter)
118125
fix: false
119126

dm/ctl/common/util.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"sync"
2626
"time"
2727

28-
"github.com/golang/protobuf/jsonpb"
28+
"github.com/gogo/protobuf/jsonpb"
2929
"github.com/golang/protobuf/proto"
3030
"github.com/pingcap/errors"
3131
"github.com/pingcap/failpoint"

dumpling/dumpling_test.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ import (
3333

3434
var _ = Suite(&testDumplingSuite{})
3535

36+
const (
37+
testDumplingSchemaName = "INFORMATION_SCHEMA"
38+
testDumplingTableName = "TABLES"
39+
)
40+
3641
func TestSuite(t *testing.T) {
3742
TestingT(t)
3843
}
@@ -72,7 +77,11 @@ func (d *testDumplingSuite) SetUpSuite(c *C) {
7277
Dir: dir,
7378
},
7479
BAList: &filter.Rules{
75-
DoDBs: []string{"information_schema"},
80+
DoDBs: []string{testDumplingSchemaName},
81+
DoTables: []*filter.Table{{
82+
Schema: testDumplingSchemaName,
83+
Name: testDumplingTableName,
84+
}},
7685
},
7786
}
7887
c.Assert(log.InitLogger(&log.Config{}), IsNil)

go.mod

+22-21
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,28 @@ module github.com/pingcap/dm
33
require (
44
github.com/BurntSushi/toml v0.3.1
55
github.com/DATA-DOG/go-sqlmock v1.5.0
6-
github.com/chaos-mesh/go-sqlsmith v0.0.0-20201120053641-47c50b530c01
6+
github.com/chaos-mesh/go-sqlsmith v0.0.0-20210914111832-b3d328d69449
77
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
88
github.com/coreos/go-semver v0.3.0
99
github.com/docker/go-units v0.4.0
1010
github.com/dustin/go-humanize v1.0.0
1111
github.com/go-mysql-org/go-mysql v1.1.3-0.20210705101833-83965e516929
12-
github.com/go-sql-driver/mysql v1.5.0
12+
github.com/go-sql-driver/mysql v1.6.0
1313
github.com/gogo/gateway v1.1.0
1414
github.com/gogo/protobuf v1.3.2
15-
github.com/golang/mock v1.4.4
16-
github.com/golang/protobuf v1.3.4
17-
github.com/google/uuid v1.1.1
18-
github.com/grpc-ecosystem/grpc-gateway v1.14.3
15+
github.com/golang/mock v1.6.0
16+
github.com/golang/protobuf v1.5.2
17+
github.com/google/uuid v1.1.2
18+
github.com/grpc-ecosystem/grpc-gateway v1.16.0
1919
github.com/kami-zh/go-capturer v0.0.0-20171211120116-e492ea43421d
2020
github.com/pingcap/check v0.0.0-20200212061837-5e12011dc712
21-
github.com/pingcap/dumpling v0.0.0-20210407092432-e1cfe4ce0a53
22-
github.com/pingcap/errors v0.11.5-0.20201126102027-b0a155152ca3
21+
github.com/pingcap/dumpling v0.0.0-20210914144241-99aca9186bc8
22+
github.com/pingcap/errors v0.11.5-0.20210513014640-40f9a1999b3b
2323
github.com/pingcap/failpoint v0.0.0-20210316064728-7acb0f0a3dfd
24-
github.com/pingcap/log v0.0.0-20210317133921-96f4fcab92a4
25-
github.com/pingcap/parser v0.0.0-20210415081931-48e7f467fd74
26-
github.com/pingcap/tidb v1.1.0-beta.0.20210330094614-60111e1c4b6f
27-
github.com/pingcap/tidb-tools v5.2.0-alpha.0.20210721090336-4921149b5e5c+incompatible
24+
github.com/pingcap/log v0.0.0-20210906054005-afc726e70354
25+
github.com/pingcap/parser v0.0.0-20210907051057-948434fa20e4
26+
github.com/pingcap/tidb v1.1.0-beta.0.20210914112841-6ebfe8aa4257
27+
github.com/pingcap/tidb-tools v5.2.0-alpha.0.20210727084616-915b22e4d42c+incompatible
2828
github.com/prometheus/client_golang v1.5.1
2929
github.com/rakyll/statik v0.1.6
3030
github.com/shopspring/decimal v0.0.0-20200105231215-408a2507e114
@@ -34,18 +34,19 @@ require (
3434
github.com/syndtr/goleveldb v1.0.1-0.20190625010220-02440ea7a285
3535
github.com/tidwall/gjson v1.6.1
3636
github.com/tidwall/sjson v1.1.2
37-
github.com/tikv/pd v1.1.0-beta.0.20210323121136-78679e5e209d
37+
github.com/tikv/pd v1.1.0-beta.0.20210818082359-acba1da0018d
3838
github.com/unrolled/render v1.0.1
3939
go.etcd.io/etcd v0.5.0-alpha.5.0.20200824191128-ae9734ed278b
40-
go.uber.org/atomic v1.7.0
41-
go.uber.org/zap v1.16.0
42-
golang.org/x/exp v0.0.0-20200513190911-00229845015e // indirect
43-
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
44-
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4
45-
golang.org/x/time v0.0.0-20191024005414-555d28b269f0
46-
google.golang.org/genproto v0.0.0-20200224152610-e50cd9704f63
47-
google.golang.org/grpc v1.27.1
40+
go.uber.org/atomic v1.9.0
41+
go.uber.org/zap v1.19.0
42+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
43+
golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069
44+
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba
45+
google.golang.org/genproto v0.0.0-20210825212027-de86158e7fda
46+
google.golang.org/grpc v1.40.0
4847
gopkg.in/yaml.v2 v2.4.0
4948
)
5049

50+
replace google.golang.org/grpc => google.golang.org/grpc v1.29.1
51+
5152
go 1.16

go.sum

+441-658
Large diffs are not rendered by default.

pkg/log/log.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ import (
1818
"fmt"
1919
"strings"
2020

21+
"github.com/pingcap/tidb/util/logutil"
22+
2123
"github.com/pingcap/errors"
2224
pclog "github.com/pingcap/log"
23-
"github.com/pingcap/tidb/util/logutil"
2425
"go.uber.org/zap"
2526
"go.uber.org/zap/zapcore"
2627

@@ -104,11 +105,7 @@ var (
104105

105106
// InitLogger initializes DM's and also the TiDB library's loggers.
106107
func InitLogger(cfg *Config) error {
107-
err := logutil.InitLogger(&logutil.LogConfig{Config: pclog.Config{Level: cfg.Level}})
108-
if err != nil {
109-
return terror.ErrInitLoggerFail.Delegate(err)
110-
}
111-
108+
// init DM logger
112109
logger, props, err := pclog.InitLogger(&pclog.Config{
113110
Level: cfg.Level,
114111
Format: cfg.Format,
@@ -128,7 +125,14 @@ func InitLogger(cfg *Config) error {
128125
appLogger = Logger{logger.WithOptions(zap.AddStacktrace(zap.DPanicLevel))}
129126
appLevel = props.Level
130127
appProps = props
131-
128+
// init and set tidb slow query logger to stdout if log level is debug
129+
if cfg.Level == "debug" {
130+
slowQueryLogger := zap.NewExample()
131+
slowQueryLogger = slowQueryLogger.With(zap.String("component", "slow query logger"))
132+
logutil.SlowQueryLogger = slowQueryLogger
133+
} else {
134+
logutil.SlowQueryLogger = zap.NewNop()
135+
}
132136
return nil
133137
}
134138

pkg/log/log_test.go

+67
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,15 @@
1414
package log
1515

1616
import (
17+
"bytes"
1718
"context"
19+
"encoding/json"
20+
"io"
21+
"os"
22+
"strings"
1823
"testing"
1924

25+
"github.com/pingcap/tidb/util/logutil"
2026
"go.uber.org/zap"
2127
"go.uber.org/zap/zapcore"
2228
"go.uber.org/zap/zaptest"
@@ -83,3 +89,64 @@ func (s *testLogSuite) TestLogLevel(c *C) {
8389
c.Assert(L().Check(zap.WarnLevel, "This is a warn log"), NotNil)
8490
c.Assert(L().Check(zap.DebugLevel, "This is a debug log"), IsNil)
8591
}
92+
93+
func captureStdout(f func()) ([]string, error) {
94+
r, w, _ := os.Pipe()
95+
stdout := os.Stdout
96+
os.Stdout = w
97+
98+
f()
99+
100+
var buf bytes.Buffer
101+
output := make(chan string, 1)
102+
errs := make(chan error, 1)
103+
104+
go func() {
105+
_, err := io.Copy(&buf, r)
106+
output <- buf.String()
107+
errs <- err
108+
r.Close()
109+
}()
110+
111+
os.Stdout = stdout
112+
w.Close()
113+
return strings.Split(<-output, "\n"), <-errs
114+
}
115+
116+
func (s *testLogSuite) TestInitSlowQueryLoggerInDebugLevel(c *C) {
117+
// test slow query logger can write debug log
118+
logLevel := "debug"
119+
cfg := &Config{Level: logLevel, Format: "json"}
120+
cfg.Adjust()
121+
output, err := captureStdout(func() {
122+
c.Assert(InitLogger(cfg), IsNil)
123+
logutil.SlowQueryLogger.Debug("this is test info")
124+
appLogger.Debug("this is from applogger")
125+
})
126+
c.Assert(err, IsNil)
127+
c.Assert(output[0], Matches, ".*this is test info.*component.*slow query logger.*")
128+
c.Assert(output[1], Matches, ".*this is from applogger.*")
129+
// test log is json formart
130+
type jsonLog struct {
131+
Component string `json:"component"`
132+
}
133+
oneLog := jsonLog{}
134+
c.Assert(json.Unmarshal([]byte(output[0]), &oneLog), IsNil)
135+
c.Assert(oneLog.Component, Equals, "slow query logger")
136+
}
137+
138+
func (s *testLogSuite) TestInitSlowQueryLoggerNotInDebugLevel(c *C) {
139+
// test slow query logger can not write log in other log level
140+
logLevel := "info"
141+
cfg := &Config{Level: logLevel, Format: "json"}
142+
cfg.Adjust()
143+
output, err := captureStdout(func() {
144+
c.Assert(InitLogger(cfg), IsNil)
145+
logutil.SlowQueryLogger.Info("this is test info")
146+
appLogger.Info("this is from applogger")
147+
})
148+
c.Assert(err, IsNil)
149+
c.Assert(output, HasLen, 2)
150+
c.Assert(output[0], Matches, ".*this is from applogger.*")
151+
c.Assert(output[1], Equals, "") // no output
152+
}

pkg/schema/tracker.go

+16-11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/pingcap/errors"
2222
"github.com/pingcap/parser/ast"
2323
"github.com/pingcap/parser/model"
24+
"github.com/pingcap/parser/terror"
2425
"github.com/pingcap/tidb-tools/pkg/dbutil"
2526
"github.com/pingcap/tidb-tools/pkg/filter"
2627
tidbConfig "github.com/pingcap/tidb/config"
@@ -29,6 +30,7 @@ import (
2930
"github.com/pingcap/tidb/infoschema"
3031
"github.com/pingcap/tidb/kv"
3132
"github.com/pingcap/tidb/session"
33+
"github.com/pingcap/tidb/sessionctx/variable"
3234
"github.com/pingcap/tidb/store/mockstore"
3335
"go.uber.org/zap"
3436

@@ -40,17 +42,14 @@ import (
4042
const (
4143
// TiDBClusteredIndex is the variable name for clustered index.
4244
TiDBClusteredIndex = "tidb_enable_clustered_index"
43-
// TiDBChangeColumnType is the variable name for column type changing.
44-
TiDBChangeColumnType = "tidb_enable_change_column_type"
4545
)
4646

4747
var (
4848
// don't read clustered index variable from downstream because it may changed during syncing
4949
// we always using OFF tidb_enable_clustered_index unless user set it in config.
5050
downstreamVars = []string{"sql_mode", "tidb_skip_utf8_check"}
5151
defaultGlobalVars = map[string]string{
52-
TiDBClusteredIndex: "OFF",
53-
TiDBChangeColumnType: "ON", // NOTE The default value of tidb_enable_change_column_type was changed to ON after the release of TiDB 5.1.
52+
TiDBClusteredIndex: "OFF",
5453
}
5554
)
5655

@@ -67,11 +66,11 @@ type Tracker struct {
6766
func NewTracker(ctx context.Context, task string, sessionCfg map[string]string, tidbConn *conn.BaseConn) (*Tracker, error) {
6867
// NOTE: tidb uses a **global** config so can't isolate tracker's config from each other. If that isolation is needed,
6968
// we might SetGlobalConfig before every call to tracker, or use some patch like https://github.com/bouk/monkey
70-
toSet := tidbConfig.NewConfig()
71-
// bypass wait time of https://github.com/pingcap/tidb/pull/20550
72-
toSet.TiKVClient.AsyncCommit.SafeWindow = 0
73-
toSet.TiKVClient.AsyncCommit.AllowedClockDrift = 0
74-
tidbConfig.StoreGlobalConfig(toSet)
69+
tidbConfig.UpdateGlobal(func(conf *tidbConfig.Config) {
70+
// bypass wait time of https://github.com/pingcap/tidb/pull/20550
71+
conf.TiKVClient.AsyncCommit.SafeWindow = 0
72+
conf.TiKVClient.AsyncCommit.AllowedClockDrift = 0
73+
})
7574

7675
if len(sessionCfg) == 0 {
7776
sessionCfg = make(map[string]string)
@@ -111,6 +110,7 @@ func NewTracker(ctx context.Context, task string, sessionCfg map[string]string,
111110

112111
// avoid data race and of course no use in DM
113112
domain.RunAutoAnalyze = false
113+
session.DisableStats4Test()
114114

115115
dom, err := session.BootstrapSession(store)
116116
if err != nil {
@@ -131,13 +131,18 @@ func NewTracker(ctx context.Context, task string, sessionCfg map[string]string,
131131
}
132132

133133
for k, v := range sessionCfg {
134-
err = se.GetSessionVars().SetSystemVar(k, v)
134+
err = se.GetSessionVars().SetSystemVarWithRelaxedValidation(k, v)
135135
if err != nil {
136+
// when user set some unsupported variable, we just ignore it
137+
if terror.ErrorEqual(err, variable.ErrUnknownSystemVar) {
138+
log.L().Warn("can not set this variable", zap.Error(err))
139+
continue
140+
}
136141
return nil, err
137142
}
138143
}
139144
for k, v := range globalVarsToSet {
140-
err = se.GetSessionVars().SetSystemVar(k, v)
145+
err = se.GetSessionVars().SetSystemVarWithRelaxedValidation(k, v)
141146
if err != nil {
142147
return nil, err
143148
}

pkg/utils/common.go

+15-4
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,10 @@ func UnpackTableID(id string) (string, string) {
265265

266266
type session struct {
267267
sessionctx.Context
268-
vars *variable.SessionVars
269-
values map[fmt.Stringer]interface{}
270-
271-
mu sync.RWMutex
268+
vars *variable.SessionVars
269+
values map[fmt.Stringer]interface{}
270+
builtinFunctionUsage map[string]uint32
271+
mu sync.RWMutex
272272
}
273273

274274
// GetSessionVars implements the sessionctx.Context interface.
@@ -291,6 +291,16 @@ func (se *session) Value(key fmt.Stringer) interface{} {
291291
return value
292292
}
293293

294+
// GetInfoSchema implements the sessionctx.Context interface.
295+
func (se *session) GetInfoSchema() sessionctx.InfoschemaMetaVersion {
296+
return nil
297+
}
298+
299+
// GetBuiltinFunctionUsage implements the sessionctx.Context interface.
300+
func (se *session) GetBuiltinFunctionUsage() map[string]uint32 {
301+
return se.builtinFunctionUsage
302+
}
303+
294304
// UTCSession can be used as a sessionctx.Context, with UTC timezone.
295305
var UTCSession *session
296306

@@ -300,6 +310,7 @@ func init() {
300310
vars.StmtCtx.TimeZone = time.UTC
301311
UTCSession.vars = vars
302312
UTCSession.values = make(map[fmt.Stringer]interface{}, 1)
313+
UTCSession.builtinFunctionUsage = make(map[string]uint32)
303314
}
304315

305316
// AdjustBinaryProtocolForDatum converts the data in binlog to TiDB datum.

0 commit comments

Comments
 (0)