2018-08-13 16:20:49 +08:00
|
|
|
package filer2
|
|
|
|
|
|
|
|
import (
|
2020-04-20 14:37:04 +08:00
|
|
|
"bytes"
|
2020-03-30 16:19:33 +08:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
|
2018-09-21 16:56:43 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2018-09-16 16:18:30 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/notification"
|
2018-08-13 16:20:49 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2020-03-30 16:19:33 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2018-08-13 16:20:49 +08:00
|
|
|
)
|
|
|
|
|
2018-09-17 15:27:56 +08:00
|
|
|
func (f *Filer) NotifyUpdateEvent(oldEntry, newEntry *Entry, deleteChunks bool) {
|
2020-03-31 04:03:43 +08:00
|
|
|
var fullpath string
|
2018-08-13 16:20:49 +08:00
|
|
|
if oldEntry != nil {
|
2020-03-31 04:03:43 +08:00
|
|
|
fullpath = string(oldEntry.FullPath)
|
2018-08-13 16:20:49 +08:00
|
|
|
} else if newEntry != nil {
|
2020-03-31 04:03:43 +08:00
|
|
|
fullpath = string(newEntry.FullPath)
|
2018-08-13 16:20:49 +08:00
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-31 04:03:43 +08:00
|
|
|
// println("fullpath:", fullpath)
|
2020-03-30 16:19:33 +08:00
|
|
|
|
2020-04-13 05:03:07 +08:00
|
|
|
if strings.HasPrefix(fullpath, SystemLogDir) {
|
2020-03-30 16:19:33 +08:00
|
|
|
return
|
|
|
|
}
|
2018-08-13 16:33:21 +08:00
|
|
|
|
2020-03-30 16:19:33 +08: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-31 04:03:43 +08:00
|
|
|
glog.V(3).Infof("notifying entry update %v", fullpath)
|
|
|
|
notification.Queue.SendMessage(fullpath, eventNotification)
|
2020-03-30 16:19:33 +08:00
|
|
|
}
|
|
|
|
|
2020-04-10 16:35:59 +08:00
|
|
|
f.logMetaEvent(fullpath, eventNotification)
|
2020-03-30 16:19:33 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-10 16:35:59 +08:00
|
|
|
func (f *Filer) logMetaEvent(fullpath string, eventNotification *filer_pb.EventNotification) {
|
2020-03-31 04:03:43 +08:00
|
|
|
|
|
|
|
dir, _ := util.FullPath(fullpath).DirAndName()
|
|
|
|
|
2020-04-13 12:00:55 +08:00
|
|
|
event := &filer_pb.SubscribeMetadataResponse{
|
2020-03-30 16:19:33 +08:00
|
|
|
Directory: dir,
|
|
|
|
EventNotification: eventNotification,
|
|
|
|
}
|
|
|
|
data, err := proto.Marshal(event)
|
|
|
|
if err != nil {
|
2020-04-13 12:00:55 +08:00
|
|
|
glog.Errorf("failed to marshal filer_pb.SubscribeMetadataResponse %+v: %v", event, err)
|
2020-03-30 16:19:33 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-10 16:35:59 +08:00
|
|
|
f.metaLogBuffer.AddToBuffer([]byte(dir), data)
|
2020-03-30 16:19:33 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Filer) logFlushFunc(startTime, stopTime time.Time, buf []byte) {
|
2020-04-09 11:32:57 +08:00
|
|
|
|
2020-04-13 05:03:07 +08:00
|
|
|
targetFile := fmt.Sprintf("%s/%04d-%02d-%02d/%02d-%02d.segment", SystemLogDir,
|
2020-03-30 16:19:33 +08:00
|
|
|
startTime.Year(), startTime.Month(), startTime.Day(), startTime.Hour(), startTime.Minute(),
|
2020-04-13 05:03:07 +08:00
|
|
|
// startTime.Second(), startTime.Nanosecond(),
|
|
|
|
)
|
2018-09-17 02:20:36 +08:00
|
|
|
|
2020-03-30 16:19:33 +08:00
|
|
|
if err := f.appendToFile(targetFile, buf); err != nil {
|
|
|
|
glog.V(0).Infof("log write failed %s: %v", targetFile, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-05 15:51:16 +08:00
|
|
|
func (f *Filer) ReadLogBuffer(lastReadTime time.Time, eachEventFn func(fullpath string, eventNotification *filer_pb.EventNotification) error) (newLastReadTime time.Time, err error) {
|
|
|
|
|
2020-04-20 14:37:04 +08:00
|
|
|
var bytesBuf *bytes.Buffer
|
|
|
|
bytesBuf = f.metaLogBuffer.ReadFromBuffer(lastReadTime)
|
|
|
|
if bytesBuf == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer f.metaLogBuffer.ReleaseMeory(bytesBuf)
|
|
|
|
buf := bytesBuf.Bytes()
|
2020-04-06 07:51:30 +08:00
|
|
|
var processedTs int64
|
2020-04-05 15:51:16 +08:00
|
|
|
|
|
|
|
for pos := 0; pos+4 < len(buf); {
|
|
|
|
|
|
|
|
size := util.BytesToUint32(buf[pos : pos+4])
|
|
|
|
entryData := buf[pos+4 : pos+4+int(size)]
|
|
|
|
|
|
|
|
logEntry := &filer_pb.LogEntry{}
|
|
|
|
err = proto.Unmarshal(entryData, logEntry)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("unexpected unmarshal filer_pb.LogEntry: %v", err)
|
|
|
|
return lastReadTime, fmt.Errorf("unexpected unmarshal filer_pb.LogEntry: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-04-13 12:00:55 +08:00
|
|
|
event := &filer_pb.SubscribeMetadataResponse{}
|
2020-04-05 15:51:16 +08:00
|
|
|
err = proto.Unmarshal(logEntry.Data, event)
|
|
|
|
if err != nil {
|
2020-04-13 12:00:55 +08:00
|
|
|
glog.Errorf("unexpected unmarshal filer_pb.SubscribeMetadataResponse: %v", err)
|
|
|
|
return lastReadTime, fmt.Errorf("unexpected unmarshal filer_pb.SubscribeMetadataResponse: %v", err)
|
2020-04-05 15:51:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
err = eachEventFn(event.Directory, event.EventNotification)
|
|
|
|
|
2020-04-06 07:51:30 +08:00
|
|
|
processedTs = logEntry.TsNs
|
|
|
|
|
2020-04-05 15:51:16 +08:00
|
|
|
if err != nil {
|
2020-04-06 07:51:30 +08:00
|
|
|
newLastReadTime = time.Unix(0, processedTs)
|
2020-04-05 15:51:16 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pos += 4 + int(size)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-06 07:51:30 +08:00
|
|
|
newLastReadTime = time.Unix(0, processedTs)
|
2020-04-05 15:51:16 +08:00
|
|
|
return
|
|
|
|
|
|
|
|
}
|