2022-02-14 09:09:31 +00:00
|
|
|
package meta_cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2022-02-14 09:09:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func SubscribeMetaEvents(mc *MetaCache, selfSignature int32, client filer_pb.FilerClient, dir string, lastTsNs int64) error {
|
|
|
|
|
|
|
|
processEventFn := func(resp *filer_pb.SubscribeMetadataResponse) error {
|
|
|
|
message := resp.EventNotification
|
|
|
|
|
|
|
|
for _, sig := range message.Signatures {
|
|
|
|
if sig == selfSignature && selfSignature != 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dir := resp.Directory
|
|
|
|
var oldPath util.FullPath
|
|
|
|
var newEntry *filer.Entry
|
|
|
|
if message.OldEntry != nil {
|
|
|
|
oldPath = util.NewFullPath(dir, message.OldEntry.Name)
|
|
|
|
glog.V(4).Infof("deleting %v", oldPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
if message.NewEntry != nil {
|
|
|
|
if message.NewParentPath != "" {
|
|
|
|
dir = message.NewParentPath
|
|
|
|
}
|
|
|
|
key := util.NewFullPath(dir, message.NewEntry.Name)
|
|
|
|
glog.V(4).Infof("creating %v", key)
|
|
|
|
newEntry = filer.FromPbEntry(dir, message.NewEntry)
|
|
|
|
}
|
2022-08-01 05:51:41 +00:00
|
|
|
err := mc.AtomicUpdateEntryFromFiler(context.Background(), oldPath, newEntry)
|
2022-02-14 09:09:31 +00:00
|
|
|
if err == nil {
|
|
|
|
if message.OldEntry != nil && message.NewEntry != nil {
|
|
|
|
oldKey := util.NewFullPath(resp.Directory, message.OldEntry.Name)
|
|
|
|
mc.invalidateFunc(oldKey, message.OldEntry)
|
|
|
|
if message.OldEntry.Name != message.NewEntry.Name {
|
|
|
|
newKey := util.NewFullPath(dir, message.NewEntry.Name)
|
|
|
|
mc.invalidateFunc(newKey, message.NewEntry)
|
|
|
|
}
|
2022-02-25 09:17:26 +00:00
|
|
|
} else if filer_pb.IsCreate(resp) {
|
2022-09-15 09:04:57 +00:00
|
|
|
// no need to invalidate
|
2022-02-25 09:17:26 +00:00
|
|
|
} else if filer_pb.IsDelete(resp) {
|
2022-02-14 09:09:31 +00:00
|
|
|
oldKey := util.NewFullPath(resp.Directory, message.OldEntry.Name)
|
|
|
|
mc.invalidateFunc(oldKey, message.OldEntry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-07-23 17:50:28 +00:00
|
|
|
var clientEpoch int32
|
2023-03-22 06:01:49 +00:00
|
|
|
metadataFollowOption := &pb.MetadataFollowOption{
|
|
|
|
ClientName: "mount",
|
|
|
|
ClientId: selfSignature,
|
|
|
|
ClientEpoch: clientEpoch,
|
|
|
|
SelfSignature: selfSignature,
|
|
|
|
PathPrefix: dir,
|
|
|
|
AdditionalPathPrefixes: nil,
|
|
|
|
DirectoriesToWatch: nil,
|
|
|
|
StartTsNs: lastTsNs,
|
|
|
|
StopTsNs: 0,
|
|
|
|
EventErrorType: pb.FatalOnError,
|
|
|
|
}
|
2022-02-14 09:09:31 +00:00
|
|
|
util.RetryForever("followMetaUpdates", func() error {
|
2022-07-23 17:50:28 +00:00
|
|
|
clientEpoch++
|
2023-03-22 06:01:49 +00:00
|
|
|
return pb.WithFilerClientFollowMetadata(client, metadataFollowOption, processEventFn)
|
2022-02-14 09:09:31 +00:00
|
|
|
}, func(err error) bool {
|
|
|
|
glog.Errorf("follow metadata updates: %v", err)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|