From fb0be5ef8f10e605fc897cacca033061af8d4bb7 Mon Sep 17 00:00:00 2001 From: Allen Zhong Date: Thu, 20 Oct 2022 17:59:55 +0800 Subject: [PATCH] cluster/api: sync pdapi & typeutil with pd 6.3.0 (#2063) --- go.mod | 2 +- go.sum | 4 +-- pkg/cluster/api/types.go | 43 ++++++++++++++++++------- pkg/cluster/api/typeutil/size.go | 10 ++++++ pkg/cluster/api/typeutil/size_test.go | 45 ++++++++++++++++++++------- pkg/cluster/manager/display.go | 2 +- 6 files changed, 80 insertions(+), 26 deletions(-) diff --git a/go.mod b/go.mod index 4b093435c9..edbb7e3464 100644 --- a/go.mod +++ b/go.mod @@ -35,7 +35,7 @@ require ( github.com/pingcap/errors v0.11.5-0.20201126102027-b0a155152ca3 github.com/pingcap/failpoint v0.0.0-20220801062533-2eaa32854a6c github.com/pingcap/fn v1.0.0 - github.com/pingcap/kvproto v0.0.0-20220525022339-6aaebf466305 + github.com/pingcap/kvproto v0.0.0-20220913050750-f6d05706948a github.com/pingcap/tidb-insight/collector v0.0.0-20220902034607-fb5ae0ddc8c1 github.com/pkg/errors v0.9.1 github.com/prometheus/client_model v0.2.0 diff --git a/go.sum b/go.sum index b412f2eee5..b0d07b0745 100644 --- a/go.sum +++ b/go.sum @@ -394,8 +394,8 @@ github.com/pingcap/failpoint v0.0.0-20220801062533-2eaa32854a6c h1:CgbKAHto5CQgW github.com/pingcap/failpoint v0.0.0-20220801062533-2eaa32854a6c/go.mod h1:4qGtCB0QK0wBzKtFEGDhxXnSnbQApw1gc9siScUl8ew= github.com/pingcap/fn v1.0.0 h1:CyA6AxcOZkQh52wIqYlAmaVmF6EvrcqFywP463pjA8g= github.com/pingcap/fn v1.0.0/go.mod h1:u9WZ1ZiOD1RpNhcI42RucFh/lBuzTu6rw88a+oF2Z24= -github.com/pingcap/kvproto v0.0.0-20220525022339-6aaebf466305 h1:TZ0teMZoKHnZDlJxNkWrp5Sgv3w+ruNbrqtBYKsfaNw= -github.com/pingcap/kvproto v0.0.0-20220525022339-6aaebf466305/go.mod h1:OYtxs0786qojVTmkVeufx93xe+jUgm56GUYRIKnmaGI= +github.com/pingcap/kvproto v0.0.0-20220913050750-f6d05706948a h1:LCtkOPEzjWk86NclzxdZ42nNtbhuIN1p6cpd/FYUqkU= +github.com/pingcap/kvproto v0.0.0-20220913050750-f6d05706948a/go.mod h1:OYtxs0786qojVTmkVeufx93xe+jUgm56GUYRIKnmaGI= github.com/pingcap/log v0.0.0-20191012051959-b742a5d432e9/go.mod h1:4rbK1p9ILyIfb6hU7OG2CiWSqMXnp3JMbiaVJ6mvoY8= github.com/pingcap/log v0.0.0-20200511115504-543df19646ad/go.mod h1:4rbK1p9ILyIfb6hU7OG2CiWSqMXnp3JMbiaVJ6mvoY8= github.com/pingcap/log v1.1.0 h1:ELiPxACz7vdo1qAvvaWJg1NrYFoY6gqAh/+Uo6aXdD8= diff --git a/pkg/cluster/api/types.go b/pkg/cluster/api/types.go index 1549b625cd..e7be3d5510 100644 --- a/pkg/cluster/api/types.go +++ b/pkg/cluster/api/types.go @@ -71,6 +71,7 @@ type StoreStatus struct { RegionWeight float64 `json:"region_weight"` RegionScore float64 `json:"region_score"` RegionSize int64 `json:"region_size"` + SlowScore uint64 `json:"slow_score,omitempty"` // added omitempty SendingSnapCount uint32 `json:"sending_snap_count,omitempty"` ReceivingSnapCount uint32 `json:"receiving_snap_count,omitempty"` ApplyingSnapCount uint32 `json:"applying_snap_count,omitempty"` @@ -98,23 +99,43 @@ type ReplicationStatus struct { StateID uint64 `json:"state_id"` } +// MetaPeer is api compatible with *metapb.Peer. +type MetaPeer struct { + *metapb.Peer + // RoleName is `Role.String()`. + // Since Role is serialized as int by json by default, + // introducing it will make the output of pd-ctl easier to identify Role. + RoleName string `json:"role_name"` + // IsLearner is `Role == "Learner"`. + // Since IsLearner was changed to Role in kvproto in 5.0, this field was introduced to ensure api compatibility. + IsLearner bool `json:"is_learner,omitempty"` +} + +// PDPeerStats is api compatible with *pdpb.PeerStats. +// NOTE: This type is exported by HTTP API. Please pay more attention when modifying it. +type PDPeerStats struct { + *pdpb.PeerStats + Peer MetaPeer `json:"peer"` +} + // RegionInfo records detail region info for api usage. type RegionInfo struct { ID uint64 `json:"id"` StartKey string `json:"start_key"` EndKey string `json:"end_key"` RegionEpoch *metapb.RegionEpoch `json:"epoch,omitempty"` - Peers []*metapb.Peer `json:"peers,omitempty"` - - Leader *metapb.Peer `json:"leader,omitempty"` - DownPeers []*pdpb.PeerStats `json:"down_peers,omitempty"` - PendingPeers []*metapb.Peer `json:"pending_peers,omitempty"` - WrittenBytes uint64 `json:"written_bytes"` - ReadBytes uint64 `json:"read_bytes"` - WrittenKeys uint64 `json:"written_keys"` - ReadKeys uint64 `json:"read_keys"` - ApproximateSize int64 `json:"approximate_size"` - ApproximateKeys int64 `json:"approximate_keys"` + Peers []MetaPeer `json:"peers,omitempty"` + + Leader MetaPeer `json:"leader,omitempty"` + DownPeers []PDPeerStats `json:"down_peers,omitempty"` + PendingPeers []MetaPeer `json:"pending_peers,omitempty"` + WrittenBytes uint64 `json:"written_bytes"` + ReadBytes uint64 `json:"read_bytes"` + WrittenKeys uint64 `json:"written_keys"` + ReadKeys uint64 `json:"read_keys"` + ApproximateSize int64 `json:"approximate_size"` + ApproximateKeys int64 `json:"approximate_keys"` + Buckets []string `json:"buckets,omitempty"` ReplicationStatus *ReplicationStatus `json:"replication_status,omitempty"` } diff --git a/pkg/cluster/api/typeutil/size.go b/pkg/cluster/api/typeutil/size.go index 52536d3a94..89445569f9 100644 --- a/pkg/cluster/api/typeutil/size.go +++ b/pkg/cluster/api/typeutil/size.go @@ -24,6 +24,16 @@ import ( // ByteSize is a retype uint64 for TOML and JSON. type ByteSize uint64 +// ParseMBFromText parses MB from text. +func ParseMBFromText(text string, value uint64) uint64 { + b := ByteSize(0) + err := b.UnmarshalText([]byte(text)) + if err != nil { + return value + } + return uint64(b / units.MiB) +} + // MarshalJSON returns the size as a JSON string. func (b ByteSize) MarshalJSON() ([]byte, error) { return []byte(`"` + units.BytesSize(float64(b)) + `"`), nil diff --git a/pkg/cluster/api/typeutil/size_test.go b/pkg/cluster/api/typeutil/size_test.go index 2ac0593f73..57c246953e 100644 --- a/pkg/cluster/api/typeutil/size_test.go +++ b/pkg/cluster/api/typeutil/size_test.go @@ -16,26 +16,49 @@ package typeutil import ( "encoding/json" + "testing" - . "github.com/pingcap/check" + "github.com/docker/go-units" + "github.com/stretchr/testify/require" ) -var _ = Suite(&testSizeSuite{}) - -type testSizeSuite struct { -} - -func (s *testSizeSuite) TestJSON(c *C) { +func TestSizeJSON(t *testing.T) { + t.Parallel() + re := require.New(t) b := ByteSize(265421587) o, err := json.Marshal(b) - c.Assert(err, IsNil) + re.NoError(err) var nb ByteSize err = json.Unmarshal(o, &nb) - c.Assert(err, IsNil) + re.NoError(err) b = ByteSize(1756821276000) o, err = json.Marshal(b) - c.Assert(err, IsNil) - c.Assert(string(o), Equals, `"1.598TiB"`) + re.NoError(err) + re.Equal(`"1.598TiB"`, string(o)) +} + +func TestParseMbFromText(t *testing.T) { + t.Parallel() + re := require.New(t) + testCases := []struct { + body []string + size uint64 + }{{ + body: []string{"10Mib", "10MiB", "10M", "10MB"}, + size: uint64(10), + }, { + body: []string{"10GiB", "10Gib", "10G", "10GB"}, + size: uint64(10 * units.GiB / units.MiB), + }, { + body: []string{"10yiB", "10aib"}, + size: uint64(1), + }} + + for _, testCase := range testCases { + for _, b := range testCase.body { + re.Equal(int(testCase.size), int(ParseMBFromText(b, 1))) + } + } } diff --git a/pkg/cluster/manager/display.go b/pkg/cluster/manager/display.go index 8b388a020f..7288db970d 100644 --- a/pkg/cluster/manager/display.go +++ b/pkg/cluster/manager/display.go @@ -242,7 +242,7 @@ func (m *Manager) Display(dopt DisplayOption, opt operator.Options) error { if m.logger.GetDisplayMode() == logprinter.DisplayModeJSON { grafanaURLs := getGrafanaURL(clusterInstInfos) if len(grafanaURLs) != 0 { - j.ClusterMetaInfo.GrafanaURLS = grafanaURLs + j.ClusterMetaInfo.GrafanaURLS = grafanaURLs } } else { urls, exist := getGrafanaURLStr(clusterInstInfos)