filer_buckets.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package filer
  2. import (
  3. "context"
  4. "math"
  5. "strings"
  6. "sync"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. )
  10. type BucketName string
  11. type BucketOption struct {
  12. Name BucketName
  13. Replication string
  14. fsync bool
  15. }
  16. type FilerBuckets struct {
  17. dirBucketsPath string
  18. buckets map[BucketName]*BucketOption
  19. sync.RWMutex
  20. }
  21. func (f *Filer) LoadBuckets() {
  22. f.buckets = &FilerBuckets{
  23. buckets: make(map[BucketName]*BucketOption),
  24. }
  25. limit := int64(math.MaxInt32)
  26. entries, _, err := f.ListDirectoryEntries(context.Background(), util.FullPath(f.DirBucketsPath), "", false, limit, "", "", "")
  27. if err != nil {
  28. glog.V(1).Infof("no buckets found: %v", err)
  29. return
  30. }
  31. shouldFsyncMap := make(map[string]bool)
  32. for _, bucket := range f.FsyncBuckets {
  33. shouldFsyncMap[bucket] = true
  34. }
  35. glog.V(1).Infof("buckets found: %d", len(entries))
  36. f.buckets.Lock()
  37. for _, entry := range entries {
  38. _, shouldFsnyc := shouldFsyncMap[entry.Name()]
  39. f.buckets.buckets[BucketName(entry.Name())] = &BucketOption{
  40. Name: BucketName(entry.Name()),
  41. Replication: entry.Replication,
  42. fsync: shouldFsnyc,
  43. }
  44. }
  45. f.buckets.Unlock()
  46. }
  47. func (f *Filer) ReadBucketOption(buketName string) (replication string, fsync bool) {
  48. f.buckets.RLock()
  49. defer f.buckets.RUnlock()
  50. option, found := f.buckets.buckets[BucketName(buketName)]
  51. if !found {
  52. return "", false
  53. }
  54. return option.Replication, option.fsync
  55. }
  56. func (f *Filer) isBucket(entry *Entry) bool {
  57. if !entry.IsDirectory() {
  58. return false
  59. }
  60. parent, dirName := entry.FullPath.DirAndName()
  61. if parent != f.DirBucketsPath {
  62. return false
  63. }
  64. if strings.HasPrefix(dirName, ".") {
  65. return false
  66. }
  67. f.buckets.RLock()
  68. defer f.buckets.RUnlock()
  69. _, found := f.buckets.buckets[BucketName(dirName)]
  70. return found
  71. }
  72. func (f *Filer) maybeAddBucket(entry *Entry) {
  73. if !entry.IsDirectory() {
  74. return
  75. }
  76. parent, dirName := entry.FullPath.DirAndName()
  77. if parent != f.DirBucketsPath {
  78. return
  79. }
  80. f.addBucket(dirName, &BucketOption{
  81. Name: BucketName(dirName),
  82. Replication: entry.Replication,
  83. })
  84. }
  85. func (f *Filer) addBucket(buketName string, bucketOption *BucketOption) {
  86. f.buckets.Lock()
  87. defer f.buckets.Unlock()
  88. f.buckets.buckets[BucketName(buketName)] = bucketOption
  89. }
  90. func (f *Filer) deleteBucket(buketName string) {
  91. f.buckets.Lock()
  92. defer f.buckets.Unlock()
  93. delete(f.buckets.buckets, BucketName(buketName))
  94. }