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.

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