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

disttask: dynamic dispatch subtasks #46593

Merged
merged 52 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
ff707d0
draft
ywqzzy Sep 1, 2023
f452ebe
fix
ywqzzy Sep 1, 2023
68472ab
support 2 step
ywqzzy Sep 4, 2023
11cc644
Merge branch 'master' of https://github.com/pingcap/tidb into dynamic…
ywqzzy Sep 4, 2023
1b92eaf
refine
ywqzzy Sep 4, 2023
7b5a702
refine
ywqzzy Sep 4, 2023
951b67d
fix
ywqzzy Sep 4, 2023
edff2e8
fix
ywqzzy Sep 4, 2023
7aad421
fix
ywqzzy Sep 4, 2023
f6a2601
fix
ywqzzy Sep 4, 2023
b147419
Merge branch 'master' of https://github.com/pingcap/tidb into dynamic…
ywqzzy Sep 4, 2023
84a0857
fix
ywqzzy Sep 4, 2023
cecdf36
fix
ywqzzy Sep 4, 2023
30c2ce1
u
ywqzzy Sep 4, 2023
688a986
tmp save
ywqzzy Sep 5, 2023
22f3eb4
basic done
ywqzzy Sep 5, 2023
4a37e67
fix all tests
ywqzzy Sep 5, 2023
5f3d3d3
Merge branch 'master' of https://github.com/pingcap/tidb into dynamic…
ywqzzy Sep 5, 2023
e1a29cf
merge master
ywqzzy Sep 5, 2023
a410eb2
fix
ywqzzy Sep 5, 2023
e3b4649
fix
ywqzzy Sep 5, 2023
00c27c5
Merge branch 'master' of https://github.com/pingcap/tidb into dynamic…
ywqzzy Sep 6, 2023
c386dae
fix
ywqzzy Sep 6, 2023
e4a33c0
Add new interface
ywqzzy Sep 6, 2023
5ba29b5
fix
ywqzzy Sep 6, 2023
99d34ed
fix
ywqzzy Sep 6, 2023
16dd57f
fix
ywqzzy Sep 6, 2023
4777946
fix
ywqzzy Sep 6, 2023
b2623d8
Merge branch 'master' of https://github.com/pingcap/tidb into dynamic…
ywqzzy Sep 7, 2023
a9eb159
fix
ywqzzy Sep 7, 2023
b2789fb
fix update task
ywqzzy Sep 8, 2023
8d865fc
Merge branch 'master' of https://github.com/pingcap/tidb into dynamic…
ywqzzy Sep 8, 2023
3aebc11
fix
ywqzzy Sep 8, 2023
37b39ae
fix
ywqzzy Sep 8, 2023
7c1c0cc
fix test
ywqzzy Sep 8, 2023
74c4601
Merge branch 'master' of https://github.com/pingcap/tidb into dynamic…
ywqzzy Sep 8, 2023
99ccedc
bazel
ywqzzy Sep 8, 2023
541ab7e
fix
ywqzzy Sep 8, 2023
6e61680
refine
ywqzzy Sep 11, 2023
0df3b3c
more tests
ywqzzy Sep 11, 2023
122aa4f
refine
ywqzzy Sep 11, 2023
34a10e7
fix
ywqzzy Sep 11, 2023
be406a2
fix
ywqzzy Sep 11, 2023
f224c32
Merge branch 'master' of https://github.com/pingcap/tidb into dynamic…
ywqzzy Sep 11, 2023
95020f8
fix lint
ywqzzy Sep 11, 2023
6d98835
fix test
ywqzzy Sep 11, 2023
a437e9d
fix
ywqzzy Sep 11, 2023
9b5932c
fix
ywqzzy Sep 11, 2023
8a1f460
comments
ywqzzy Sep 11, 2023
dea1c54
fix
ywqzzy Sep 11, 2023
164a00e
Merge branch 'master' of https://github.com/pingcap/tidb into dynamic…
ywqzzy Sep 12, 2023
851a802
fix
ywqzzy Sep 12, 2023
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
20 changes: 13 additions & 7 deletions ddl/backfilling_dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,12 @@ func NewBackfillingDispatcherExt(d DDL) (dispatcher.Extension, error) {
}, nil
}

