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.

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