Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
Signed-off-by: lhy1024 <[email protected]>
  • Loading branch information
lhy1024 committed Sep 13, 2023
1 parent 32a81e3 commit 541b431
Show file tree
Hide file tree
Showing 13 changed files with 497 additions and 144 deletions.
42 changes: 42 additions & 0 deletions pkg/errs/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2023 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 errs

import "github.com/pingcap/errors"

var (
// ErrOperatorNotFound is error info for operator not found.
ErrOperatorNotFound = errors.New("operator not found")
// ErrAddOperator is error info for already have an operator when adding operator.
ErrAddOperator = errors.New("failed to add operator, maybe already have one")
// ErrRegionNotAdjacent is error info for region not adjacent.
ErrRegionNotAdjacent = errors.New("two regions are not adjacent")
// ErrRegionNotFound is error info for region not found.
ErrRegionNotFound = func(regionID uint64) error {
return errors.Errorf("region %v not found", regionID)
}
// ErrRegionAbnormalPeer is error info for region has abnormal peer.
ErrRegionAbnormalPeer = func(regionID uint64) error {
return errors.Errorf("region %v has abnormal peer", regionID)
}
// ErrStoreNotFoundByID is error info for store not found.
ErrStoreNotFoundByID = func(storeID uint64) error {
return errors.Errorf("store %v not found", storeID)
}
// ErrPluginNotFound is error info for plugin not found.
ErrPluginNotFound = func(pluginPath string) error {
return errors.Errorf("plugin is not found: %s", pluginPath)
}
)
79 changes: 7 additions & 72 deletions pkg/mcs/scheduling/server/apis/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package apis

import (
"net/http"
"strconv"
"sync"
"time"

Expand All @@ -27,7 +26,6 @@ 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/operator"
"github.com/tikv/pd/pkg/utils/apiutil"
"github.com/tikv/pd/pkg/utils/apiutil/multiservicesapi"
"github.com/unrolled/render"
Expand Down Expand Up @@ -115,76 +113,13 @@ func (s *Service) RegisterCheckersRouter() {
func (s *Service) RegisterOperatorsRouter() {
router := s.root.Group("operators")
router.GET("", getOperators)
router.GET("/:id", getOperatorByID)
}

// @Tags operators
// @Summary Get an operator by ID.
// @Param region_id path int true "A Region's Id"
// @Produce json
// @Success 200 {object} operator.OpWithStatus
// @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)
id := c.Param("id")

regionID, err := strconv.ParseUint(id, 10, 64)
if err != nil {
c.String(http.StatusBadRequest, err.Error())
return
}

opController := svr.GetCoordinator().GetOperatorController()
if opController == nil {
c.String(http.StatusInternalServerError, err.Error())
return
}

c.IndentedJSON(http.StatusOK, opController.GetOperatorStatus(regionID))
}

// @Tags operators
// @Summary List operators.
// @Param kind query string false "Specify the operator kind." Enums(admin, leader, region, waiting)
// @Produce json
// @Success 200 {array} operator.Operator
// @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)
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()
} 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...)
}
}

c.IndentedJSON(http.StatusOK, results)
router.POST("", createOperator)
router.GET("/:id", getOperatorByRegion)
router.DELETE("/:id", deleteOperatorByRegion)
router.GET("/records", getOperatorRecords)
// todo: add more operators api [post]
// todo: add test for pd-ctl
// todo: add test for api
}

// @Tags checkers
Expand Down
Loading

0 comments on commit 541b431

Please sign in to comment.