Skip to content
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

Fix the compatibility with PD v3.x #1274

Merged
merged 2 commits into from
Apr 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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