-
Notifications
You must be signed in to change notification settings - Fork 728
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
api: add a new scheduler to balance the regions of the given key range #8988
Open
bufferflies
wants to merge
9
commits into
tikv:master
Choose a base branch
from
bufferflies:balance_key_range
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+378
−2
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e693b3e
add scheduler config
bufferflies 23ff7d0
add new scheduler for key range
bufferflies 1e6d628
pass ut
bufferflies d1da5b5
lint
bufferflies d0cfc2d
pass ut
bufferflies d86148f
rename balance-key-range to balance-range
bufferflies 8bdb7bc
use hex encode
bufferflies 0696ba6
rename
bufferflies 5d5ee0f
add table configuration
bufferflies File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
// 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 schedulers | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/unrolled/render" | ||
|
||
"github.com/pingcap/log" | ||
|
||
"github.com/tikv/pd/pkg/core" | ||
"github.com/tikv/pd/pkg/core/constant" | ||
"github.com/tikv/pd/pkg/errs" | ||
sche "github.com/tikv/pd/pkg/schedule/core" | ||
"github.com/tikv/pd/pkg/schedule/filter" | ||
"github.com/tikv/pd/pkg/schedule/operator" | ||
"github.com/tikv/pd/pkg/schedule/plan" | ||
"github.com/tikv/pd/pkg/schedule/types" | ||
"github.com/tikv/pd/pkg/utils/syncutil" | ||
) | ||
|
||
type balanceRangeSchedulerHandler struct { | ||
rd *render.Render | ||
config *balanceRangeSchedulerConfig | ||
} | ||
|
||
func newBalanceRangeHandler(conf *balanceRangeSchedulerConfig) http.Handler { | ||
handler := &balanceRangeSchedulerHandler{ | ||
config: conf, | ||
rd: render.New(render.Options{IndentJSON: true}), | ||
} | ||
router := mux.NewRouter() | ||
router.HandleFunc("/config", handler.updateConfig).Methods(http.MethodPost) | ||
router.HandleFunc("/list", handler.listConfig).Methods(http.MethodGet) | ||
return router | ||
} | ||
|
||
func (handler *balanceRangeSchedulerHandler) updateConfig(w http.ResponseWriter, _ *http.Request) { | ||
handler.rd.JSON(w, http.StatusBadRequest, "update config is not supported") | ||
} | ||
|
||
func (handler *balanceRangeSchedulerHandler) listConfig(w http.ResponseWriter, _ *http.Request) { | ||
conf := handler.config.clone() | ||
if err := handler.rd.JSON(w, http.StatusOK, conf); err != nil { | ||
log.Error("failed to marshal balance key range scheduler config", errs.ZapError(err)) | ||
} | ||
} | ||
|
||
type balanceRangeSchedulerConfig struct { | ||
syncutil.RWMutex | ||
schedulerConfig | ||
balanceRangeSchedulerParam | ||
} | ||
|
||
type balanceRangeSchedulerParam struct { | ||
Role string `json:"role"` | ||
Engine string `json:"engine"` | ||
Timeout time.Duration `json:"timeout"` | ||
Ranges []core.KeyRange `json:"ranges"` | ||
TableName string `json:"table-name"` | ||
} | ||
|
||
func (conf *balanceRangeSchedulerConfig) clone() *balanceRangeSchedulerParam { | ||
conf.RLock() | ||
defer conf.RUnlock() | ||
ranges := make([]core.KeyRange, len(conf.Ranges)) | ||
copy(ranges, conf.Ranges) | ||
return &balanceRangeSchedulerParam{ | ||
Ranges: ranges, | ||
Role: conf.Role, | ||
Engine: conf.Engine, | ||
Timeout: conf.Timeout, | ||
TableName: conf.TableName, | ||
} | ||
} | ||
|
||
// EncodeConfig serializes the config. | ||
func (s *balanceRangeScheduler) EncodeConfig() ([]byte, error) { | ||
s.conf.RLock() | ||
defer s.conf.RUnlock() | ||
return EncodeConfig(s.conf) | ||
} | ||
|
||
// ReloadConfig reloads the config. | ||
func (s *balanceRangeScheduler) ReloadConfig() error { | ||
s.conf.Lock() | ||
defer s.conf.Unlock() | ||
|
||
newCfg := &balanceRangeSchedulerConfig{} | ||
if err := s.conf.load(newCfg); err != nil { | ||
return err | ||
} | ||
s.conf.Ranges = newCfg.Ranges | ||
s.conf.Timeout = newCfg.Timeout | ||
s.conf.Role = newCfg.Role | ||
s.conf.Engine = newCfg.Engine | ||
return nil | ||
} | ||
|
||
type balanceRangeScheduler struct { | ||
*BaseScheduler | ||
conf *balanceRangeSchedulerConfig | ||
handler http.Handler | ||
filters []filter.Filter | ||
filterCounter *filter.Counter | ||
} | ||
|
||
// ServeHTTP implements the http.Handler interface. | ||
func (s *balanceRangeScheduler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
s.handler.ServeHTTP(w, r) | ||
} | ||
|
||
// Schedule schedules the balance key range operator. | ||
func (*balanceRangeScheduler) Schedule(_cluster sche.SchedulerCluster, _dryRun bool) ([]*operator.Operator, []plan.Plan) { | ||
log.Debug("balance key range scheduler is scheduling, need to implement") | ||
return nil, nil | ||
} | ||
|
||
// IsScheduleAllowed checks if the scheduler is allowed to schedule new operators. | ||
func (s *balanceRangeScheduler) IsScheduleAllowed(cluster sche.SchedulerCluster) bool { | ||
allowed := s.OpController.OperatorCount(operator.OpRange) < cluster.GetSchedulerConfig().GetRegionScheduleLimit() | ||
if !allowed { | ||
operator.IncOperatorLimitCounter(s.GetType(), operator.OpRange) | ||
} | ||
return allowed | ||
} | ||
|
||
// BalanceRangeCreateOption is used to create a scheduler with an option. | ||
type BalanceRangeCreateOption func(s *balanceRangeScheduler) | ||
|
||
// newBalanceRangeScheduler creates a scheduler that tends to keep given peer role on | ||
// special store balanced. | ||
func newBalanceRangeScheduler(opController *operator.Controller, conf *balanceRangeSchedulerConfig, options ...BalanceRangeCreateOption) Scheduler { | ||
s := &balanceRangeScheduler{ | ||
BaseScheduler: NewBaseScheduler(opController, types.BalanceRangeScheduler, conf), | ||
conf: conf, | ||
handler: newBalanceRangeHandler(conf), | ||
} | ||
for _, option := range options { | ||
option(s) | ||
} | ||
s.filters = []filter.Filter{ | ||
&filter.StoreStateFilter{ActionScope: s.GetName(), TransferLeader: true, OperatorLevel: constant.Medium}, | ||
filter.NewSpecialUseFilter(s.GetName()), | ||
} | ||
s.filterCounter = filter.NewCounter(s.GetName()) | ||
return s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Name it as
alias
for better, since pd does not understand the table concept.