-
Notifications
You must be signed in to change notification settings - Fork 720
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
mcs: make scheduling server support operator http interface #7090
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c4c9359
mcs: make scheduling server support operator http interface
lhy1024 9aa69f1
add some test
lhy1024 0dae55b
Merge branch 'master' of github.com:tikv/pd into sche-redirect2
lhy1024 d8af115
add test after region heartbeat forward
lhy1024 09ca45d
wait review
lhy1024 8f46243
move function out from pd-ctl
lhy1024 51780f2
add more test
lhy1024 27b4458
Merge branch 'master' of github.com:tikv/pd into sche-redirect2
lhy1024 7b9b756
fix conflict
lhy1024 3a7d8b2
make test stable
lhy1024 37b3c53
address comments
lhy1024 2fb4aa3
rename
lhy1024 5e4559f
address comments
lhy1024 b5e8d21
Merge branch 'master' into sche-redirect2
ti-chi-bot[bot] 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,9 @@ import ( | |
"github.com/joho/godotenv" | ||
scheserver "github.com/tikv/pd/pkg/mcs/scheduling/server" | ||
"github.com/tikv/pd/pkg/mcs/utils" | ||
"github.com/tikv/pd/pkg/schedule" | ||
sche "github.com/tikv/pd/pkg/schedule/core" | ||
"github.com/tikv/pd/pkg/schedule/handler" | ||
"github.com/tikv/pd/pkg/schedule/operator" | ||
"github.com/tikv/pd/pkg/utils/apiutil" | ||
"github.com/tikv/pd/pkg/utils/apiutil/multiservicesapi" | ||
|
@@ -35,6 +38,7 @@ import ( | |
|
||
// APIPathPrefix is the prefix of the API path. | ||
const APIPathPrefix = "/scheduling/api/v1" | ||
const handlerKey = "handler" | ||
|
||
var ( | ||
once sync.Once | ||
|
@@ -62,6 +66,18 @@ type Service struct { | |
rd *render.Render | ||
} | ||
|
||
type server struct { | ||
server *scheserver.Server | ||
} | ||
|
||
func (s *server) GetCoordinator() *schedule.Coordinator { | ||
return s.server.GetCoordinator() | ||
} | ||
|
||
func (s *server) GetCluster() sche.SharedCluster { | ||
return s.server.GetCluster() | ||
} | ||
|
||
func createIndentRender() *render.Render { | ||
return render.New(render.Options{ | ||
IndentJSON: true, | ||
|
@@ -81,6 +97,7 @@ func NewService(srv *scheserver.Service) *Service { | |
apiHandlerEngine.Use(gzip.Gzip(gzip.DefaultCompression)) | ||
apiHandlerEngine.Use(func(c *gin.Context) { | ||
c.Set(multiservicesapi.ServiceContextKey, srv.Server) | ||
c.Set(handlerKey, handler.NewHandler(&server{server: srv.Server})) | ||
c.Next() | ||
}) | ||
apiHandlerEngine.Use(multiservicesapi.ServiceRedirector()) | ||
|
@@ -115,7 +132,10 @@ func (s *Service) RegisterCheckersRouter() { | |
func (s *Service) RegisterOperatorsRouter() { | ||
router := s.root.Group("operators") | ||
router.GET("", getOperators) | ||
router.GET("/:id", getOperatorByID) | ||
router.POST("", createOperator) | ||
router.GET("/:id", getOperatorByRegion) | ||
router.DELETE("/:id", deleteOperatorByRegion) | ||
router.GET("/records", getOperatorRecords) | ||
} | ||
|
||
// @Tags operators | ||
|
@@ -126,8 +146,8 @@ func (s *Service) RegisterOperatorsRouter() { | |
// @Failure 400 {string} string "The input is invalid." | ||
// @Failure 500 {string} string "PD server failed to proceed the request." | ||
// @Router /operators/{id} [GET] | ||
func getOperatorByID(c *gin.Context) { | ||
svr := c.MustGet(multiservicesapi.ServiceContextKey).(*scheserver.Server) | ||
func getOperatorByRegion(c *gin.Context) { | ||
handler := c.MustGet(handlerKey).(*handler.Handler) | ||
id := c.Param("id") | ||
|
||
regionID, err := strconv.ParseUint(id, 10, 64) | ||
|
@@ -136,13 +156,13 @@ func getOperatorByID(c *gin.Context) { | |
return | ||
} | ||
|
||
opController := svr.GetCoordinator().GetOperatorController() | ||
if opController == nil { | ||
op, err := handler.GetOperatorStatus(regionID) | ||
if err != nil { | ||
c.String(http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
|
||
c.IndentedJSON(http.StatusOK, opController.GetOperatorStatus(regionID)) | ||
c.IndentedJSON(http.StatusOK, op) | ||
} | ||
|
||
// @Tags operators | ||
|
@@ -153,40 +173,104 @@ func getOperatorByID(c *gin.Context) { | |
// @Failure 500 {string} string "PD server failed to proceed the request." | ||
// @Router /operators [GET] | ||
func getOperators(c *gin.Context) { | ||
svr := c.MustGet(multiservicesapi.ServiceContextKey).(*scheserver.Server) | ||
handler := c.MustGet(handlerKey).(*handler.Handler) | ||
var ( | ||
results []*operator.Operator | ||
ops []*operator.Operator | ||
err error | ||
) | ||
|
||
opController := svr.GetCoordinator().GetOperatorController() | ||
if opController == nil { | ||
c.String(http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
kinds := c.QueryArray("kind") | ||
if len(kinds) == 0 { | ||
results = opController.GetOperators() | ||
results, err = handler.GetOperators() | ||
} else { | ||
for _, kind := range kinds { | ||
switch kind { | ||
case "admin": | ||
ops = opController.GetOperatorsOfKind(operator.OpAdmin) | ||
case "leader": | ||
ops = opController.GetOperatorsOfKind(operator.OpLeader) | ||
case "region": | ||
ops = opController.GetOperatorsOfKind(operator.OpRegion) | ||
case "waiting": | ||
ops = opController.GetWaitingOperators() | ||
} | ||
results = append(results, ops...) | ||
} | ||
results, err = handler.GetOperatorsByKinds(kinds) | ||
} | ||
|
||
if err != nil { | ||
c.String(http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
c.IndentedJSON(http.StatusOK, results) | ||
} | ||
|
||
// @Tags operator | ||
// @Summary Cancel a Region's pending operator. | ||
// @Param region_id path int true "A Region's Id" | ||
// @Produce json | ||
// @Success 200 {string} string "The pending operator is canceled." | ||
// @Failure 400 {string} string "The input is invalid." | ||
// @Failure 500 {string} string "PD server failed to proceed the request." | ||
// @Router /operators/{region_id} [delete] | ||
func deleteOperatorByRegion(c *gin.Context) { | ||
handler := c.MustGet(handlerKey).(*handler.Handler) | ||
id := c.Param("id") | ||
|
||
regionID, err := strconv.ParseUint(id, 10, 64) | ||
if err != nil { | ||
c.String(http.StatusBadRequest, err.Error()) | ||
return | ||
} | ||
|
||
if err = handler.RemoveOperator(regionID); err != nil { | ||
c.String(http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
|
||
c.String(http.StatusOK, "The pending operator is canceled.") | ||
} | ||
|
||
// @Tags operator | ||
// @Summary lists the finished operators since the given timestamp in second. | ||
// @Param from query integer false "From Unix timestamp" | ||
// @Produce json | ||
// @Success 200 {object} []operator.OpRecord | ||
// @Failure 400 {string} string "The request is invalid." | ||
// @Failure 500 {string} string "PD server failed to proceed the request." | ||
// @Router /operators/records [get] | ||
func getOperatorRecords(c *gin.Context) { | ||
handler := c.MustGet(handlerKey).(*handler.Handler) | ||
from, err := apiutil.ParseTime(c.Query("from")) | ||
if err != nil { | ||
c.String(http.StatusBadRequest, err.Error()) | ||
return | ||
} | ||
records, err := handler.GetRecords(from) | ||
if err != nil { | ||
c.String(http.StatusInternalServerError, err.Error()) | ||
return | ||
} | ||
c.IndentedJSON(http.StatusOK, records) | ||
} | ||
|
||
// FIXME: details of input json body params | ||
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 does this mean? 👀 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. only copy from previous code |
||
// @Tags operator | ||
// @Summary Create an operator. | ||
// @Accept json | ||
// @Param body body object true "json params" | ||
// @Produce json | ||
// @Success 200 {string} string "The operator is created." | ||
// @Failure 400 {string} string "The input is invalid." | ||
// @Failure 500 {string} string "PD server failed to proceed the request." | ||
// @Router /operators [post] | ||
func createOperator(c *gin.Context) { | ||
handler := c.MustGet(handlerKey).(*handler.Handler) | ||
var input map[string]interface{} | ||
if err := c.BindJSON(&input); err != nil { | ||
c.String(http.StatusBadRequest, err.Error()) | ||
return | ||
} | ||
statusCode, result, err := handler.HandleOperatorCreation(input) | ||
if err != nil { | ||
c.String(statusCode, err.Error()) | ||
return | ||
} | ||
if statusCode == http.StatusOK && result == nil { | ||
c.String(http.StatusOK, "The operator is created.") | ||
return | ||
} | ||
c.IndentedJSON(statusCode, result) | ||
} | ||
|
||
// @Tags checkers | ||
// @Summary Get checker by name | ||
// @Param name path string true "The name of the checker." | ||
|
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.
Do we have it here?
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.
It is a copy from server/api, maybe we need to fix all other comments too.