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.

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