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.

318 lines
8.3 KiB

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