Skip to content

Commit

Permalink
statistics: rewrite unlock stats handler (#46478)
Browse files Browse the repository at this point in the history
ref #46351
  • Loading branch information
Rustin170506 authored Aug 30, 2023
1 parent 4f2f5e1 commit 187573b
Show file tree
Hide file tree
Showing 7 changed files with 321 additions and 190 deletions.
6 changes: 2 additions & 4 deletions executor/lockstats/lock_stats_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,12 @@ func (e *LockExec) Next(_ context.Context, _ *chunk.Chunk) error {
return err
}

sv := e.Ctx().GetSessionVars()

msg, err := h.AddLockedTables(tids, pids, e.Tables, sv.MaxChunkSize)
msg, err := h.AddLockedTables(tids, pids, e.Tables)
if err != nil {
return err
}
if msg != "" {
sv.StmtCtx.AppendWarning(errors.New(msg))
e.Ctx().GetSessionVars().StmtCtx.AppendWarning(errors.New(msg))
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion statistics/handle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ go_library(
"handle_hist.go",
"historical_stats_handler.go",
"lock_stats_handler.go",
"unlock_stats_handler.go",
"update.go",
],
importpath = "github.com/pingcap/tidb/statistics/handle",
Expand Down Expand Up @@ -67,7 +68,7 @@ go_test(
embed = [":handle"],
flaky = True,
race = "on",
shard_count = 27,
shard_count = 29,
deps = [
"//config",
"//domain",
Expand Down
20 changes: 18 additions & 2 deletions statistics/handle/handletest/statslock/stats_lcok_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func TestStatsLockAndUnlockTable(t *testing.T) {
require.Equal(t, int64(2), tblStats2.RealtimeCount)
}

func TestStatsLockTableRepeatedly(t *testing.T) {
func TestStatsLockTableAndUnlockTableRepeatedly(t *testing.T) {
restore := config.RestoreFunc()
defer restore()
config.UpdateGlobal(func(conf *config.Config) {
Expand Down Expand Up @@ -113,14 +113,30 @@ func TestStatsLockTableRepeatedly(t *testing.T) {
// Lock the table again and check the warning.
tableLocked1 := handle.GetTableLockedAndClearForTest()
tk.MustExec("lock stats t")
tk.MustQuery("show warnings").Sort().Check(testkit.Rows(
tk.MustQuery("show warnings").Check(testkit.Rows(
"Warning 1105 skip locking locked table: test.t",
))

err = handle.LoadLockedTables()
require.Nil(t, err)
tableLocked2 := handle.GetTableLockedAndClearForTest()
require.Equal(t, tableLocked1, tableLocked2)

// Unlock the table.
tk.MustExec("unlock stats t")
rows = tk.MustQuery("select count(*) from mysql.stats_table_locked").Rows()
num, _ = strconv.Atoi(rows[0][0].(string))
require.Equal(t, num, 0)

tk.MustExec("analyze table test.t")
tblStats2 := handle.GetTableStats(tbl.Meta())
require.Equal(t, int64(2), tblStats2.RealtimeCount)

// Unlock the table again and check the warning.
tk.MustExec("unlock stats t")
tk.MustQuery("show warnings").Check(testkit.Rows(
"Warning 1105 skip unlocking unlocked table: test.t",
))
}

func TestStatsLockAndUnlockTables(t *testing.T) {
Expand Down
Loading

0 comments on commit 187573b

Please sign in to comment.