seaweedfs/weed/pb/filer_pb_tail.go

96 lines
2.7 KiB
Go
Raw Normal View History

2021-08-05 07:25:46 +08:00
package pb
import (
"context"
"fmt"
"github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
"google.golang.org/grpc"
"io"
"time"
)
type ProcessMetadataFunc func(resp *filer_pb.SubscribeMetadataResponse) error
func FollowMetadata(filerAddress ServerAddress, grpcDialOption grpc.DialOption, clientName string,
2021-09-01 14:26:26 +08:00
pathPrefix string, additionalPathPrefixes []string, lastTsNs int64, selfSignature int32,
2021-08-05 07:25:46 +08:00
processEventFn ProcessMetadataFunc, fatalOnError bool) error {
2021-09-01 14:26:26 +08:00
err := WithFilerClient(filerAddress, grpcDialOption, makeFunc(clientName,
2021-10-17 17:01:20 +08:00
pathPrefix, additionalPathPrefixes, &lastTsNs, selfSignature, processEventFn, fatalOnError))
2021-08-05 07:25:46 +08:00
if err != nil {
return fmt.Errorf("subscribing filer meta change: %v", err)
}
return err
}
func WithFilerClientFollowMetadata(filerClient filer_pb.FilerClient,
2021-10-17 17:01:20 +08:00
clientName string, pathPrefix string, lastTsNs *int64, selfSignature int32,
2021-08-05 07:25:46 +08:00
processEventFn ProcessMetadataFunc, fatalOnError bool) error {
2021-09-01 14:30:28 +08:00
err := filerClient.WithFilerClient(makeFunc(clientName,
pathPrefix, nil, lastTsNs, selfSignature, processEventFn, fatalOnError))
2021-08-05 07:25:46 +08:00
if err != nil {
return fmt.Errorf("subscribing filer meta change: %v", err)
}
return nil
}
2021-10-17 17:01:20 +08:00
func makeFunc(clientName string, pathPrefix string, additionalPathPrefixes []string, lastTsNs *int64, selfSignature int32,
2021-09-01 14:30:28 +08:00
processEventFn ProcessMetadataFunc, fatalOnError bool) func(client filer_pb.SeaweedFilerClient) error {
2021-08-05 07:25:46 +08:00
return func(client filer_pb.SeaweedFilerClient) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
stream, err := client.SubscribeMetadata(ctx, &filer_pb.SubscribeMetadataRequest{
2021-09-01 14:26:26 +08:00
ClientName: clientName,
PathPrefix: pathPrefix,
PathPrefixes: additionalPathPrefixes,
2021-10-17 17:01:20 +08:00
SinceNs: *lastTsNs,
2021-09-01 14:26:26 +08:00
Signature: selfSignature,
2021-08-05 07:25:46 +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 {
if fatalOnError {
glog.Fatalf("process %v: %v", resp, err)
} else {
glog.Errorf("process %v: %v", resp, err)
}
}
2021-10-17 17:01:20 +08:00
*lastTsNs = resp.TsNs
2021-08-05 07:25:46 +08:00
}
}
}
func AddOffsetFunc(processEventFn ProcessMetadataFunc, offsetInterval time.Duration, offsetFunc func(counter int64, offset int64) error) ProcessMetadataFunc {
var counter int64
var lastWriteTime time.Time
return func(resp *filer_pb.SubscribeMetadataResponse) error {
if err := processEventFn(resp); err != nil {
return err
}
counter++
if lastWriteTime.Add(offsetInterval).Before(time.Now()) {
counter = 0
lastWriteTime = time.Now()
if err := offsetFunc(counter, resp.TsNs); err != nil {
return err
}
}
return nil
}
}