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.

271 lines
6.8 KiB

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