2020-04-22 12:16:13 +08:00
|
|
|
package meta_cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
|
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"
|
|
|
|
"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)
|
|
|
|
if err == nil && message.OldEntry != nil && message.NewEntry != nil {
|
|
|
|
key := util.NewFullPath(dir, message.NewEntry.Name)
|
|
|
|
mc.invalidateFunc(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
2020-04-22 12:16:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
err := client.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
2020-09-10 02:21:23 +08:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
stream, err := client.SubscribeMetadata(ctx, &filer_pb.SubscribeMetadataRequest{
|
2020-04-22 12:16:13 +08:00
|
|
|
ClientName: "mount",
|
|
|
|
PathPrefix: dir,
|
|
|
|
SinceNs: lastTsNs,
|
2020-08-29 14:48:48 +08:00
|
|
|
Signature: selfSignature,
|
2020-04-22 12:16:13 +08:00
|
|
|
})
|
|
|
|
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 {
|
2021-01-12 05:50:15 +08:00
|
|
|
glog.Fatalf("process %v: %v", resp, err)
|
2020-04-22 12:16:13 +08:00
|
|
|
}
|
|
|
|
lastTsNs = resp.TsNs
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-07-11 21:16:48 +08:00
|
|
|
glog.Errorf("subscribing filer meta change: %v", err)
|
2020-04-22 12:16:13 +08:00
|
|
|
}
|
2020-09-10 02:21:23 +08:00
|
|
|
time.Sleep(time.Second)
|
2020-04-22 12:16:13 +08:00
|
|
|
}
|
|
|
|
}
|