2020-04-22 04:16:13 +00:00
|
|
|
package meta_cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer2"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
|
|
|
)
|
|
|
|
|
2020-04-27 23:52:20 +00:00
|
|
|
func SubscribeMetaEvents(mc *MetaCache, client filer_pb.FilerClient, dir string, lastTsNs int64) error {
|
2020-04-22 04:16:13 +00:00
|
|
|
|
|
|
|
processEventFn := func(resp *filer_pb.SubscribeMetadataResponse) error {
|
|
|
|
message := resp.EventNotification
|
2020-04-30 01:20:54 +00:00
|
|
|
var oldPath util.FullPath
|
|
|
|
var newEntry *filer2.Entry
|
2020-04-22 04:16:13 +00:00
|
|
|
if message.OldEntry != nil {
|
2020-04-30 01:20:54 +00:00
|
|
|
oldPath = util.NewFullPath(resp.Directory, message.OldEntry.Name)
|
|
|
|
glog.V(4).Infof("deleting %v", oldPath)
|
2020-04-22 04:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if message.NewEntry != nil {
|
|
|
|
dir := resp.Directory
|
|
|
|
if message.NewParentPath != "" {
|
|
|
|
dir = message.NewParentPath
|
|
|
|
}
|
|
|
|
key := util.NewFullPath(dir, message.NewEntry.Name)
|
|
|
|
glog.V(4).Infof("creating %v", key)
|
2020-04-30 01:20:54 +00:00
|
|
|
newEntry = filer2.FromPbEntry(dir, message.NewEntry)
|
2020-04-22 04:16:13 +00:00
|
|
|
}
|
2020-04-30 01:20:54 +00:00
|
|
|
return mc.AtomicUpdateEntry(context.Background(), oldPath, newEntry)
|
2020-04-22 04:16:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
err := client.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
|
|
|
stream, err := client.SubscribeMetadata(context.Background(), &filer_pb.SubscribeMetadataRequest{
|
|
|
|
ClientName: "mount",
|
|
|
|
PathPrefix: dir,
|
|
|
|
SinceNs: lastTsNs,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("subscribe: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
resp, listenErr := stream.Recv()
|
|
|
|
if listenErr == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if listenErr != nil {
|
|
|
|
return listenErr
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := processEventFn(resp); err != nil {
|
2020-07-11 13:16:48 +00:00
|
|
|
glog.Fatalf("process %v: %v", resp, err)
|
2020-04-22 04:16:13 +00:00
|
|
|
}
|
|
|
|
lastTsNs = resp.TsNs
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-07-11 13:16:48 +00:00
|
|
|
glog.Errorf("subscribing filer meta change: %v", err)
|
2020-04-22 04:16:13 +00:00
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|