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.

282 lines
6.1 KiB

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