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.

264 lines
6.5 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. }
  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. fileIdDeletionQueue: util.NewUnboundedQueue(),
  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, o_excl bool) 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. glog.V(3).Infof("mkdir %s: %v", dirPath, mkdirErr)
  96. return fmt.Errorf("mkdir %s: %v", dirPath, mkdirErr)
  97. }
  98. } else {
  99. f.NotifyUpdateEvent(nil, dirEntry, false)
  100. }
  101. } else if !dirEntry.IsDirectory() {
  102. glog.Errorf("CreateEntry %s: %s should be a directory", entry.FullPath, dirPath)
  103. return fmt.Errorf("%s is a file", dirPath)
  104. }
  105. // cache the directory entry
  106. f.cacheSetDirectory(dirPath, dirEntry, i)
  107. // remember the direct parent directory entry
  108. if i == len(dirParts)-1 {
  109. lastDirectoryEntry = dirEntry
  110. }
  111. }
  112. if lastDirectoryEntry == nil {
  113. glog.Errorf("CreateEntry %s: lastDirectoryEntry is nil", entry.FullPath)
  114. return fmt.Errorf("parent folder not found: %v", entry.FullPath)
  115. }
  116. /*
  117. if !hasWritePermission(lastDirectoryEntry, entry) {
  118. glog.V(0).Infof("directory %s: %v, entry: uid=%d gid=%d",
  119. lastDirectoryEntry.FullPath, lastDirectoryEntry.Attr, entry.Uid, entry.Gid)
  120. return fmt.Errorf("no write permission in folder %v", lastDirectoryEntry.FullPath)
  121. }
  122. */
  123. oldEntry, _ := f.FindEntry(ctx, entry.FullPath)
  124. glog.V(4).Infof("CreateEntry %s: old entry: %v exclusive:%v", entry.FullPath, oldEntry, o_excl)
  125. if oldEntry == nil {
  126. if err := f.store.InsertEntry(ctx, entry); err != nil {
  127. glog.Errorf("insert entry %s: %v", entry.FullPath, err)
  128. return fmt.Errorf("insert entry %s: %v", entry.FullPath, err)
  129. }
  130. } else {
  131. if o_excl {
  132. glog.V(3).Infof("EEXIST: entry %s already exists", entry.FullPath)
  133. return fmt.Errorf("EEXIST: entry %s already exists", entry.FullPath)
  134. }
  135. if err := f.UpdateEntry(ctx, oldEntry, entry); err != nil {
  136. glog.Errorf("update entry %s: %v", entry.FullPath, err)
  137. return fmt.Errorf("update entry %s: %v", entry.FullPath, err)
  138. }
  139. }
  140. f.NotifyUpdateEvent(oldEntry, entry, true)
  141. f.deleteChunksIfNotNew(oldEntry, entry)
  142. glog.V(4).Infof("CreateEntry %s: created", entry.FullPath)
  143. return nil
  144. }
  145. func (f *Filer) UpdateEntry(ctx context.Context, oldEntry, entry *Entry) (err error) {
  146. if oldEntry != nil {
  147. if oldEntry.IsDirectory() && !entry.IsDirectory() {
  148. glog.Errorf("existing %s is a directory", entry.FullPath)
  149. return fmt.Errorf("existing %s is a directory", entry.FullPath)
  150. }
  151. if !oldEntry.IsDirectory() && entry.IsDirectory() {
  152. glog.Errorf("existing %s is a file", entry.FullPath)
  153. return fmt.Errorf("existing %s is a file", entry.FullPath)
  154. }
  155. }
  156. return f.store.UpdateEntry(ctx, entry)
  157. }
  158. func (f *Filer) FindEntry(ctx context.Context, p FullPath) (entry *Entry, err error) {
  159. now := time.Now()
  160. if string(p) == "/" {
  161. return &Entry{
  162. FullPath: p,
  163. Attr: Attr{
  164. Mtime: now,
  165. Crtime: now,
  166. Mode: os.ModeDir | 0755,
  167. Uid: OS_UID,
  168. Gid: OS_GID,
  169. },
  170. }, nil
  171. }
  172. return f.store.FindEntry(ctx, p)
  173. }
  174. func (f *Filer) ListDirectoryEntries(ctx context.Context, p FullPath, startFileName string, inclusive bool, limit int) ([]*Entry, error) {
  175. if strings.HasSuffix(string(p), "/") && len(p) > 1 {
  176. p = p[0 : len(p)-1]
  177. }
  178. return f.store.ListDirectoryEntries(ctx, p, startFileName, inclusive, limit)
  179. }
  180. func (f *Filer) cacheDelDirectory(dirpath string) {
  181. if dirpath == "/" {
  182. return
  183. }
  184. if f.directoryCache == nil {
  185. return
  186. }
  187. f.directoryCache.Delete(dirpath)
  188. return
  189. }
  190. func (f *Filer) cacheGetDirectory(dirpath string) *Entry {
  191. if f.directoryCache == nil {
  192. return nil
  193. }
  194. item := f.directoryCache.Get(dirpath)
  195. if item == nil {
  196. return nil
  197. }
  198. return item.Value().(*Entry)
  199. }
  200. func (f *Filer) cacheSetDirectory(dirpath string, dirEntry *Entry, level int) {
  201. if f.directoryCache == nil {
  202. return
  203. }
  204. minutes := 60
  205. if level < 10 {
  206. minutes -= level * 6
  207. }
  208. f.directoryCache.Set(dirpath, dirEntry, time.Duration(minutes)*time.Minute)
  209. }