Skip to content

Commit 200f525

Browse files
committed
statistics: rightly deal with timout when to send sync load
Signed-off-by: Weizhen Wang <[email protected]>
1 parent b11d034 commit 200f525

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

pkg/statistics/handle/syncload/stats_syncload.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,13 @@ func (s *statsSyncLoad) SendLoadRequests(sc *stmtctx.StatementContext, neededHis
114114
select {
115115
case s.StatsLoad.NeededItemsCh <- task:
116116
metrics.SyncLoadDedupCounter.Inc()
117-
result, ok := <-task.ResultCh
118-
intest.Assert(ok, "task.ResultCh cannot be closed")
119-
return result, nil
117+
select {
118+
case <-timer.C:
119+
return nil, errors.New("sync load stats channel is full and timeout sending task to channel")
120+
case result, ok := <-task.ResultCh:
121+
intest.Assert(ok, "task.ResultCh cannot be closed")
122+
return result, nil
123+
}
120124
case <-timer.C:
121125
return nil, errors.New("sync load stats channel is full and timeout sending task to channel")
122126
}

pkg/statistics/handle/syncload/stats_syncload_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,3 +365,43 @@ func TestRetry(t *testing.T) {
365365
}
366366
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/pkg/statistics/handle/syncload/mockReadStatsForOneFail"))
367367
}
368+
369+
func TestSendWaitTooLong(t *testing.T) {
370+
originConfig := config.GetGlobalConfig()
371+
newConfig := config.NewConfig()
372+
newConfig.Performance.StatsLoadConcurrency = -1 // no worker to consume channel
373+
newConfig.Performance.StatsLoadQueueSize = 1
374+
config.StoreGlobalConfig(newConfig)
375+
defer config.StoreGlobalConfig(originConfig)
376+
store, dom := testkit.CreateMockStoreAndDomain(t)
377+
378+
tk := testkit.NewTestKit(t, store)
379+
tk.MustExec("use test")
380+
tk.MustExec("create table t(a int, b int, c int, primary key(a), key idx(b,c))")
381+
tk.MustExec("insert into t values (1,1,1),(2,2,2),(3,3,3)")
382+
383+
oriLease := dom.StatsHandle().Lease()
384+
dom.StatsHandle().SetLease(1)
385+
defer func() {
386+
dom.StatsHandle().SetLease(oriLease)
387+
}()
388+
tk.MustExec("analyze table t all columns")
389+
h := dom.StatsHandle()
390+
is := dom.InfoSchema()
391+
tbl, err := is.TableByName(context.Background(), pmodel.NewCIStr("test"), pmodel.NewCIStr("t"))
392+
require.NoError(t, err)
393+
tableInfo := tbl.Meta()
394+
neededColumns := make([]model.StatsLoadItem, 0, len(tableInfo.Columns))
395+
for _, col := range tableInfo.Columns {
396+
neededColumns = append(neededColumns, model.StatsLoadItem{TableItemID: model.TableItemID{TableID: tableInfo.ID, ID: col.ID, IsIndex: false}, FullLoad: true})
397+
}
398+
stmtCtx := stmtctx.NewStmtCtx()
399+
timeout := time.Nanosecond * 100
400+
require.NoError(t, h.SendLoadRequests(stmtCtx, neededColumns, timeout))
401+
stmtCtx1 := stmtctx.NewStmtCtx()
402+
require.NoError(t, h.SendLoadRequests(stmtCtx1, neededColumns, timeout))
403+
for _, resultCh := range stmtCtx1.StatsLoad.ResultCh {
404+
rs1, _ := <-resultCh
405+
require.Error(t, rs1.Err)
406+
}
407+
}

0 commit comments

Comments
 (0)