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.

317 lines
8.3 KiB

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