Skip to content

Commit

Permalink
Fix the compatibility with PD v3.x (#1274)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucklove authored Apr 2, 2021
1 parent cf55cab commit 9049356
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion pkg/cluster/api/pdapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ import (
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/tiup/pkg/logger/log"
"github.com/pingcap/tiup/pkg/utils"
"golang.org/x/mod/semver"
)

// PDClient is an HTTP client of the PD server
type PDClient struct {
version string
addrs []string
tlsEnabled bool
httpClient *utils.HTTPClient
Expand All @@ -46,11 +48,30 @@ func NewPDClient(addrs []string, timeout time.Duration, tlsConfig *tls.Config) *
enableTLS = true
}

return &PDClient{
cli := &PDClient{
addrs: addrs,
tlsEnabled: enableTLS,
httpClient: utils.NewHTTPClient(timeout, tlsConfig),
}

cli.tryIdentifyVersion()
return cli
}

func (pc *PDClient) tryIdentifyVersion() {
endpoints := pc.getEndpoints(pdVersionURI)
response := map[string]string{}
_, err := tryURLs(endpoints, func(endpoint string) ([]byte, error) {
body, err := pc.httpClient.Get(endpoint)
if err != nil {
return body, err
}

return body, json.Unmarshal(body, &response)
})
if err == nil {
pc.version = response["version"]
}
}

// GetURL builds the the client URL of PDClient
Expand All @@ -70,6 +91,7 @@ const (
// nolint (some is unused now)
var (
pdPingURI = "pd/ping"
pdVersionURI = "pd/api/v1/version"
pdConfigURI = "pd/api/v1/config"
pdClusterIDURI = "pd/api/v1/cluster"
pdConfigReplicate = "pd/api/v1/config/replicate"
Expand Down Expand Up @@ -735,6 +757,11 @@ func (pc *PDClient) CheckRegion(state string) (*RegionsInfo, error) {
// SetReplicationConfig sets a config key value of PD replication, it has the
// same effect as `pd-ctl config set key value`
func (pc *PDClient) SetReplicationConfig(key string, value int) error {
// Only support for pd version >= v4.0.0
if pc.version == "" || semver.Compare(pc.version, "v4.0.0") < 0 {
return nil
}

data := map[string]interface{}{"set": map[string]interface{}{key: value}}
body, err := json.Marshal(data)
if err != nil {
Expand All @@ -747,6 +774,11 @@ func (pc *PDClient) SetReplicationConfig(key string, value int) error {
// SetAllStoreLimits sets store for all stores and types, it has the same effect
// as `pd-ctl store limit all value`
func (pc *PDClient) SetAllStoreLimits(value int) error {
// Only support for pd version >= v4.0.0
if pc.version == "" || semver.Compare(pc.version, "v4.0.0") < 0 {
return nil
}

data := map[string]interface{}{"rate": value}
body, err := json.Marshal(data)
if err != nil {
Expand Down

0 comments on commit 9049356

Please sign in to comment.