Repository: tikv/pd
Pull request #4324 (2021-11-11 11:39:43)

scheduler: low space tikv can not balance 
Signed-off-by: bufferflies <1045931706@qq.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


If you want to open the **Challenge Program** pull request, please use the following template:
https://raw.githubusercontent.com/tikv/.github/master/.github/PULL_REQUEST_TEMPLATE/challenge-program.md
You can use it with query parameters: https://github.com/tikv/pd/compare/master...${you branch}?template=challenge-program.md
-->

### What problem does this PR solve?
close: #4323
<!-- Add the issue link with a summary if it exists. -->

### What is changed and how it works?
available space unit is not same 
### Check List

<!-- Remove the items that are not applicable. -->

Tests

<!-- At least one of them must be included. -->

- Unit test
- Integration test

capacity
  store1,store4: 161gb
  others: 215gb
available ratio and score
![image](https://user-images.githubusercontent.com/23159587/141400684-77a2b94d-d2d5-45ba-9bea-3cda28241305.png)

balance:
![image](https://user-images.githubusercontent.com/23159587/141400916-1049e344-31a6-4fca-bd95-c5b8837d3ffb.png)

there is no balance roll back 




Code changes

Side effects

Related changes

### 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
None
```


ti-chi-bot: 
[FORMAT CHECKER NOTIFICATION]

Please provide the associated issue number in the commit messages, for example: `close #12345`, or `ref #12345`.


<!--Checker Notification Identifier-->

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/4324/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/4324/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: 
[FORMAT CHECKER NOTIFICATION]

Please provide the associated issue number in the commit messages, for example: `close #12345`, or `ref #12345`.


<!--Checker Notification Identifier-->

codecov[bot]: # [Codecov](https://codecov.io/gh/tikv/pd/pull/4324?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv) Report
> Merging [#4324](https://codecov.io/gh/tikv/pd/pull/4324?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv) (3c64200) into [master](https://codecov.io/gh/tikv/pd/commit/bce753c64d7bc00677421bc572f07baaecada279?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv) (bce753c) will **decrease** coverage by `0.12%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/tikv/pd/pull/4324/graphs/tree.svg?width=650&height=150&src=pr&token=J1yRL471OI&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv)](https://codecov.io/gh/tikv/pd/pull/4324?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv)

```diff
@@            Coverage Diff             @@
##           master    #4324      +/-   ##
==========================================
- Coverage   74.97%   74.85%   -0.13%     
==========================================
  Files         264      264              
  Lines       27271    27271              
==========================================
- Hits        20447    20414      -33     
- Misses       5020     5039      +19     
- Partials     1804     1818      +14     
```

| Flag | Coverage Δ | |
|---|---|---|
| unittests | `74.85% <100.00%> (-0.13%)` | :arrow_down: |

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/4324?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv) | Coverage Δ | |
|---|---|---|
| [server/core/store.go](https://codecov.io/gh/tikv/pd/pull/4324/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv#diff-c2VydmVyL2NvcmUvc3RvcmUuZ28=) | `80.82% <100.00%> (+1.50%)` | :arrow_up: |
| [server/id/id.go](https://codecov.io/gh/tikv/pd/pull/4324/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv#diff-c2VydmVyL2lkL2lkLmdv) | `76.19% <0.00%> (-14.29%)` | :arrow_down: |
| [server/schedulers/shuffle\_hot\_region.go](https://codecov.io/gh/tikv/pd/pull/4324/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv#diff-c2VydmVyL3NjaGVkdWxlcnMvc2h1ZmZsZV9ob3RfcmVnaW9uLmdv) | `55.55% <0.00%> (-10.11%)` | :arrow_down: |
| [server/kv/etcd\_kv.go](https://codecov.io/gh/tikv/pd/pull/4324/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv#diff-c2VydmVyL2t2L2V0Y2Rfa3YuZ28=) | `70.42% <0.00%> (-9.86%)` | :arrow_down: |
| [pkg/dashboard/adapter/manager.go](https://codecov.io/gh/tikv/pd/pull/4324/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv#diff-cGtnL2Rhc2hib2FyZC9hZGFwdGVyL21hbmFnZXIuZ28=) | `75.53% <0.00%> (-7.45%)` | :arrow_down: |
| [server/tso/tso.go](https://codecov.io/gh/tikv/pd/pull/4324/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv#diff-c2VydmVyL3Rzby90c28uZ28=) | `60.22% <0.00%> (-6.82%)` | :arrow_down: |
| [server/tso/allocator\_manager.go](https://codecov.io/gh/tikv/pd/pull/4324/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv#diff-c2VydmVyL3Rzby9hbGxvY2F0b3JfbWFuYWdlci5nbw==) | `63.50% <0.00%> (-2.17%)` | :arrow_down: |
| [server/grpc\_service.go](https://codecov.io/gh/tikv/pd/pull/4324/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv#diff-c2VydmVyL2dycGNfc2VydmljZS5nbw==) | `50.10% <0.00%> (-1.22%)` | :arrow_down: |
| [server/tso/global\_allocator.go](https://codecov.io/gh/tikv/pd/pull/4324/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv#diff-c2VydmVyL3Rzby9nbG9iYWxfYWxsb2NhdG9yLmdv) | `60.84% <0.00%> (-1.06%)` | :arrow_down: |
| [server/schedule/checker/rule\_checker.go](https://codecov.io/gh/tikv/pd/pull/4324/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv#diff-c2VydmVyL3NjaGVkdWxlL2NoZWNrZXIvcnVsZV9jaGVja2VyLmdv) | `78.97% <0.00%> (-1.03%)` | :arrow_down: |
| ... and [16 more](https://codecov.io/gh/tikv/pd/pull/4324/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/tikv/pd/pull/4324?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/tikv/pd/pull/4324?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv). Last update [bce753c...3c64200](https://codecov.io/gh/tikv/pd/pull/4324?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=tikv).

ti-chi-bot: 
[FORMAT CHECKER NOTIFICATION]

Please provide the associated issue number in the commit messages, for example: `close #12345`, or `ref #12345`.


<!--Checker Notification Identifier-->

ti-chi-bot: 
[FORMAT CHECKER NOTIFICATION]

Please provide the associated issue number in the commit messages, for example: `close #12345`, or `ref #12345`.


<!--Checker Notification Identifier-->

ti-chi-bot: 
[FORMAT CHECKER NOTIFICATION]

Please provide the associated issue number in the commit messages, for example: `close #12345`, or `ref #12345`.


<!--Checker Notification Identifier-->

ti-chi-bot: 
[FORMAT CHECKER NOTIFICATION]

Please provide the associated issue number in the commit messages, for example: `close #12345`, or `ref #12345`.


<!--Checker Notification Identifier-->

ti-chi-bot: 
[FORMAT CHECKER NOTIFICATION]

Please provide the associated issue number in the commit messages, for example: `close #12345`, or `ref #12345`.


<!--Checker Notification Identifier-->

ti-chi-bot: 
[FORMAT CHECKER NOTIFICATION]

Please provide the associated issue number in the commit messages, for example: `close #12345`, or `ref #12345`.


<!--Checker Notification Identifier-->

nolouch: /merge
ti-chi-bot: @nolouch: 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.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>
ti-chi-bot: This pull request has been accepted and is ready to merge. <details>Commit hash: 500b2ff5e61ed5c2c866de2f63f7c9d3cd9e0946</details>
ti-chi-bot: @bufferflies: 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/store.go b/server/core/store.go
index 3957e426f..c01ff2a9c 100644
--- a/server/core/store.go
+++ b/server/core/store.go
@@ -343,7 +343,7 @@ func (s *StoreInfo) regionScoreV1(highSpaceRatio, lowSpaceRatio float64, delta i
 }
 
 func (s *StoreInfo) regionScoreV2(delta int64, lowSpaceRatio float64) float64 {
-	A := float64(s.GetAvgAvailable())
+	A := float64(s.GetAvgAvailable()) / gb
 	C := float64(s.GetCapacity()) / gb
 	R := float64(s.GetRegionSize() + delta)
 	if R < 0 {


Test patch:
diff --git a/server/core/store_test.go b/server/core/store_test.go
index d123a1657..700be818e 100644
--- a/server/core/store_test.go
+++ b/server/core/store_test.go
@@ -127,3 +127,47 @@ func (s *testStoreSuite) TestLowSpaceRatio(c *C) {
 	store.rawStats.Available = store.rawStats.Capacity >> 2
 	c.Assert(store.IsLowSpace(0.8), IsFalse)
 }
+
+func (s *testStoreSuite) TestLowSpaceScoreV2(c *C) {
+	testdata := []struct {
+		bigger *StoreInfo
+		small  *StoreInfo
+	}{{
+		// store1 and store2 has same store available ratio and store1 less 50gb
+		bigger: NewStoreInfoWithAvailable(1, 20*gb, 100*gb, 1.4),
+		small:  NewStoreInfoWithAvailable(2, 200*gb, 1000*gb, 1.4),
+	}, {
+		// store1 and store2 has same available space and less than 50gb
+		bigger: NewStoreInfoWithAvailable(1, 10*gb, 1000*gb, 1.4),
+		small:  NewStoreInfoWithAvailable(2, 10*gb, 100*gb, 1.4),
+	}, {
+		// store1 and store2 has same available ratio less than 0.2
+		bigger: NewStoreInfoWithAvailable(1, 10*gb, 1000*gb, 1.4),
+		small:  NewStoreInfoWithAvailable(2, 1*gb, 100*gb, 1.4),
+	}, {
+		// store1 and store2 has same available ratio
+		// but the store1 ratio less than store2 ((50-10)/50=0.8<(200-100)/200=0.5)
+		bigger: NewStoreInfoWithAvailable(1, 10*gb, 100*gb, 1.4),
+		small:  NewStoreInfoWithAvailable(2, 100*gb, 1000*gb, 1.4),
+	}, {
+		// store1 and store2 has same usedSize and capacity
+		// but the bigger's amp is bigger
+		bigger: NewStoreInfoWithAvailable(1, 10*gb, 100*gb, 1.5),
+		small:  NewStoreInfoWithAvailable(2, 10*gb, 100*gb, 1.4),
+	}, {
+		// store1 and store2 has same capacity and regionSize（40g)
+		// but store1 has less available space size
+		bigger: NewStoreInfoWithAvailable(1, 60*gb, 100*gb, 1),
+		small:  NewStoreInfoWithAvailable(2, 80*gb, 100*gb, 2),
+	}, {
+		// store1 and store2 has same capacity and store2 (40g) has twice usedSize than store1 (20g)
+		// but store1 has higher amp, so store1(60g) has more regionSize (40g)
+		bigger: NewStoreInfoWithAvailable(1, 80*gb, 100*gb, 3),
+		small:  NewStoreInfoWithAvailable(2, 60*gb, 100*gb, 1),
+	}}
+	for _, v := range testdata {
+		score1 := v.bigger.regionScoreV2(0, 0.8)
+		score2 := v.small.regionScoreV2(0, 0.8)
+		c.Assert(score1, Greater, score2)
+	}
+}
diff --git a/server/core/test_util.go b/server/core/test_util.go
index 531dd3751..fb39a74df 100644
--- a/server/core/test_util.go
+++ b/server/core/test_util.go
@@ -84,6 +84,24 @@ func NewTestRegionInfo(start, end []byte) *RegionInfo {
 	}}
 }
 
+// NewStoreInfoWithAvailable is create with available and capacity
+func NewStoreInfoWithAvailable(id, available, capacity uint64, amp float64) *StoreInfo {
+	stats := &pdpb.StoreStats{}
+	stats.Capacity = capacity
+	stats.Available = available
+	usedSize := capacity - available
+	regionSize := (float64(usedSize) * amp) / mb
+	store := NewStoreInfo(
+		&metapb.Store{
+			Id: id,
+		},
+		SetStoreStats(stats),
+		SetRegionCount(int(regionSize/96)),
+		SetRegionSize(int64(regionSize)),
+	)
+	return store
+}
+
 // NewStoreInfoWithLabel is create a store with specified labels.
 func NewStoreInfoWithLabel(id uint64, regionCount int, labels map[string]string) *StoreInfo {
 	storeLabels := make([]*metapb.StoreLabel, 0, len(labels))