Skip to content

Commit

Permalink
replace config
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <jinhao.hu@pingcap.com>
  • Loading branch information
HuSharp committed Feb 21, 2024
1 parent 46fd313 commit 96303f2
Show file tree
Hide file tree
Showing 11 changed files with 477 additions and 258 deletions.
48 changes: 34 additions & 14 deletions client/http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,25 @@ const (
leaderPrefix = "/pd/api/v1/leader"
transferLeader = "/pd/api/v1/leader/transfer"
// Config
Config = "/pd/api/v1/config"
ClusterVersion = "/pd/api/v1/config/cluster-version"
ScheduleConfig = "/pd/api/v1/config/schedule"
ReplicateConfig = "/pd/api/v1/config/replicate"
Config = "/pd/api/v1/config"
ClusterVersion = "/pd/api/v1/config/cluster-version"
ScheduleConfig = "/pd/api/v1/config/schedule"
ReplicateConfig = "/pd/api/v1/config/replicate"
LabelPropertyConfig = "/pd/api/v1/config/label-property"
ReplicationModeConfig = "/pd/api/v1/config/replication-mode"
PDServerConfig = "/pd/api/v1/config/pd-server"
// Rule
PlacementRule = "/pd/api/v1/config/rule"
PlacementRules = "/pd/api/v1/config/rules"
PlacementRulesInBatch = "/pd/api/v1/config/rules/batch"
placementRulesByGroup = "/pd/api/v1/config/rules/group"
PlacementRuleBundle = "/pd/api/v1/config/placement-rule"
placementRuleGroup = "/pd/api/v1/config/rule_group"
placementRuleGroups = "/pd/api/v1/config/rule_groups"
RegionLabelRule = "/pd/api/v1/config/region-label/rule"
RegionLabelRules = "/pd/api/v1/config/region-label/rules"
RegionLabelRulesByIDs = "/pd/api/v1/config/region-label/rules/ids"
PlacementRules = "/pd/api/v1/config/rules"
PlacementRulesInBatch = "/pd/api/v1/config/rules/batch"
placementRulesByGroup = "/pd/api/v1/config/rules/group"
placementRulesByRegion = "/pd/api/v1/config/rules/region"
PlacementRule = "/pd/api/v1/config/rule"
PlacementRuleBundle = "/pd/api/v1/config/placement-rule"
placementRuleGroup = "/pd/api/v1/config/rule_group"
placementRuleGroups = "/pd/api/v1/config/rule_groups"
RegionLabelRule = "/pd/api/v1/config/region-label/rule"
RegionLabelRules = "/pd/api/v1/config/region-label/rules"
RegionLabelRulesByIDs = "/pd/api/v1/config/region-label/rules/ids"
// Scheduler
Schedulers = "/pd/api/v1/schedulers"
scatterRangeScheduler = "/pd/api/v1/schedulers/scatter-range-"
Expand Down Expand Up @@ -157,6 +161,14 @@ func PlacementRuleByGroupAndID(group, id string) string {
return fmt.Sprintf("%s/%s/%s", PlacementRule, group, id)
}

// PlacementRulesByRegion returns the path of PD HTTP API to get placement rules by region.
func PlacementRulesByRegion(region string, detail bool) string {
if detail {
return fmt.Sprintf("%s/%s/detail", placementRulesByRegion, region)
}
return fmt.Sprintf("%s/%s", placementRulesByRegion, region)
}

// PlacementRuleBundleByGroup returns the path of PD HTTP API to get placement rule bundle by group.
func PlacementRuleBundleByGroup(group string) string {
return fmt.Sprintf("%s/%s", PlacementRuleBundle, group)
Expand All @@ -167,6 +179,14 @@ func PlacementRuleBundleWithPartialParameter(partial bool) string {
return fmt.Sprintf("%s?partial=%t", PlacementRuleBundle, partial)
}

// PlacementRuleBundleByGroupWithRegexpParameter returns the path of PD HTTP API to get placement rule bundle with regexp parameter.
func PlacementRuleBundleByGroupWithRegexpParameter(group string, regexp bool) string {
if regexp {
return fmt.Sprintf("%s/%s?regexp", PlacementRuleBundle, group)
}
return fmt.Sprintf("%s/%s", PlacementRuleBundle, group)
}

// PlacementRuleGroupByID returns the path of PD HTTP API to get placement rule group by ID.
func PlacementRuleGroupByID(id string) string {
return fmt.Sprintf("%s/%s", placementRuleGroup, id)
Expand Down
25 changes: 24 additions & 1 deletion client/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"time"

"github.com/pingcap/errors"
Expand Down Expand Up @@ -190,6 +192,12 @@ func (ci *clientInner) doRequest(
opt(req.Header)
}
req.Header.Set(xCallerIDKey, callerID)
if req.Header.Get(XForbiddenForwardToMicroServiceHeader) == "true" {
if req.Header.Get(XForwardedToMicroServiceHeader) == "true" {
log.Warn("the request is forwarded to micro service unexpectedly")
return -1, errors.New("the request is forwarded to micro service unexpectedly")
}
}

start := time.Now()
resp, err := ci.cli.Do(req)
Expand Down Expand Up @@ -217,14 +225,18 @@ func (ci *clientInner) doRequest(
logFields = append(logFields, zap.String("status", resp.Status))

bs, readErr := io.ReadAll(resp.Body)
var bodyStr string
if readErr != nil {
bodyStr = fmt.Sprintf("read body failed: %s", readErr.Error())
logFields = append(logFields, zap.NamedError("read-body-error", err))
} else {
// need to remove the last newline character
bodyStr = string(bytes.TrimRight(bs, "\n"))
logFields = append(logFields, zap.ByteString("body", bs))
}

log.Error("[pd] request failed with a non-200 status", logFields...)
return resp.StatusCode, errors.Errorf("request pd http api failed with status: '%s'", resp.Status)
return resp.StatusCode, errors.Errorf("request pd http api failed with status: '%s', body: '%s'", resp.Status, bodyStr)
}

if res == nil {
Expand Down Expand Up @@ -346,6 +358,10 @@ func (c *client) WithBackoffer(bo *retry.Backoffer) Client {
const (
pdAllowFollowerHandleKey = "PD-Allow-Follower-Handle"
xCallerIDKey = "X-Caller-ID"
// XForbiddenForwardToMicroServiceHeader is used to indicate that forwarding the request to a microservice is explicitly disallowed.
XForbiddenForwardToMicroServiceHeader = "X-Forbidden-Forward-To-MicroService"
// XForwardedToMicroServiceHeader is used to signal that the request has already been forwarded to a microservice.
XForwardedToMicroServiceHeader = "X-Forwarded-To-MicroService"
)

// HeaderOption configures the HTTP header.
Expand All @@ -358,6 +374,13 @@ func WithAllowFollowerHandle() HeaderOption {
}
}

// WithForbiddenForwardToMicroServiceHeader sets the header field to indicate that forwarding the request to a microservice is explicitly disallowed.
func WithForbiddenForwardToMicroServiceHeader(val bool) HeaderOption {
return func(header http.Header) {
header.Add(XForbiddenForwardToMicroServiceHeader, strconv.FormatBool(val))
}
}

func (c *client) request(ctx context.Context, reqInfo *requestInfo, headerOpts ...HeaderOption) error {
return c.inner.requestWithRetry(ctx, reqInfo.
WithCallerID(c.callerID).
Expand Down
Loading

0 comments on commit 96303f2

Please sign in to comment.