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: correct the remain ranges check for adding index #41460

Merged
merged 6 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
30 changes: 21 additions & 9 deletions ddl/backfilling.go
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,12 @@ type backfillScheduler struct {
copReqSenderPool *copReqSenderPool // for add index in ingest way.
}

const backfillTaskChanSize = 1024
var backfillTaskChanSize = 1024

// SetBackfillTaskChanSizeForTest is only used for test.
func SetBackfillTaskChanSizeForTest(n int) {
backfillTaskChanSize = n
}

func newBackfillScheduler(ctx context.Context, info *reorgInfo, sessPool *sessionPool,
tp backfillerType, tbl table.PhysicalTable, decColMap map[int64]decoder.Column,
Expand Down Expand Up @@ -1067,12 +1072,18 @@ func (dc *ddlCtx) writePhysicalTableRecord(sessPool *sessionPool, t table.Physic
}

for {
if startKey.Cmp(endKey) >= 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we remove this check?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, kvRanges[len(kvRanges)-1].EndKey may equal to endKey.

The function splitTableRanges() doesn't handle the cases like start_key <= end_key.

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

is above start_key > end_key scenario for splitTableRanges() not handle?

Copy link
Contributor

Choose a reason for hiding this comment

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

It handles in line1082

Copy link
Collaborator

Choose a reason for hiding this comment

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

My question is if we send a smaller start_key to `splitTableRanges(), will it return an err?

break
}
kvRanges, err := splitTableRanges(t, reorgInfo.d.store, startKey, endKey, backfillTaskChanSize)
if err != nil {
return errors.Trace(err)
}
scheduler.setMaxWorkerSize(len(kvRanges))
if len(kvRanges) == 0 {
break
}

scheduler.setMaxWorkerSize(len(kvRanges))
Benjamin2037 marked this conversation as resolved.
Show resolved Hide resolved
err = scheduler.adjustWorkerSize()
if err != nil {
return errors.Trace(err)
Expand All @@ -1095,14 +1106,15 @@ func (dc *ddlCtx) writePhysicalTableRecord(sessPool *sessionPool, t table.Physic
if err != nil {
return errors.Trace(err)
}

if len(remains) == 0 {
if ingestBeCtx != nil {
ingestBeCtx.EngMgr.ResetWorkers(ingestBeCtx, job.ID, reorgInfo.currElement.ID)
}
break
if len(remains) > 0 {
startKey = remains[0].StartKey
} else {
lastFinishedKey := kvRanges[len(kvRanges)-1].EndKey
startKey = lastFinishedKey.Next()
tangenta marked this conversation as resolved.
Show resolved Hide resolved
}
startKey = remains[0].StartKey
}
if ingestBeCtx != nil {
ingestBeCtx.EngMgr.ResetWorkers(ingestBeCtx, job.ID, reorgInfo.currElement.ID)
}
return nil
}
Expand Down
23 changes: 23 additions & 0 deletions tests/realtikvtest/addindextest/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,29 @@ func TestAddIndexIngestCancel(t *testing.T) {
require.Empty(t, ingest.LitBackCtxMgr.Keys())
}

func TestAddIndexSplitTableRanges(t *testing.T) {
store := realtikvtest.CreateMockStoreAndSetup(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("drop database if exists addindexlit;")
tk.MustExec("create database addindexlit;")
tk.MustExec("use addindexlit;")
tk.MustExec(`set global tidb_ddl_enable_fast_reorg=on;`)

tk.MustExec("create table t (a int primary key, b int);")
for i := 0; i < 8; i++ {
tk.MustExec(fmt.Sprintf("insert into t values (%d, %d);", i*10000, i*10000))
}
tk.MustQuery("split table t between (0) and (80000) regions 7;").Check(testkit.Rows("6 1"))

ddl.SetBackfillTaskChanSizeForTest(4)
tangenta marked this conversation as resolved.
Show resolved Hide resolved
tk.MustExec("alter table t add index idx(b);")
tk.MustExec("admin check table t;")
ddl.SetBackfillTaskChanSizeForTest(7)
tk.MustExec("alter table t add index idx_2(b);")
tk.MustExec("admin check table t;")
ddl.SetBackfillTaskChanSizeForTest(1024)
}

type testCallback struct {
ddl.Callback
OnJobRunBeforeExported func(job *model.Job)
Expand Down