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.

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