seaweedfs/weed/filer2/filer_notify.go

80 lines
2 KiB
Go
Raw Normal View History

2018-08-13 08:20:49 +00:00
package filer2
import (
2020-03-30 08:19:33 +00:00
"fmt"
"strings"
"time"
"github.com/golang/protobuf/proto"
2018-09-21 08:56:43 +00:00
"github.com/chrislusf/seaweedfs/weed/glog"
2018-09-16 08:18:30 +00:00
"github.com/chrislusf/seaweedfs/weed/notification"
2018-08-13 08:20:49 +00:00
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
2020-03-30 08:19:33 +00:00
"github.com/chrislusf/seaweedfs/weed/util"
2018-08-13 08:20:49 +00:00
)
2018-09-17 07:27:56 +00:00
func (f *Filer) NotifyUpdateEvent(oldEntry, newEntry *Entry, deleteChunks bool) {
2020-03-30 20:03:43 +00:00
var fullpath string
2018-08-13 08:20:49 +00:00
if oldEntry != nil {
2020-03-30 20:03:43 +00:00
fullpath = string(oldEntry.FullPath)
2018-08-13 08:20:49 +00:00
} else if newEntry != nil {
2020-03-30 20:03:43 +00:00
fullpath = string(newEntry.FullPath)
2018-08-13 08:20:49 +00:00
} else {
return
}
2020-03-30 20:03:43 +00:00
// println("fullpath:", fullpath)
2020-03-30 08:19:33 +00:00
2020-03-30 20:03:43 +00:00
if strings.HasPrefix(fullpath, "/.meta") {
2020-03-30 08:19:33 +00:00
return
}
2018-08-13 08:33:21 +00:00
2020-03-30 08:19:33 +00:00
newParentPath := ""
if newEntry != nil {
newParentPath, _ = newEntry.FullPath.DirAndName()
}
eventNotification := &filer_pb.EventNotification{
OldEntry: oldEntry.ToProtoEntry(),
NewEntry: newEntry.ToProtoEntry(),
DeleteChunks: deleteChunks,
NewParentPath: newParentPath,
}
if notification.Queue != nil {
2020-03-30 20:03:43 +00:00
glog.V(3).Infof("notifying entry update %v", fullpath)
notification.Queue.SendMessage(fullpath, eventNotification)
2020-03-30 08:19:33 +00:00
}
2020-03-30 20:03:43 +00:00
f.logMetaEvent(time.Now(), fullpath, eventNotification)
2020-03-30 08:19:33 +00:00
}
2020-03-30 20:03:43 +00:00
func (f *Filer) logMetaEvent(ts time.Time, fullpath string, eventNotification *filer_pb.EventNotification) {
dir, _ := util.FullPath(fullpath).DirAndName()
2020-03-30 08:19:33 +00:00
event := &filer_pb.FullEventNotification{
Directory: dir,
EventNotification: eventNotification,
}
data, err := proto.Marshal(event)
if err != nil {
glog.Errorf("failed to marshal filer_pb.FullEventNotification %+v: %v", event, err)
return
}
f.metaLogBuffer.AddToBuffer(ts, []byte(dir), data)
}
func (f *Filer) logFlushFunc(startTime, stopTime time.Time, buf []byte) {
targetFile := fmt.Sprintf("/.meta/log/%04d/%02d/%02d/%02d/%02d/%02d-%02d.log",
startTime.Year(), startTime.Month(), startTime.Day(), startTime.Hour(), startTime.Minute(),
startTime.Second(), stopTime.Second())
2018-09-16 18:20:36 +00:00
2020-03-30 08:19:33 +00:00
if err := f.appendToFile(targetFile, buf); err != nil {
glog.V(0).Infof("log write failed %s: %v", targetFile, err)
}
}