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.

196 lines
6.5 KiB

7 years ago
7 years ago
5 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
4 years ago
  1. package filer2
  2. import (
  3. "context"
  4. "strings"
  5. "time"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "github.com/chrislusf/seaweedfs/weed/stats"
  8. "github.com/chrislusf/seaweedfs/weed/util"
  9. )
  10. type FilerStore interface {
  11. // GetName gets the name to locate the configuration in filer.toml file
  12. GetName() string
  13. // Initialize initializes the file store
  14. Initialize(configuration util.Configuration, prefix string) error
  15. InsertEntry(context.Context, *Entry) error
  16. UpdateEntry(context.Context, *Entry) (err error)
  17. // err == filer2.ErrNotFound if not found
  18. FindEntry(context.Context, util.FullPath) (entry *Entry, err error)
  19. DeleteEntry(context.Context, util.FullPath) (err error)
  20. DeleteFolderChildren(context.Context, util.FullPath) (err error)
  21. ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int) ([]*Entry, error)
  22. ListDirectoryPrefixedEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int, prefix string) ([]*Entry, error)
  23. BeginTransaction(ctx context.Context) (context.Context, error)
  24. CommitTransaction(ctx context.Context) error
  25. RollbackTransaction(ctx context.Context) error
  26. Shutdown()
  27. }
  28. type FilerLocalStore interface {
  29. UpdateOffset(filer string, lastTsNs int64) error
  30. ReadOffset(filer string) (lastTsNs int64, err error)
  31. }
  32. type FilerStoreWrapper struct {
  33. ActualStore FilerStore
  34. }
  35. func NewFilerStoreWrapper(store FilerStore) *FilerStoreWrapper {
  36. if innerStore, ok := store.(*FilerStoreWrapper); ok {
  37. return innerStore
  38. }
  39. return &FilerStoreWrapper{
  40. ActualStore: store,
  41. }
  42. }
  43. func (fsw *FilerStoreWrapper) GetName() string {
  44. return fsw.ActualStore.GetName()
  45. }
  46. func (fsw *FilerStoreWrapper) Initialize(configuration util.Configuration, prefix string) error {
  47. return fsw.ActualStore.Initialize(configuration, prefix)
  48. }
  49. func (fsw *FilerStoreWrapper) InsertEntry(ctx context.Context, entry *Entry) error {
  50. stats.FilerStoreCounter.WithLabelValues(fsw.ActualStore.GetName(), "insert").Inc()
  51. start := time.Now()
  52. defer func() {
  53. stats.FilerStoreHistogram.WithLabelValues(fsw.ActualStore.GetName(), "insert").Observe(time.Since(start).Seconds())
  54. }()
  55. filer_pb.BeforeEntrySerialization(entry.Chunks)
  56. return fsw.ActualStore.InsertEntry(ctx, entry)
  57. }
  58. func (fsw *FilerStoreWrapper) UpdateEntry(ctx context.Context, entry *Entry) error {
  59. stats.FilerStoreCounter.WithLabelValues(fsw.ActualStore.GetName(), "update").Inc()
  60. start := time.Now()
  61. defer func() {
  62. stats.FilerStoreHistogram.WithLabelValues(fsw.ActualStore.GetName(), "update").Observe(time.Since(start).Seconds())
  63. }()
  64. filer_pb.BeforeEntrySerialization(entry.Chunks)
  65. return fsw.ActualStore.UpdateEntry(ctx, entry)
  66. }
  67. func (fsw *FilerStoreWrapper) FindEntry(ctx context.Context, fp util.FullPath) (entry *Entry, err error) {
  68. stats.FilerStoreCounter.WithLabelValues(fsw.ActualStore.GetName(), "find").Inc()
  69. start := time.Now()
  70. defer func() {
  71. stats.FilerStoreHistogram.WithLabelValues(fsw.ActualStore.GetName(), "find").Observe(time.Since(start).Seconds())
  72. }()
  73. entry, err = fsw.ActualStore.FindEntry(ctx, fp)
  74. if err != nil {
  75. return nil, err
  76. }
  77. filer_pb.AfterEntryDeserialization(entry.Chunks)
  78. return
  79. }
  80. func (fsw *FilerStoreWrapper) DeleteEntry(ctx context.Context, fp util.FullPath) (err error) {
  81. stats.FilerStoreCounter.WithLabelValues(fsw.ActualStore.GetName(), "delete").Inc()
  82. start := time.Now()
  83. defer func() {
  84. stats.FilerStoreHistogram.WithLabelValues(fsw.ActualStore.GetName(), "delete").Observe(time.Since(start).Seconds())
  85. }()
  86. return fsw.ActualStore.DeleteEntry(ctx, fp)
  87. }
  88. func (fsw *FilerStoreWrapper) DeleteFolderChildren(ctx context.Context, fp util.FullPath) (err error) {
  89. stats.FilerStoreCounter.WithLabelValues(fsw.ActualStore.GetName(), "deleteFolderChildren").Inc()
  90. start := time.Now()
  91. defer func() {
  92. stats.FilerStoreHistogram.WithLabelValues(fsw.ActualStore.GetName(), "deleteFolderChildren").Observe(time.Since(start).Seconds())
  93. }()
  94. return fsw.ActualStore.DeleteFolderChildren(ctx, fp)
  95. }
  96. func (fsw *FilerStoreWrapper) ListDirectoryEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int) ([]*Entry, error) {
  97. stats.FilerStoreCounter.WithLabelValues(fsw.ActualStore.GetName(), "list").Inc()
  98. start := time.Now()
  99. defer func() {
  100. stats.FilerStoreHistogram.WithLabelValues(fsw.ActualStore.GetName(), "list").Observe(time.Since(start).Seconds())
  101. }()
  102. entries, err := fsw.ActualStore.ListDirectoryEntries(ctx, dirPath, startFileName, includeStartFile, limit)
  103. if err != nil {
  104. return nil, err
  105. }
  106. for _, entry := range entries {
  107. filer_pb.AfterEntryDeserialization(entry.Chunks)
  108. }
  109. return entries, err
  110. }
  111. func (fsw *FilerStoreWrapper) ListDirectoryPrefixedEntries(ctx context.Context, dirPath util.FullPath, startFileName string, includeStartFile bool, limit int, prefix string) ([]*Entry, error) {
  112. stats.FilerStoreCounter.WithLabelValues(fsw.ActualStore.GetName(), "list").Inc()
  113. start := time.Now()
  114. defer func() {
  115. stats.FilerStoreHistogram.WithLabelValues(fsw.ActualStore.GetName(), "list").Observe(time.Since(start).Seconds())
  116. }()
  117. entries, err := fsw.ActualStore.ListDirectoryPrefixedEntries(ctx, dirPath, startFileName, includeStartFile, limit, prefix)
  118. if err.Error() == UnsupportedListDirectoryPrefixedErr.Error() {
  119. count := 0
  120. notPrefixed, err := fsw.ActualStore.ListDirectoryEntries(ctx, dirPath, startFileName, includeStartFile, limit)
  121. if err != nil {
  122. return nil, err
  123. }
  124. if prefix == "" {
  125. entries = notPrefixed
  126. } else {
  127. var lastFileName string
  128. for count < limit {
  129. for _, entry := range notPrefixed {
  130. lastFileName = entry.Name()
  131. if strings.HasPrefix(entry.Name(), prefix) {
  132. count++
  133. entries = append(entries, entry)
  134. }
  135. }
  136. if count >= limit {
  137. break
  138. }
  139. notPrefixed, err = fsw.ActualStore.ListDirectoryEntries(ctx, dirPath, lastFileName, includeStartFile, limit)
  140. if err != nil {
  141. return nil, err
  142. }
  143. if len(notPrefixed) == 0 {
  144. break
  145. }
  146. }
  147. }
  148. } else if err != nil {
  149. return nil, err
  150. }
  151. for _, entry := range entries {
  152. filer_pb.AfterEntryDeserialization(entry.Chunks)
  153. }
  154. return entries, nil
  155. }
  156. func (fsw *FilerStoreWrapper) BeginTransaction(ctx context.Context) (context.Context, error) {
  157. return fsw.ActualStore.BeginTransaction(ctx)
  158. }
  159. func (fsw *FilerStoreWrapper) CommitTransaction(ctx context.Context) error {
  160. return fsw.ActualStore.CommitTransaction(ctx)
  161. }
  162. func (fsw *FilerStoreWrapper) RollbackTransaction(ctx context.Context) error {
  163. return fsw.ActualStore.RollbackTransaction(ctx)
  164. }
  165. func (fsw *FilerStoreWrapper) Shutdown() {
  166. fsw.ActualStore.Shutdown()
  167. }