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: support pre-split index regions before creating index #57553

Merged
merged 17 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 12 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/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ go_library(
"index.go",
"index_cop.go",
"index_merge_tmp.go",
"index_presplit.go",
"job_scheduler.go",
"job_submitter.go",
"job_worker.go",
Expand Down Expand Up @@ -152,6 +153,7 @@ go_library(
"//pkg/util/context",
"//pkg/util/dbterror",
"//pkg/util/dbterror/exeerrors",
"//pkg/util/dbterror/plannererrors",
"//pkg/util/domainutil",
"//pkg/util/engine",
"//pkg/util/execdetails",
Expand Down
1 change: 0 additions & 1 deletion pkg/ddl/backfilling.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ type reorgBackfillTask struct {
startKey kv.Key
endKey kv.Key
jobID int64
sqlQuery string
priority int
}

Expand Down
62 changes: 62 additions & 0 deletions pkg/ddl/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"github.com/pingcap/tidb/pkg/metrics"
"github.com/pingcap/tidb/pkg/parser/ast"
"github.com/pingcap/tidb/pkg/parser/charset"
"github.com/pingcap/tidb/pkg/parser/format"
pmodel "github.com/pingcap/tidb/pkg/parser/model"
"github.com/pingcap/tidb/pkg/parser/mysql"
"github.com/pingcap/tidb/pkg/parser/terror"
Expand Down Expand Up @@ -4611,6 +4612,10 @@ func (e *executor) CreatePrimaryKey(ctx sessionctx.Context, ti ast.Ident, indexN
}
}

splitOpt, err := buildIndexPresplitOpt(indexOption)
if err != nil {
return errors.Trace(err)
}
sqlMode := ctx.GetSessionVars().SQLMode
// global is set to 'false' is just there to be backwards compatible,
// to avoid unmarshal issues, it is now part of indexOption.
Expand All @@ -4637,6 +4642,7 @@ func (e *executor) CreatePrimaryKey(ctx sessionctx.Context, ti ast.Ident, indexN
SQLMode: sqlMode,
Global: false,
IsPK: true,
SplitOpt: splitOpt,
}},
OpType: model.OpAddIndex,
}
Expand Down Expand Up @@ -4884,6 +4890,11 @@ func (e *executor) createIndex(ctx sessionctx.Context, ti ast.Ident, keyType ast
return e.addHypoIndexIntoCtx(ctx, ti.Schema, ti.Name, indexInfo)
}

splitOpt, err := buildIndexPresplitOpt(indexOption)
if err != nil {
return errors.Trace(err)
}

// global is set to 'false' is just there to be backwards compatible,
// to avoid unmarshal issues, it is now part of indexOption.
global := false
Expand All @@ -4906,6 +4917,7 @@ func (e *executor) createIndex(ctx sessionctx.Context, ti ast.Ident, keyType ast
IndexOption: indexOption,
HiddenCols: hiddenCols,
Global: global,
SplitOpt: splitOpt,
}},
OpType: model.OpAddIndex,
}
Expand Down Expand Up @@ -4995,6 +5007,56 @@ func initJobReorgMetaFromVariables(job *model.Job, sctx sessionctx.Context) erro
return nil
}

func buildIndexPresplitOpt(indexOpt *ast.IndexOption) (*model.IndexArgSplitOpt, error) {
if indexOpt == nil {
return nil, nil
}
opt := indexOpt.SplitOpt
if opt == nil {
return nil, nil
}
lowers := make([]string, 0, len(opt.Lower))
for _, expL := range opt.Lower {
var sb strings.Builder
rCtx := format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)
err := expL.Restore(rCtx)
if err != nil {
return nil, errors.Trace(err)
}
lowers = append(lowers, sb.String())
}
uppers := make([]string, 0, len(opt.Upper))
for _, expU := range opt.Upper {
var sb strings.Builder
rCtx := format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)
err := expU.Restore(rCtx)
if err != nil {
return nil, errors.Trace(err)
}
uppers = append(uppers, sb.String())
}
valLists := make([][]string, 0, len(opt.ValueLists))
for _, lst := range opt.ValueLists {
values := make([]string, 0, len(lst))
for _, exp := range lst {
var sb strings.Builder
rCtx := format.NewRestoreCtx(format.DefaultRestoreFlags, &sb)
err := exp.Restore(rCtx)
if err != nil {
return nil, errors.Trace(err)
}
values = append(values, sb.String())
}
valLists = append(valLists, values)
}
return &model.IndexArgSplitOpt{
Lower: lowers,
Upper: uppers,
Num: opt.Num,
ValueLists: valLists,
}, nil
}

// LastReorgMetaFastReorgDisabled is used for test.
var LastReorgMetaFastReorgDisabled bool

Expand Down
7 changes: 7 additions & 0 deletions pkg/ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,13 @@ SwitchIndexState:
indexInfo.BackfillState = model.BackfillStateRunning
}
}
err = presplitIndexRegions(jobCtx.stepCtx, w.sess.Context, w.store, tblInfo, allIndexInfos, job.ReorgMeta, args)
tangenta marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
if !isRetryableJobError(err, job.ErrorCount) {
job.State = model.JobStateCancelled
}
return ver, err
}
for _, indexInfo := range allIndexInfos {
indexInfo.State = model.StateDeleteOnly
moveAndUpdateHiddenColumnsToPublic(tblInfo, indexInfo)
Expand Down
Loading