2020-04-22 12:16:13 +08:00
|
|
|
package meta_cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-01 15:21:19 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer"
|
2020-04-22 12:16:13 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2021-08-05 07:25:46 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb"
|
2020-04-22 12:16:13 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
)
|
|
|
|
|
2020-08-29 14:48:48 +08:00
|
|
|
func SubscribeMetaEvents(mc *MetaCache, selfSignature int32, client filer_pb.FilerClient, dir string, lastTsNs int64) error {
|
2020-04-22 12:16:13 +08:00
|
|
|
|
|
|
|
processEventFn := func(resp *filer_pb.SubscribeMetadataResponse) error {
|
|
|
|
message := resp.EventNotification
|
2020-08-29 14:48:48 +08:00
|
|
|
|
|
|
|
for _, sig := range message.Signatures {
|
|
|
|
if sig == selfSignature && selfSignature != 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 10:24:15 +08:00
|
|
|
dir := resp.Directory
|
2020-04-30 09:20:54 +08:00
|
|
|
var oldPath util.FullPath
|
2020-09-01 15:21:19 +08:00
|
|
|
var newEntry *filer.Entry
|
2020-04-22 12:16:13 +08:00
|
|
|
if message.OldEntry != nil {
|
2020-10-26 10:24:15 +08:00
|
|
|
oldPath = util.NewFullPath(dir, message.OldEntry.Name)
|
2020-04-30 09:20:54 +08:00
|
|
|
glog.V(4).Infof("deleting %v", oldPath)
|
2020-04-22 12:16:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if message.NewEntry != nil {
|
|
|
|
if message.NewParentPath != "" {
|
|
|
|
dir = message.NewParentPath
|
|
|
|
}
|
|
|
|
key := util.NewFullPath(dir, message.NewEntry.Name)
|
|
|
|
glog.V(4).Infof("creating %v", key)
|
2020-09-01 15:21:19 +08:00
|
|
|
newEntry = filer.FromPbEntry(dir, message.NewEntry)
|
2020-04-22 12:16:13 +08:00
|
|
|
}
|
2020-10-26 10:24:15 +08:00
|
|
|
err := mc.AtomicUpdateEntryFromFiler(context.Background(), oldPath, newEntry)
|
2021-06-06 04:09:33 +08:00
|
|
|
if err == nil {
|
|
|
|
if message.OldEntry != nil && message.NewEntry != nil {
|
2021-08-28 19:22:19 +08:00
|
|
|
oldKey := util.NewFullPath(resp.Directory, message.OldEntry.Name)
|
2022-01-13 03:51:13 +08:00
|
|
|
mc.invalidateFunc(oldKey, message.OldEntry)
|
2021-08-28 20:21:01 +08:00
|
|
|
if message.OldEntry.Name != message.NewEntry.Name {
|
|
|
|
newKey := util.NewFullPath(dir, message.NewEntry.Name)
|
2022-01-13 03:51:13 +08:00
|
|
|
mc.invalidateFunc(newKey, message.NewEntry)
|
2021-08-28 20:21:01 +08:00
|
|
|
}
|
2021-06-06 04:09:33 +08:00
|
|
|
} else if message.OldEntry == nil && message.NewEntry != nil {
|
|
|
|
// no need to invaalidate
|
|
|
|
} else if message.OldEntry != nil && message.NewEntry == nil {
|
|
|
|
oldKey := util.NewFullPath(resp.Directory, message.OldEntry.Name)
|
2022-01-13 03:51:13 +08:00
|
|
|
mc.invalidateFunc(oldKey, message.OldEntry)
|
2021-06-05 17:23:07 +08:00
|
|
|
}
|
2020-10-26 10:24:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
2020-04-22 12:16:13 +08:00
|
|
|
}
|
|
|
|
|
2021-08-16 03:37:35 +08:00
|
|
|
util.RetryForever("followMetaUpdates", func() error {
|
2021-12-30 16:23:57 +08:00
|
|
|
return pb.WithFilerClientFollowMetadata(client, "mount", selfSignature, dir, &lastTsNs, selfSignature, processEventFn, true)
|
2021-08-16 03:37:35 +08:00
|
|
|
}, func(err error) bool {
|
|
|
|
glog.Errorf("follow metadata updates: %v", err)
|
|
|
|
return true
|
2021-08-05 07:25:46 +08:00
|
|
|
})
|
2020-04-22 12:16:13 +08:00
|
|
|
|
2021-08-16 03:40:22 +08:00
|
|
|
return nil
|
2020-04-22 12:16:13 +08:00
|
|
|
}
|