-
Notifications
You must be signed in to change notification settings - Fork 735
scheduler: decouple the keyranges with different schedulers #9330
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
base: master
Are you sure you want to change the base?
Changes from all commits
e1ec26d
a39754d
fdd5a97
1270f87
fe030a0
1a2d2ab
8d91e0d
dbff105
5bb9eb5
01f2334
7b54827
338ca04
0e2a025
02af066
7ce9e71
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2025 TiKV Project Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package keyrange | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/tikv/pd/pkg/utils/keyutil" | ||
) | ||
|
||
// Manager is a manager for key ranges. | ||
type Manager struct { | ||
sync.Mutex | ||
sortedKeyRanges *keyutil.KeyRanges | ||
} | ||
|
||
// NewManager creates a new Manager. | ||
func NewManager() *Manager { | ||
return &Manager{ | ||
sortedKeyRanges: &keyutil.KeyRanges{}, | ||
} | ||
} | ||
|
||
// GetNonOverlappingKeyRanges returns the non-overlapping key ranges of the given base key range. | ||
func (s *Manager) GetNonOverlappingKeyRanges(base *keyutil.KeyRange) []keyutil.KeyRange { | ||
s.Lock() | ||
defer s.Unlock() | ||
return s.sortedKeyRanges.SubtractKeyRanges(base) | ||
} | ||
|
||
// Append appends the key ranges to the manager. | ||
func (s *Manager) Append(rs []keyutil.KeyRange) { | ||
s.Lock() | ||
defer s.Unlock() | ||
for _, r := range rs { | ||
s.sortedKeyRanges.Append(r.StartKey, r.EndKey) | ||
} | ||
s.sortedKeyRanges.SortAndDeduce() | ||
} | ||
|
||
// Delete deletes the overlapping key ranges from the manager. | ||
func (s *Manager) Delete(rs []keyutil.KeyRange) { | ||
s.Lock() | ||
defer s.Unlock() | ||
for _, r := range rs { | ||
s.sortedKeyRanges.Delete(&r) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -429,7 +429,12 @@ func makeInfluence(op *operator.Operator, plan *solver, usedRegions map[uint64]s | |
// It randomly selects a health region from the source store, then picks | ||
// the best follower peer and transfers the leader. | ||
func (s *balanceLeaderScheduler) transferLeaderOut(solver *solver, collector *plan.Collector) *operator.Operator { | ||
solver.Region = filter.SelectOneRegion(solver.RandLeaderRegions(solver.sourceStoreID(), s.conf.getRanges()), | ||
rs := s.conf.getRanges() | ||
if IsDefaultKeyRange(rs) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about the hot region scheduler? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hot region scheduler can schedule all the regions, not restricting this limit. |
||
km := solver.GetKeyRangeManager() | ||
rs = km.GetNonOverlappingKeyRanges(&rs[0]) | ||
} | ||
solver.Region = filter.SelectOneRegion(solver.RandLeaderRegions(solver.sourceStoreID(), rs), | ||
collector, filter.NewRegionPendingFilter(), filter.NewRegionDownFilter()) | ||
if solver.Region == nil { | ||
log.Debug("store has no leader", zap.String("scheduler", s.GetName()), zap.Uint64("store-id", solver.sourceStoreID())) | ||
|
@@ -473,7 +478,12 @@ func (s *balanceLeaderScheduler) transferLeaderOut(solver *solver, collector *pl | |
// It randomly selects a health region from the target store, then picks | ||
// the worst follower peer and transfers the leader. | ||
func (s *balanceLeaderScheduler) transferLeaderIn(solver *solver, collector *plan.Collector) *operator.Operator { | ||
solver.Region = filter.SelectOneRegion(solver.RandFollowerRegions(solver.targetStoreID(), s.conf.getRanges()), | ||
rs := s.conf.getRanges() | ||
if IsDefaultKeyRange(rs) { | ||
km := solver.GetKeyRangeManager() | ||
rs = km.GetNonOverlappingKeyRanges(&rs[0]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will it slow down the balance leader's speed? |
||
} | ||
solver.Region = filter.SelectOneRegion(solver.RandFollowerRegions(solver.targetStoreID(), rs), | ||
nil, filter.NewRegionPendingFilter(), filter.NewRegionDownFilter()) | ||
if solver.Region == nil { | ||
log.Debug("store has no follower", zap.String("scheduler", s.GetName()), zap.Uint64("store-id", solver.targetStoreID())) | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -137,6 +137,11 @@ func (s *balanceRegionScheduler) Schedule(cluster sche.SchedulerCluster, dryRun | |||||||||||||||||||||||||||||||||||||||||
solver.Step++ | ||||||||||||||||||||||||||||||||||||||||||
var sourceIndex int | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
rs := s.conf.Ranges | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the Ranges always have a value? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, it can be seen as pd/pkg/schedule/schedulers/utils.go Lines 218 to 237 in 6f886e0
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if we change the range manually? |
||||||||||||||||||||||||||||||||||||||||||
if IsDefaultKeyRange(rs) { | ||||||||||||||||||||||||||||||||||||||||||
km := solver.GetKeyRangeManager() | ||||||||||||||||||||||||||||||||||||||||||
rs = km.GetNonOverlappingKeyRanges(&rs[0]) | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
// sourcesStore is sorted by region score desc, so we pick the first store as source store. | ||||||||||||||||||||||||||||||||||||||||||
for sourceIndex, solver.Source = range sourceStores { | ||||||||||||||||||||||||||||||||||||||||||
retryLimit := s.getLimit(solver.Source) | ||||||||||||||||||||||||||||||||||||||||||
|
@@ -147,21 +152,21 @@ func (s *balanceRegionScheduler) Schedule(cluster sche.SchedulerCluster, dryRun | |||||||||||||||||||||||||||||||||||||||||
for range retryLimit { | ||||||||||||||||||||||||||||||||||||||||||
// Priority pick the region that has a pending peer. | ||||||||||||||||||||||||||||||||||||||||||
// Pending region may mean the disk is overload, remove the pending region firstly. | ||||||||||||||||||||||||||||||||||||||||||
solver.Region = filter.SelectOneRegion(cluster.RandPendingRegions(solver.sourceStoreID(), s.conf.Ranges), collector, | ||||||||||||||||||||||||||||||||||||||||||
solver.Region = filter.SelectOneRegion(cluster.RandPendingRegions(solver.sourceStoreID(), rs), collector, | ||||||||||||||||||||||||||||||||||||||||||
append(baseRegionFilters, filter.NewRegionWitnessFilter(solver.sourceStoreID()))...) | ||||||||||||||||||||||||||||||||||||||||||
if solver.Region == nil { | ||||||||||||||||||||||||||||||||||||||||||
// Then pick the region that has a follower in the source store. | ||||||||||||||||||||||||||||||||||||||||||
solver.Region = filter.SelectOneRegion(cluster.RandFollowerRegions(solver.sourceStoreID(), s.conf.Ranges), collector, | ||||||||||||||||||||||||||||||||||||||||||
solver.Region = filter.SelectOneRegion(cluster.RandFollowerRegions(solver.sourceStoreID(), rs), collector, | ||||||||||||||||||||||||||||||||||||||||||
append(baseRegionFilters, filter.NewRegionWitnessFilter(solver.sourceStoreID()), pendingFilter)...) | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
if solver.Region == nil { | ||||||||||||||||||||||||||||||||||||||||||
// Then pick the region has the leader in the source store. | ||||||||||||||||||||||||||||||||||||||||||
solver.Region = filter.SelectOneRegion(cluster.RandLeaderRegions(solver.sourceStoreID(), s.conf.Ranges), collector, | ||||||||||||||||||||||||||||||||||||||||||
solver.Region = filter.SelectOneRegion(cluster.RandLeaderRegions(solver.sourceStoreID(), rs), collector, | ||||||||||||||||||||||||||||||||||||||||||
append(baseRegionFilters, filter.NewRegionWitnessFilter(solver.sourceStoreID()), pendingFilter)...) | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
if solver.Region == nil { | ||||||||||||||||||||||||||||||||||||||||||
// Finally, pick learner. | ||||||||||||||||||||||||||||||||||||||||||
solver.Region = filter.SelectOneRegion(cluster.RandLearnerRegions(solver.sourceStoreID(), s.conf.Ranges), collector, | ||||||||||||||||||||||||||||||||||||||||||
solver.Region = filter.SelectOneRegion(cluster.RandLearnerRegions(solver.sourceStoreID(), rs), collector, | ||||||||||||||||||||||||||||||||||||||||||
append(baseRegionFilters, filter.NewRegionWitnessFilter(solver.sourceStoreID()), pendingFilter)...) | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
if solver.Region == nil { | ||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For every time we append, we need to sort?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done, using adjust function to check the two regions