Skip to content

Commit

Permalink
Merge branch 'master' of github.com:yibin87/tidb
Browse files Browse the repository at this point in the history
  • Loading branch information
yibin87 committed Feb 24, 2022
2 parents 91aa3a6 + 1636191 commit 3fc5eee
Show file tree
Hide file tree
Showing 630 changed files with 46,310 additions and 38,340 deletions.
28 changes: 0 additions & 28 deletions .github/workflows/bug-closed.yml

This file was deleted.

1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ linters:
- makezero
- durationcheck
- prealloc
- predeclared

linters-settings:
staticcheck:
Expand Down
41 changes: 24 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

include Makefile.common

.PHONY: all clean test gotest server dev benchkv benchraw check checklist parser tidy ddltest build_br build_lightning build_lightning-ctl build_dumpling
.PHONY: all clean test gotest server dev benchkv benchraw check checklist parser tidy ddltest build_br build_lightning build_lightning-ctl build_dumpling ut

default: server buildsucc

Expand All @@ -28,7 +28,7 @@ all: dev server benchkv
parser:
@echo "remove this command later, when our CI script doesn't call it"

dev: checklist check explaintest devgotest gogenerate br_unit_test test_part_parser_dev
dev: checklist check explaintest gogenerate br_unit_test test_part_parser_dev
@>&2 echo "Great, all tests passed."

# Install the check tools.
Expand Down Expand Up @@ -115,34 +115,34 @@ explaintest: server_check
ddltest:
@cd cmd/ddltest && $(GO) test -o ../../bin/ddltest -c

devgotest: failpoint-enable
# grep regex: Filter out all tidb logs starting with:
# - '[20' (like [2021/09/15 ...] [INFO]..)
# - 'PASS:' to ignore passed tests
# - 'ok ' to ignore passed directories
@echo "Running in native mode."
@export log_level=info; export TZ='Asia/Shanghai'; \
$(GOTEST) -ldflags '$(TEST_LDFLAGS)' $(EXTRA_TEST_ARGS) -cover $(PACKAGES_TIDB_TESTS) -check.p true > gotest.log || { $(FAILPOINT_DISABLE); grep -v '^\([[]20\|PASS:\|ok \)' 'gotest.log'; exit 1; }
@$(FAILPOINT_DISABLE)
CLEAN_UT_BINARY := find . -name '*.test.bin'| xargs rm

ut: failpoint-enable tools/bin/ut
tools/bin/ut $(X);
ut: tools/bin/ut tools/bin/xprog failpoint-enable
tools/bin/ut $(X) || { $(FAILPOINT_DISABLE); exit 1; }
@$(FAILPOINT_DISABLE)
@$(CLEAN_UT_BINARY)

gotest: failpoint-enable
@echo "Running in native mode."
@export log_level=info; export TZ='Asia/Shanghai'; \
$(GOTEST) -ldflags '$(TEST_LDFLAGS)' $(EXTRA_TEST_ARGS) -timeout 20m -cover $(PACKAGES_TIDB_TESTS) -coverprofile=coverage.txt -check.p true > gotest.log || { $(FAILPOINT_DISABLE); cat 'gotest.log'; exit 1; }
@$(FAILPOINT_DISABLE)

gotest_in_verify_ci: failpoint-enable tools/bin/gotestsum
gotest_in_verify_ci: tools/bin/xprog tools/bin/ut failpoint-enable
@echo "Running gotest_in_verify_ci"
@mkdir -p $(TEST_COVERAGE_DIR)
@export TZ='Asia/Shanghai'; \
CGO_ENABLED=1 tools/bin/gotestsum --junitfile "$(TEST_COVERAGE_DIR)/tidb-junit-report.xml" -- -v -p $(P) \
-ldflags '$(TEST_LDFLAGS)' $(EXTRA_TEST_ARGS) -coverprofile="$(TEST_COVERAGE_DIR)/tidb_cov.unit_test.out" \
$(PACKAGES_TIDB_TESTS) -check.p true || { $(FAILPOINT_DISABLE); exit 1; }
tools/bin/ut --junitfile "$(TEST_COVERAGE_DIR)/tidb-junit-report.xml" --coverprofile "$(TEST_COVERAGE_DIR)/tidb_cov.unit_test.out" --except unstable.txt || { $(FAILPOINT_DISABLE); exit 1; }
@$(FAILPOINT_DISABLE)
@$(CLEAN_UT_BINARY)

