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.

273 lines
6.2 KiB

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