From 483b82b09faf8870a33ce600e22fa4ba9f29214b Mon Sep 17 00:00:00 2001 From: AI-Login-is-already-taken <956308585@qq.com> Date: Fri, 17 Sep 2021 23:17:02 +0800 Subject: [PATCH] fix --- ddl/ddl_api.go | 97 ++++++++++++++++++++++++-------------------------- ddl/table.go | 15 ++++---- 2 files changed, 55 insertions(+), 57 deletions(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index dc0a29a1139a1..1c81feb327433 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -6315,107 +6315,104 @@ func (d *ddl) AlterTablePartitionOptions(ctx sessionctx.Context, ident ast.Ident if err != nil { return errors.Trace(err) } - ptDef := &model.PartitionDefinition{} + var policyRefInfo *model.PolicyRefInfo + var placementSettings *model.PlacementSettings if spec.Options != nil { for _, op := range spec.Options { switch op.Tp { case ast.TableOptionPlacementPolicy: - ptDef.PlacementPolicyRef = &model.PolicyRefInfo{ + policyRefInfo = &model.PolicyRefInfo{ Name: model.NewCIStr(op.StrValue), } case ast.TableOptionPlacementPrimaryRegion: - if ptDef.DirectPlacementOpts == nil { - ptDef.DirectPlacementOpts = &model.PlacementSettings{} + if placementSettings == nil { + placementSettings = &model.PlacementSettings{} } - ptDef.DirectPlacementOpts.PrimaryRegion = op.StrValue + placementSettings.PrimaryRegion = op.StrValue case ast.TableOptionPlacementRegions: - if ptDef.DirectPlacementOpts == nil { - ptDef.DirectPlacementOpts = &model.PlacementSettings{} + if placementSettings == nil { + placementSettings = &model.PlacementSettings{} } - ptDef.DirectPlacementOpts.Regions = op.StrValue + placementSettings.Regions = op.StrValue case ast.TableOptionPlacementFollowerCount: - if ptDef.DirectPlacementOpts == nil { - ptDef.DirectPlacementOpts = &model.PlacementSettings{} + if placementSettings == nil { + placementSettings = &model.PlacementSettings{} } - ptDef.DirectPlacementOpts.Followers = op.UintValue + placementSettings.Followers = op.UintValue case ast.TableOptionPlacementVoterCount: - if ptDef.DirectPlacementOpts == nil { - ptDef.DirectPlacementOpts = &model.PlacementSettings{} + if placementSettings == nil { + placementSettings = &model.PlacementSettings{} } - ptDef.DirectPlacementOpts.Voters = op.UintValue + placementSettings.Voters = op.UintValue case ast.TableOptionPlacementLearnerCount: - if ptDef.DirectPlacementOpts == nil { - ptDef.DirectPlacementOpts = &model.PlacementSettings{} + if placementSettings == nil { + placementSettings = &model.PlacementSettings{} } - ptDef.DirectPlacementOpts.Learners = op.UintValue + placementSettings.Learners = op.UintValue case ast.TableOptionPlacementSchedule: - if ptDef.DirectPlacementOpts == nil { - ptDef.DirectPlacementOpts = &model.PlacementSettings{} + if placementSettings == nil { + placementSettings = &model.PlacementSettings{} } - ptDef.DirectPlacementOpts.Schedule = op.StrValue + placementSettings.Schedule = op.StrValue case ast.TableOptionPlacementConstraints: - if ptDef.DirectPlacementOpts == nil { - ptDef.DirectPlacementOpts = &model.PlacementSettings{} + if placementSettings == nil { + placementSettings = &model.PlacementSettings{} } - ptDef.DirectPlacementOpts.Constraints = op.StrValue + placementSettings.Constraints = op.StrValue case ast.TableOptionPlacementLeaderConstraints: - if ptDef.DirectPlacementOpts == nil { - ptDef.DirectPlacementOpts = &model.PlacementSettings{} + if placementSettings == nil { + placementSettings = &model.PlacementSettings{} } - ptDef.DirectPlacementOpts.LeaderConstraints = op.StrValue + placementSettings.LeaderConstraints = op.StrValue case ast.TableOptionPlacementLearnerConstraints: - if ptDef.DirectPlacementOpts == nil { - ptDef.DirectPlacementOpts = &model.PlacementSettings{} + if placementSettings == nil { + placementSettings = &model.PlacementSettings{} } - ptDef.DirectPlacementOpts.LearnerConstraints = op.StrValue + placementSettings.LearnerConstraints = op.StrValue case ast.TableOptionPlacementFollowerConstraints: - if ptDef.DirectPlacementOpts == nil { - ptDef.DirectPlacementOpts = &model.PlacementSettings{} + if placementSettings == nil { + placementSettings = &model.PlacementSettings{} } - ptDef.DirectPlacementOpts.FollowerConstraints = op.StrValue + placementSettings.FollowerConstraints = op.StrValue case ast.TableOptionPlacementVoterConstraints: - if ptDef.DirectPlacementOpts == nil { - ptDef.DirectPlacementOpts = &model.PlacementSettings{} + if placementSettings == nil { + placementSettings = &model.PlacementSettings{} } - ptDef.DirectPlacementOpts.VoterConstraints = op.StrValue + placementSettings.VoterConstraints = op.StrValue default: - return errors.Trace(errors.New("unknown placement policy option")) + return errors.Trace(errors.New("unknown partition option")) } } } // Can not use both a placement policy and direct assignment. If you alter specify both in a CREATE TABLE or ALTER TABLE an error will be returned. - if ptDef.DirectPlacementOpts != nil && ptDef.PlacementPolicyRef != nil { - return errors.Trace(ErrPlacementPolicyWithDirectOption.GenWithStackByArgs(ptDef.PlacementPolicyRef.Name)) + if placementSettings != nil && policyRefInfo != nil { + return errors.Trace(ErrPlacementPolicyWithDirectOption.GenWithStackByArgs(policyRefInfo.Name)) } - if ptDef.DirectPlacementOpts != nil { + if placementSettings != nil { // check the direct placement option compatibility. - if err := checkPolicyValidation(ptDef.DirectPlacementOpts); err != nil { + if err := checkPolicyValidation(placementSettings); err != nil { return errors.Trace(err) } } - if ptDef.PlacementPolicyRef != nil { - // placement policy reference will override the direct placement options. - policy, ok := ctx.GetInfoSchema().(infoschema.InfoSchema).PolicyByName(ptDef.PlacementPolicyRef.Name) + if policyRefInfo != nil { + policy, ok := ctx.GetInfoSchema().(infoschema.InfoSchema).PolicyByName(policyRefInfo.Name) if !ok { - return errors.Trace(infoschema.ErrPlacementPolicyNotExists.GenWithStackByArgs(ptDef.PlacementPolicyRef.Name)) + return errors.Trace(infoschema.ErrPlacementPolicyNotExists.GenWithStackByArgs(policyRefInfo.Name)) } - ptDef.PlacementPolicyRef.ID = policy.ID + policyRefInfo.ID = policy.ID } + job := &model.Job{ SchemaID: schema.ID, TableID: meta.ID, SchemaName: schema.Name.L, Type: model.ActionAlterTablePartitionPolicy, BinlogInfo: &model.HistoryInfo{}, - Args: []interface{}{partitionID, ptDef}, + Args: []interface{}{partitionID, policyRefInfo, placementSettings}, } err = d.doDDLJob(ctx, job) - if err != nil { - return errors.Trace(err) - } - err = d.callHookOnChanged(err) return errors.Trace(err) } diff --git a/ddl/table.go b/ddl/table.go index 48065c99949f1..8d533f7db3baf 100644 --- a/ddl/table.go +++ b/ddl/table.go @@ -1243,8 +1243,9 @@ func onAlterTablePartitionAttributes(t *meta.Meta, job *model.Job) (ver int64, e func onAlterTablePartitionOptions(t *meta.Meta, job *model.Job) (ver int64, err error) { var partitionID int64 - ptDef := &model.PartitionDefinition{} - err = job.DecodeArgs(&partitionID, ptDef) + policyRefInfo := &model.PolicyRefInfo{} + placementSettings := &model.PlacementSettings{} + err = job.DecodeArgs(&partitionID, policyRefInfo, placementSettings) if err != nil { job.State = model.JobStateCancelled return 0, errors.Trace(err) @@ -1259,11 +1260,11 @@ func onAlterTablePartitionOptions(t *meta.Meta, job *model.Job) (ver int64, err job.State = model.JobStateCancelled return 0, errors.Trace(table.ErrUnknownPartition.GenWithStackByArgs("drop?", tblInfo.Name.O)) } - for idx, orgPtDef := range ptInfo.Definitions { - if orgPtDef.ID == partitionID { - orgPtDef.DirectPlacementOpts = ptDef.DirectPlacementOpts - orgPtDef.PlacementPolicyRef = ptDef.PlacementPolicyRef - ptInfo.Definitions[idx] = orgPtDef + for idx, ptDef := range ptInfo.Definitions { + if ptDef.ID == partitionID { + ptDef.DirectPlacementOpts = placementSettings + ptDef.PlacementPolicyRef = policyRefInfo + ptInfo.Definitions[idx] = ptDef break } }