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.

310 lines
8.1 KiB

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