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

util: imporve the precision of human size (#1778) #1782

Merged
merged 4 commits into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ require (
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 // indirect
github.com/coreos/go-semver v0.2.0
github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf
github.com/dustin/go-humanize v0.0.0-20180421182945-02af3965c54e
github.com/docker/go-units v0.4.0
github.com/dustin/go-humanize v0.0.0-20180421182945-02af3965c54e // indirect
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385 // indirect
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
github.com/gogo/protobuf v1.0.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/docker/go-units v0.4.0 h1:3uh0PgVws3nIA0Q+MwDC8yjEPf9zjRfZZWXZYDct3Tw=
github.com/docker/go-units v0.4.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dustin/go-humanize v0.0.0-20180421182945-02af3965c54e h1:Fw7ZmgiklsLh5EQWyHh1sumKSCG1+yjEctIpGKib87s=
github.com/dustin/go-humanize v0.0.0-20180421182945-02af3965c54e/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
Expand Down
8 changes: 4 additions & 4 deletions pkg/typeutil/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package typeutil
import (
"strconv"

gh "github.com/dustin/go-humanize"
"github.com/docker/go-units"
"github.com/pkg/errors"
)

Expand All @@ -25,7 +25,7 @@ type ByteSize uint64

// MarshalJSON returns the size as a JSON string.
func (b ByteSize) MarshalJSON() ([]byte, error) {
return []byte(`"` + gh.IBytes(uint64(b)) + `"`), nil
return []byte(`"` + units.BytesSize(float64(b)) + `"`), nil
}

// UnmarshalJSON parses a JSON string into the bytesize.
Expand All @@ -34,7 +34,7 @@ func (b *ByteSize) UnmarshalJSON(text []byte) error {
if err != nil {
return errors.WithStack(err)
}
v, err := gh.ParseBytes(s)
v, err := units.RAMInBytes(s)
if err != nil {
return errors.WithStack(err)
}
Expand All @@ -44,7 +44,7 @@ func (b *ByteSize) UnmarshalJSON(text []byte) error {

// UnmarshalText parses a Toml string into the bytesize.
func (b *ByteSize) UnmarshalText(text []byte) error {
v, err := gh.ParseBytes(string(text))
v, err := units.RAMInBytes(string(text))
if err != nil {
return errors.WithStack(err)
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/typeutil/size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ func (s *testSizeSuite) TestJSON(c *C) {
var nb ByteSize
err = json.Unmarshal(o, &nb)
c.Assert(err, IsNil)

b = ByteSize(1756821276000)
o, err = json.Marshal(b)
c.Assert(err, IsNil)
c.Assert(string(o), Equals, `"1.598TiB"`)
}
19 changes: 19 additions & 0 deletions server/api/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package api

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
Expand All @@ -22,6 +23,7 @@ import (
"strings"
"time"

"github.com/docker/go-units"
. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
Expand Down Expand Up @@ -134,9 +136,26 @@ func (s *testStoreSuite) TestStoresList(c *C) {

func (s *testStoreSuite) TestStoreGet(c *C) {
url := fmt.Sprintf("%s/store/1", s.urlPrefix)
s.svr.StoreHeartbeat(
context.Background(), &pdpb.StoreHeartbeatRequest{
Header: &pdpb.RequestHeader{ClusterId: s.svr.ClusterID()},
Stats: &pdpb.StoreStats{
StoreId: 1,
Capacity: 1798985089024,
Available: 1709868695552,
UsedSize: 85150956358,
},
},
)

info := new(StoreInfo)
err := readJSONWithURL(url, info)

c.Assert(err, IsNil)
capacity, _ := units.RAMInBytes("1.636TiB")
available, _ := units.RAMInBytes("1.555TiB")
c.Assert(int64(info.Status.Capacity), Equals, capacity)
c.Assert(int64(info.Status.Available), Equals, available)
checkStoresInfo(c, []*StoreInfo{info}, s.stores[:1])
}

Expand Down