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

placement: merge the rules if the constraints are the same #46529

Merged
merged 8 commits into from
Sep 25, 2023

Conversation

nolouch
Copy link
Member

@nolouch nolouch commented Aug 30, 2023

What problem does this PR solve?

Issue Number: close #45395

Problem Summary:

 create placement policy retail_pos constraints="[+retail_region=fast]";
 CREATE TABLE t1 (a INT);
 ALTER TABLE t1  PLACEMENT POLICY=retail_pos;

The rules in PD side:

[
  {
    "group_id": "TiDB_DDL_90",
    "id": "table_rule_90_0",
    "index": 40,
    "start_key": "7480000000000000ff5a00000000000000f8",
    "end_key": "7480000000000000ff5b00000000000000f8",
    "role": "leader",
    "is_witness": false,
    "count": 1,
    "label_constraints": [
      {
        "key": "retail_region",
        "op": "in",
        "values": [
          "fast"
        ]
      },
      ....
    ],
    "create_timestamp": 1689589620
  },
  {
    "group_id": "TiDB_DDL_90",
    "id": "table_rule_90_1",
    "index": 40,
    "start_key": "7480000000000000ff5a00000000000000f8",
    "end_key": "7480000000000000ff5b00000000000000f8",
    "role": "voter",
    "is_witness": false,
    "count": 2,
    "label_constraints": [
      {
        "key": "retail_region",
        "op": "in",
        "values": [
          "fast"
        ]
      },
      ...
    ],
    "create_timestamp": 1689589620
  },
  {
    "group_id": "pd",
    "id": "default",
    "start_key": "",
    "end_key": "",
    "role": "voter",
    "is_witness": false,
    "count": 3
  }
]

There is no need to split into two rules because we do not need to distinguish roles, two rules perhaps exist some problems. such as location-labels may not work in tikv/pd#6637

What is changed and how it works?

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

None

@ti-chi-bot ti-chi-bot bot added release-note-none Denotes a PR that doesn't merit a release note. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Aug 30, 2023
@codecov
Copy link

codecov bot commented Aug 30, 2023

Codecov Report

Merging #46529 (2d9de72) into master (d3d30f5) will decrease coverage by 0.5134%.
Report is 178 commits behind head on master.
The diff coverage is 92.1739%.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #46529        +/-   ##
================================================
- Coverage   73.3326%   72.8193%   -0.5134%     
================================================
  Files          1322       1361        +39     
  Lines        396474     407518     +11044     
