Skip to content

executor: add retriver for tikv_region_peers #30657

Merged
ti-chi-bot merged 30 commits intopingcap:masterfrom
IcePigZDB:optikvregionpeers-executor
Feb 7, 2022
Merged

executor: add retriver for tikv_region_peers #30657
ti-chi-bot merged 30 commits intopingcap:masterfrom
IcePigZDB:optikvregionpeers-executor

Conversation

@IcePigZDB
Copy link
Contributor

@IcePigZDB IcePigZDB commented Dec 13, 2021

ref: #28330

What problem does this PR solve?

Issue Number: close #31050
Problem Summary:
The optimization idea is to add TikvRegionPeersExtractor to push down STORE_ID and REGION_ID and add tikvRegionPeersRetriever to fetch regions information from PD.

What is changed and how it works?

Time cost comparison:

# select count(*) from information_schema.tikv_region_peers;
+----------+
| count(*) |
+----------+
|   700476 |
+----------+
new: 1 row in set (10.30 sec)
old: 1 row in set (10.29 sec)
# almost cost the same time when fetch all regionsInfo

# select * from tikv_region_peers where region_id=395005;
+-----------+---------+----------+------------+-----------+--------+--------------+
| REGION_ID | PEER_ID | STORE_ID | IS_LEARNER | IS_LEADER | STATUS | DOWN_SECONDS |
+-----------+---------+----------+------------+-----------+--------+--------------+
|    395005 |  410054 |        4 |          0 |         1 | NORMAL |         NULL |
+-----------+---------+----------+------------+-----------+--------+--------------+
new: 1 row in set (0.00 sec)
old: 1 row in set (11.37 sec)
# faster when specify one region_id

# select * from tikv_region_peers where region_id in (395005,395015,395017);
+-----------+---------+----------+------------+-----------+--------+--------------+
| REGION_ID | PEER_ID | STORE_ID | IS_LEARNER | IS_LEADER | STATUS | DOWN_SECONDS |
+-----------+---------+----------+------------+-----------+--------+--------------+
|    395005 |  410054 |        4 |          0 |         1 | NORMAL |         NULL |
|    395015 |  410061 |        4 |          0 |         1 | NORMAL |         NULL |
|    395017 |  759007 |        1 |          0 |         1 | NORMAL |         NULL |
+-----------+---------+----------+------------+-----------+--------+--------------+
new: 3 rows in set (0.00 sec)
old: 3 rows in set (10.10 sec)
# faster when specify three region_id

# select count(*) from tikv_region_peers where store_id=4;
+----------+
| count(*) |
+----------+
|   182409 |
+----------+
new: 1 row in set (5.45 sec)
old: 1 row in set (10.38 sec)
# faster when specify one store_id

# select count(*) from tikv_region_peers where region_id=395005 and store_id=4;
+----------+
| count(*) |
+----------+
|        1 |
+----------+
new: 1 row in set (4.80 sec)
old: 1 row in set (11.03 sec)
# faster when specify one store_id

# select * from tikv_region_peers where region_id in (395005,395015,395017) and store_id=4;
+-----------+---------+----------+------------+-----------+--------+--------------+
| REGION_ID | PEER_ID | STORE_ID | IS_LEARNER | IS_LEADER | STATUS | DOWN_SECONDS |
+-----------+---------+----------+------------+-----------+--------+--------------+
|    395005 |  410054 |        4 |          0 |         1 | NORMAL |         NULL |
|    395015 |  410061 |        4 |          0 |         1 | NORMAL |         NULL |
+-----------+---------+----------+------------+-----------+--------+--------------+
new: 2 rows in set (4.88 sec)
old: 2 rows in set (10.69 sec)
# faster when specify one store_id

# select * from tikv_region_peers where region_id in (395005,395015,395017) and store_id=0;
Empty set (0.01 sec)
Empty set (10.83 sec)
# faster when specify one store_id

# select count(*) from tikv_region_peers where store_id in (4,7);
+----------+
| count(*) |
+----------+
|   364012 |
+----------+
1 row in set (9.32 sec)
1 row in set (10.30 sec)
# closer when specify two store_id