// OnTick implements dispatcher.Extension interface.
func (*backfillingDispatcherExt) OnTick(_ context.Context, _ *proto.Task) {
}

// OnNextStage generate next stage's plan.
func (h *backfillingDispatcherExt) OnNextStage(ctx context.Context, _ dispatcher.TaskHandle, gTask *proto.Task) ([][]byte, error) {
// OnNextSubtasksBatch generate batch of next stage's plan.
func (h *backfillingDispatcherExt) OnNextSubtasksBatch(ctx context.Context, _ dispatcher.TaskHandle, gTask *proto.Task) ([][]byte, error) {
var globalTaskMeta BackfillGlobalMeta
if err := json.Unmarshal(gTask.Meta, &globalTaskMeta); err != nil {
return nil, err
Expand All @@ -76,7 +77,6 @@ func (h *backfillingDispatcherExt) OnNextStage(ctx context.Context, _ dispatcher
if err != nil {
return nil, err
}
gTask.Step = proto.StepOne
return subTaskMetas, nil
}

Expand All @@ -87,7 +87,6 @@ func (h *backfillingDispatcherExt) OnNextStage(ctx context.Context, _ dispatcher
if err != nil {
return nil, err
}
gTask.Step = proto.StepOne
return subtaskMeta, nil
case proto.StepOne:
serverNodes, err := dispatcher.GenerateSchedulerNodes(ctx)
Expand All @@ -103,15 +102,22 @@ func (h *backfillingDispatcherExt) OnNextStage(ctx context.Context, _ dispatcher
for range serverNodes {
subTaskMetas = append(subTaskMetas, metaBytes)
}
gTask.Step = proto.StepTwo
return subTaskMetas, nil
case proto.StepTwo:
return nil, nil
default:
return nil, nil
}
}

// StageFinished check if current stage finished.
func (*backfillingDispatcherExt) StageFinished(_ *proto.Task) bool {
return true
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this function always return true?

}

// Finished check if current task finished.
func (*backfillingDispatcherExt) Finished(task *proto.Task) bool {
return task.Step == proto.StepOne
}

// OnErrStage generate error handling stage's plan.
func (*backfillingDispatcherExt) OnErrStage(_ context.Context, _ dispatcher.TaskHandle, task *proto.Task, receiveErr []error) (meta []byte, err error) {
// We do not need extra meta info when rolling back
Expand Down
36 changes: 29 additions & 7 deletions ddl/backfilling_dispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestBackfillingDispatcher(t *testing.T) {
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")

// test partition table OnNextStage.
/// 1. test partition table.
tk.MustExec("create table tp1(id int primary key, v int) PARTITION BY RANGE (id) (\n " +
"PARTITION p0 VALUES LESS THAN (10),\n" +
"PARTITION p1 VALUES LESS THAN (100),\n" +
Expand All @@ -48,23 +48,25 @@ func TestBackfillingDispatcher(t *testing.T) {
tbl, err := dom.InfoSchema().TableByName(model.NewCIStr("test"), model.NewCIStr("tp1"))
require.NoError(t, err)
tblInfo := tbl.Meta()
metas, err := dsp.OnNextStage(context.Background(), nil, gTask)

// 1.1 OnNextSubtasksBatch
metas, err := dsp.OnNextSubtasksBatch(context.Background(), nil, gTask)
require.NoError(t, err)
require.Equal(t, proto.StepOne, gTask.Step)
require.Equal(t, len(tblInfo.Partition.Definitions), len(metas))
for i, par := range tblInfo.Partition.Definitions {
var subTask ddl.BackfillSubTaskMeta
require.NoError(t, json.Unmarshal(metas[i], &subTask))
require.Equal(t, par.ID, subTask.PhysicalTableID)
}

// test partition table OnNextStage after step1 finished.
// 1.2 test partition table OnNextSubtasksBatch after StepInit finished.
gTask.State = proto.TaskStateRunning
metas, err = dsp.OnNextStage(context.Background(), nil, gTask)
gTask.Step++
metas, err = dsp.OnNextSubtasksBatch(context.Background(), nil, gTask)
require.NoError(t, err)
require.Equal(t, 0, len(metas))

// test partition table OnErrStage.
// 1.3 test partition table OnErrStage.
errMeta, err := dsp.OnErrStage(context.Background(), nil, gTask, []error{errors.New("mockErr")})
require.NoError(t, err)
require.Nil(t, errMeta)
Expand All @@ -73,10 +75,30 @@ func TestBackfillingDispatcher(t *testing.T) {
require.NoError(t, err)
require.Nil(t, errMeta)

/// 2. test non partition table.
// 2.1 empty table
tk.MustExec("create table t1(id int primary key, v int)")
gTask = createAddIndexGlobalTask(t, dom, "test", "t1", ddl.BackfillTaskType)
_, err = dsp.OnNextStage(context.Background(), nil, gTask)
metas, err = dsp.OnNextSubtasksBatch(context.Background(), nil, gTask)
require.NoError(t, err)
require.Equal(t, 0, len(metas))
// 2.2 non empty table.
tk.MustExec("create table t2(id bigint auto_random primary key)")
tk.MustExec("insert into t2 values (), (), (), (), (), ()")
tk.MustExec("insert into t2 values (), (), (), (), (), ()")
tk.MustExec("insert into t2 values (), (), (), (), (), ()")
tk.MustExec("insert into t2 values (), (), (), (), (), ()")
gTask = createAddIndexGlobalTask(t, dom, "test", "t2", ddl.BackfillTaskType)
// 2.2.1 stepInit
metas, err = dsp.OnNextSubtasksBatch(context.Background(), nil, gTask)
require.NoError(t, err)
require.Equal(t, 1, len(metas))
// 2.2.2 stepOne
gTask.Step++
gTask.State = proto.TaskStateRunning
metas, err = dsp.OnNextSubtasksBatch(context.Background(), nil, gTask)
require.NoError(t, err)
require.Equal(t, 1, len(metas))
}

func createAddIndexGlobalTask(t *testing.T, dom *domain.Domain, dbName, tblName string, taskType string) *proto.Task {
Expand Down
2 changes: 1 addition & 1 deletion ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -1942,7 +1942,7 @@ func (w *worker) updateJobRowCount(taskKey string, jobID int64) {
logutil.BgLogger().Warn("cannot get global task", zap.String("category", "ddl"), zap.String("task_key", taskKey), zap.Error(err))
return
}
rowCount, err := taskMgr.GetSubtaskRowCount(gTask.ID, proto.StepOne)
rowCount, err := taskMgr.GetSubtaskRowCount(gTask.ID, proto.StepInit)
if err != nil {
logutil.BgLogger().Warn("cannot get subtask row count", zap.String("category", "ddl"), zap.String("task_key", taskKey), zap.Error(err))
return
Expand Down
4 changes: 2 additions & 2 deletions ddl/stage_scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ func newBackfillDistScheduler(ctx context.Context, id string, taskID int64, task

func (s *backfillDistScheduler) GetSubtaskExecutor(ctx context.Context, task *proto.Task, summary *execute.Summary) (execute.SubtaskExecutor, error) {
switch task.Step {
case proto.StepOne:
case proto.StepInit:
return NewBackfillSchedulerHandle(ctx, task.Meta, s.d, false, summary)
case proto.StepTwo:
case proto.StepOne:
return NewBackfillSchedulerHandle(ctx, task.Meta, s.d, true, nil)
default:
return nil, errors.Errorf("unknown backfill step %d for task %d", task.Step, task.ID)
Expand Down
3 changes: 2 additions & 1 deletion disttask/framework/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ go_test(
name = "framework_test",
timeout = "short",
srcs = [
"framework_dynamic_dispatch_test.go",
"framework_err_handling_test.go",
"framework_ha_test.go",
"framework_rollback_test.go",
"framework_test.go",
],
flaky = True,
race = "off",
shard_count = 24,
shard_count = 26,
deps = [
"//disttask/framework/dispatcher",
"//disttask/framework/mock",
Expand Down
Loading