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.

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