Skip to content

Commit

Permalink
Merge branch 'master' into ut-statistics-handle
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkingrei authored Jan 8, 2022
2 parents a623812 + 8c70432 commit 0b4d979
Show file tree
Hide file tree
Showing 82 changed files with 1,264 additions and 476 deletions.
7 changes: 4 additions & 3 deletions bindinfo/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (h *BindHandle) Update(fullLoad bool) (err error) {
}

exec := h.sctx.Context.(sqlexec.RestrictedSQLExecutor)
stmt, err := exec.ParseWithParamsInternal(context.TODO(), `SELECT original_sql, bind_sql, default_db, status, create_time, update_time, charset, collation, source
stmt, err := exec.ParseWithParams(context.TODO(), true, `SELECT original_sql, bind_sql, default_db, status, create_time, update_time, charset, collation, source
FROM mysql.bind_info WHERE update_time > %? ORDER BY update_time, create_time`, updateTime)
if err != nil {
return err
Expand Down Expand Up @@ -697,7 +697,7 @@ func (h *BindHandle) extractCaptureFilterFromStorage() (filter *captureFilter) {
tables: make(map[stmtctx.TableEntry]struct{}),
}
exec := h.sctx.Context.(sqlexec.RestrictedSQLExecutor)
stmt, err := exec.ParseWithParamsInternal(context.TODO(), `SELECT filter_type, filter_value FROM mysql.capture_plan_baselines_blacklist order by filter_type`)
stmt, err := exec.ParseWithParams(context.TODO(), true, `SELECT filter_type, filter_value FROM mysql.capture_plan_baselines_blacklist order by filter_type`)
if err != nil {
logutil.BgLogger().Warn("[sql-bind] failed to parse query for mysql.capture_plan_baselines_blacklist load", zap.Error(err))
return
Expand Down Expand Up @@ -923,8 +923,9 @@ func (h *BindHandle) SaveEvolveTasksToStore() {
}

func getEvolveParameters(ctx sessionctx.Context) (time.Duration, time.Time, time.Time, error) {
stmt, err := ctx.(sqlexec.RestrictedSQLExecutor).ParseWithParamsInternal(
stmt, err := ctx.(sqlexec.RestrictedSQLExecutor).ParseWithParams(
context.TODO(),
true,
"SELECT variable_name, variable_value FROM mysql.global_variables WHERE variable_name IN (%?, %?, %?)",
variable.TiDBEvolvePlanTaskMaxTime,
variable.TiDBEvolvePlanTaskStartTime,
Expand Down
44 changes: 18 additions & 26 deletions br/pkg/lightning/log/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,60 @@ package log_test
import (
"regexp"
"strings"
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/tidb/br/pkg/lightning/log"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

var _ = Suite(&testFilterSuite{})

type testFilterSuite struct{}

func (s *testFilterSuite) TestFilter(c *C) {
func TestFilter(t *testing.T) {
logger, buffer := log.MakeTestLogger()
logger.Warn("the message", zap.Int("number", 123456), zap.Ints("array", []int{7, 8, 9}))
c.Assert(
buffer.Stripped(), Equals,
`{"$lvl":"WARN","$msg":"the message","number":123456,"array":[7,8,9]}`,
)

require.Equal(t, `{"$lvl":"WARN","$msg":"the message","number":123456,"array":[7,8,9]}`, buffer.Stripped())

logger, buffer = log.MakeTestLogger(zap.WrapCore(func(c zapcore.Core) zapcore.Core {
return log.NewFilterCore(c, "github.com/pingcap/br/")
}), zap.AddCaller())
logger.Warn("the message", zap.Int("number", 123456), zap.Ints("array", []int{7, 8, 9}))
c.Assert(buffer.Stripped(), HasLen, 0)
require.Len(t, buffer.Stripped(), 0)

logger, buffer = log.MakeTestLogger(zap.WrapCore(func(c zapcore.Core) zapcore.Core {
return log.NewFilterCore(c, "github.com/pingcap/tidb/br/").With([]zap.Field{zap.String("a", "b")})
}), zap.AddCaller())
logger.Warn("the message", zap.Int("number", 123456), zap.Ints("array", []int{7, 8, 9}))
c.Assert(
buffer.Stripped(), Equals,
`{"$lvl":"WARN","$msg":"the message","a":"b","number":123456,"array":[7,8,9]}`,
)
require.Equal(t, `{"$lvl":"WARN","$msg":"the message","a":"b","number":123456,"array":[7,8,9]}`, buffer.Stripped())

logger, buffer = log.MakeTestLogger(zap.WrapCore(func(c zapcore.Core) zapcore.Core {
return log.NewFilterCore(c, "github.com/pingcap/br/").With([]zap.Field{zap.String("a", "b")})
}), zap.AddCaller())
logger.Warn("the message", zap.Int("number", 123456), zap.Ints("array", []int{7, 8, 9}))
c.Assert(buffer.Stripped(), HasLen, 0)
require.Len(t, buffer.Stripped(), 0)

// Fields won't trigger filter.
logger, buffer = log.MakeTestLogger(zap.WrapCore(func(c zapcore.Core) zapcore.Core {
return log.NewFilterCore(c, "github.com/pingcap/check/").With([]zap.Field{zap.String("a", "b")})
}), zap.AddCaller())
logger.Warn("the message", zap.String("stack", "github.com/pingcap/tidb/br/"))
c.Assert(buffer.Stripped(), HasLen, 0)
require.Len(t, buffer.Stripped(), 0)
}

// PASS: filter_test.go:82: testFilterSuite.BenchmarkFilterRegexMatchString 1000000 1163 ns/op
// PASS: filter_test.go:64: testFilterSuite.BenchmarkFilterStringsContains 10000000 159 ns/op
// BenchmarkFilterStringsContains-16 16693887 66.68 ns/op
// BenchmarkFilterRegexMatchString-16 2350828 510.6 ns/op
//
// Run `go test github.com/pingcap/tidb/br/pkg/lightning/log -check.b -test.v` to get benchmark result.
func (s *testFilterSuite) BenchmarkFilterStringsContains(c *C) {
c.ResetTimer()
// Run `go test -run='^$' -bench=. -v github.com/pingcap/tidb/br/pkg/lightning/log` to get benchmark result.
func BenchmarkFilterStringsContains(b *testing.B) {
b.ResetTimer()

inputs := []string{
"github.com/pingcap/tidb/some/package/path",
"github.com/tikv/pd/some/package/path",
"github.com/pingcap/tidb/br/some/package/path",
}
filters := []string{"github.com/pingcap/tidb/", "github.com/tikv/pd/"}
for i := 0; i < c.N; i++ {
for i := 0; i < b.N; i++ {
for i := range inputs {
for j := range filters {
_ = strings.Contains(inputs[i], filters[j])
Expand All @@ -75,16 +67,16 @@ func (s *testFilterSuite) BenchmarkFilterStringsContains(c *C) {
}
}

func (s *testFilterSuite) BenchmarkFilterRegexMatchString(c *C) {
c.ResetTimer()
func BenchmarkFilterRegexMatchString(b *testing.B) {
b.ResetTimer()

inputs := []string{
"github.com/pingcap/tidb/some/package/path",
"github.com/tikv/pd/some/package/path",
"github.com/pingcap/tidb/br/some/package/path",
}
filters := regexp.MustCompile(`github.com/(pingcap/tidb|tikv/pd)/`)
for i := 0; i < c.N; i++ {
for i := 0; i < b.N; i++ {
for i := range inputs {
_ = filters.MatchString(inputs[i])
}
Expand Down
43 changes: 0 additions & 43 deletions br/pkg/lightning/log/log_serial_test.go

This file was deleted.

60 changes: 42 additions & 18 deletions br/pkg/lightning/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,61 @@
package log_test

import (
"io"
"os"
"testing"

. "github.com/pingcap/check"
"github.com/pingcap/tidb/br/pkg/lightning/log"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)

func TestLog(t *testing.T) {
TestingT(t)
}

type logSuite struct{}

var _ = Suite(&logSuite{})

func (s *logSuite) TestConfigAdjust(c *C) {
func TestConfigAdjust(t *testing.T) {
cfg := &log.Config{}
cfg.Adjust()
c.Assert(cfg.Level, Equals, "info")
require.Equal(t, "info", cfg.Level)

cfg.File = "."
err := log.InitLogger(cfg, "info")
c.Assert(err, ErrorMatches, "can't use directory as log file name")
log.L().Named("xx")
require.EqualError(t, err, "can't use directory as log file name")
}

func (s *logSuite) TestTestLogger(c *C) {
func TestTestLogger(t *testing.T) {
logger, buffer := log.MakeTestLogger()
logger.Warn("the message", zap.Int("number", 123456), zap.Ints("array", []int{7, 8, 9}))
c.Assert(
buffer.Stripped(), Equals,
`{"$lvl":"WARN","$msg":"the message","number":123456,"array":[7,8,9]}`,
)
require.Equal(t, `{"$lvl":"WARN","$msg":"the message","number":123456,"array":[7,8,9]}`, buffer.Stripped())
}

func TestInitStdoutLogger(t *testing.T) {
r, w, err := os.Pipe()
require.NoError(t, err)
oldStdout := os.Stdout
os.Stdout = w

msg := "logger is initialized to stdout"
outputC := make(chan string, 1)
go func() {
buf := make([]byte, 4096)
n := 0
for {
nn, err := r.Read(buf[n:])
if nn == 0 || err == io.EOF {
break
}
require.NoError(t, err)
n += nn
}
outputC <- string(buf[:n])
}()

logCfg := &log.Config{File: "-"}
err = log.InitLogger(logCfg, "info")
require.NoError(t, err)
log.L().Info(msg)

os.Stdout = oldStdout
require.NoError(t, w.Close())
output := <-outputC
require.NoError(t, r.Close())
require.Contains(t, output, msg)
}
4 changes: 1 addition & 3 deletions br/pkg/lightning/restore/checksum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ import (
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/memory"
tmock "github.com/pingcap/tidb/util/mock"
"github.com/pingcap/tidb/util/trxevents"
"github.com/pingcap/tipb/go-tipb"
"github.com/tikv/client-go/v2/oracle"
pd "github.com/tikv/pd/client"
Expand Down Expand Up @@ -393,7 +391,7 @@ type mockChecksumKVClient struct {
}

// a mock client for checksum request
func (c *mockChecksumKVClient) Send(ctx context.Context, req *kv.Request, vars interface{}, sessionMemTracker *memory.Tracker, enabledRateLimitAction bool, eventCb trxevents.EventCallback) kv.Response {
func (c *mockChecksumKVClient) Send(ctx context.Context, req *kv.Request, vars interface{}, option *kv.ClientSendOption) kv.Response {
if c.curErrCount < c.maxErrCount {
c.curErrCount++
return &mockErrorResponse{err: "tikv timeout"}
Expand Down
56 changes: 56 additions & 0 deletions cmd/explaintest/r/collation_check_use_collation.result
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,62 @@ a
select a as a_col from t where t.a = (select a collate utf8mb4_general_ci from t1);
a_col
a
drop table if exists t;
create table t(a enum('a', 'b'), b varchar(20));
insert into t values ("a", "b");
select * from t where a in (a);
a b
a b
drop table if exists t;
create table t(a enum('a', 'b') charset utf8mb4 collate utf8mb4_general_ci, b varchar(20));
insert into t values ("B", "b");
select * from t where 'B' collate utf8mb4_general_ci in (a);
a b
b b
select * from t where 'B' collate utf8mb4_bin in (a);
a b
select * from t where 'B' collate utf8mb4_bin in (a, b);
a b
select * from t where 'B' collate utf8mb4_bin in (a, "a", 1);
a b
select * from t where 'B' collate utf8mb4_bin in (a, "B", 1);
a b
b b
select * from t where 1 in (a);
a b
select * from t where 2 in (a);
a b
b b
select * from t where 1 in (a, 0);
a b
drop table if exists t;
create table t(a set('a', 'b'), b varchar(20));
insert into t values ("a", "b");
select * from t where a in (a);
a b
a b
drop table if exists t;
create table t(a set('a', 'b') charset utf8mb4 collate utf8mb4_general_ci, b varchar(20));
insert into t values ("B", "b");
select * from t where 'B' collate utf8mb4_general_ci in (a);
a b
b b
select * from t where 'B' collate utf8mb4_bin in (a);
a b
select * from t where 'B' collate utf8mb4_bin in (a, b);
a b
select * from t where 'B' collate utf8mb4_bin in (a, "a", 1);
a b
select * from t where 'B' collate utf8mb4_bin in (a, "B", 1);
a b
b b
select * from t where 1 in (a);
a b
select * from t where 2 in (a);
a b
b b
select * from t where 1 in (a, 0);
a b
drop table if exists tbl_2;
create table tbl_2 ( col_20 bigint not null , col_21 smallint not null , col_22 decimal(24,10) default null , col_23 tinyint default 71 not null , col_24 bigint not null , col_25 tinyint default 18 , col_26 varchar(330) collate utf8_bin not null , col_27 char(77) collate utf8mb4_unicode_ci , col_28 char(46) collate utf8_general_ci not null , col_29 smallint unsigned not null , primary key idx_13 ( col_27(5) ) , key idx_14 ( col_24 ) , unique key idx_15 ( col_23,col_21,col_28,col_29,col_24 ) ) collate utf8_bin ;
insert ignore into tbl_2 values ( 5888267793391993829,5371,94.63,-109,5728076076919247337,89,'WUicqUTgdGJcjbC','SapBPqczTWWSN','xUSwH',49462 );
Expand Down
Loading

0 comments on commit 0b4d979

Please sign in to comment.