You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.8 KiB

3 months ago
3 years ago
  1. package shell
  2. import (
  3. "bytes"
  4. "flag"
  5. "fmt"
  6. "github.com/seaweedfs/seaweedfs/weed/filer"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  8. "io"
  9. "math"
  10. )
  11. func init() {
  12. Commands = append(Commands, &commandS3BucketQuotaEnforce{})
  13. }
  14. type commandS3BucketQuotaEnforce struct {
  15. }
  16. func (c *commandS3BucketQuotaEnforce) Name() string {
  17. return "s3.bucket.quota.enforce"
  18. }
  19. func (c *commandS3BucketQuotaEnforce) Help() string {
  20. return `check quota for all buckets, make the bucket read only if over the limit
  21. Example:
  22. s3.bucket.quota.enforce -apply
  23. `
  24. }
  25. func (c *commandS3BucketQuotaEnforce) HasTag(CommandTag) bool {
  26. return false
  27. }
  28. func (c *commandS3BucketQuotaEnforce) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  29. bucketCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  30. applyQuotaLimit := bucketCommand.Bool("apply", false, "actually change the buckets readonly attribute")
  31. if err = bucketCommand.Parse(args); err != nil {
  32. return nil
  33. }
  34. infoAboutSimulationMode(writer, *applyQuotaLimit, "-apply")
  35. // collect collection information
  36. topologyInfo, _, err := collectTopologyInfo(commandEnv, 0)
  37. if err != nil {
  38. return err
  39. }
  40. collectionInfos := make(map[string]*CollectionInfo)
  41. collectCollectionInfo(topologyInfo, collectionInfos)
  42. // read buckets path
  43. var filerBucketsPath string
  44. filerBucketsPath, err = readFilerBucketsPath(commandEnv)
  45. if err != nil {
  46. return fmt.Errorf("read buckets: %v", err)
  47. }
  48. // read existing filer configuration
  49. fc, err := filer.ReadFilerConf(commandEnv.option.FilerAddress, commandEnv.option.GrpcDialOption, commandEnv.MasterClient)
  50. if err != nil {
  51. return err
  52. }
  53. // process each bucket
  54. hasConfChanges := false
  55. err = filer_pb.List(commandEnv, filerBucketsPath, "", func(entry *filer_pb.Entry, isLast bool) error {
  56. if !entry.IsDirectory {
  57. return nil
  58. }
  59. collection := getCollectionName(commandEnv, entry.Name)
  60. var collectionSize float64
  61. if collectionInfo, found := collectionInfos[collection]; found {
  62. collectionSize = collectionInfo.Size
  63. }
  64. if c.processEachBucket(fc, filerBucketsPath, entry, writer, collectionSize) {
  65. hasConfChanges = true
  66. }
  67. return nil
  68. }, "", false, math.MaxUint32)
  69. if err != nil {
  70. return fmt.Errorf("list buckets under %v: %v", filerBucketsPath, err)
  71. }
  72. // apply the configuration changes
  73. if hasConfChanges && *applyQuotaLimit {
  74. var buf2 bytes.Buffer
  75. fc.ToText(&buf2)
  76. if err = commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  77. return filer.SaveInsideFiler(client, filer.DirectoryEtcSeaweedFS, filer.FilerConfName, buf2.Bytes())
  78. }); err != nil && err != filer_pb.ErrNotFound {
  79. return err
  80. }
  81. }
  82. return err
  83. }
  84. func (c *commandS3BucketQuotaEnforce) processEachBucket(fc *filer.FilerConf, filerBucketsPath string, entry *filer_pb.Entry, writer io.Writer, collectionSize float64) (hasConfChanges bool) {
  85. locPrefix := filerBucketsPath + "/" + entry.Name + "/"
  86. locConf := fc.MatchStorageRule(locPrefix)
  87. locConf.LocationPrefix = locPrefix
  88. if entry.Quota > 0 {
  89. if locConf.ReadOnly {
  90. if collectionSize < float64(entry.Quota) {
  91. locConf.ReadOnly = false
  92. hasConfChanges = true
  93. }
  94. } else {
  95. if collectionSize > float64(entry.Quota) {
  96. locConf.ReadOnly = true
  97. hasConfChanges = true
  98. }
  99. }
  100. } else {
  101. if locConf.ReadOnly {
  102. locConf.ReadOnly = false
  103. hasConfChanges = true
  104. }
  105. }
  106. if hasConfChanges {
  107. fmt.Fprintf(writer, " %s\tsize:%.0f", entry.Name, collectionSize)
  108. fmt.Fprintf(writer, "\tquota:%d\tusage:%.2f%%", entry.Quota, collectionSize*100/float64(entry.Quota))
  109. fmt.Fprintln(writer)
  110. if locConf.ReadOnly {
  111. fmt.Fprintf(writer, " changing bucket %s to read only!\n", entry.Name)
  112. } else {
  113. fmt.Fprintf(writer, " changing bucket %s to writable.\n", entry.Name)
  114. }
  115. fc.SetLocationConf(locConf)
  116. }
  117. return
  118. }