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: retry delete when write conflict happens in txn #40503

Merged
merged 7 commits into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions ttl/ttlworker/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ type ttlTableSession struct {
expire time.Time
}

func (s *ttlTableSession) ExecuteSQLWithCheck(ctx context.Context, sql string) (rows []chunk.Row, shouldRetry bool, err error) {
func (s *ttlTableSession) ExecuteSQLWithCheck(ctx context.Context, sql string) (rows []chunk.Row, _ bool, err error) {
lcwangchao marked this conversation as resolved.
Show resolved Hide resolved
tracer := metrics.PhaseTracerFromCtx(ctx)
defer tracer.EnterPhase(tracer.Phase())

Expand All @@ -165,6 +165,7 @@ func (s *ttlTableSession) ExecuteSQLWithCheck(ctx context.Context, sql string) (
return nil, false, err
}

shouldRetry := true
err = s.RunInTxn(ctx, func() error {
tracer.EnterPhase(metrics.PhaseQuery)
defer tracer.EnterPhase(tracer.Phase())
Expand All @@ -178,7 +179,6 @@ func (s *ttlTableSession) ExecuteSQLWithCheck(ctx context.Context, sql string) (
}

if err != nil {
shouldRetry = true
return err
}
return nil
Expand Down
15 changes: 13 additions & 2 deletions ttl/ttlworker/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ type mockSession struct {
evalExpire time.Time
resetTimeZoneCalls int
closed bool
commitErr error
}

func newMockSession(t *testing.T, tbl ...*cache.PhysicalTable) *mockSession {
Expand Down Expand Up @@ -186,9 +187,12 @@ func (s *mockSession) ExecuteSQL(ctx context.Context, sql string, args ...interf
return s.rows, s.execErr
}

func (s *mockSession) RunInTxn(_ context.Context, fn func() error) (err error) {
func (s *mockSession) RunInTxn(_ context.Context, fn func() error) error {
require.False(s.t, s.closed)
return fn()
if err := fn(); err != nil {
return err
}
return s.commitErr
}

func (s *mockSession) ResetWithGlobalTimeZone(_ context.Context) (err error) {
Expand Down Expand Up @@ -238,6 +242,13 @@ func TestExecuteSQLWithCheck(t *testing.T) {
require.Equal(t, 1, len(rows))
require.Equal(t, int64(12), rows[0].GetInt64(0))
require.Equal(t, 3, s.resetTimeZoneCalls)

s.commitErr = errors.New("mockCommitErr")
rows, shouldRetry, err = tblSe.ExecuteSQLWithCheck(ctx, "select 1")
require.EqualError(t, err, "mockCommitErr")
require.True(t, shouldRetry)
require.Nil(t, rows)
require.Equal(t, 4, s.resetTimeZoneCalls)
}

func TestValidateTTLWork(t *testing.T) {
Expand Down