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

store/tikv: Make RangeTaskRunner support dividing task by multiple regions #10482

Merged
merged 45 commits into from
Jun 20, 2019

Conversation

MyonKeminta
Copy link
Contributor

What problem does this PR solve?

This PR continue improves #10379 and should be merged after that PR.
Also requires pingcap/kvproto#396 and tikv/pd#1535 .

The implementation of RangeTaskRunner in #10379 divides the range by regions in one thread, and dispatch these tasks to the worker threads. But if the concurrency is set too high or the tasks costs too short time to finish, then the single thread which is responsible to fetch region meta and divide the range will be the bottleneck of RangeTaskRunner. This PR makes it support divides the ranges by multiple regions to reduce the main thread or RangeTaskRunner.

What is changed and how it works?

  • A new interface ScanRegions of PD is added in pdpb: add ScanRegions kvproto#396 .
  • Add method BatchLoadRegions to RegionCache. It invokes ScanRegions of PDClient, fill the scanned regions to RegionCache, and return the end key of the last scanned region.
  • In RangeTaskRunner, the main threads invokes BatchLoadRegions to divide the region into multiple tasks, and the number of regions of each divided subtask is configurable.

Check List

Tests

  • Unit test
  • Integration test

Code changes

  • Has exported function/method change

Side effects

  • Possible performance regression
  • Increased code complexity

Related changes

  • None

MyonKeminta and others added 24 commits May 7, 2019 11:57
Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
@codecov
Copy link

codecov bot commented May 15, 2019

Codecov Report

Merging #10482 into master will decrease coverage by 0.0204%.
The diff coverage is 69.0721%.

@@               Coverage Diff                @@
##             master     #10482        +/-   ##
================================================
- Coverage   81.0157%   80.9952%   -0.0205%     
================================================
  Files           419        419                
  Lines         88747      88825        +78     
================================================
+ Hits          71899      71944        +45     
- Misses        11629      11652        +23     
- Partials       5219       5229        +10

@MyonKeminta
Copy link
Contributor Author

/run-all-tests pd=pr/1535

}
regions = append(regions, region)
}
if len(regions) == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Duplicate condition checking with L531.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not the same, since not every item in metas will be added to regions

if len(regions) == 0 {
return nil, errors.New("receive Regions with no peer")
}
if len(regions) < len(metas) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems len(regions) always equal to len(metas)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Regions with no leader will be ignored.

Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
@disksing
Copy link
Contributor

the rest LGTM

MyonKeminta and others added 4 commits June 18, 2019 13:09
Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
@MyonKeminta
Copy link
Contributor Author

/run-all-tests

Signed-off-by: MyonKeminta <MyonKeminta@users.noreply.github.com>
@disksing
Copy link
Contributor

LGTM

@disksing disksing added the status/LGT1 Indicates that a PR has LGTM 1. label Jun 19, 2019
@MyonKeminta
Copy link
Contributor Author

@disksing Do you think this should be cherry-picked to release-3.0?

@disksing
Copy link
Contributor

I think yes. PTAL @zhangjinpeng1987

@MyonKeminta
Copy link
Contributor Author

@lysu Could you please help me check if I've used the new refactored region cache the correct way?

defer c.RUnlock()

regions := make([]*Region, 0, len(c.regions))
for _, region := range c.regions {
Copy link
Contributor

@lonng lonng Jun 19, 2019

Choose a reason for hiding this comment

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

You can use copy(regions, c.regions) directly.

Copy link
Contributor Author

@MyonKeminta MyonKeminta Jun 20, 2019

Choose a reason for hiding this comment

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

c.regions is a map and I only needs its values. It seems that copy doesn't work well here.

c.RLock()
defer c.RUnlock()

regions := make([]*Region, 0, len(c.regions))
Copy link
Contributor

Choose a reason for hiding this comment

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

We only need limit regions but copy the entire slice here. It's OK because the mocktikv will not contain a lot of regions.

Copy link
Contributor Author

@MyonKeminta MyonKeminta Jun 20, 2019

Choose a reason for hiding this comment

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

The regions is in arbitrary order and ScanRegions needs to scan regions by key. I get all regions here and do a sorting below. I think this will be faster than finding regions by key for at most limit times, because the mock cluster doesn't support quickly finding region by key. You can see that in GetRegionByKey method, it does a linear searching on c.regions.

for _, region := range regions {
leader := region.leaderPeer()
if leader == nil {
leader = &metapb.Peer{}
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems violate the semantics of this function if we return invalid leader.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the same as PD's ScanRegions API. It uses a repeated field for leaders so that it can't be nil. Therefore we judge if a region has no leader by checking if the leader's ID is 0.

@MyonKeminta
Copy link
Contributor Author

/run-all-tests
Please note that there are a lot of changes in go.mod and go.sum. Maybe they are caused by upgrading PD, but I'm not sure. @disksing PTAL.

@MyonKeminta
Copy link
Contributor Author

/run-unit-test

Copy link
Contributor

@zhangjinpeng87 zhangjinpeng87 left a comment

Choose a reason for hiding this comment

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

LGTM

@MyonKeminta MyonKeminta merged commit c12abec into pingcap:master Jun 20, 2019
@MyonKeminta MyonKeminta deleted the misono/range-task-multi-region branch June 20, 2019 08:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/tikv status/LGT1 Indicates that a PR has LGTM 1.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants