Skip to content

Commit

Permalink
refactor handler
Browse files Browse the repository at this point in the history
Signed-off-by: lhy1024 <[email protected]>
  • Loading branch information
lhy1024 committed Oct 12, 2023
1 parent a768778 commit 600de27
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 112 deletions.
15 changes: 15 additions & 0 deletions errors.toml
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,11 @@ error = '''
build rule list failed, %s
'''

["PD:placement:ErrKeyFormat"]
error = '''
key should be in hex format
'''

["PD:placement:ErrLoadRule"]
error = '''
load rule failed
Expand All @@ -551,6 +556,11 @@ error = '''
load rule group failed
'''

["PD:placement:ErrPlacementDisabled"]
error = '''
placement rules feature is disabled
'''

["PD:placement:ErrRuleContent"]
error = '''
invalid rule content, %s
Expand Down Expand Up @@ -606,6 +616,11 @@ error = '''
region %v has abnormal peer
'''

["PD:region:ErrRegionInvalidID"]
error = '''
invalid region id
'''

["PD:region:ErrRegionNotAdjacent"]
error = '''
two regions are not adjacent
Expand Down
12 changes: 8 additions & 4 deletions pkg/errs/errno.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ var (

// region errors
var (
// ErrRegionInvalidID is error info for region id invalid.
ErrRegionInvalidID = errors.Normalize("invalid region id", errors.RFCCodeText("PD:region:ErrRegionInvalidID"))
// ErrRegionNotAdjacent is error info for region not adjacent.
ErrRegionNotAdjacent = errors.Normalize("two regions are not adjacent", errors.RFCCodeText("PD:region:ErrRegionNotAdjacent"))
// ErrRegionNotFound is error info for region not found.
Expand Down Expand Up @@ -153,10 +155,12 @@ var (

// placement errors
var (
ErrRuleContent = errors.Normalize("invalid rule content, %s", errors.RFCCodeText("PD:placement:ErrRuleContent"))
ErrLoadRule = errors.Normalize("load rule failed", errors.RFCCodeText("PD:placement:ErrLoadRule"))
ErrLoadRuleGroup = errors.Normalize("load rule group failed", errors.RFCCodeText("PD:placement:ErrLoadRuleGroup"))
ErrBuildRuleList = errors.Normalize("build rule list failed, %s", errors.RFCCodeText("PD:placement:ErrBuildRuleList"))
ErrRuleContent = errors.Normalize("invalid rule content, %s", errors.RFCCodeText("PD:placement:ErrRuleContent"))
ErrLoadRule = errors.Normalize("load rule failed", errors.RFCCodeText("PD:placement:ErrLoadRule"))
ErrLoadRuleGroup = errors.Normalize("load rule group failed", errors.RFCCodeText("PD:placement:ErrLoadRuleGroup"))
ErrBuildRuleList = errors.Normalize("build rule list failed, %s", errors.RFCCodeText("PD:placement:ErrBuildRuleList"))
ErrPlacementDisabled = errors.Normalize("placement rules feature is disabled", errors.RFCCodeText("PD:placement:ErrPlacementDisabled"))
ErrKeyFormat = errors.Normalize("key should be in hex format", errors.RFCCodeText("PD:placement:ErrKeyFormat"))
)

// region label errors
Expand Down
10 changes: 10 additions & 0 deletions pkg/mcs/scheduling/server/apis/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ func (s *Service) RegisterHotspotRouter() {
router.GET("/buckets", getHotBuckets)
}

// RegisterConfigRouter registers the router of the config handler.
func (s *Service) RegisterConfigRouter() {
// router := s.root.Group("config")
// router.GET("/rule", getHotBuckets)
// router.GET("/rules", getHotBuckets)
// router.GET("/rule_group", getHotBuckets)
// router.GET("/rule_groups", getHotBuckets)
// router.GET("/placement-rule", getHotBuckets)
}

// RegisterOperatorsRouter registers the router of the operators handler.
func (s *Service) RegisterOperatorsRouter() {
router := s.root.Group("operators")
Expand Down
43 changes: 43 additions & 0 deletions pkg/schedule/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"encoding/hex"
"net/http"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -1040,3 +1041,45 @@ func (h *Handler) GetHotBuckets(regionIDs ...uint64) (HotBucketsResponse, error)
}
return ret, nil
}

// GetRuleManager returns the rule manager.
func (h *Handler) GetRuleManager() (*placement.RuleManager, error) {
c := h.GetCluster()
if c == nil {
return nil, errs.ErrNotBootstrapped
}
if !c.GetSharedConfig().IsPlacementRulesEnabled() {
return nil, errs.ErrPlacementDisabled
}
return c.GetRuleManager(), nil
}

// PreCheckForRegion checks if the region is valid.
func (h *Handler) PreCheckForRegion(regionStr string) (*core.RegionInfo, int, error) {
c := h.GetCluster()
if c == nil {
return nil, http.StatusInternalServerError, errs.ErrNotBootstrapped.GenWithStackByArgs()
}
regionID, err := strconv.ParseUint(regionStr, 10, 64)
if err != nil {
return nil, http.StatusBadRequest, errs.ErrRegionInvalidID.FastGenByArgs()
}
region := c.GetRegion(regionID)
if region == nil {
return nil, http.StatusNotFound, errs.ErrRegionNotFound.FastGenByArgs(regionID)
}
return region, http.StatusOK, nil
}

// CheckRegionPlacementRule checks if the region matches the placement rules.
func (h *Handler) CheckRegionPlacementRule(region *core.RegionInfo) (*placement.RegionFit, error) {
c := h.GetCluster()
if c == nil {
return nil, errs.ErrNotBootstrapped.GenWithStackByArgs()
}
manager, err := h.GetRuleManager()
if err != nil {
return nil, err
}
return manager.FitRegion(c, region), nil
}
Loading

0 comments on commit 600de27

Please sign in to comment.