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.

270 lines
6.0 KiB

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