gotest_unstable_in_verify_ci: tools/bin/xprog tools/bin/ut failpoint-enable
@echo "Running gotest_in_verify_ci"
@mkdir -p $(TEST_COVERAGE_DIR)
@export TZ='Asia/Shanghai'; \
tools/bin/ut --junitfile "$(TEST_COVERAGE_DIR)/tidb-junit-report.xml" --coverprofile "$(TEST_COVERAGE_DIR)/tidb_cov.unit_test.out" --only unstable.txt || { $(FAILPOINT_DISABLE); exit 1; }
@$(FAILPOINT_DISABLE)
@$(CLEAN_UT_BINARY)

race: failpoint-enable
@export log_level=debug; \
Expand Down Expand Up @@ -216,6 +216,10 @@ tools/bin/ut: tools/check/ut.go
cd tools/check; \
$(GO) build -o ../bin/ut ut.go

tools/bin/xprog: tools/check/xprog.go
cd tools/check; \
$(GO) build -o ../bin/xprog xprog.go

tools/bin/megacheck: tools/check/go.mod
cd tools/check; \
$(GO) build -o ../bin/megacheck honnef.co/go/tools/cmd/megacheck
Expand Down Expand Up @@ -421,3 +425,6 @@ dumpling_bins:

tools/bin/gotestsum: tools/check/go.mod
cd tools/check && $(GO) build -o ../bin/gotestsum gotest.tools/gotestsum

generate_grafana_scripts:
@cd metrics/grafana && mv tidb_summary.json tidb_summary.json.committed && ./generate_json.sh && diff -u tidb_summary.json.committed tidb_summary.json && rm tidb_summary.json.committed
56 changes: 56 additions & 0 deletions bindinfo/capture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,62 @@ func TestIssue25505(t *testing.T) {
}
}

func TestCaptureUserFilter(t *testing.T) {
store, dom, clean := testkit.CreateMockStoreAndDomain(t)
defer clean()
tk := testkit.NewTestKit(t, store)

stmtsummary.StmtSummaryByDigestMap.Clear()
tk.MustExec("SET GLOBAL tidb_capture_plan_baselines = on")
defer func() {
tk.MustExec("SET GLOBAL tidb_capture_plan_baselines = off")
}()
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int)")

require.True(t, tk.Session().Auth(&auth.UserIdentity{Username: "root", Hostname: "%"}, nil, nil))
tk.MustExec("select * from t where a > 10")
tk.MustExec("select * from t where a > 10")
tk.MustExec("admin capture bindings")
rows := tk.MustQuery("show global bindings").Rows()
require.Len(t, rows, 1)
require.Equal(t, "select * from `test` . `t` where `a` > ?", rows[0][0])

// test user filter
utilCleanBindingEnv(tk, dom)
stmtsummary.StmtSummaryByDigestMap.Clear()
tk.MustExec("insert into mysql.capture_plan_baselines_blacklist(filter_type, filter_value) values('user', 'root')")
tk.MustExec("select * from t where a > 10")
tk.MustExec("select * from t where a > 10")
tk.MustExec("admin capture bindings")
rows = tk.MustQuery("show global bindings").Rows()
require.Len(t, rows, 0) // cannot capture the stmt

// change another user
tk.MustExec(`create user usr1`)
tk.MustExec(`grant all on *.* to usr1 with grant option`)
tk2 := testkit.NewTestKit(t, store)
tk2.MustExec("use test")
require.True(t, tk2.Session().Auth(&auth.UserIdentity{Username: "usr1", Hostname: "%"}, nil, nil))
tk2.MustExec("select * from t where a > 10")
tk2.MustExec("select * from t where a > 10")
tk2.MustExec("admin capture bindings")
rows = tk2.MustQuery("show global bindings").Rows()
require.Len(t, rows, 1) // can capture the stmt

// use user-filter with other types of filter together
utilCleanBindingEnv(tk, dom)
stmtsummary.StmtSummaryByDigestMap.Clear()
tk.MustExec("insert into mysql.capture_plan_baselines_blacklist(filter_type, filter_value) values('user', 'root')")
tk.MustExec("insert into mysql.capture_plan_baselines_blacklist(filter_type, filter_value) values('table', 'test.t')")
tk2.MustExec("select * from t where a > 10")
tk2.MustExec("select * from t where a > 10")
tk2.MustExec("admin capture bindings")
rows = tk2.MustQuery("show global bindings").Rows()
require.Len(t, rows, 0) // filtered by the table filter
}

