2021-08-04 23:25:46 +00:00
|
|
|
package pb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2021-08-04 23:25:46 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-06-01 17:28:47 +00:00
|
|
|
type EventErrorType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
TrivialOnError EventErrorType = iota
|
|
|
|
FatalOnError
|
|
|
|
RetryForeverOnError
|
|
|
|
)
|
|
|
|
|
2023-03-22 06:01:49 +00:00
|
|
|
// MetadataFollowOption is used to control the behavior of the metadata following
|
|
|
|
// process. Part of it is used as a cursor to resume the following process.
|
|
|
|
type MetadataFollowOption struct {
|
|
|
|
ClientName string
|
|
|
|
ClientId int32
|
|
|
|
ClientEpoch int32
|
|
|
|
SelfSignature int32
|
|
|
|
PathPrefix string
|
|
|
|
AdditionalPathPrefixes []string
|
|
|
|
DirectoriesToWatch []string
|
|
|
|
StartTsNs int64
|
|
|
|
StopTsNs int64
|
|
|
|
EventErrorType EventErrorType
|
|
|
|
}
|
|
|
|
|
2021-08-04 23:25:46 +00:00
|
|
|
type ProcessMetadataFunc func(resp *filer_pb.SubscribeMetadataResponse) error
|
|
|
|
|
2023-03-22 06:01:49 +00:00
|
|
|
func FollowMetadata(filerAddress ServerAddress, grpcDialOption grpc.DialOption, option *MetadataFollowOption, processEventFn ProcessMetadataFunc) error {
|
2021-08-04 23:25:46 +00:00
|
|
|
|
2023-03-22 06:01:49 +00:00
|
|
|
err := WithFilerClient(true, option.SelfSignature, filerAddress, grpcDialOption, makeSubscribeMetadataFunc(option, processEventFn))
|
2021-08-04 23:25:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("subscribing filer meta change: %v", err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-22 06:01:49 +00:00
|
|
|
func WithFilerClientFollowMetadata(filerClient filer_pb.FilerClient, option *MetadataFollowOption, processEventFn ProcessMetadataFunc) error {
|
2021-08-04 23:25:46 +00:00
|
|
|
|
2023-03-22 06:01:49 +00:00
|
|
|
err := filerClient.WithFilerClient(true, makeSubscribeMetadataFunc(option, processEventFn))
|
2021-08-04 23:25:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("subscribing filer meta change: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-22 06:01:49 +00:00
|
|
|
func makeSubscribeMetadataFunc(option *MetadataFollowOption, processEventFn ProcessMetadataFunc) func(client filer_pb.SeaweedFilerClient) error {
|
2021-08-04 23:25:46 +00:00
|
|
|
return func(client filer_pb.SeaweedFilerClient) error {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
stream, err := client.SubscribeMetadata(ctx, &filer_pb.SubscribeMetadataRequest{
|
2023-03-22 06:01:49 +00:00
|
|
|
ClientName: option.ClientName,
|
|
|
|
PathPrefix: option.PathPrefix,
|
|
|
|
PathPrefixes: option.AdditionalPathPrefixes,
|
|
|
|
Directories: option.DirectoriesToWatch,
|
|
|
|
SinceNs: option.StartTsNs,
|
|
|
|
Signature: option.SelfSignature,
|
|
|
|
ClientId: option.ClientId,
|
|
|
|
ClientEpoch: option.ClientEpoch,
|
|
|
|
UntilNs: option.StopTsNs,
|
2021-08-04 23:25:46 +00: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 {
|
2023-03-22 06:01:49 +00:00
|
|
|
switch option.EventErrorType {
|
2022-06-01 17:28:47 +00:00
|
|
|
case TrivialOnError:
|
|
|
|
glog.Errorf("process %v: %v", resp, err)
|
|
|
|
case FatalOnError:
|
2021-08-04 23:25:46 +00:00
|
|
|
glog.Fatalf("process %v: %v", resp, err)
|
2022-06-01 17:28:47 +00:00
|
|
|
case RetryForeverOnError:
|
2023-10-01 18:33:56 +00:00
|
|
|
util.RetryUntil("followMetaUpdates", func() error {
|
2022-06-01 17:28:47 +00:00
|
|
|
return processEventFn(resp)
|
|
|
|
}, func(err error) bool {
|
|
|
|
glog.Errorf("process %v: %v", resp, err)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
default:
|
2021-08-04 23:25:46 +00:00
|
|
|
glog.Errorf("process %v: %v", resp, err)
|
|
|
|
}
|
|
|
|
}
|
2023-03-22 06:01:49 +00:00
|
|
|
option.StartTsNs = resp.TsNs
|
2021-08-04 23:25:46 +00: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()) {
|
|
|
|
lastWriteTime = time.Now()
|
|
|
|
if err := offsetFunc(counter, resp.TsNs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-10 09:00:11 +00:00
|
|
|
counter = 0
|
2021-08-04 23:25:46 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|