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.

123 lines
3.2 KiB

6 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
7 years ago
5 years ago
5 years ago
  1. package filer2
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "github.com/golang/protobuf/proto"
  7. "github.com/chrislusf/seaweedfs/weed/glog"
  8. "github.com/chrislusf/seaweedfs/weed/notification"
  9. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  10. "github.com/chrislusf/seaweedfs/weed/util"
  11. )
  12. func (f *Filer) NotifyUpdateEvent(oldEntry, newEntry *Entry, deleteChunks bool) {
  13. var fullpath string
  14. if oldEntry != nil {
  15. fullpath = string(oldEntry.FullPath)
  16. } else if newEntry != nil {
  17. fullpath = string(newEntry.FullPath)
  18. } else {
  19. return
  20. }
  21. // println("fullpath:", fullpath)
  22. if strings.HasPrefix(fullpath, "/.meta") {
  23. return
  24. }
  25. newParentPath := ""
  26. if newEntry != nil {
  27. newParentPath, _ = newEntry.FullPath.DirAndName()
  28. }
  29. eventNotification := &filer_pb.EventNotification{
  30. OldEntry: oldEntry.ToProtoEntry(),
  31. NewEntry: newEntry.ToProtoEntry(),
  32. DeleteChunks: deleteChunks,
  33. NewParentPath: newParentPath,
  34. }
  35. if notification.Queue != nil {
  36. glog.V(3).Infof("notifying entry update %v", fullpath)
  37. notification.Queue.SendMessage(fullpath, eventNotification)
  38. }
  39. if false {
  40. f.logMetaEvent(time.Now(), fullpath, eventNotification)
  41. }
  42. }
  43. func (f *Filer) logMetaEvent(ts time.Time, fullpath string, eventNotification *filer_pb.EventNotification) {
  44. dir, _ := util.FullPath(fullpath).DirAndName()
  45. event := &filer_pb.FullEventNotification{
  46. Directory: dir,
  47. EventNotification: eventNotification,
  48. }
  49. data, err := proto.Marshal(event)
  50. if err != nil {
  51. glog.Errorf("failed to marshal filer_pb.FullEventNotification %+v: %v", event, err)
  52. return
  53. }
  54. f.metaLogBuffer.AddToBuffer(ts, []byte(dir), data)
  55. }
  56. func (f *Filer) logFlushFunc(startTime, stopTime time.Time, buf []byte) {
  57. targetFile := fmt.Sprintf("/.meta/log/%04d/%02d/%02d/%02d/%02d/%02d.%09d.log",
  58. startTime.Year(), startTime.Month(), startTime.Day(), startTime.Hour(), startTime.Minute(),
  59. startTime.Second(), startTime.Nanosecond())
  60. if err := f.appendToFile(targetFile, buf); err != nil {
  61. glog.V(0).Infof("log write failed %s: %v", targetFile, err)
  62. }
  63. }
  64. func (f *Filer) ReadLogBuffer(lastReadTime time.Time, eachEventFn func(fullpath string, eventNotification *filer_pb.EventNotification) error) (newLastReadTime time.Time, err error) {
  65. var buf []byte
  66. newLastReadTime, buf = f.metaLogBuffer.ReadFromBuffer(lastReadTime)
  67. var processedTs int64
  68. for pos := 0; pos+4 < len(buf); {
  69. size := util.BytesToUint32(buf[pos : pos+4])
  70. entryData := buf[pos+4 : pos+4+int(size)]
  71. logEntry := &filer_pb.LogEntry{}
  72. err = proto.Unmarshal(entryData, logEntry)
  73. if err != nil {
  74. glog.Errorf("unexpected unmarshal filer_pb.LogEntry: %v", err)
  75. return lastReadTime, fmt.Errorf("unexpected unmarshal filer_pb.LogEntry: %v", err)
  76. }
  77. event := &filer_pb.FullEventNotification{}
  78. err = proto.Unmarshal(logEntry.Data, event)
  79. if err != nil {
  80. glog.Errorf("unexpected unmarshal filer_pb.FullEventNotification: %v", err)
  81. return lastReadTime, fmt.Errorf("unexpected unmarshal filer_pb.FullEventNotification: %v", err)
  82. }
  83. err = eachEventFn(event.Directory, event.EventNotification)
  84. processedTs = logEntry.TsNs
  85. if err != nil {
  86. newLastReadTime = time.Unix(0, processedTs)
  87. return
  88. }
  89. pos += 4 + int(size)
  90. }
  91. newLastReadTime = time.Unix(0, processedTs)
  92. return
  93. }