# select * from tikv_region_peers where region_id in (395005,395015,395017) and store_id in (1,4);
+-----------+---------+----------+------------+-----------+--------+--------------+
| REGION_ID | PEER_ID | STORE_ID | IS_LEARNER | IS_LEADER | STATUS | DOWN_SECONDS |
+-----------+---------+----------+------------+-----------+--------+--------------+
|    395005 |  410054 |        4 |          0 |         1 | NORMAL |         NULL |
|    395015 |  410061 |        4 |          0 |         1 | NORMAL |         NULL |
|    395017 |  759007 |        1 |          0 |         1 | NORMAL |         NULL |
+-----------+---------+----------+------------+-----------+--------+--------------+
new: 3 rows in set (12.90 sec)
old: 3 rows in set (12.03 sec)
# slower when specify two or more store_id

Check List

Tests

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

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

None

ref: pingcap#28330

Signed-off-by: IcePigZDB <icepigzdb@gmail.com>
Signed-off-by: IcePigZDB <icepigzdb@gmail.com>
Signed-off-by: IcePigZDB <icepigzdb@gmail.com>
Signed-off-by: IcePigZDB <icepigzdb@gmail.com>
@ti-chi-bot
Copy link
Member

ti-chi-bot commented Dec 13, 2021

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • nolouch
  • rleungx

To complete the pull request process, please ask the reviewers in the list 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 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.

Details

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added release-note-none Denotes a PR that doesn't merit a release note. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Dec 13, 2021
@sre-bot
Copy link
Contributor

sre-bot commented Dec 13, 2021

Please follow PR Title Format:

  • pkg [, pkg2, pkg3]: what is changed

Or if the count of mainly changed packages are more than 3, use

  • *: what is changed

After you have format title, you can leave a comment /run-check_title to recheck it

@IcePigZDB IcePigZDB changed the title Optikvregionpeers executor executor: add retriver for tikv_region_peers Dec 13, 2021
@IcePigZDB IcePigZDB marked this pull request as ready for review December 13, 2021 03:09
@ti-chi-bot ti-chi-bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Dec 13, 2021
@zhouqiang-cl
Copy link
Contributor

/rebuild

@JmPotato
Copy link
Member

The rest LGTM basically.

@IcePigZDB
Copy link
Contributor Author

/run-all-tests

@sre-bot
Copy link
Contributor

sre-bot commented Dec 22, 2021

Comment on lines +1045 to +1047
if len(e.extractor.RegionIDs) < 1 {
return e.packTiKVRegionPeersRows(regionsInfoByStoreID)
}
Copy link
Member

Choose a reason for hiding this comment

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

(1/3) I think this judgment is unnecessary since it will not enter the len(e.extractor.RegionIDs) > 0 logic below either.

Copy link
Contributor Author

@IcePigZDB IcePigZDB Dec 22, 2021

Choose a reason for hiding this comment

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

Refactor and move them before intersect.
The predicates extract by extractor is and case only, and len(regionsInfoByStoreID) == 0 may include two cases:

  1. no store_id predicate: Do not intersect
  2. no corresponding store regionsInfo: Intersect

so we should check the length of extracted predicate rather than result slice regionsInfoByStoreID or regionsInfoByRegionID.

	// len(regionsInfoByStoreID) == 0 include two cases:
	// 1. no store_id predicate 2. no corresponding store regionsInfo
	if len(e.extractor.RegionIDs) == 0 {
		return e.packTiKVRegionPeersRows(regionsInfoByStoreID)
	}

	if len(e.extractor.StoreIDs) == 0 {
		return e.packTiKVRegionPeersRows(regionsInfoByRegionID)
	}

	// intersect
	for _, region := range regionsInfoByRegionID {
		if _, ok := regionMap[region.ID]; ok {
			regionsInfo = append(regionsInfo, region)
		}
	}

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I missed that part. Nice catch 👍. But what about the or condition 🤔?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, I missed that part. Nice catch 👍. But what about the or condition 🤔?

		if len(e.extractor.RegionIDs) < 1 {
			return e.packTiKVRegionPeersRows(regionsInfoByStoreID)
		}

