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

ddl: fix addindex wrong rowcount on dxf when the job txn failed #58575

Merged
merged 3 commits into from
Dec 27, 2024
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
2 changes: 2 additions & 0 deletions pkg/ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,7 @@ func doReorgWorkForCreateIndex(
indexInfo.BackfillState = model.BackfillStateReadyToMerge
}
ver, err = updateVersionAndTableInfo(jobCtx, job, tbl.Meta(), true)
failpoint.InjectCall("afterBackfillStateRunningDone", job)
return false, ver, errors.Trace(err)
case model.BackfillStateReadyToMerge:
failpoint.Inject("mockDMLExecutionStateBeforeMerge", func(_ failpoint.Value) {
Expand Down Expand Up @@ -2510,6 +2511,7 @@ func (w *worker) executeDistTask(stepCtx context.Context, t table.Table, reorgIn
// It's possible that the task state is succeed but the ddl job is paused.
// When task in succeed state, we can skip the dist task execution/scheduing process.
if task.State == proto.TaskStateSucceed {
w.updateDistTaskRowCount(taskKey, reorgInfo.Job.ID)
logutil.DDLLogger().Info(
"task succeed, start to resume the ddl job",
zap.String("task-key", taskKey))
Expand Down
1 change: 1 addition & 0 deletions pkg/ddl/job_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ func (w *worker) transitOneJobStep(
return 0, err
}
err = w.updateDDLJob(jobCtx, job, updateRawArgs)
failpoint.InjectCall("afterUpdateJobToTable", job, &err)
if err = w.handleUpdateJobError(jobCtx, job, err); err != nil {
w.sess.Rollback()
jobCtx.unlockSchemaVersion(jobCtx, job.ID)
Expand Down
1 change: 1 addition & 0 deletions tests/realtikvtest/addindextest1/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ go_test(
"//pkg/testkit",
"//pkg/testkit/testfailpoint",
"//tests/realtikvtest",
"@com_github_pingcap_errors//:errors",
"@com_github_pingcap_failpoint//:failpoint",
"@com_github_stretchr_testify//require",
"@com_github_tikv_client_go_v2//util",
Expand Down
26 changes: 22 additions & 4 deletions tests/realtikvtest/addindextest1/disttask_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"testing"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/pkg/config"
"github.com/pingcap/tidb/pkg/ddl/ingest"
Expand Down Expand Up @@ -173,9 +174,26 @@ func TestAddIndexDistCancel(t *testing.T) {
tk2 := testkit.NewTestKit(t, store)
tk2.MustExec("use addindexlit;")
tk2.MustExec(`set global tidb_ddl_enable_fast_reorg=on;`)
tk.MustExec("set @@global.tidb_enable_dist_task = 1;")
tk2.MustExec("create table t2 (a int, b int);")
tk2.MustExec("insert into t2 values (1, 1), (2, 2), (3, 3);")

var counter atomic.Int32
var enableTrigger atomic.Bool
var targetJobID atomic.Int64
testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/afterBackfillStateRunningDone", func(job *model.Job) {
// fail for one index when finish backfill, and check row count right
if counter.Add(1) == 1 {
targetJobID.Store(job.ID)
enableTrigger.Store(true)
}
})
testfailpoint.EnableCall(t, "github.com/pingcap/tidb/pkg/ddl/afterUpdateJobToTable", func(job *model.Job, errP *error) {
if enableTrigger.Load() && job.ID == targetJobID.Load() {
*errP = errors.New("mock error")
enableTrigger.Store(false)
}
})
wg := &sync.WaitGroup{}
wg.Add(2)
go func() {
Expand All @@ -191,10 +209,10 @@ func TestAddIndexDistCancel(t *testing.T) {
require.Len(t, rows, 2)
require.True(t, strings.Contains(rows[0][12].(string) /* comments */, "ingest"))
require.True(t, strings.Contains(rows[1][12].(string) /* comments */, "ingest"))
require.Equal(t, rows[0][7].(string) /* row_count */, "3")
require.Equal(t, rows[1][7].(string) /* row_count */, "3")

tk.MustExec("set @@global.tidb_enable_dist_task = 1;")
require.Equal(t, "3", rows[0][7].(string) /* row_count */)
require.Equal(t, "3", rows[1][7].(string) /* row_count */)
testfailpoint.Disable(t, "github.com/pingcap/tidb/pkg/ddl/afterBackfillStateRunningDone")
testfailpoint.Disable(t, "github.com/pingcap/tidb/pkg/ddl/afterUpdateJobToTable")

// test cancel is timely
enter := make(chan struct{})
Expand Down