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.

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