================================================
+ Hits         290745     296752      +6007     
- Misses        87208      92033      +4825     
- Partials      18521      18733       +212     
Flag Coverage Δ
integration 33.4624% <83.4782%> (?)
unit 73.0971% <92.1739%> (-0.2356%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
dumpling 53.9913% <ø> (-0.0532%) ⬇️
parser 84.9484% <ø> (-0.0234%) ⬇️
br 48.8855% <ø> (-3.7563%) ⬇️

Signed-off-by: nolouch <nolouch@gmail.com>
Signed-off-by: nolouch <nolouch@gmail.com>
@nolouch
Copy link
Member Author

nolouch commented Sep 1, 2023

/retest-required

@nolouch nolouch requested a review from CabinfeverB September 1, 2023 07:20
@nolouch
Copy link
Member Author

nolouch commented Sep 5, 2023

/retest-required

Signed-off-by: nolouch <nolouch@gmail.com>
@ti-chi-bot ti-chi-bot bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Sep 10, 2023
@nolouch
Copy link
Member Author

nolouch commented Sep 10, 2023

/retest-required

Signed-off-by: nolouch <nolouch@gmail.com>
func (constraints Constraints) FingerPrint() string {
copied := make(Constraints, len(constraints))
copy(copied, constraints)
sort.SliceStable(copied, func(i, j int) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slices.SortStableFunction is better. it can remove BCE.

Signed-off-by: nolouch <nolouch@gmail.com>
@nolouch
Copy link
Member Author

nolouch commented Sep 11, 2023

PTAL @CabinfeverB @JmPotato

@nolouch
Copy link
Member Author

nolouch commented Sep 11, 2023

/test unit-test

@tiprow
Copy link

tiprow bot commented Sep 11, 2023

@nolouch: The specified target(s) for /test were not found.
The following commands are available to trigger required jobs:

  • /test tiprow_fast_test

Use /test all to run all jobs.

In response to this:

/test unit-test

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

ddl/placement/bundle.go Outdated Show resolved Hide resolved
ddl/placement/constraints.go Outdated Show resolved Hide resolved
ddl/placement/bundle.go Outdated Show resolved Hide resolved
}
}
if leaderGroup != nil && canBecameLeaderNum == 1 {
leaderGroup.MergeTransformableRoles()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some comments here or somewhere to explain why when two or more groups have a leader or voter role, we should skip MergeTransformableRoles?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. But if 1 leader and 2 followers in group1 and 2 voters in group2, can we merge group1 to voters?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. But if 1 leader and 2 followers in group1 and 2 voters in group2, can we merge group1 to voters?

No. They don't share same constraints.

Copy link
Member Author

@nolouch nolouch Sep 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. But if 1 leader and 2 followers in group1 and 2 voters in group2, can we merge group1 to voters?

that means the group 1 has leader priority than group 2, so it's not transformable. I added comments.

// One Bundle is from one PlacementSettings, rule share same location labels, so we can use the first rule's location labels.
var locationLabels []string
tempRules := b.Rules[:0]
id := 0
for _, rule := range b.Rules {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some extra checks for rules in Tidy? I think Tidy can only be called when some fields are empty (StartKeyHex/EndKeyHex .etc).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some extra checks for rules in Tidy? I think Tidy can only be called when some fields are empty (StartKeyHex/EndKeyHex .etc).

Does it mean Bundle is not associated with a table?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the comments saied, it's optimized rules, try to simplify the rules.

@lcwangchao lcwangchao requested a review from xhebox September 12, 2023 08:46
ddl/placement/bundle.go Outdated Show resolved Hide resolved
// One Bundle is from one PlacementSettings, rule share same location labels, so we can use the first rule's location labels.
var locationLabels []string
tempRules := b.Rules[:0]
id := 0
for _, rule := range b.Rules {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add some extra checks for rules in Tidy? I think Tidy can only be called when some fields are empty (StartKeyHex/EndKeyHex .etc).

Does it mean Bundle is not associated with a table?

ddl/placement/bundle.go Show resolved Hide resolved
if len(c.rules) == 0 {
return
}
newRules := make([]*Rule, 0, len(c.rules))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The maximum length is 2?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe more.

}
}
if leaderGroup != nil && canBecameLeaderNum == 1 {
leaderGroup.MergeTransformableRoles()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. But if 1 leader and 2 followers in group1 and 2 voters in group2, can we merge group1 to voters?

Copy link
Contributor

@CabinfeverB CabinfeverB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And I have a question: as PR description, the cause of multiple rules is multiple tables instead of role?

Signed-off-by: nolouch <nolouch@gmail.com>
@nolouch
Copy link
Member Author

nolouch commented Sep 22, 2023

And I have a question: as PR description, the cause of multiple rules is multiple tables instead of role?

it is caused by multi-role setting. see

func NewBundleFromConstraintsOptions(options *model.PlacementSettings) (*Bundle, error) {
if options == nil {
return nil, fmt.Errorf("%w: options can not be nil", ErrInvalidPlacementOptions)
}
if len(options.PrimaryRegion) > 0 || len(options.Regions) > 0 || len(options.Schedule) > 0 {
return nil, fmt.Errorf("%w: should be [LEADER/VOTER/LEARNER/FOLLOWER]_CONSTRAINTS=.. [VOTERS/FOLLOWERS/LEARNERS]=.., mixed other sugar options %s", ErrInvalidPlacementOptions, options)
}
constraints := options.Constraints
leaderConst := options.LeaderConstraints
learnerConstraints := options.LearnerConstraints
followerConstraints := options.FollowerConstraints
followerCount := options.Followers
learnerCount := options.Learners
commonConstraints, err := NewConstraintsFromYaml([]byte(constraints))
if err != nil {
return nil, fmt.Errorf("%w: 'Constraints' should be [constraint1, ...] or any yaml compatible array representation", err)
}
rules := []*Rule{}
leaderConstraints, err := NewConstraintsFromYaml([]byte(leaderConst))
if err != nil {
return nil, fmt.Errorf("%w: 'LeaderConstraints' should be [constraint1, ...] or any yaml compatible array representation", err)
}
for _, cnst := range commonConstraints {
if err := leaderConstraints.Add(cnst); err != nil {
return nil, fmt.Errorf("%w: LeaderConstraints conflicts with Constraints", err)
}
}
rules = append(rules, NewRule(Leader, 1, leaderConstraints))
followerRules, err := NewRules(Voter, followerCount, followerConstraints)
if err != nil {
return nil, fmt.Errorf("%w: invalid FollowerConstraints", err)
}
for _, rule := range followerRules {
// give a default of 2 followers
if rule.Count == 0 {
rule.Count = 2
}
for _, cnst := range commonConstraints {
if err := rule.Constraints.Add(cnst); err != nil {
return nil, fmt.Errorf("%w: FollowerConstraints conflicts with Constraints", err)
}
}
}
rules = append(rules, followerRules...)
learnerRules, err := NewRules(Learner, learnerCount, learnerConstraints)
if err != nil {
return nil, fmt.Errorf("%w: invalid LearnerConstraints", err)
}
for _, rule := range learnerRules {
if rule.Count == 0 {
if len(rule.Constraints) > 0 {
return nil, fmt.Errorf("%w: specify learner constraints without specify how many learners to be placed", ErrInvalidPlacementOptions)
}
}
for _, cnst := range commonConstraints {
if err := rule.Constraints.Add(cnst); err != nil {
return nil, fmt.Errorf("%w: LearnerConstraints conflicts with Constraints", err)
}
}
if rule.Count > 0 {
rules = append(rules, rule)
}
}
labels, err := newLocationLabelsFromSurvivalPreferences(options.SurvivalPreferences)
if err != nil {
return nil, err
}
for _, rule := range rules {
rule.LocationLabels = labels
}
return &Bundle{Rules: rules}, nil
}

@nolouch
Copy link
Member Author

nolouch commented Sep 22, 2023

PTAL @lcwangchao @xhebox @CabinfeverB thanks.

@ti-chi-bot ti-chi-bot bot added needs-1-more-lgtm Indicates a PR needs 1 more LGTM. approved labels Sep 22, 2023
Signed-off-by: nolouch <nolouch@gmail.com>
@CabinfeverB
Copy link
Contributor

/test unit-test

@tiprow
Copy link

tiprow bot commented Sep 25, 2023

@CabinfeverB: The specified target(s) for /test were not found.
The following commands are available to trigger required jobs:

  • /test tiprow_fast_test

Use /test all to run all jobs.

In response to this:

/test unit-test

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@ti-chi-bot
Copy link

ti-chi-bot bot commented Sep 25, 2023

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: CabinfeverB, xhebox

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Sep 25, 2023
@ti-chi-bot
Copy link

ti-chi-bot bot commented Sep 25, 2023

[LGTM Timeline notifier]

Timeline:

  • 2023-09-22 08:47:39.758424669 +0000 UTC m=+5449.476766885: ☑️ agreed by xhebox.
  • 2023-09-25 08:25:32.05808706 +0000 UTC m=+263321.776429276: ☑️ agreed by CabinfeverB.

@nolouch
Copy link
Member Author

nolouch commented Sep 25, 2023

/test unit-test

@tiprow
Copy link

tiprow bot commented Sep 25, 2023

@nolouch: The specified target(s) for /test were not found.
The following commands are available to trigger required jobs:

  • /test tiprow_fast_test

Use /test all to run all jobs.

In response to this:

/test unit-test

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@ti-chi-bot ti-chi-bot bot merged commit 4cc6c18 into pingcap:master Sep 25, 2023
@nolouch nolouch deleted the improve-bundle branch September 25, 2023 11:58
nolouch added a commit to nolouch/tidb that referenced this pull request Oct 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm release-note-none Denotes a PR that doesn't merit a release note. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

reduce rule numbers if not set the role in the rule SQL
5 participants