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.

247 lines
5.6 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package filer2
  2. import (
  3. "fmt"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/operation"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "github.com/karlseguin/ccache"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "time"
  12. "github.com/chrislusf/seaweedfs/weed/wdclient"
  13. "context"
  14. )
  15. type Filer struct {
  16. store FilerStore
  17. directoryCache *ccache.Cache
  18. MasterClient *wdclient.MasterClient
  19. }
  20. func NewFiler(masters []string) *Filer {
  21. return &Filer{
  22. directoryCache: ccache.New(ccache.Configure().MaxSize(1000).ItemsToPrune(100)),
  23. MasterClient: wdclient.NewMasterClient(context.Background(), "filer", masters),
  24. }
  25. }
  26. func (f *Filer) SetStore(store FilerStore) {
  27. f.store = store
  28. }
  29. func (f *Filer) DisableDirectoryCache() {
  30. f.directoryCache = nil
  31. }
  32. func (fs *Filer) GetMaster() string {
  33. return fs.MasterClient.GetMaster()
  34. }
  35. func (fs *Filer) KeepConnectedToMaster() {
  36. fs.MasterClient.KeepConnectedToMaster()
  37. }
  38. func (f *Filer) CreateEntry(entry *Entry) error {
  39. dirParts := strings.Split(string(entry.FullPath), "/")
  40. // fmt.Printf("directory parts: %+v\n", dirParts)
  41. var lastDirectoryEntry *Entry
  42. for i := 1; i < len(dirParts); i++ {
  43. dirPath := "/" + filepath.Join(dirParts[:i]...)
  44. // fmt.Printf("%d directory: %+v\n", i, dirPath)
  45. // first check local cache
  46. dirEntry := f.cacheGetDirectory(dirPath)
  47. // not found, check the store directly
  48. if dirEntry == nil {
  49. glog.V(4).Infof("find uncached directory: %s", dirPath)
  50. dirEntry, _ = f.FindEntry(FullPath(dirPath))
  51. } else {
  52. glog.V(4).Infof("found cached directory: %s", dirPath)
  53. }
  54. // no such existing directory
  55. if dirEntry == nil {
  56. // create the directory
  57. now := time.Now()
  58. dirEntry = &Entry{
  59. FullPath: FullPath(dirPath),
  60. Attr: Attr{
  61. Mtime: now,
  62. Crtime: now,
  63. Mode: os.ModeDir | 0770,
  64. Uid: entry.Uid,
  65. Gid: entry.Gid,
  66. },
  67. }
  68. glog.V(2).Infof("create directory: %s %v", dirPath, dirEntry.Mode)
  69. mkdirErr := f.store.InsertEntry(dirEntry)
  70. if mkdirErr != nil {
  71. return fmt.Errorf("mkdir %s: %v", dirPath, mkdirErr)
  72. }
  73. } else if !dirEntry.IsDirectory() {
  74. return fmt.Errorf("%s is a file", dirPath)
  75. }
  76. // cache the directory entry
  77. f.cacheSetDirectory(dirPath, dirEntry, i)
  78. // remember the direct parent directory entry
  79. if i == len(dirParts)-1 {
  80. lastDirectoryEntry = dirEntry
  81. }
  82. }
  83. if lastDirectoryEntry == nil {
  84. return fmt.Errorf("parent folder not found: %v", entry.FullPath)
  85. }
  86. /*
  87. if !hasWritePermission(lastDirectoryEntry, entry) {
  88. glog.V(0).Infof("directory %s: %v, entry: uid=%d gid=%d",
  89. lastDirectoryEntry.FullPath, lastDirectoryEntry.Attr, entry.Uid, entry.Gid)
  90. return fmt.Errorf("no write permission in folder %v", lastDirectoryEntry.FullPath)
  91. }
  92. */
  93. oldEntry, _ := f.FindEntry(entry.FullPath)
  94. if err := f.store.InsertEntry(entry); err != nil {
  95. return fmt.Errorf("insert entry %s: %v", entry.FullPath, err)
  96. }
  97. f.deleteChunksIfNotNew(oldEntry, entry)
  98. return nil
  99. }
  100. func (f *Filer) UpdateEntry(entry *Entry) (err error) {
  101. return f.store.UpdateEntry(entry)
  102. }
  103. func (f *Filer) FindEntry(p FullPath) (entry *Entry, err error) {
  104. return f.store.FindEntry(p)
  105. }
  106. func (f *Filer) DeleteEntryMetaAndData(p FullPath, isRecursive bool, shouldDeleteChunks bool) (err error) {
  107. entry, err := f.FindEntry(p)
  108. if err != nil {
  109. return err
  110. }
  111. if entry.IsDirectory() {
  112. entries, err := f.ListDirectoryEntries(p, "", false, 1)
  113. if err != nil {
  114. return fmt.Errorf("list folder %s: %v", p, err)
  115. }
  116. if isRecursive {
  117. for _, sub := range entries {
  118. f.DeleteEntryMetaAndData(sub.FullPath, isRecursive, shouldDeleteChunks)
  119. }
  120. } else {
  121. if len(entries) > 0 {
  122. return fmt.Errorf("folder %s is not empty", p)
  123. }
  124. }
  125. f.cacheDelDirectory(string(p))
  126. }
  127. if shouldDeleteChunks {
  128. f.deleteChunks(entry.Chunks)
  129. }
  130. return f.store.DeleteEntry(p)
  131. }
  132. func (f *Filer) ListDirectoryEntries(p FullPath, startFileName string, inclusive bool, limit int) ([]*Entry, error) {
  133. if strings.HasSuffix(string(p), "/") && len(p) > 1 {
  134. p = p[0 : len(p)-1]
  135. }
  136. return f.store.ListDirectoryEntries(p, startFileName, inclusive, limit)
  137. }
  138. func (f *Filer) cacheDelDirectory(dirpath string) {
  139. if f.directoryCache == nil {
  140. return
  141. }
  142. f.directoryCache.Delete(dirpath)
  143. return
  144. }
  145. func (f *Filer) cacheGetDirectory(dirpath string) *Entry {
  146. if f.directoryCache == nil {
  147. return nil
  148. }
  149. item := f.directoryCache.Get(dirpath)
  150. if item == nil {
  151. return nil
  152. }
  153. return item.Value().(*Entry)
  154. }
  155. func (f *Filer) cacheSetDirectory(dirpath string, dirEntry *Entry, level int) {
  156. if f.directoryCache == nil {
  157. return
  158. }
  159. minutes := 60
  160. if level < 10 {
  161. minutes -= level * 6
  162. }
  163. f.directoryCache.Set(dirpath, dirEntry, time.Duration(minutes)*time.Minute)
  164. }
  165. func (f *Filer) deleteChunks(chunks []*filer_pb.FileChunk) {
  166. for _, chunk := range chunks {
  167. f.DeleteFileByFileId(chunk.FileId)
  168. }
  169. }
  170. func (f *Filer) DeleteFileByFileId(fileId string) {
  171. fileUrlOnVolume, err := f.MasterClient.LookupFileId(fileId)
  172. if err != nil {
  173. glog.V(0).Infof("can not find file %s: %v", fileId, err)
  174. }
  175. if err := operation.DeleteFromVolumeServer(fileUrlOnVolume, ""); err != nil {
  176. glog.V(0).Infof("deleting file %s: %v", fileId, err)
  177. }
  178. }
  179. func (f *Filer) deleteChunksIfNotNew(oldEntry, newEntry *Entry) {
  180. if oldEntry == nil {
  181. return
  182. }
  183. if newEntry == nil {
  184. f.deleteChunks(oldEntry.Chunks)
  185. }
  186. var toDelete []*filer_pb.FileChunk
  187. for _, oldChunk := range oldEntry.Chunks {
  188. found := false
  189. for _, newChunk := range newEntry.Chunks {
  190. if oldChunk.FileId == newChunk.FileId {
  191. found = true
  192. break
  193. }
  194. }
  195. if !found {
  196. toDelete = append(toDelete, oldChunk)
  197. }
  198. }
  199. f.deleteChunks(toDelete)
  200. }