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.

323 lines
8.6 KiB

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