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
6 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, SystemLogDir) {
  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. f.logMetaEvent(fullpath, eventNotification)
  40. }
  41. func (f *Filer) logMetaEvent(fullpath string, eventNotification *filer_pb.EventNotification) {
  42. dir, _ := util.FullPath(fullpath).DirAndName()
  43. event := &filer_pb.FullEventNotification{
  44. Directory: dir,
  45. EventNotification: eventNotification,
  46. }
  47. data, err := proto.Marshal(event)
  48. if err != nil {
  49. glog.Errorf("failed to marshal filer_pb.FullEventNotification %+v: %v", event, err)
  50. return
  51. }
  52. f.metaLogBuffer.AddToBuffer([]byte(dir), data)
  53. }
  54. func (f *Filer) logFlushFunc(startTime, stopTime time.Time, buf []byte) {
  55. targetFile := fmt.Sprintf("%s/%04d-%02d-%02d/%02d-%02d.segment", SystemLogDir,
  56. startTime.Year(), startTime.Month(), startTime.Day(), startTime.Hour(), startTime.Minute(),
  57. // startTime.Second(), startTime.Nanosecond(),
  58. )
  59. if err := f.appendToFile(targetFile, buf); err != nil {
  60. glog.V(0).Infof("log write failed %s: %v", targetFile, err)
  61. }
  62. }
  63. func (f *Filer) ReadLogBuffer(lastReadTime time.Time, eachEventFn func(fullpath string, eventNotification *filer_pb.EventNotification) error) (newLastReadTime time.Time, err error) {
  64. var buf []byte
  65. newLastReadTime, buf = f.metaLogBuffer.ReadFromBuffer(lastReadTime)
  66. var processedTs int64
  67. for pos := 0; pos+4 < len(buf); {
  68. size := util.BytesToUint32(buf[pos : pos+4])
  69. entryData := buf[pos+4 : pos+4+int(size)]
  70. logEntry := &filer_pb.LogEntry{}
  71. err = proto.Unmarshal(entryData, logEntry)
  72. if err != nil {
  73. glog.Errorf("unexpected unmarshal filer_pb.LogEntry: %v", err)
  74. return lastReadTime, fmt.Errorf("unexpected unmarshal filer_pb.LogEntry: %v", err)
  75. }
  76. event := &filer_pb.FullEventNotification{}
  77. err = proto.Unmarshal(logEntry.Data, event)
  78. if err != nil {
  79. glog.Errorf("unexpected unmarshal filer_pb.FullEventNotification: %v", err)
  80. return lastReadTime, fmt.Errorf("unexpected unmarshal filer_pb.FullEventNotification: %v", err)
  81. }
  82. err = eachEventFn(event.Directory, event.EventNotification)
  83. processedTs = logEntry.TsNs
  84. if err != nil {
  85. newLastReadTime = time.Unix(0, processedTs)
  86. return
  87. }
  88. pos += 4 + int(size)
  89. }
  90. newLastReadTime = time.Unix(0, processedTs)
  91. return
  92. }