* ddl: check count here * ddl: drop all rules that will be overrided * ddl: change ID format * ddl: local placement deifinition * ddl: support `ALTER PLACEMENT` clause * ddl: add test cases * ddl: fix ID format * ddl: copy comments too * ddl: allow ignore replicas with dict constraints * ddl: rename COUNT to REPLICAS * ddl: clone method for RuleOp * ddl: checkPlacementSpec => buildPlacementSpec * ddl: remove TODO It is possible to incrementally add replicas. Consider drop later. Let us support it correctly first. * ddl: ID format Datbase/schema ID is added back. A common prefix will improve the efficience of PD batch operation. Drop a whole db/table will only need one loop for rules. * ddl: move const/util to placement package * ddl: fix test * ddl: fix format error * ddl: error check fix * ddl: use an explicit condition flag * ddl: adapt parser changes * ddl: fix go.mod/simplify code path * ddl: go mod tidy * ddl: improve tests * ddl: return new rules instead of appending * ddl: return constraints instead of appending * ddl: fix test * ddl: one more test * ddl: remove rules rollback, meaningless * ddl: fix slice * ddl: add period to comments * Update ddl/ddl_api.go Co-authored-by: tangenta <tangenta@126.com> * ddl: remove unused arguments * infosync: do not request PD if no ruleOperations * ddl: a new test suite Co-authored-by: tangenta <tangenta@126.com> Co-authored-by: ti-srebot <66930949+ti-srebot@users.noreply.github.com>
72 lines
1.9 KiB
Go
72 lines
1.9 KiB
Go
// Copyright 2020 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,
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package placement
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/pingcap/errors"
|
|
)
|
|
|
|
func checkLabelConstraint(label string) (LabelConstraint, error) {
|
|
r := LabelConstraint{}
|
|
|
|
if len(label) < 4 {
|
|
return r, errors.Errorf("label constraint should be in format '{+|-}key=value', but got '%s'", label)
|
|
}
|
|
|
|
var op LabelConstraintOp
|
|
switch label[0] {
|
|
case '+':
|
|
op = In
|
|
case '-':
|
|
op = NotIn
|
|
default:
|
|
return r, errors.Errorf("label constraint should be in format '{+|-}key=value', but got '%s'", label)
|
|
}
|
|
|
|
kv := strings.Split(label[1:], "=")
|
|
if len(kv) != 2 {
|
|
return r, errors.Errorf("label constraint should be in format '{+|-}key=value', but got '%s'", label)
|
|
}
|
|
|
|
key := strings.TrimSpace(kv[0])
|
|
if key == "" {
|
|
return r, errors.Errorf("label constraint should be in format '{+|-}key=value', but got '%s'", label)
|
|
}
|
|
|
|
val := strings.TrimSpace(kv[1])
|
|
if val == "" {
|
|
return r, errors.Errorf("label constraint should be in format '{+|-}key=value', but got '%s'", label)
|
|
}
|
|
|
|
r.Key = key
|
|
r.Op = op
|
|
r.Values = []string{val}
|
|
return r, nil
|
|
}
|
|
|
|
// CheckLabelConstraints will check labels, and build LabelConstraints for rule.
|
|
func CheckLabelConstraints(labels []string) ([]LabelConstraint, error) {
|
|
constraints := make([]LabelConstraint, 0, len(labels))
|
|
for _, str := range labels {
|
|
label, err := checkLabelConstraint(strings.TrimSpace(str))
|
|
if err != nil {
|
|
return constraints, err
|
|
}
|
|
constraints = append(constraints, label)
|
|
}
|
|
return constraints, nil
|
|
}
|