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.

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