This repository has been archived by the owner on Dec 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 773
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add supernode disk gc feature
Signed-off-by: Starnop <[email protected]> Signed-off-by: Starnop <[email protected]>
- Loading branch information
Showing
21 changed files
with
664 additions
and
28 deletions.
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
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,117 @@ | ||
/* | ||
* Copyright The Dragonfly Authors. | ||
* | ||
* 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, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package fileutils | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
|
||
"github.com/dragonflyoss/Dragonfly/pkg/errortypes" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// Fsize is a wrapper type which indicates the file size. | ||
type Fsize int64 | ||
|
||
const ( | ||
B Fsize = 1 | ||
KB = 1024 * B | ||
MB = 1024 * KB | ||
GB = 1024 * MB | ||
) | ||
|
||
// fsizeRegex only supports the format G(B)/M(B)/K(B)/B or pure number. | ||
var fsizeRegex = regexp.MustCompile("^([0-9]+)([GMK]B?|B)$") | ||
|
||
// MarshalYAML implements the yaml.Marshaler interface. | ||
func (f Fsize) MarshalYAML() (interface{}, error) { | ||
result := FsizeToString(f) | ||
return result, nil | ||
} | ||
|
||
// UnmarshalYAML implements the yaml.Unmarshaler interface. | ||
func (f *Fsize) UnmarshalYAML(unmarshal func(interface{}) error) error { | ||
var fsizeStr string | ||
if err := unmarshal(&fsizeStr); err != nil { | ||
return err | ||
} | ||
|
||
fsize, err := StringToFSize(fsizeStr) | ||
if err != nil { | ||
return err | ||
} | ||
*f = Fsize(fsize) | ||
return nil | ||
} | ||
|
||
// FsizeToString parses a Fsize value into string. | ||
func FsizeToString(fsize Fsize) string { | ||
var ( | ||
n = int64(fsize) | ||
symbol = "B" | ||
unit = B | ||
) | ||
if n == 0 { | ||
return "0B" | ||
} | ||
|
||
switch int64(0) { | ||
case n % int64(GB): | ||
symbol = "GB" | ||
unit = GB | ||
case n % int64(MB): | ||
symbol = "MB" | ||
unit = MB | ||
case n % int64(KB): | ||
symbol = "KB" | ||
unit = KB | ||
} | ||
return fmt.Sprintf("%v%v", n/int64(unit), symbol) | ||
} | ||
|
||
// StringToFSize parses a string into Fsize. | ||
func StringToFSize(fsize string) (Fsize, error) { | ||
var n int | ||
n, err := strconv.Atoi(fsize) | ||
if err == nil && n >= 0 { | ||
return Fsize(n), nil | ||
} | ||
if n < 0 { | ||
return 0, errors.Wrapf(errortypes.ErrInvalidValue, "%s is not a negative value fsize", fsize) | ||
} | ||
|
||
matches := fsizeRegex.FindStringSubmatch(fsize) | ||
if len(matches) != 3 { | ||
return 0, errors.Wrapf(errortypes.ErrInvalidValue, "%s and supported format: G(B)/M(B)/K(B)/B or pure number", fsize) | ||
} | ||
n, _ = strconv.Atoi(matches[1]) | ||
switch unit := matches[2]; { | ||
case unit == "G" || unit == "GB": | ||
n *= int(GB) | ||
case unit == "M" || unit == "MB": | ||
n *= int(MB) | ||
case unit == "K" || unit == "KB": | ||
n *= int(KB) | ||
case unit == "B": | ||
// Value already correct | ||
default: | ||
return 0, errors.Wrapf(errortypes.ErrInvalidValue, "%s and supported format: G(B)/M(B)/K(B)/B or pure number", fsize) | ||
} | ||
return Fsize(n), 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,111 @@ | ||
/* | ||
* Copyright The Dragonfly Authors. | ||
* | ||
* 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, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package fileutils | ||
|
||
import ( | ||
"github.com/dragonflyoss/Dragonfly/pkg/errortypes" | ||
|
||
"github.com/go-check/check" | ||
"gopkg.in/yaml.v2" | ||
) | ||
|
||
type FsizeTestSuite struct { | ||
tmpDir string | ||
username string | ||
} | ||
|
||
func init() { | ||
check.Suite(&FsizeTestSuite{}) | ||
} | ||
|
||
func (suite *FsizeTestSuite) TestFsizeToString(c *check.C) { | ||
var cases = []struct { | ||
fsize Fsize | ||
fsizeStr string | ||
}{ | ||
{B, "1B"}, | ||
{1024 * B, "1KB"}, | ||
{3 * MB, "3MB"}, | ||
{0 * GB, "0B"}, | ||
} | ||
|
||
for _, ca := range cases { | ||
result := FsizeToString(ca.fsize) | ||
c.Assert(result, check.Equals, ca.fsizeStr) | ||
} | ||
} | ||
|
||
func (suite *FsizeTestSuite) TestStringToFSize(c *check.C) { | ||
var cases = []struct { | ||
fsizeStr string | ||
fsize Fsize | ||
errAssertFunc errortypes.ErrAssertFunc | ||
}{ | ||
{"0B", 0 * B, errortypes.IsNilError}, | ||
{"1B", B, errortypes.IsNilError}, | ||
{"10G", 10 * GB, errortypes.IsNilError}, | ||
{"1024", 1 * KB, errortypes.IsNilError}, | ||
{"-1", 0, errortypes.IsInvalidValue}, | ||
{"10b", 0, errortypes.IsInvalidValue}, | ||
} | ||
|
||
for _, ca := range cases { | ||
result, err := StringToFSize(ca.fsizeStr) | ||
c.Assert(ca.errAssertFunc(err), check.Equals, true) | ||
c.Assert(result, check.DeepEquals, ca.fsize) | ||
} | ||
} | ||
|
||
func (suite *FsizeTestSuite) TestMarshalYAML(c *check.C) { | ||
var cases = []struct { | ||
input Fsize | ||
output string | ||
}{ | ||
{5 * MB, "5MB\n"}, | ||
{1 * GB, "1GB\n"}, | ||
{1 * KB, "1KB\n"}, | ||
{1 * B, "1B\n"}, | ||
{0, "0B\n"}, | ||
} | ||
|
||
for _, ca := range cases { | ||
output, err := yaml.Marshal(ca.input) | ||
c.Check(err, check.IsNil) | ||
c.Check(string(output), check.Equals, ca.output) | ||
} | ||
} | ||
|
||
func (suite *FsizeTestSuite) TestUnMarshalYAML(c *check.C) { | ||
var cases = []struct { | ||
input string | ||
output Fsize | ||
}{ | ||
{"5M\n", 5 * MB}, | ||
{"1G\n", 1 * GB}, | ||
{"1B\n", 1 * B}, | ||
{"1\n", 1 * B}, | ||
{"1024\n", 1 * KB}, | ||
{"1K\n", 1 * KB}, | ||
} | ||
|
||
for _, ca := range cases { | ||
var output Fsize | ||
err := yaml.Unmarshal([]byte(ca.input), &output) | ||
c.Check(err, check.IsNil) | ||
c.Check(output, check.Equals, ca.output) | ||
} | ||
} |
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
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.