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.

136 lines
3.6 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, 0)
  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. if err = commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  73. return filer.SaveInsideFiler(client, filer.DirectoryEtcSeaweedFS, filer.FilerConfName, buf2.Bytes())
  74. }); err != nil && err != filer_pb.ErrNotFound {
  75. return err
  76. }
  77. }
  78. return err
  79. }
  80. func (c *commandS3BucketQuotaEnforce) processEachBucket(fc *filer.FilerConf, filerBucketsPath string, entry *filer_pb.Entry, writer io.Writer, collectionSize uint64) (hasConfChanges bool) {
  81. locPrefix := filerBucketsPath + "/" + entry.Name + "/"
  82. locConf := fc.MatchStorageRule(locPrefix)
  83. locConf.LocationPrefix = locPrefix
  84. if entry.Quota > 0 {
  85. if locConf.ReadOnly {
  86. if collectionSize < uint64(entry.Quota) {
  87. locConf.ReadOnly = false
  88. hasConfChanges = true
  89. }
  90. } else {
  91. if collectionSize > uint64(entry.Quota) {
  92. locConf.ReadOnly = true
  93. hasConfChanges = true
  94. }
  95. }
  96. } else {
  97. if locConf.ReadOnly {
  98. locConf.ReadOnly = false
  99. hasConfChanges = true
  100. }
  101. }
  102. if hasConfChanges {
  103. fmt.Fprintf(writer, " %s\tsize:%d", entry.Name, collectionSize)
  104. fmt.Fprintf(writer, "\tquota:%d\tusage:%.2f%%", entry.Quota, float64(collectionSize)*100/float64(entry.Quota))
  105. fmt.Fprintln(writer)
  106. if locConf.ReadOnly {
  107. fmt.Fprintf(writer, " changing bucket %s to read only!\n", entry.Name)
  108. } else {
  109. fmt.Fprintf(writer, " changing bucket %s to writable.\n", entry.Name)
  110. }
  111. fc.AddLocationConf(locConf)
  112. }
  113. return
  114. }