Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

statistics: rightly deal with timout when to send sync load (#57712) #57754

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions statistics/handle/handle_hist.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import (
)

// RetryCount is the max retry count for a sync load task.
const RetryCount = 3
const RetryCount = 2

var globalStatsSyncLoadSingleFlight singleflight.Group

Expand Down Expand Up @@ -95,8 +95,12 @@ func (h *Handle) SendLoadRequests(sc *stmtctx.StatementContext, neededHistItems
}
select {
case h.StatsLoad.NeededItemsCh <- task:
result := <-task.ResultCh
return result, nil
select {
case <-timer.C:
return nil, errors.New("sync load took too long to return")
case result := <-task.ResultCh:
return result, nil
}
case <-timer.C:
return nil, errors.New("sync load stats channel is full and timeout sending task to channel")
}
Expand Down
44 changes: 44 additions & 0 deletions statistics/handle/handle_hist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,47 @@ SELECT * FROM EmployeeGenerator;
tk.MustQuery("show stats_histograms where table_name='employees3' and Column_name='PRIMARY' and Partition_name='global'").CheckContain("19958")
}
}

func TestSendLoadRequestsWaitTooLong(t *testing.T) {
originConfig := config.GetGlobalConfig()
newConfig := config.NewConfig()
newConfig.Performance.StatsLoadConcurrency = 0 // no worker to consume channel
newConfig.Performance.StatsLoadQueueSize = 10000
config.StoreGlobalConfig(newConfig)
defer config.StoreGlobalConfig(originConfig)
store, dom := testkit.CreateMockStoreAndDomain(t)

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t(a int, b int, c int, primary key(a), key idx(b,c))")
tk.MustExec("insert into t values (1,1,1),(2,2,2),(3,3,3)")

oriLease := dom.StatsHandle().Lease()
dom.StatsHandle().SetLease(1)
defer func() {
dom.StatsHandle().SetLease(oriLease)
}()
tk.MustExec("analyze table t all columns")
h := dom.StatsHandle()
is := dom.InfoSchema()
tbl, err := is.TableByName(model.NewCIStr("test"), model.NewCIStr("t"))
require.NoError(t, err)
tableInfo := tbl.Meta()
neededColumns := make([]model.TableItemID, 0, len(tableInfo.Columns))
for _, col := range tableInfo.Columns {
neededColumns = append(neededColumns, model.TableItemID{TableID: tableInfo.ID, ID: col.ID, IsIndex: false})
}
stmtCtx := &stmtctx.StatementContext{}
timeout := time.Nanosecond * 100
require.NoError(t, h.SendLoadRequests(stmtCtx, neededColumns, timeout))
for _, resultCh := range stmtCtx.StatsLoad.ResultCh {
rs1 := <-resultCh
require.Error(t, rs1.Err)
}
stmtCtx1 := &stmtctx.StatementContext{}
require.NoError(t, h.SendLoadRequests(stmtCtx1, neededColumns, timeout))
for _, resultCh := range stmtCtx1.StatsLoad.ResultCh {
rs1 := <-resultCh
require.Error(t, rs1.Err)
}
}