Repository: tikv/pd
Pull request #5319 (2022-07-18 05:31:54)

core: fix the wrong statistics of the region tree (#5301)
This is an automated cherry-pick of #5301

Signed-off-by: Ryan Leung <rleungx@gmail.com>

<!--
Thank you for working on PD! Please read PD's [CONTRIBUTING](https://github.com/tikv/pd/blob/master/CONTRIBUTING.md) document **BEFORE** filing this PR.
PR Title Format:
1. pkg [, pkg2, pkg3]: what's changed
2. *: what's changed
-->

### What problem does this PR solve?

<!--

Please create an issue first to describe the problem.
There MUST be one line starting with "Issue Number:  " and 
linking the relevant issues via the "close" or "ref".
For more info, check https://github.com/tikv/pd/blob/master/CONTRIBUTING.md#linking-issues.

-->
Issue Number: Close https://github.com/tikv/pd/issues/5318

### What is changed and how does it work?

<!--

You could use the "commit message" code block to add more description to the final commit message.
For more info, check https://github.com/tikv/pd/blob/master/CONTRIBUTING.md#format-of-the-commit-message.

-->

```commit-message
```

### Check List

<!-- Remove the items that are not applicable. -->

Tests

<!-- At least one of these tests must be included. -->

- Unit test

### Release note

<!-- A bugfix or a new feature needs a release note. If there is no need release note, just uncomment the below line. -->

```release-note
Fix the issue that the statistics of the region tree may be not accurate
```


ti-chi-bot: [REVIEW NOTIFICATION]

This pull request has not been approved.


To complete the [pull request process](https://book.prow.tidb.io/#/en/workflows/pr), please ask the reviewers in the [list](https://prow.tidb.io/tichi/repos/tikv/pd/pulls/5319/owners) to review by filling `/cc @reviewer` in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the [list](https://prow.tidb.io/tichi/repos/tikv/pd/pulls/5319/owners) by filling  `/assign @committer` in the comment to help you merge this pull request.

The full list of commands accepted by this bot can be found [here](https://prow.tidb.io/command-help?repo=tikv%2Fpd).

<details>

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.
</details>

<!--Review Notification Identifier-->
ti-chi-bot: @ti-chi-bot: This cherry pick PR is for a release branch and has not yet been approved by release team.
Adding the `do-not-merge/cherry-pick-not-approved` label.

To merge this cherry pick, it must first be approved by the collaborators.

**AFTER** it has been approved by collaborators, please ping the release team in a comment to request a cherry pick review.


<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
</details>
bufferflies: plz fix the conflicts fro this pr @rleungx .
codecov[bot]: # [Codecov](https://codecov.io/gh/tikv/pd/pull/5319?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv) Report
> Merging [#5319](https://codecov.io/gh/tikv/pd/pull/5319?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv) (85a3f6d) into [release-6.1](https://codecov.io/gh/tikv/pd/commit/d82f4fab6cf37cd1eca9c3574984e12a7ae27c42?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv) (d82f4fa) will **increase** coverage by `0.04%`.
> The diff coverage is `100.00%`.

```diff
@@               Coverage Diff               @@
##           release-6.1    #5319      +/-   ##
===============================================
+ Coverage        75.38%   75.42%   +0.04%     
===============================================
  Files              307      307              
  Lines            30321    30387      +66     
===============================================
+ Hits             22856    22919      +63     
- Misses            5466     5473       +7     
+ Partials          1999     1995       -4     
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `75.42% <100.00%> (+0.04%)` | :arrow_up: |

Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv#carryforward-flags-in-the-pull-request-comment) to find out more.

| [Impacted Files](https://codecov.io/gh/tikv/pd/pull/5319?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv) | Coverage Δ | |
|---|---|---|
| [server/core/region\_tree.go](https://codecov.io/gh/tikv/pd/pull/5319/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv#diff-c2VydmVyL2NvcmUvcmVnaW9uX3RyZWUuZ28=) | `94.16% <100.00%> (ø)` | |
| [pkg/errs/errs.go](https://codecov.io/gh/tikv/pd/pull/5319/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv#diff-cGtnL2VycnMvZXJycy5nbw==) | `75.00% <0.00%> (-25.00%)` | :arrow_down: |
| [pkg/dashboard/adapter/manager.go](https://codecov.io/gh/tikv/pd/pull/5319/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv#diff-cGtnL2Rhc2hib2FyZC9hZGFwdGVyL21hbmFnZXIuZ28=) | `74.71% <0.00%> (-11.50%)` | :arrow_down: |
| [server/storage/kv/etcd\_kv.go](https://codecov.io/gh/tikv/pd/pull/5319/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv#diff-c2VydmVyL3N0b3JhZ2Uva3YvZXRjZF9rdi5nbw==) | `69.56% <0.00%> (-10.15%)` | :arrow_down: |
| [server/region\_syncer/client.go](https://codecov.io/gh/tikv/pd/pull/5319/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv#diff-c2VydmVyL3JlZ2lvbl9zeW5jZXIvY2xpZW50Lmdv) | `79.85% <0.00%> (-5.98%)` | :arrow_down: |
| [server/schedule/labeler/rules.go](https://codecov.io/gh/tikv/pd/pull/5319/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv#diff-c2VydmVyL3NjaGVkdWxlL2xhYmVsZXIvcnVsZXMuZ28=) | `87.50% <0.00%> (-2.28%)` | :arrow_down: |
| [server/grpc\_service.go](https://codecov.io/gh/tikv/pd/pull/5319/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv#diff-c2VydmVyL2dycGNfc2VydmljZS5nbw==) | `51.65% <0.00%> (-1.13%)` | :arrow_down: |
| [server/statistics/region\_collection.go](https://codecov.io/gh/tikv/pd/pull/5319/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv#diff-c2VydmVyL3N0YXRpc3RpY3MvcmVnaW9uX2NvbGxlY3Rpb24uZ28=) | `97.48% <0.00%> (-0.90%)` | :arrow_down: |
| [server/api/region.go](https://codecov.io/gh/tikv/pd/pull/5319/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv#diff-c2VydmVyL2FwaS9yZWdpb24uZ28=) | `68.58% <0.00%> (-0.58%)` | :arrow_down: |
| [server/cluster/cluster.go](https://codecov.io/gh/tikv/pd/pull/5319/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv#diff-c2VydmVyL2NsdXN0ZXIvY2x1c3Rlci5nbw==) | `82.84% <0.00%> (-0.35%)` | :arrow_down: |
| ... and [24 more](https://codecov.io/gh/tikv/pd/pull/5319/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv) | |

Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv)

ti-chi-bot: @rleungx: It seems you want to merge this PR, I will help you trigger all the tests:

/run-all-tests


<details>

Instructions for interacting with me using PR comments are available [here](https://prow.tidb.net/command-help).  If you have questions or suggestions related to my behavior, please file an issue against the [ti-community-infra/tichi](https://github.com/ti-community-infra/tichi/issues/new?title=Prow%20issue:) repository.
</details>
ti-chi-bot: This pull request has been accepted and is ready to merge. <details>Commit hash: 85a3f6dd826a7ca7e977b68e88fc7af20bd65068</details>
ti-chi-bot: @ti-chi-bot: Your PR was out of date, I have automatically updated it for you.

At the same time I will also trigger all tests for you:

/run-all-tests

If the CI test fails, you just re-trigger the test that failed and the bot will merge the PR for you after the CI passes.


<details>

Instructions for interacting with me using PR comments are available [here](https://prow.tidb.io/command-help).  If you have questions or suggestions related to my behavior, please file an issue against the [ti-community-infra/tichi](https://github.com/ti-community-infra/tichi/issues/new?title=Prow%20issue:) repository.
</details>

Patch:
diff --git a/server/core/region_tree.go b/server/core/region_tree.go
index 4bb1dc9d0..aa32b8a10 100644
--- a/server/core/region_tree.go
+++ b/server/core/region_tree.go
@@ -151,8 +151,8 @@ func (t *regionTree) remove(region *RegionInfo) {
 		return
 	}
 
-	t.totalSize -= region.approximateSize
-	regionWriteBytesRate, regionWriteKeysRate := region.GetWriteRate()
+	t.totalSize -= result.(*regionItem).region.GetApproximateSize()
+	regionWriteBytesRate, regionWriteKeysRate := result.(*regionItem).region.GetWriteRate()
 	t.totalWriteBytesRate -= regionWriteBytesRate
 	t.totalWriteKeysRate -= regionWriteKeysRate
 	t.tree.Remove(result)


Test patch:
diff --git a/server/core/region_tree_test.go b/server/core/region_tree_test.go
index 92c26744a..48a3ca7a6 100644
--- a/server/core/region_tree_test.go
+++ b/server/core/region_tree_test.go
@@ -130,7 +130,7 @@ func (s *testRegionSuite) TestRegionTreeStat(c *C) {
 	c.Assert(tree.totalSize, Equals, int64(4))
 	updateNewItem(tree, s.newRegionWithStat("b", "e", 5, 6))
 	c.Assert(tree.totalSize, Equals, int64(6))
-	tree.remove(s.newRegionWithStat("a", "b", 1, 2))
+	tree.remove(s.newRegionWithStat("a", "b", 2, 2))
 	c.Assert(tree.totalSize, Equals, int64(5))
 	tree.remove(s.newRegionWithStat("f", "g", 1, 2))
 	c.Assert(tree.totalSize, Equals, int64(5))