2020-09-01 15:21:19 +08:00
|
|
|
package filer
|
2018-08-13 16:20:49 +08:00
|
|
|
|
|
|
|
import (
|
2020-04-28 14:49:46 +08:00
|
|
|
"context"
|
2020-03-30 16:19:33 +08:00
|
|
|
"fmt"
|
2024-01-08 16:03:08 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util/log_buffer"
|
2020-04-28 14:49:46 +08:00
|
|
|
"io"
|
2022-12-20 03:30:50 +08:00
|
|
|
"regexp"
|
2020-03-30 16:19:33 +08:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-08-18 03:05:07 +08:00
|
|
|
"google.golang.org/protobuf/proto"
|
2020-03-30 16:19:33 +08:00
|
|
|
|
2022-07-29 15:17:28 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/notification"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2018-08-13 16:20:49 +08:00
|
|
|
)
|
|
|
|
|
2020-08-29 14:48:48 +08:00
|
|
|
func (f *Filer) NotifyUpdateEvent(ctx context.Context, oldEntry, newEntry *Entry, deleteChunks, isFromOtherCluster bool, signatures []int32) {
|
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
|
|
|
|
}
|
2020-09-10 02:21:23 +08:00
|
|
|
foundSelf := false
|
|
|
|
for _, sig := range signatures {
|
|
|
|
if sig == f.Signature {
|
|
|
|
foundSelf = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !foundSelf {
|
|
|
|
signatures = append(signatures, f.Signature)
|
|
|
|
}
|
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{
|
2020-07-01 23:06:20 +08:00
|
|
|
OldEntry: oldEntry.ToProtoEntry(),
|
|
|
|
NewEntry: newEntry.ToProtoEntry(),
|
|
|
|
DeleteChunks: deleteChunks,
|
|
|
|
NewParentPath: newParentPath,
|
|
|
|
IsFromOtherCluster: isFromOtherCluster,
|
2020-09-10 02:21:23 +08:00
|
|
|
Signatures: signatures,
|
2020-03-30 16:19:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if notification.Queue != nil {
|
2020-07-01 23:06:20 +08:00
|
|
|
glog.V(3).Infof("notifying entry update %v", fullpath)
|
2021-02-11 16:59:36 +08:00
|
|
|
if err := notification.Queue.SendMessage(fullpath, eventNotification); err != nil {
|
|
|
|
// throw message
|
|
|
|
glog.Error(err)
|
|
|
|
}
|
2020-03-30 16:19:33 +08:00
|
|
|
}
|
|
|
|
|
2020-06-29 05:34:51 +08:00
|
|
|
f.logMetaEvent(ctx, fullpath, eventNotification)
|
2020-03-30 16:19:33 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-06-29 05:34:51 +08:00
|
|
|
func (f *Filer) logMetaEvent(ctx context.Context, 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,
|
2020-04-22 12:16:13 +08:00
|
|
|
TsNs: time.Now().UnixNano(),
|
2020-03-30 16:19:33 +08:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-04-03 07:25:43 +08:00
|
|
|
f.LocalMetaLogBuffer.AddDataToBuffer([]byte(dir), data, event.TsNs)
|
2020-03-30 16:19:33 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-03-08 02:50:09 +08:00
|
|
|
func (f *Filer) logFlushFunc(logBuffer *log_buffer.LogBuffer, startTime, stopTime time.Time, buf []byte) {
|
2020-04-09 11:32:57 +08:00
|
|
|
|
2020-10-22 15:35:46 +08:00
|
|
|
if len(buf) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-30 12:01:14 +08:00
|
|
|
startTime, stopTime = startTime.UTC(), stopTime.UTC()
|
|
|
|
|
2021-09-25 16:18:44 +08:00
|
|
|
targetFile := fmt.Sprintf("%s/%04d-%02d-%02d/%02d-%02d.%08x", SystemLogDir,
|
2022-07-24 01:50:28 +08:00
|
|
|
startTime.Year(), startTime.Month(), startTime.Day(), startTime.Hour(), startTime.Minute(), f.UniqueFilerId,
|
2020-04-13 05:03:07 +08:00
|
|
|
// startTime.Second(), startTime.Nanosecond(),
|
|
|
|
)
|
2018-09-17 02:20:36 +08:00
|
|
|
|
2020-07-15 02:25:50 +08:00
|
|
|
for {
|
|
|
|
if err := f.appendToFile(targetFile, buf); err != nil {
|
2022-05-11 15:45:19 +08:00
|
|
|
glog.V(0).Infof("metadata log write failed %s: %v", targetFile, err)
|
2020-07-15 02:25:50 +08:00
|
|
|
time.Sleep(737 * time.Millisecond)
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
2020-03-30 16:19:33 +08:00
|
|
|
}
|
|
|
|
}
|
2020-04-28 14:49:46 +08:00
|
|
|
|
2022-12-20 03:30:50 +08:00
|
|
|
var (
|
|
|
|
VolumeNotFoundPattern = regexp.MustCompile(`volume \d+? not found`)
|
|
|
|
)
|
|
|
|
|
2024-03-08 02:50:09 +08:00
|
|
|
func (f *Filer) ReadPersistedLogBuffer(startPosition log_buffer.MessagePosition, stopTsNs int64, eachLogEntryFn log_buffer.EachLogEntryFuncType) (lastTsNs int64, isDone bool, err error) {
|
2020-04-28 14:49:46 +08:00
|
|
|
|
2024-07-11 15:04:24 +08:00
|
|
|
visitor, visitErr := f.collectPersistedLogBuffer(startPosition, stopTsNs)
|
|
|
|
if visitErr != nil {
|
|
|
|
if visitErr == io.EOF {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = fmt.Errorf("reading from persisted logs: %v", visitErr)
|
|
|
|
return
|
2020-04-28 14:49:46 +08:00
|
|
|
}
|
2024-07-11 15:04:24 +08:00
|
|
|
var logEntry *filer_pb.LogEntry
|
|
|
|
for {
|
|
|
|
logEntry, visitErr = visitor.GetNext()
|
|
|
|
if visitErr != nil {
|
|
|
|
if visitErr == io.EOF {
|
2022-05-31 06:04:19 +08:00
|
|
|
break
|
|
|
|
}
|
2024-07-11 15:04:24 +08:00
|
|
|
err = fmt.Errorf("read next from persisted logs: %v", visitErr)
|
|
|
|
return
|
2022-05-31 06:04:19 +08:00
|
|
|
}
|
2024-07-11 15:04:24 +08:00
|
|
|
isDone, visitErr = eachLogEntryFn(logEntry)
|
|
|
|
if visitErr != nil {
|
|
|
|
err = fmt.Errorf("process persisted log entry: %v", visitErr)
|
|
|
|
return
|
2020-04-28 14:49:46 +08:00
|
|
|
}
|
2024-07-11 15:04:24 +08:00
|
|
|
lastTsNs = logEntry.TsNs
|
|
|
|
if isDone {
|
|
|
|
return
|
2020-04-28 14:49:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-11 15:04:24 +08:00
|
|
|
return
|
2020-04-28 14:49:46 +08:00
|
|
|
}
|