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.

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