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

ttl: fix the issue that TTL job may hang some time when shrink the delete worker count (#55572) #55612

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
3 changes: 2 additions & 1 deletion ttl/ttlworker/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ go_test(
embed = [":ttlworker"],
flaky = True,
race = "on",
shard_count = 37,
shard_count = 38,
deps = [
"//domain",
"//infoschema",
Expand Down Expand Up @@ -88,6 +88,7 @@ go_test(
"@com_github_stretchr_testify//assert",
"@com_github_stretchr_testify//require",
"@com_github_tikv_client_go_v2//testutils",
"@org_golang_x_exp//slices",
"@org_golang_x_time//rate",
"@org_uber_go_atomic//:atomic",
"@org_uber_go_zap//:zap",
Expand Down
20 changes: 20 additions & 0 deletions ttl/ttlworker/del.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ func (t *ttlDeleteTask) doDelete(ctx context.Context, rawSe session.Session) (re
tracer.EnterPhase(metrics.PhaseOther)

leftRows := t.rows
defer func() {
if len(leftRows) > 0 {
t.statistics.IncErrorRows(len(leftRows))
}
}()

se := newTableSession(rawSe, t.tbl, t.expire)
for len(leftRows) > 0 {
maxBatch := variable.TTLDeleteBatchSize.Load()
Expand Down Expand Up @@ -207,6 +213,18 @@ func (b *ttlDelRetryBuffer) DoRetry(do func(*ttlDeleteTask) [][]types.Datum) tim
return b.retryInterval
}

// Drain drains a retry buffer.
func (b *ttlDelRetryBuffer) Drain() {
for ele := b.list.Front(); ele != nil; ele = ele.Next() {
if item, ok := ele.Value.(*ttlDelRetryItem); ok {
item.task.statistics.IncErrorRows(len(item.task.rows))
} else {
logutil.BgLogger().Error(fmt.Sprintf("invalid retry buffer item type: %T", ele))
}
}
b.list = list.New()
}

func (b *ttlDelRetryBuffer) recordRetryItem(task *ttlDeleteTask, retryRows [][]types.Datum, retryCnt int) bool {
if len(retryRows) == 0 {
return false
Expand Down Expand Up @@ -276,6 +294,8 @@ func (w *ttlDeleteWorker) loop() error {
timer := time.NewTimer(w.retryBuffer.retryInterval)
defer timer.Stop()

// drain retry buffer to make sure the statistics are correct
defer w.retryBuffer.Drain()
for w.Status() == workerStatusRunning {
tracer.EnterPhase(metrics.PhaseIdle)
select {
Expand Down
Loading