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.

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