seaweedfs/weed/server/filer_grpc_server_sub_meta.go

203 lines
5.9 KiB
Go
Raw Permalink Normal View History

2020-03-30 08:19:33 +00:00
package weed_server
import (
2020-04-20 07:08:47 +00:00
"fmt"
2020-09-09 18:21:23 +00:00
"github.com/chrislusf/seaweedfs/weed/util/log_buffer"
2020-04-05 07:51:16 +00:00
"strings"
"time"
2020-04-20 07:08:47 +00:00
"github.com/golang/protobuf/proto"
2020-09-01 07:21:19 +00:00
"github.com/chrislusf/seaweedfs/weed/filer"
2020-03-30 08:19:33 +00:00
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
2020-04-05 19:51:21 +00:00
"github.com/chrislusf/seaweedfs/weed/util"
2020-03-30 08:19:33 +00:00
)
2020-04-13 04:00:55 +00:00
func (fs *FilerServer) SubscribeMetadata(req *filer_pb.SubscribeMetadataRequest, stream filer_pb.SeaweedFiler_SubscribeMetadataServer) error {
2020-03-30 08:19:33 +00:00
peerAddress := findClientAddress(stream.Context(), 0)
2020-04-05 07:51:16 +00:00
clientName := fs.addClient(req.ClientName, peerAddress)
2020-03-30 08:19:33 +00:00
2020-04-05 07:51:16 +00:00
defer fs.deleteClient(clientName)
2020-03-30 08:19:33 +00:00
2020-04-28 06:49:46 +00:00
lastReadTime := time.Unix(0, req.SinceNs)
glog.V(0).Infof(" %v starts to subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
2020-04-20 06:54:32 +00:00
2020-09-09 18:21:23 +00:00
eachEventNotificationFn := fs.eachEventNotificationFn(req, stream, clientName, req.Signature)
2020-07-05 22:43:06 +00:00
eachLogEntryFn := eachLogEntryFn(eachEventNotificationFn)
var processedTsNs int64
var err error
2020-07-05 22:43:06 +00:00
2020-09-09 18:21:23 +00:00
for {
processedTsNs, err = fs.filer.ReadPersistedLogBuffer(lastReadTime, eachLogEntryFn)
if err != nil {
return fmt.Errorf("reading from persisted logs: %v", err)
}
if processedTsNs != 0 {
lastReadTime = time.Unix(0, processedTsNs)
}
2020-09-09 18:21:23 +00:00
lastReadTime, err = fs.filer.MetaAggregator.MetaLogBuffer.LoopProcessLogData(lastReadTime, func() bool {
fs.filer.MetaAggregator.ListenersLock.Lock()
fs.filer.MetaAggregator.ListenersCond.Wait()
fs.filer.MetaAggregator.ListenersLock.Unlock()
return true
}, eachLogEntryFn)
if err != nil {
if err == log_buffer.ResumeFromDiskError {
continue
}
2020-09-09 18:21:23 +00:00
glog.Errorf("processed to %v: %v", lastReadTime, err)
time.Sleep(3127 * time.Millisecond)
if err != log_buffer.ResumeError {
break
}
}
}
2020-07-05 22:43:06 +00:00
return err
}
func (fs *FilerServer) SubscribeLocalMetadata(req *filer_pb.SubscribeMetadataRequest, stream filer_pb.SeaweedFiler_SubscribeLocalMetadataServer) error {
peerAddress := findClientAddress(stream.Context(), 0)
clientName := fs.addClient(req.ClientName, peerAddress)
defer fs.deleteClient(clientName)
lastReadTime := time.Unix(0, req.SinceNs)
glog.V(0).Infof(" %v local subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
2020-09-09 18:21:23 +00:00
eachEventNotificationFn := fs.eachEventNotificationFn(req, stream, clientName, req.Signature)
eachLogEntryFn := eachLogEntryFn(eachEventNotificationFn)
var processedTsNs int64
var err error
2020-09-09 18:21:23 +00:00
for {
// println("reading from persisted logs ...")
processedTsNs, err = fs.filer.ReadPersistedLogBuffer(lastReadTime, eachLogEntryFn)
if err != nil {
return fmt.Errorf("reading from persisted logs: %v", err)
}
if processedTsNs != 0 {
lastReadTime = time.Unix(0, processedTsNs)
}
// glog.V(0).Infof("after local log reads, %v local subscribe %s from %+v", clientName, req.PathPrefix, lastReadTime)
// println("reading from in memory logs ...")
2020-09-09 18:21:23 +00:00
lastReadTime, err = fs.filer.LocalMetaLogBuffer.LoopProcessLogData(lastReadTime, func() bool {
fs.listenersLock.Lock()
fs.listenersCond.Wait()
fs.listenersLock.Unlock()
return true
}, eachLogEntryFn)
if err != nil {
if err == log_buffer.ResumeFromDiskError {
continue
}
2020-09-09 18:21:23 +00:00
glog.Errorf("processed to %v: %v", lastReadTime, err)
time.Sleep(3127 * time.Millisecond)
if err != log_buffer.ResumeError {
break
}
}
}
return err
}
2020-07-05 22:43:06 +00:00
func eachLogEntryFn(eachEventNotificationFn func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64) error) func(logEntry *filer_pb.LogEntry) error {
return func(logEntry *filer_pb.LogEntry) error {
event := &filer_pb.SubscribeMetadataResponse{}
if err := proto.Unmarshal(logEntry.Data, event); err != nil {
glog.Errorf("unexpected unmarshal filer_pb.SubscribeMetadataResponse: %v", err)
return fmt.Errorf("unexpected unmarshal filer_pb.SubscribeMetadataResponse: %v", err)
}
if err := eachEventNotificationFn(event.Directory, event.EventNotification, event.TsNs); err != nil {
return err
}
return nil
}
}
2020-09-09 18:21:23 +00:00
func (fs *FilerServer) eachEventNotificationFn(req *filer_pb.SubscribeMetadataRequest, stream filer_pb.SeaweedFiler_SubscribeMetadataServer, clientName string, clientSignature int32) func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64) error {
2020-07-05 22:43:06 +00:00
return func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64) error {
2020-04-20 07:08:47 +00:00
2020-09-09 18:21:23 +00:00
foundSelf := false
for _, sig := range eventNotification.Signatures {
if sig == clientSignature && clientSignature != 0 {
return nil
}
2020-09-09 18:21:23 +00:00
if sig == fs.filer.Signature {
foundSelf = true
}
}
if !foundSelf {
eventNotification.Signatures = append(eventNotification.Signatures, fs.filer.Signature)
}
2020-04-20 07:08:47 +00:00
// get complete path to the file or directory
var entryName string
if eventNotification.OldEntry != nil {
entryName = eventNotification.OldEntry.Name
} else if eventNotification.NewEntry != nil {
entryName = eventNotification.NewEntry.Name
}
fullpath := util.Join(dirPath, entryName)
// skip on filer internal meta logs
2020-09-01 07:21:19 +00:00
if strings.HasPrefix(fullpath, filer.SystemLogDir) {
2020-04-05 07:51:16 +00:00
return nil
2020-03-30 08:19:33 +00:00
}
2020-04-20 07:08:47 +00:00
if !strings.HasPrefix(fullpath, req.PathPrefix) {
if eventNotification.NewParentPath != "" {
newFullPath := util.Join(eventNotification.NewParentPath, entryName)
if !strings.HasPrefix(newFullPath, req.PathPrefix) {
return nil
}
} else {
return nil
}
2020-04-20 07:08:47 +00:00
}
message := &filer_pb.SubscribeMetadataResponse{
Directory: dirPath,
EventNotification: eventNotification,
TsNs: tsNs,
2020-04-20 07:08:47 +00:00
}
// println("sending", dirPath, entryName)
2020-04-20 07:08:47 +00:00
if err := stream.Send(message); err != nil {
glog.V(0).Infof("=> client %v: %+v", clientName, err)
return err
}
return nil
}
2020-03-30 08:19:33 +00:00
}
2020-04-05 07:51:16 +00:00
func (fs *FilerServer) addClient(clientType string, clientAddress string) (clientName string) {
2020-03-30 08:19:33 +00:00
clientName = clientType + "@" + clientAddress
glog.V(0).Infof("+ listener %v", clientName)
return
}
2020-04-05 07:51:16 +00:00
func (fs *FilerServer) deleteClient(clientName string) {
2020-03-30 08:19:33 +00:00
glog.V(0).Infof("- listener %v", clientName)
}