In origin version implemention,the use of regionsInfoByStoreID and regionsInfoByRegionID make or difficult to use.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about the newest verion of implmention. I think we need to exclude unwanted regions got from store_id by region_id when specify both store_id and region_id, so I keep the regionsInfoByStoreID.

Comment on lines +1058 to +1060
if len(e.extractor.StoreIDs) < 1 {
return e.packTiKVRegionPeersRows(regionsInfoByRegionID)
}
Copy link
Member

Choose a reason for hiding this comment

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

(2/3) Ditto.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ditto.

Comment on lines +1063 to +1069
// intersect
for _, region := range regionsInfoByRegionID {
if _, ok := regionMap[region.ID]; ok {
regionsInfo = append(regionsInfo, region)
}
}
return e.packTiKVRegionPeersRows(regionsInfo)
Copy link
Member

Choose a reason for hiding this comment

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

(3/3) We can intersect both regionsInfoByStoreID and regionsInfoByRegionID here, which would make the code more easy and clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ditto.

Co-authored-by: JmPotato <ghzpotato@gmail.com>
HotHistory = "/pd/api/v1/hotspot/regions/history"
Regions = "/pd/api/v1/regions"
RegionByID = "/pd/api/v1/region/id/"
StoreRegions = "/pd/api/v1/regions/store/"
Copy link
Member

Choose a reason for hiding this comment

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

Prefer to keep the same format, e.g. /pd/api/v1/regions/store

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, and hence add + "/" in implemention code

pdapi.StoreRegions+"/"+strconv.FormatUint(storeID, 10), nil, &regionsInfo)
pdapi.RegionByID+"/"+strconv.FormatUint(regionID, 10), nil, &regionInfo)

if e.extractor.SkipRequest || e.retrieved {
return nil, nil
}
e.retrieved = true
Copy link
Member

Choose a reason for hiding this comment

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

We should change it to true when it is actually retrieved

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The position of clusterServerInfoRetriever is at the begining too. More specifically, tikvRegionsPeersRetriver's implementation has many return cases, it would be redundant if put e.retrieved = true before returns.


var regionsInfo, regionsInfoByStoreID, regionsInfoByRegionID []helper.RegionInfo
regionMap := make(map[int64]struct{})
if len(e.extractor.StoreIDs) > 0 {
Copy link
Member

Choose a reason for hiding this comment

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

not necessary

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactor and remove if len(e.extractor.StoreIDs) > 0 {.

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

nolouch commented Jan 25, 2022

ptal @JmPotato @rleungx

@ti-chi-bot ti-chi-bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Jan 25, 2022
@JmPotato
Copy link
Member

/merge

@ti-chi-bot
Copy link
Member

@JmPotato: /merge is only allowed for the committers, you can assign this pull request to the committer in list by filling /assign @committer in the comment to help merge this pull request.

Details

In response to this:

/merge

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 ti-community-infra/tichi repository.

@crazycs520
Copy link
Contributor

/merge

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

DetailsCommit hash: d81c254

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Feb 7, 2022
@crazycs520
Copy link
Contributor

/merge

@nolouch nolouch removed the needs-cherry-pick-release-5.4 Should cherry pick this PR to release-5.4 branch. label Feb 7, 2022
@crazycs520
Copy link
Contributor

/merge

1 similar comment
@crazycs520
Copy link
Contributor

/merge

@ti-chi-bot ti-chi-bot added the needs-cherry-pick-release-5.4 Should cherry pick this PR to release-5.4 branch. label Feb 7, 2022
@ti-chi-bot ti-chi-bot merged commit 43b818b into pingcap:master Feb 7, 2022
ti-srebot pushed a commit to ti-srebot/tidb that referenced this pull request Feb 7, 2022
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>
@ti-srebot
Copy link
Contributor

cherry pick to release-5.4 in PR #32132

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-cherry-pick-release-5.4 Should cherry pick this PR to release-5.4 branch. 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. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

REGION_ID=xxx does not work on table INFORMATION_SCHEMA.TIKV_REGION_PEERS

9 participants