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

*: Remove old syntax ALTER TABLE ... ADD/REMOVE/ALTER PLACEMENT POLICY ... #29603

Merged
merged 5 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
*: Remove some syntax with ALTER TABLE ADD/REMOVE/ALTER PLACEMENT
  • Loading branch information
lcwangchao committed Nov 9, 2021
commit c17144f03d8b578628c2a5f371774e052a99650e
3 changes: 0 additions & 3 deletions ddl/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,6 @@ var (
// ErrTableOptionInsertMethodUnsupported is returned when create/alter table with insert method option.
ErrTableOptionInsertMethodUnsupported = dbterror.ClassDDL.NewStd(mysql.ErrTableOptionInsertMethodUnsupported)

// ErrInvalidPlacementSpec is returned when add/alter an invalid placement rule
ErrInvalidPlacementSpec = dbterror.ClassDDL.NewStd(mysql.ErrInvalidPlacementSpec)

// ErrInvalidPlacementPolicyCheck is returned when txn_scope and commit data changing do not meet the placement policy
ErrInvalidPlacementPolicyCheck = dbterror.ClassDDL.NewStd(mysql.ErrPlacementPolicyCheck)

Expand Down
54 changes: 0 additions & 54 deletions ddl/placement/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"strings"

"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/util/codec"
Expand Down Expand Up @@ -237,59 +236,6 @@ func NewBundleFromOptions(options *model.PlacementSettings) (bundle *Bundle, err
return bundle, err
}

// ApplyPlacementSpec will apply actions defined in PlacementSpec to the bundle.
func (b *Bundle) ApplyPlacementSpec(specs []*ast.PlacementSpec) error {
for _, spec := range specs {
var role PeerRoleType
switch spec.Role {
case ast.PlacementRoleFollower:
role = Follower
case ast.PlacementRoleLeader:
if spec.Replicas == 0 {
spec.Replicas = 1
}
if spec.Replicas > 1 {
return ErrLeaderReplicasMustOne
}
role = Leader
case ast.PlacementRoleLearner:
role = Learner
case ast.PlacementRoleVoter:
role = Voter
default:
return ErrMissingRoleField
}

if spec.Tp == ast.PlacementAlter || spec.Tp == ast.PlacementDrop {
origLen := len(b.Rules)
newRules := b.Rules[:0]
for _, r := range b.Rules {
if r.Role != role {
newRules = append(newRules, r)
}
}
b.Rules = newRules

// alter == drop + add new rules
if spec.Tp == ast.PlacementDrop {
// error if no rules will be dropped
if len(b.Rules) == origLen {
return fmt.Errorf("%w: %s", ErrNoRulesToDrop, role)
}
continue
}
}

newRules, err := NewRules(role, spec.Replicas, spec.Constraints)
if err != nil {
return err
}
b.Rules = append(b.Rules, newRules...)
}

return nil
}

// String implements fmt.Stringer.
func (b *Bundle) String() string {
t, err := json.Marshal(b)
Expand Down
183 changes: 0 additions & 183 deletions ddl/placement/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

. "github.com/pingcap/check"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/model"
"github.com/pingcap/tidb/tablecodec"
"github.com/pingcap/tidb/util/codec"
Expand Down Expand Up @@ -342,188 +341,6 @@ func (s *testBundleSuite) TestGetLeaderDCByBundle(c *C) {
}
}

func (s *testBundleSuite) TestApplyPlacmentSpec(c *C) {
type TestCase struct {
name string
input []*ast.PlacementSpec
output []*Rule
err error
}
var tests []TestCase

tests = append(tests, TestCase{
name: "empty",
input: []*ast.PlacementSpec{},
output: []*Rule{},
})

rules, err := NewRules(Voter, 3, `["+zone=sh", "+zone=sh"]`)
c.Assert(err, IsNil)
c.Assert(rules, HasLen, 1)
tests = append(tests, TestCase{
name: "add voter array",
input: []*ast.PlacementSpec{{
Role: ast.PlacementRoleVoter,
Tp: ast.PlacementAdd,
Replicas: 3,
Constraints: `["+zone=sh", "+zone=sh"]`,
}},
output: rules,
})

rules, err = NewRules(Learner, 3, `["+zone=sh", "+zone=sh"]`)
c.Assert(err, IsNil)
c.Assert(rules, HasLen, 1)
tests = append(tests, TestCase{
name: "add learner array",
input: []*ast.PlacementSpec{{
Role: ast.PlacementRoleLearner,
Tp: ast.PlacementAdd,
Replicas: 3,
Constraints: `["+zone=sh", "+zone=sh"]`,
}},
output: rules,
})

rules, err = NewRules(Follower, 3, `["+zone=sh", "+zone=sh"]`)
c.Assert(err, IsNil)
c.Assert(rules, HasLen, 1)
tests = append(tests, TestCase{
name: "add follower array",
input: []*ast.PlacementSpec{{
Role: ast.PlacementRoleFollower,
Tp: ast.PlacementAdd,
Replicas: 3,
Constraints: `["+zone=sh", "+zone=sh"]`,
}},
output: rules,
})

tests = append(tests, TestCase{
name: "add invalid constraints",
input: []*ast.PlacementSpec{{
Role: ast.PlacementRoleVoter,
Tp: ast.PlacementAdd,
Replicas: 3,
Constraints: "ne",
}},
err: ErrInvalidConstraintsFormat,
})

tests = append(tests, TestCase{
name: "add empty role",
input: []*ast.PlacementSpec{{
Tp: ast.PlacementAdd,
Replicas: 3,
Constraints: "",
}},
err: ErrMissingRoleField,
})

tests = append(tests, TestCase{
name: "add multiple leaders",
input: []*ast.PlacementSpec{{
Role: ast.PlacementRoleLeader,
Tp: ast.PlacementAdd,
Replicas: 3,
Constraints: "",
}},
err: ErrLeaderReplicasMustOne,
})

rules, err = NewRules(Leader, 1, "")
c.Assert(err, IsNil)
c.Assert(rules, HasLen, 1)
tests = append(tests, TestCase{
name: "omit leader field",
input: []*ast.PlacementSpec{{
Role: ast.PlacementRoleLeader,
Tp: ast.PlacementAdd,
Constraints: "",
}},
output: rules,
})

rules, err = NewRules(Follower, 3, `["-zone=sh","+zone=bj"]`)
c.Assert(err, IsNil)
c.Assert(rules, HasLen, 1)
tests = append(tests, TestCase{
name: "drop",
input: []*ast.PlacementSpec{
{
Role: ast.PlacementRoleFollower,
Tp: ast.PlacementAdd,
Replicas: 3,
Constraints: `["- zone=sh", "+zone = bj"]`,
},
{
Role: ast.PlacementRoleVoter,
Tp: ast.PlacementAdd,
Replicas: 3,
Constraints: `["+ zone=sh", "-zone = bj"]`,
},
{
Role: ast.PlacementRoleVoter,
Tp: ast.PlacementDrop,
},
},
output: rules,
})

tests = append(tests, TestCase{
name: "drop unexisted",
input: []*ast.PlacementSpec{{
Role: ast.PlacementRoleLeader,
Tp: ast.PlacementDrop,
Constraints: "",
}},
err: ErrNoRulesToDrop,
})

rules1, err := NewRules(Follower, 3, `["-zone=sh","+zone=bj"]`)
c.Assert(err, IsNil)
c.Assert(rules1, HasLen, 1)
rules2, err := NewRules(Voter, 3, `["+zone=sh","-zone=bj"]`)
c.Assert(err, IsNil)
c.Assert(rules2, HasLen, 1)
tests = append(tests, TestCase{
name: "alter",
input: []*ast.PlacementSpec{
{
Role: ast.PlacementRoleFollower,
Tp: ast.PlacementAdd,
Replicas: 3,
Constraints: `["- zone=sh", "+zone = bj"]`,
},
{
Role: ast.PlacementRoleVoter,
Tp: ast.PlacementAdd,
Replicas: 3,
Constraints: `["- zone=sh", "+zone = bj"]`,
},
{
Role: ast.PlacementRoleVoter,
Tp: ast.PlacementAlter,
Replicas: 3,
Constraints: `["+ zone=sh", "-zone = bj"]`,
},
},
output: append(rules1, rules2...),
})

for _, t := range tests {
comment := Commentf("%s", t.name)
bundle := &Bundle{}
err := bundle.ApplyPlacementSpec(t.input)
if t.err == nil {
c.Assert(err, IsNil)
matchRules(t.output, bundle.Rules, comment.CheckCommentString(), c)
} else {
c.Assert(errors.Is(err, t.err), IsTrue, comment)
}
}
}

func (s *testBundleSuite) TestString(c *C) {
bundle := &Bundle{
ID: GroupID(1),
Expand Down
1 change: 0 additions & 1 deletion errno/errcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -1046,7 +1046,6 @@ const (
ErrUnsupportedConstraintCheck = 8231
ErrTableOptionUnionUnsupported = 8232
ErrTableOptionInsertMethodUnsupported = 8233
ErrInvalidPlacementSpec = 8234
ErrDDLReorgElementNotExist = 8235
ErrPlacementPolicyCheck = 8236
ErrInvalidAttributesSpec = 8237
Expand Down
1 change: 0 additions & 1 deletion errno/errname.go
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,6 @@ var MySQLErrName = map[uint16]*mysql.ErrMessage{
ErrPartitionStatsMissing: mysql.Message("Build table: %s global-level stats failed due to missing partition-level stats", nil),
ErrNotSupportedWithSem: mysql.Message("Feature '%s' is not supported when security enhanced mode is enabled", nil),

ErrInvalidPlacementSpec: mysql.Message("Invalid placement policy '%s': %s", nil),
ErrPlacementPolicyCheck: mysql.Message("Placement policy didn't meet the constraint, reason: %s", nil),
ErrMultiStatementDisabled: mysql.Message("client has multi-statement capability disabled. Run SET GLOBAL tidb_multi_statement_mode='ON' after you understand the security risk", nil),
ErrAsOf: mysql.Message("invalid as of timestamp: %s", nil),
Expand Down
Loading