minfreespace.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package util
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. "strconv"
  7. "strings"
  8. )
  9. // MinFreeSpaceType is the type of MinFreeSpace.
  10. type MinFreeSpaceType int
  11. const (
  12. // AsPercent set the MinFreeSpaceType to a percentage value from 0 to 100.
  13. AsPercent MinFreeSpaceType = iota
  14. // AsBytes set the MinFreeSpaceType to a absolute value bytes.
  15. AsBytes
  16. )
  17. // MinFreeSpace is type that defines the limit for the minimum free space.
  18. type MinFreeSpace struct {
  19. Type MinFreeSpaceType
  20. Bytes uint64
  21. Percent float32
  22. Raw string
  23. }
  24. // IsLow tells whether the free space is low or not.
  25. func (s MinFreeSpace) IsLow(freeBytes uint64, freePercent float32) (yes bool, desc string) {
  26. switch s.Type {
  27. case AsPercent:
  28. yes = freePercent < s.Percent
  29. op := IfElse(yes, "<", ">=")
  30. return yes, fmt.Sprintf("disk free %.2f%% %s required %.2f%%", freePercent, op, s.Percent)
  31. case AsBytes:
  32. yes = freeBytes < s.Bytes
  33. op := IfElse(yes, "<", ">=")
  34. return yes, fmt.Sprintf("disk free %s %s required %s",
  35. BytesToHumanReadable(freeBytes), op, BytesToHumanReadable(s.Bytes))
  36. }
  37. return false, ""
  38. }
  39. // String returns a string representation of MinFreeSpace.
  40. func (s MinFreeSpace) String() string {
  41. switch s.Type {
  42. case AsPercent:
  43. return fmt.Sprintf("%.2f%%", s.Percent)
  44. default:
  45. return s.Raw
  46. }
  47. }
  48. // MustParseMinFreeSpace parses comma-separated argument for min free space setting.
  49. // minFreeSpace has the high priority than minFreeSpacePercent if it is set.
  50. func MustParseMinFreeSpace(minFreeSpace string, minFreeSpacePercent string) (spaces []MinFreeSpace) {
  51. ss := strings.Split(EmptyTo(minFreeSpace, minFreeSpacePercent), ",")
  52. for _, freeString := range ss {
  53. if vv, e := ParseMinFreeSpace(freeString); e == nil {
  54. spaces = append(spaces, *vv)
  55. } else {
  56. glog.Fatalf("The value specified in -minFreeSpace not a valid value %s", freeString)
  57. }
  58. }
  59. return spaces
  60. }
  61. var ErrMinFreeSpaceBadValue = errors.New("minFreeSpace is invalid")
  62. // ParseMinFreeSpace parses min free space expression s as percentage like 1,10 or human readable size like 10G
  63. func ParseMinFreeSpace(s string) (*MinFreeSpace, error) {
  64. if percent, e := strconv.ParseFloat(s, 32); e == nil {
  65. if percent < 0 || percent > 100 {
  66. return nil, ErrMinFreeSpaceBadValue
  67. }
  68. return &MinFreeSpace{Type: AsPercent, Percent: float32(percent), Raw: s}, nil
  69. }
  70. if directSize, e := ParseBytes(s); e == nil {
  71. if directSize <= 100 {
  72. return nil, ErrMinFreeSpaceBadValue
  73. }
  74. return &MinFreeSpace{Type: AsBytes, Bytes: directSize, Raw: s}, nil
  75. }
  76. return nil, ErrMinFreeSpaceBadValue
  77. }