-
Notifications
You must be signed in to change notification settings - Fork 759
scheduler: graceful shutdown implement #9720
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f752039
graceful shutdown pd-impl
hujiatao0 4f7a46e
Merge remote-tracking branch 'origin/master' into graceful-shutdown-impl
hujiatao0 9a48995
update change
hujiatao0 c73b3da
fmt change
hujiatao0 6a484f7
fix test
hujiatao0 bd3dc45
add remove test for stopping store scheduler
hujiatao0 e594cf1
add command interface for new scheduler
hujiatao0 d449bd0
Merge remote-tracking branch 'origin/master' into graceful-shutdown-impl
hujiatao0 4858fa7
add new field for stopping store scheduler
hujiatao0 baf11b1
Merge remote-tracking branch 'origin/master' into graceful-shutdown-impl
hujiatao0 30c9a5c
add recovery interface for stopping store scheduler
hujiatao0 823fe90
add schedule filter
hujiatao0 0207d77
Merge remote-tracking branch 'origin/master' into graceful-shutdown-impl
hujiatao0 ce1ac49
add filter index
hujiatao0 3f97f8f
config update
hujiatao0 97736fc
resolve comments
hujiatao0 588ebf1
Merge remote-tracking branch 'origin/master' into graceful-shutdown-impl
hujiatao0 5f818ac
fix test
hujiatao0 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 hidden or 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 hidden or 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 hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,306 @@ | ||||||||
| // 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" | ||||||||
| "strconv" | ||||||||
|
|
||||||||
| "github.com/gorilla/mux" | ||||||||
| "github.com/unrolled/render" | ||||||||
| "go.uber.org/zap" | ||||||||
|
|
||||||||
| "github.com/pingcap/log" | ||||||||
|
|
||||||||
| "github.com/tikv/pd/pkg/core" | ||||||||
| sche "github.com/tikv/pd/pkg/schedule/core" | ||||||||
| "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/keyutil" | ||||||||
| ) | ||||||||
|
|
||||||||
| type stoppingStoreType string | ||||||||
|
|
||||||||
| const ( | ||||||||
| gracefulShutdownStore stoppingStoreType = "graceful-shutdown" | ||||||||
| ) | ||||||||
|
|
||||||||
| type evictStoppingStoreSchedulerConfig struct { | ||||||||
| baseDefaultSchedulerConfig | ||||||||
|
|
||||||||
| cluster *core.BasicCluster | ||||||||
| // EvictedStores stores the stopping stores that are being evicted | ||||||||
| EvictedStores []uint64 `json:"evicted-stores"` | ||||||||
| // Batch is used to generate multiple operators by one scheduling | ||||||||
| Batch int `json:"batch"` | ||||||||
| } | ||||||||
|
|
||||||||
| func initEvictStoppingStoreSchedulerConfig() *evictStoppingStoreSchedulerConfig { | ||||||||
| return &evictStoppingStoreSchedulerConfig{ | ||||||||
| baseDefaultSchedulerConfig: newBaseDefaultSchedulerConfig(), | ||||||||
| EvictedStores: make([]uint64, 0), | ||||||||
| Batch: EvictLeaderBatchSize, | ||||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| func (conf *evictStoppingStoreSchedulerConfig) persistLocked(updateFn func()) error { | ||||||||
| var ( | ||||||||
| oldEvictedStores = conf.EvictedStores | ||||||||
| oldBatch = conf.Batch | ||||||||
| ) | ||||||||
| updateFn() | ||||||||
| if err := conf.save(); err != nil { | ||||||||
| conf.EvictedStores = oldEvictedStores | ||||||||
| conf.Batch = oldBatch | ||||||||
| return err | ||||||||
| } | ||||||||
| return nil | ||||||||
| } | ||||||||
|
|
||||||||
| func (conf *evictStoppingStoreSchedulerConfig) getStores() []uint64 { | ||||||||
| conf.RLock() | ||||||||
| defer conf.RUnlock() | ||||||||
| return conf.EvictedStores | ||||||||
| } | ||||||||
|
|
||||||||
| func (*evictStoppingStoreSchedulerConfig) getKeyRangesByID(uint64) []keyutil.KeyRange { | ||||||||
| return []keyutil.KeyRange{keyutil.NewKeyRange("", "")} | ||||||||
| } | ||||||||
|
|
||||||||
| func (conf *evictStoppingStoreSchedulerConfig) getBatch() int { | ||||||||
| conf.RLock() | ||||||||
| defer conf.RUnlock() | ||||||||
| return conf.Batch | ||||||||
| } | ||||||||
|
|
||||||||
| func (conf *evictStoppingStoreSchedulerConfig) evictStore() uint64 { | ||||||||
| conf.RLock() | ||||||||
| defer conf.RUnlock() | ||||||||
| if len(conf.EvictedStores) == 0 { | ||||||||
| return 0 | ||||||||
| } | ||||||||
| return conf.EvictedStores[0] | ||||||||
| } | ||||||||
|
|
||||||||
| func (conf *evictStoppingStoreSchedulerConfig) setStoreAndPersist(id uint64) error { | ||||||||
| conf.Lock() | ||||||||
| defer conf.Unlock() | ||||||||
| return conf.persistLocked(func() { | ||||||||
| conf.EvictedStores = []uint64{id} | ||||||||
| }) | ||||||||
| } | ||||||||
|
|
||||||||
| func (conf *evictStoppingStoreSchedulerConfig) clearEvictedAndPersist() (oldID uint64, err error) { | ||||||||
| oldID = conf.evictStore() | ||||||||
| conf.Lock() | ||||||||
| defer conf.Unlock() | ||||||||
| if oldID > 0 { | ||||||||
hujiatao0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
| err = conf.persistLocked(func() { | ||||||||
| conf.EvictedStores = []uint64{} | ||||||||
| }) | ||||||||
| } | ||||||||
| return | ||||||||
| } | ||||||||
|
|
||||||||
| type evictStoppingStoreHandler struct { | ||||||||
| rd *render.Render | ||||||||
| config *evictStoppingStoreSchedulerConfig | ||||||||
| } | ||||||||
|
|
||||||||
| func newEvictStoppingStoreHandler(config *evictStoppingStoreSchedulerConfig) http.Handler { | ||||||||
| h := &evictStoppingStoreHandler{ | ||||||||
| config: config, | ||||||||
| rd: render.New(render.Options{IndentJSON: true}), | ||||||||
| } | ||||||||
| router := mux.NewRouter() | ||||||||
| router.HandleFunc("/config", h.updateConfig).Methods(http.MethodPost) | ||||||||
| router.HandleFunc("/list", h.listConfig).Methods(http.MethodGet) | ||||||||
| return router | ||||||||
| } | ||||||||
|
|
||||||||
| func (handler *evictStoppingStoreHandler) updateConfig(w http.ResponseWriter, r *http.Request) { | ||||||||
| handler.rd.JSON(w, http.StatusOK, "Config updated.") | ||||||||
hujiatao0 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
| } | ||||||||
|
|
||||||||
| func (handler *evictStoppingStoreHandler) listConfig(w http.ResponseWriter, _ *http.Request) { | ||||||||
| handler.config.RLock() | ||||||||
| defer handler.config.RUnlock() | ||||||||
| conf := &evictStoppingStoreSchedulerConfig{ | ||||||||
| Batch: handler.config.Batch, | ||||||||
| EvictedStores: handler.config.EvictedStores, | ||||||||
| } | ||||||||
| handler.rd.JSON(w, http.StatusOK, conf) | ||||||||
| } | ||||||||
|
|
||||||||
| type evictStoppingStoreScheduler struct { | ||||||||
| *BaseScheduler | ||||||||
| conf *evictStoppingStoreSchedulerConfig | ||||||||
| handler http.Handler | ||||||||
| } | ||||||||
|
|
||||||||
| // ServeHTTP implements the http.Handler interface. | ||||||||
| func (s *evictStoppingStoreScheduler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||||||||
| s.handler.ServeHTTP(w, r) | ||||||||
| } | ||||||||
|
|
||||||||
| // EncodeConfig implements the Scheduler interface. | ||||||||
| func (s *evictStoppingStoreScheduler) EncodeConfig() ([]byte, error) { | ||||||||
| return EncodeConfig(s.conf) | ||||||||
| } | ||||||||
|
|
||||||||
| // ReloadConfig implements the Scheduler interface. | ||||||||
| func (s *evictStoppingStoreScheduler) ReloadConfig() error { | ||||||||
| s.conf.Lock() | ||||||||
| defer s.conf.Unlock() | ||||||||
|
|
||||||||
| newCfg := &evictStoppingStoreSchedulerConfig{} | ||||||||
| if err := s.conf.load(newCfg); err != nil { | ||||||||
| return err | ||||||||
| } | ||||||||
| if newCfg.Batch == 0 { | ||||||||
| newCfg.Batch = EvictLeaderBatchSize | ||||||||
| } | ||||||||
|
|
||||||||
| s.conf.EvictedStores = newCfg.EvictedStores | ||||||||
| s.conf.Batch = newCfg.Batch | ||||||||
| return nil | ||||||||
| } | ||||||||
|
|
||||||||
| // PrepareConfig implements the Scheduler interface. | ||||||||
| func (s *evictStoppingStoreScheduler) PrepareConfig(cluster sche.SchedulerCluster) error { | ||||||||
| evictStore := s.conf.evictStore() | ||||||||
| if evictStore != 0 { | ||||||||
| if err := cluster.SlowStoreEvicted(evictStore); err != nil { | ||||||||
| return err | ||||||||
| } | ||||||||
| } | ||||||||
| return nil | ||||||||
| } | ||||||||
|
|
||||||||
| // CleanConfig implements the Scheduler interface. | ||||||||
| func (s *evictStoppingStoreScheduler) CleanConfig(cluster sche.SchedulerCluster) { | ||||||||
| s.cleanupEvictLeader(cluster) | ||||||||
| } | ||||||||
|
|
||||||||
| func (s *evictStoppingStoreScheduler) prepareEvictLeader(cluster sche.SchedulerCluster, storeID uint64) error { | ||||||||
| if err := cluster.SlowStoreEvicted(storeID); err != nil { | ||||||||
hujiatao0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||
| log.Info("failed to evict stopping store", zap.Uint64("store-id", storeID), zap.Error(err)) | ||||||||
| return err | ||||||||
| } | ||||||||
|
|
||||||||
| if err := s.conf.setStoreAndPersist(storeID); err != nil { | ||||||||
| log.Info("failed to persist evicted stopping store", zap.Uint64("store-id", storeID), zap.Error(err)) | ||||||||
| cluster.SlowStoreRecovered(storeID) | ||||||||
| return err | ||||||||
| } | ||||||||
|
|
||||||||
| return nil | ||||||||
| } | ||||||||
|
|
||||||||
| func (s *evictStoppingStoreScheduler) cleanupEvictLeader(cluster sche.SchedulerCluster) { | ||||||||
| evictStoppingStore, err := s.conf.clearEvictedAndPersist() | ||||||||
| if err != nil { | ||||||||
| log.Info("evict-stopping-store-scheduler persist config failed", zap.Uint64("store-id", evictStoppingStore)) | ||||||||
|
||||||||
| log.Info("evict-stopping-store-scheduler persist config failed", zap.Uint64("store-id", evictStoppingStore)) | |
| log.Warn("evict-stopping-store-scheduler persist config failed", zap.Uint64("store-id", evictStoppingStore)) | |
| return |
hujiatao0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
hujiatao0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
hujiatao0 marked this conversation as resolved.
Show resolved
Hide resolved
hujiatao0 marked this conversation as resolved.
Show resolved
Hide resolved
hujiatao0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
hujiatao0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
hujiatao0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
hujiatao0 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
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.
Uh oh!
There was an error while loading. Please reload this page.