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.

139 lines
3.7 KiB

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