Skip to content

Commit

Permalink
ddl: fix flaky test TestGC (pingcap#46932)
Browse files Browse the repository at this point in the history
  • Loading branch information
okJiang authored Sep 13, 2023
1 parent 0937f1d commit 762b2df
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
8 changes: 7 additions & 1 deletion disttask/framework/dispatcher/dispatcher_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ var (
checkTaskRunningInterval = 3 * time.Second
)

// WaitTaskFinished is used to sync the test.
var WaitTaskFinished = make(chan struct{})

func (dm *Manager) getRunningTaskCnt() int {
dm.runningTasks.RLock()
defer dm.runningTasks.RUnlock()
Expand Down Expand Up @@ -199,13 +202,16 @@ func (dm *Manager) dispatchTaskLoop() {
}

func (dm *Manager) gcSubtaskHistoryTable() {
logutil.Logger(dm.ctx).Info("task table gc loop start")
historySubtaskTableGcInterval := defaultHistorySubtaskTableGcInterval
failpoint.Inject("historySubtaskTableGcInterval", func(val failpoint.Value) {
if seconds, ok := val.(int); ok {
historySubtaskTableGcInterval = time.Second * time.Duration(seconds)
}

<-WaitTaskFinished
})

logutil.Logger(dm.ctx).Info("task table gc loop start")
ticker := time.NewTicker(historySubtaskTableGcInterval)
defer ticker.Stop()
for {
Expand Down
31 changes: 18 additions & 13 deletions disttask/framework/framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,9 +575,8 @@ func TestGC(t *testing.T) {
defer ctrl.Finish()
RegisterTaskMeta(t, ctrl, &m, &testDispatcherExt{})

failpoint.Enable("github.com/pingcap/tidb/disttask/framework/storage/subtaskHistoryKeepSeconds", "return(1)")
// 10s to wait all subtask completed
failpoint.Enable("github.com/pingcap/tidb/disttask/framework/dispatcher/historySubtaskTableGcInterval", "return(10)")
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/disttask/framework/storage/subtaskHistoryKeepSeconds", "return(1)"))
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/disttask/framework/dispatcher/historySubtaskTableGcInterval", "return(1)"))
defer func() {
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/disttask/framework/storage/subtaskHistoryKeepSeconds"))
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/disttask/framework/dispatcher/historySubtaskTableGcInterval"))
Expand All @@ -589,18 +588,24 @@ func TestGC(t *testing.T) {
mgr, err := storage.GetTaskManager()
require.NoError(t, err)

// check the subtask history table
historySubTasksCnt, err := storage.GetSubtasksFromHistoryForTest(mgr)
require.NoError(t, err)
require.Equal(t, 4, historySubTasksCnt)
var historySubTasksCnt int
require.Eventually(t, func() bool {
historySubTasksCnt, err = storage.GetSubtasksFromHistoryForTest(mgr)
if err != nil {
return false
}
return historySubTasksCnt == 4
}, 10*time.Second, 500*time.Millisecond)

// wait for gc
time.Sleep(10 * time.Second)
dispatcher.WaitTaskFinished <- struct{}{}

// check the subtask in history table removed.
historySubTasksCnt, err = storage.GetSubtasksFromHistoryForTest(mgr)
require.NoError(t, err)
require.Equal(t, 0, historySubTasksCnt)
require.Eventually(t, func() bool {
historySubTasksCnt, err := storage.GetSubtasksFromHistoryForTest(mgr)
if err != nil {
return false
}
return historySubTasksCnt == 0
}, 10*time.Second, 500*time.Millisecond)

distContext.Close()
}

0 comments on commit 762b2df

Please sign in to comment.