Skip to content

Commit

Permalink
This is an automated cherry-pick of tikv#7674
Browse files Browse the repository at this point in the history
close tikv#7672

Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
CabinfeverB authored and ti-chi-bot committed Jan 9, 2024
1 parent 79aac73 commit cae3b44
Show file tree
Hide file tree
Showing 8 changed files with 834 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/schedule/schedulers/evict_leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ func (handler *evictLeaderHandler) UpdateConfig(w http.ResponseWriter, r *http.R
handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
handler.rd.JSON(w, http.StatusOK, nil)
handler.rd.JSON(w, http.StatusOK, "The scheduler has been applied to the store.")
}

func (handler *evictLeaderHandler) ListConfig(w http.ResponseWriter, r *http.Request) {
Expand Down
48 changes: 48 additions & 0 deletions pkg/schedule/schedulers/evict_slow_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,54 @@ func (conf *evictSlowStoreSchedulerConfig) clearAndPersist() (oldID uint64, err
return
}

<<<<<<< HEAD
=======
type evictSlowStoreHandler struct {
rd *render.Render
config *evictSlowStoreSchedulerConfig
}

func newEvictSlowStoreHandler(config *evictSlowStoreSchedulerConfig) http.Handler {
h := &evictSlowStoreHandler{
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 *evictSlowStoreHandler) UpdateConfig(w http.ResponseWriter, r *http.Request) {
var input map[string]interface{}
if err := apiutil.ReadJSONRespondError(handler.rd, w, r.Body, &input); err != nil {
return
}
recoveryDurationGapFloat, ok := input["recovery-duration"].(float64)
if !ok {
handler.rd.JSON(w, http.StatusInternalServerError, errors.New("invalid argument for 'recovery-duration'").Error())
return
}
handler.config.Lock()
defer handler.config.Unlock()
prevRecoveryDurationGap := handler.config.RecoveryDurationGap
recoveryDurationGap := uint64(recoveryDurationGapFloat)
handler.config.RecoveryDurationGap = recoveryDurationGap
if err := handler.config.persistLocked(); err != nil {
handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
handler.config.RecoveryDurationGap = prevRecoveryDurationGap
return
}
log.Info("evict-slow-store-scheduler update 'recovery-duration' - unit: s", zap.Uint64("prev", prevRecoveryDurationGap), zap.Uint64("cur", recoveryDurationGap))
handler.rd.JSON(w, http.StatusOK, "Config updated.")
}

func (handler *evictSlowStoreHandler) ListConfig(w http.ResponseWriter, r *http.Request) {
conf := handler.config.Clone()
handler.rd.JSON(w, http.StatusOK, conf)
}

>>>>>>> 8b8c78a78 (scheduler: add aduit log for scheduler config API and add resp msg for evict-leader (#7674))
type evictSlowStoreScheduler struct {
*BaseScheduler
conf *evictSlowStoreSchedulerConfig
Expand Down
49 changes: 49 additions & 0 deletions pkg/schedule/schedulers/evict_slow_trend.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,56 @@ func (conf *evictSlowTrendSchedulerConfig) clearAndPersist(cluster schedule.Clus
}
storeSlowTrendEvictedStatusGauge.WithLabelValues(address, strconv.FormatUint(oldID, 10)).Set(0)
conf.EvictedStores = []uint64{}
<<<<<<< HEAD
return oldID, conf.Persist()
=======
return oldID, conf.persistLocked()
}

type evictSlowTrendHandler struct {
rd *render.Render
config *evictSlowTrendSchedulerConfig
}

func newEvictSlowTrendHandler(config *evictSlowTrendSchedulerConfig) http.Handler {
h := &evictSlowTrendHandler{
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 *evictSlowTrendHandler) UpdateConfig(w http.ResponseWriter, r *http.Request) {
var input map[string]interface{}
if err := apiutil.ReadJSONRespondError(handler.rd, w, r.Body, &input); err != nil {
return
}
recoveryDurationGapFloat, ok := input["recovery-duration"].(float64)
if !ok {
handler.rd.JSON(w, http.StatusInternalServerError, errors.New("invalid argument for 'recovery-duration'").Error())
return
}
handler.config.Lock()
defer handler.config.Unlock()
prevRecoveryDurationGap := handler.config.RecoveryDurationGap
recoveryDurationGap := uint64(recoveryDurationGapFloat)
handler.config.RecoveryDurationGap = recoveryDurationGap
if err := handler.config.persistLocked(); err != nil {
handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
handler.config.RecoveryDurationGap = prevRecoveryDurationGap
return
}
log.Info("evict-slow-trend-scheduler update 'recovery-duration' - unit: s", zap.Uint64("prev", prevRecoveryDurationGap), zap.Uint64("cur", recoveryDurationGap))
handler.rd.JSON(w, http.StatusOK, "Config updated.")
}

func (handler *evictSlowTrendHandler) ListConfig(w http.ResponseWriter, r *http.Request) {
conf := handler.config.Clone()
handler.rd.JSON(w, http.StatusOK, conf)
>>>>>>> 8b8c78a78 (scheduler: add aduit log for scheduler config API and add resp msg for evict-leader (#7674))
}

type evictSlowTrendScheduler struct {
Expand Down
2 changes: 1 addition & 1 deletion pkg/schedule/schedulers/grant_leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func (handler *grantLeaderHandler) UpdateConfig(w http.ResponseWriter, r *http.R
handler.rd.JSON(w, http.StatusInternalServerError, err.Error())
return
}
handler.rd.JSON(w, http.StatusOK, nil)
handler.rd.JSON(w, http.StatusOK, "The scheduler has been applied to the store.")
}

func (handler *grantLeaderHandler) ListConfig(w http.ResponseWriter, r *http.Request) {
Expand Down
12 changes: 9 additions & 3 deletions server/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ func getFunctionName(f interface{}) string {
// @BasePath /pd/api/v1
func createRouter(prefix string, svr *server.Server) *mux.Router {
serviceMiddle := newServiceMiddlewareBuilder(svr)
registerPrefix := func(router *mux.Router, prefixPath string,
registerPrefix := func(router *mux.Router, prefixPath, name string,
handleFunc func(http.ResponseWriter, *http.Request), opts ...createRouteOption) {
routeCreateFunc(router.PathPrefix(prefixPath), serviceMiddle.createHandler(handleFunc),
getFunctionName(handleFunc), opts...)
name, opts...)
}
registerFunc := func(router *mux.Router, path string,
handleFunc func(http.ResponseWriter, *http.Request), opts ...createRouteOption) {
Expand Down Expand Up @@ -148,7 +148,8 @@ func createRouter(prefix string, svr *server.Server) *mux.Router {
registerFunc(clusterRouter, "/schedulers/diagnostic/{name}", diagnosticHandler.GetDiagnosticResult, setMethods(http.MethodGet), setAuditBackend(prometheus))

schedulerConfigHandler := newSchedulerConfigHandler(svr, rd)
registerPrefix(apiRouter, "/scheduler-config", schedulerConfigHandler.GetSchedulerConfig, setAuditBackend(prometheus))
registerPrefix(apiRouter, "/scheduler-config", "HandleSchedulerConfig", schedulerConfigHandler.HandleSchedulerConfig, setMethods(http.MethodPost, http.MethodDelete, http.MethodPut, http.MethodPatch), setAuditBackend(localLog, prometheus))
registerPrefix(apiRouter, "/scheduler-config", "GetSchedulerConfig", schedulerConfigHandler.HandleSchedulerConfig, setMethods(http.MethodGet), setAuditBackend(prometheus))

clusterHandler := newClusterHandler(svr, rd)
registerFunc(apiRouter, "/cluster", clusterHandler.GetCluster, setMethods(http.MethodGet), setAuditBackend(prometheus))
Expand Down Expand Up @@ -363,9 +364,14 @@ func createRouter(prefix string, svr *server.Server) *mux.Router {
registerFunc(apiRouter, "/admin/reset-ts", tsoAdminHandler.ResetTS, setMethods(http.MethodPost), setAuditBackend(localLog, prometheus))

// API to set or unset failpoints
<<<<<<< HEAD
failpoint.Inject("enableFailpointAPI", func() {
// this function will be named to "func2". It may be used in test
registerPrefix(apiRouter, "/fail", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
=======
if enableFailPointAPI {
registerPrefix(apiRouter, "/fail", "FailPoint", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
>>>>>>> 8b8c78a78 (scheduler: add aduit log for scheduler config API and add resp msg for evict-leader (#7674))
// The HTTP handler of failpoint requires the full path to be the failpoint path.
r.URL.Path = strings.TrimPrefix(r.URL.Path, prefix+apiPrefix+"/fail")
new(failpoint.HttpHandler).ServeHTTP(w, r)
Expand Down
2 changes: 1 addition & 1 deletion server/api/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ func newSchedulerConfigHandler(svr *server.Server, rd *render.Render) *scheduler
}
}

func (h *schedulerConfigHandler) GetSchedulerConfig(w http.ResponseWriter, r *http.Request) {
func (h *schedulerConfigHandler) HandleSchedulerConfig(w http.ResponseWriter, r *http.Request) {
handler := h.svr.GetHandler()
sh, err := handler.GetSchedulerConfigHandler()
if err == nil && sh != nil {
Expand Down
9 changes: 9 additions & 0 deletions server/api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,18 @@ func (suite *serviceTestSuite) TestServiceLabels() {
suite.Equal("Profile", serviceLabel)

accessPaths = suite.svr.GetServiceLabels("GetSchedulerConfig")
<<<<<<< HEAD
suite.Len(accessPaths, 1)
suite.Equal("/pd/api/v1/scheduler-config", accessPaths[0].Path)
suite.Equal("", accessPaths[0].Method)
=======
re.Len(accessPaths, 1)
re.Equal("/pd/api/v1/scheduler-config", accessPaths[0].Path)
re.Equal("GET", accessPaths[0].Method)
accessPaths = suite.svr.GetServiceLabels("HandleSchedulerConfig")
re.Len(accessPaths, 4)
re.Equal("/pd/api/v1/scheduler-config", accessPaths[0].Path)
>>>>>>> 8b8c78a78 (scheduler: add aduit log for scheduler config API and add resp msg for evict-leader (#7674))

accessPaths = suite.svr.GetServiceLabels("ResignLeader")
suite.Len(accessPaths, 1)
Expand Down
Loading

0 comments on commit cae3b44

Please sign in to comment.