-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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 create and drop placement policy #27574
Merged
ti-chi-bot
merged 13 commits into
pingcap:master
from
AilinKid:create-drop-placement-policy
Aug 30, 2021
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9e61c9f
support create and drop placement policy
AilinKid 7694a70
clean code
AilinKid 927ed78
fmt
AilinKid 2c213c0
lint
AilinKid f856dc1
Merge branch 'master' into create-drop-placement-policy
AilinKid 5109c46
.
AilinKid 602d158
make errdoc
AilinKid c40eeca
Merge branch 'master' into create-drop-placement-policy
AilinKid 7e2d56d
Merge branch 'master' into create-drop-placement-policy
AilinKid 4c7a32e
add leaderConstraints
AilinKid 2192fef
Merge branch 'master' into create-drop-placement-policy
AilinKid 5a3764e
.
AilinKid 74e45a4
Merge branch 'master' into create-drop-placement-policy
ti-chi-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
// Copyright 2021 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ddl | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pingcap/errors" | ||
"github.com/pingcap/parser/model" | ||
"github.com/pingcap/tidb/infoschema" | ||
"github.com/pingcap/tidb/meta" | ||
"github.com/pingcap/tidb/util/placementpolicy" | ||
) | ||
|
||
func onCreatePlacementPolicy(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ error) { | ||
policyInfo := &placementpolicy.PolicyInfo{} | ||
if err := job.DecodeArgs(policyInfo); err != nil { | ||
job.State = model.JobStateCancelled | ||
return ver, errors.Trace(err) | ||
} | ||
policyInfo.State = model.StateNone | ||
|
||
err := checkPlacementPolicyNotExistAndCancelExistJob(d, t, job, policyInfo) | ||
if err != nil { | ||
return ver, errors.Trace(err) | ||
} | ||
switch policyInfo.State { | ||
case model.StateNone: | ||
// none -> public | ||
policyInfo.State = model.StatePublic | ||
err = t.CreatePolicy(policyInfo) | ||
if err != nil { | ||
return ver, errors.Trace(err) | ||
} | ||
job.SchemaID = policyInfo.ID | ||
|
||
ver, err = updateSchemaVersion(t, job) | ||
if err != nil { | ||
return ver, errors.Trace(err) | ||
} | ||
// Finish this job. | ||
job.FinishDBJob(model.JobStateDone, model.StatePublic, ver, nil) | ||
return ver, nil | ||
default: | ||
// We can't enter here. | ||
return ver, ErrInvalidDDLState.GenWithStackByArgs("policy", policyInfo.State) | ||
} | ||
} | ||
|
||
func getPolicyInfo(t *meta.Meta, policyID int64) (*placementpolicy.PolicyInfo, error) { | ||
policy, err := t.GetPolicy(policyID) | ||
if err != nil { | ||
if meta.ErrPolicyNotExists.Equal(err) { | ||
return nil, infoschema.ErrPlacementPolicyNotExists.GenWithStackByArgs( | ||
fmt.Sprintf("(Policy ID %d)", policyID), | ||
) | ||
} | ||
return nil, err | ||
} | ||
return policy, nil | ||
} | ||
|
||
func checkPlacementPolicyNotExistAndCancelExistJob(d *ddlCtx, t *meta.Meta, job *model.Job, info *placementpolicy.PolicyInfo) error { | ||
currVer, err := t.GetSchemaVersion() | ||
if err != nil { | ||
return err | ||
} | ||
is := d.infoCache.GetLatest() | ||
if is.SchemaMetaVersion() == currVer { | ||
// Use cached policy. | ||
_, ok := is.PolicyByName(info.Name) | ||
if ok { | ||
job.State = model.JobStateCancelled | ||
return infoschema.ErrPlacementPolicyExists.GenWithStackByArgs(info.Name) | ||
} | ||
return nil | ||
} | ||
// Check in meta directly. | ||
policies, err := t.ListPolicies() | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
for _, policy := range policies { | ||
if policy.Name.L == info.Name.L { | ||
job.State = model.JobStateCancelled | ||
return infoschema.ErrPlacementPolicyExists.GenWithStackByArgs(info.Name) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func checkPlacementPolicyExistAndCancelNonExistJob(t *meta.Meta, job *model.Job, policyID int64) (*placementpolicy.PolicyInfo, error) { | ||
policy, err := getPolicyInfo(t, policyID) | ||
if err == nil { | ||
return policy, nil | ||
} | ||
if infoschema.ErrPlacementPolicyNotExists.Equal(err) { | ||
job.State = model.JobStateCancelled | ||
} | ||
return nil, err | ||
} | ||
|
||
func onDropPlacementPolicy(t *meta.Meta, job *model.Job) (ver int64, _ error) { | ||
policyInfo, err := checkPlacementPolicyExistAndCancelNonExistJob(t, job, job.SchemaID) | ||
if err != nil { | ||
return ver, errors.Trace(err) | ||
} | ||
switch policyInfo.State { | ||
case model.StatePublic: | ||
// public -> write only | ||
policyInfo.State = model.StateWriteOnly | ||
err = t.UpdatePolicy(policyInfo) | ||
if err != nil { | ||
return ver, errors.Trace(err) | ||
} | ||
ver, err = updateSchemaVersion(t, job) | ||
if err != nil { | ||
return ver, errors.Trace(err) | ||
} | ||
// Update the job state when all affairs done. | ||
job.SchemaState = model.StateWriteOnly | ||
case model.StateWriteOnly: | ||
// write only -> delete only | ||
policyInfo.State = model.StateDeleteOnly | ||
err = t.UpdatePolicy(policyInfo) | ||
if err != nil { | ||
return ver, errors.Trace(err) | ||
} | ||
ver, err = updateSchemaVersion(t, job) | ||
if err != nil { | ||
return ver, errors.Trace(err) | ||
} | ||
// Update the job state when all affairs done. | ||
job.SchemaState = model.StateDeleteOnly | ||
case model.StateDeleteOnly: | ||
policyInfo.State = model.StateNone | ||
if err = t.DropPolicy(policyInfo.ID); err != nil { | ||
return ver, errors.Trace(err) | ||
} | ||
ver, err = updateSchemaVersion(t, job) | ||
if err != nil { | ||
return ver, errors.Trace(err) | ||
} | ||
// TODO: Reset all the policy reference, (modify meta & notify pd) | ||
// If any partitions currently use this policy, they will be converted to the policy used by the table | ||
// they belong to. If any databases use this policy, they will be converted to the default placement_policy policy. | ||
|
||
// Finish this job. By now policy don't consider the binlog sync. | ||
job.FinishDBJob(model.JobStateDone, model.StateNone, ver, nil) | ||
default: | ||
err = ErrInvalidDDLState.GenWithStackByArgs("policy", policyInfo.State) | ||
} | ||
return ver, errors.Trace(err) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should do some checks here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this work is remained as described above into next pull request. otherwise, this is a too huge pr