func TestCaptureFilter(t *testing.T) {
store, dom, clean := testkit.CreateMockStoreAndDomain(t)
defer clean()
Expand Down
46 changes: 25 additions & 21 deletions bindinfo/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,11 @@ func (h *BindHandle) Update(fullLoad bool) (err error) {
}

exec := h.sctx.Context.(sqlexec.RestrictedSQLExecutor)
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
}

// No need to acquire the session context lock for ExecRestrictedStmt, it
// No need to acquire the session context lock for ExecRestrictedSQL, it
// uses another background session.
rows, _, err := exec.ExecRestrictedStmt(context.Background(), stmt)
rows, _, err := exec.ExecRestrictedSQL(context.TODO(), nil, `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 {
h.bindInfo.Unlock()
Expand Down Expand Up @@ -661,6 +657,7 @@ type captureFilter struct {
dbs map[string]struct{}
frequency int64
tables map[stmtctx.TableEntry]struct{}
users map[string]struct{}

fail bool
currentDB string
Expand Down Expand Up @@ -690,24 +687,20 @@ func (cf *captureFilter) Leave(in ast.Node) (out ast.Node, ok bool) {
}

func (cf *captureFilter) isEmpty() bool {
return len(cf.dbs) == 0 && len(cf.tables) == 0
return len(cf.dbs) == 0 && len(cf.tables) == 0 && len(cf.users) == 0
}

func (h *BindHandle) extractCaptureFilterFromStorage() (filter *captureFilter) {
filter = &captureFilter{
dbs: make(map[string]struct{}),
frequency: 1,
tables: make(map[stmtctx.TableEntry]struct{}),
users: make(map[string]struct{}),
}
exec := h.sctx.Context.(sqlexec.RestrictedSQLExecutor)
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
}
// No need to acquire the session context lock for ExecRestrictedStmt, it
// No need to acquire the session context lock for ExecRestrictedSQL, it
// uses another background session.
rows, _, err := exec.ExecRestrictedStmt(context.TODO(), stmt)
rows, _, err := exec.ExecRestrictedSQL(context.TODO(), nil, `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 load mysql.capture_plan_baselines_blacklist", zap.Error(err))
return
Expand All @@ -729,6 +722,8 @@ func (h *BindHandle) extractCaptureFilterFromStorage() (filter *captureFilter) {
Table: strs[1],
}
filter.tables[tblEntry] = struct{}{}
case "user":
filter.users[valStr] = struct{}{}
case "frequency":
f, err := strconv.ParseInt(valStr, 10, 64)
if err != nil {
Expand Down Expand Up @@ -771,6 +766,19 @@ func (h *BindHandle) CaptureBaselines() {
if captureFilter.fail {
continue
}

if len(captureFilter.users) > 0 {
filteredByUser := true
for user := range bindableStmt.Users {
if _, ok := captureFilter.users[user]; !ok {
filteredByUser = false // some user not in the black-list has processed this stmt
break
}
}
if filteredByUser {
continue
}
}
}
dbName := utilparser.GetDefaultDB(stmt, bindableStmt.Schema)
normalizedSQL, digest := parser.NormalizeDigest(utilparser.RestoreWithDefaultDB(stmt, dbName, bindableStmt.Query))
Expand Down Expand Up @@ -926,9 +934,9 @@ func (h *BindHandle) SaveEvolveTasksToStore() {
}

func getEvolveParameters(ctx sessionctx.Context) (time.Duration, time.Time, time.Time, error) {
stmt, err := ctx.(sqlexec.RestrictedSQLExecutor).ParseWithParams(
rows, _, err := ctx.(sqlexec.RestrictedSQLExecutor).ExecRestrictedSQL(
context.TODO(),
true,
nil,
"SELECT variable_name, variable_value FROM mysql.global_variables WHERE variable_name IN (%?, %?, %?)",
variable.TiDBEvolvePlanTaskMaxTime,
variable.TiDBEvolvePlanTaskStartTime,
Expand All @@ -937,10 +945,6 @@ func getEvolveParameters(ctx sessionctx.Context) (time.Duration, time.Time, time
if err != nil {
return 0, time.Time{}, time.Time{}, err
}
rows, _, err := ctx.(sqlexec.RestrictedSQLExecutor).ExecRestrictedStmt(context.TODO(), stmt)
if err != nil {
return 0, time.Time{}, time.Time{}, err
}
maxTime, startTimeStr, endTimeStr := int64(variable.DefTiDBEvolvePlanTaskMaxTime), variable.DefTiDBEvolvePlanTaskStartTime, variable.DefAutoAnalyzeEndTime
for _, row := range rows {
switch row.GetString(0) {
Expand Down
Loading

0 comments on commit 3fc5eee

Please sign in to comment.