Skip to content

Commit

Permalink
ddl: support pre-split index regions before creating index (#57553)
Browse files Browse the repository at this point in the history
close #57551, close #57552
  • Loading branch information
tangenta authored Dec 18, 2024
1 parent 5ac0b2e commit 177a03c
Show file tree
Hide file tree
Showing 18 changed files with 7,193 additions and 6,454 deletions.
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
74 changes: 74 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 @@ -4614,6 +4615,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 @@ -4640,6 +4645,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 @@ -4887,6 +4893,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 @@ -4909,6 +4920,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 @@ -4999,6 +5011,68 @@ 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
}
if len(opt.ValueLists) > 0 {
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{
Num: opt.Num,
ValueLists: valLists,
}, 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())
}
maxSplitRegionNum := int64(config.GetGlobalConfig().SplitRegionMaxNum)
if opt.Num > maxSplitRegionNum {
return nil, errors.Errorf("Split index region num exceeded the limit %v", maxSplitRegionNum)
} else if opt.Num < 1 {
return nil, errors.Errorf("Split index region num should be greater than 0")
}
return &model.IndexArgSplitOpt{
Lower: lowers,
Upper: uppers,
Num: opt.Num,
}, 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, jobCtx.store, tblInfo, allIndexInfos, job.ReorgMeta, args)
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

0 comments on commit 177a03c

Please sign in to comment.