This repository has been archived by the owner on Dec 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
config: allow four byte-size config to be specified using human-readable units ("100 GiB") #471
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
76c9a52
Makefile: add `make finish-prepare` action
kennytm fd67357
config: accept human-readable size for most byte-related config
kennytm c91399e
restore: reduce chance of spurious errors from TestGcTTLManagerSingle
kennytm b9f8262
Merge branch 'master' into kennytm/human-readable-size-config
glorv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Copyright 2020 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package config | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/docker/go-units" | ||
) | ||
|
||
// ByteSize is an alias of int64 which accepts human-friendly strings like | ||
// '10G' when read from TOML. | ||
type ByteSize int64 | ||
|
||
// UnmarshalText implements encoding.TextUnmarshaler | ||
func (size *ByteSize) UnmarshalText(b []byte) error { | ||
res, err := units.RAMInBytes(string(b)) | ||
if err != nil { | ||
return err | ||
} | ||
*size = ByteSize(res) | ||
return nil | ||
} | ||
|
||
// UnmarshalJSON implements json.Unmarshaler (for testing) | ||
func (size *ByteSize) UnmarshalJSON(b []byte) error { | ||
var res int64 | ||
if err := json.Unmarshal(b, &res); err != nil { | ||
return err | ||
} | ||
*size = ByteSize(res) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Copyright 2020 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package config_test | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
|
||
"github.com/BurntSushi/toml" | ||
. "github.com/pingcap/check" | ||
|
||
"github.com/pingcap/tidb-lightning/lightning/config" | ||
) | ||
|
||
type byteSizeTestSuite struct{} | ||
|
||
var _ = Suite(&byteSizeTestSuite{}) | ||
|
||
func (s *byteSizeTestSuite) TestByteSizeTOMLDecode(c *C) { | ||
testCases := []struct { | ||
input string | ||
output config.ByteSize | ||
err string | ||
}{ | ||
{ | ||
input: "x = 10000", | ||
output: 10000, | ||
}, | ||
{ | ||
input: "x = 107_374_182_400", | ||
output: 107_374_182_400, | ||
}, | ||
{ | ||
input: "x = '10k'", | ||
output: 10 * 1024, | ||
}, | ||
{ | ||
input: "x = '10PiB'", | ||
output: 10 * 1024 * 1024 * 1024 * 1024 * 1024, | ||
}, | ||
{ | ||
input: "x = '10 KB'", | ||
output: 10 * 1024, | ||
}, | ||
{ | ||
input: "x = '32768'", | ||
output: 32768, | ||
}, | ||
{ | ||
input: "x = -1", | ||
err: "invalid size: '-1'", | ||
}, | ||
{ | ||
input: "x = 'invalid value'", | ||
err: "invalid size: 'invalid value'", | ||
}, | ||
{ | ||
input: "x = true", | ||
err: "invalid size: 'true'", | ||
}, | ||
{ | ||
input: "x = 256.0", | ||
output: 256, | ||
}, | ||
{ | ||
input: "x = 256.9", | ||
output: 256, | ||
}, | ||
{ | ||
input: "x = 10e+9", | ||
output: 10_000_000_000, | ||
}, | ||
{ | ||
input: "x = '2.5MB'", | ||
output: 5 * 512 * 1024, | ||
}, | ||
{ | ||
input: "x = 2020-01-01T00:00:00Z", | ||
err: "invalid size: '2020-01-01T00:00:00Z'", | ||
}, | ||
{ | ||
input: "x = ['100000']", | ||
err: "toml: cannot load TOML value.*", | ||
}, | ||
{ | ||
input: "x = { size = '100000' }", | ||
err: "toml: cannot load TOML value.*", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
comment := Commentf("input: `%s`", tc.input) | ||
var output struct{ X config.ByteSize } | ||
err := toml.Unmarshal([]byte(tc.input), &output) | ||
if tc.err != "" { | ||
c.Assert(err, ErrorMatches, tc.err, comment) | ||
} else { | ||
c.Assert(err, IsNil, comment) | ||
c.Assert(output.X, Equals, tc.output, comment) | ||
} | ||
} | ||
} | ||
|
||
func (s *byteSizeTestSuite) TestByteSizeTOMLAndJSONEncode(c *C) { | ||
var input struct { | ||
X config.ByteSize `toml:"x" json:"x"` | ||
} | ||
input.X = 1048576 | ||
|
||
var output strings.Builder | ||
err := toml.NewEncoder(&output).Encode(input) | ||
c.Assert(err, IsNil) | ||
c.Assert(output.String(), Equals, "x = 1048576\n") | ||
|
||
js, err := json.Marshal(input) | ||
c.Assert(err, IsNil) | ||
c.Assert(string(js), Equals, `{"x":1048576}`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shall we also implement
MarshalText
andMarshalJSON
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems no need to implement the Marshal methods as the unit test passes without them.