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