mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-11-16 08:45:22 +01:00
filer: wake metadata subscribers only for directories they can match (#12669)
* log_buffer: let a subscriber skip wake-ups for unwanted partition keys Every append woke every registered subscriber. SetSubscriberWakeFilter lets a reader sleep through appends whose partition key it would filter out; it still reads them on its next poll. Flushes and keyless appends wake everyone. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer_pb: decide subscription matches from an event's directories MetadataDirectoryMayMatchSubscription rules out events whose directory no child path of can match the subscription, and ScanMetadataEventDirectories reads an event's directory and new parent path without allocating. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: wake subscribers of a move's destination directory Appends are keyed by the source directory, so a filtered subscriber of the destination would otherwise only see the move on its next poll. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: wake metadata subscribers only for directories they can match Every metadata event woke and was scanned by every subscriber, so filer CPU grew with the number of subscribers watching other paths. Readers now register a directory wake filter, and the per-entry prefilter rules entries out from their directories before the skeleton scan. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * filer: test that moves into a subscribed prefix pass the prefilter Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
f13b763552
commit
8e15110b4f
10 files changed
+414
-18
No files matched your search
@@ -683,6 +683,8 @@ func (fs *FilerServer) SubscribeMetadata(req *filer_pb.SubscribeMetadataRequest,
|
||||
aggReaderName := fmt.Sprintf("aggMeta:%s:%d:%d", clientName, req.ClientId, req.ClientEpoch)
|
||||
aggNotifyChan := fs.filer.MetaAggregator.MetaLogBuffer.RegisterSubscriber(aggNotifyName)
|
||||
defer fs.filer.MetaAggregator.MetaLogBuffer.UnregisterSubscriber(aggNotifyName)
|
||||
fs.filer.MetaAggregator.MetaLogBuffer.SetSubscriberWakeFilter(aggReaderName, subscriptionWakeFilter(req))
|
||||
defer fs.filer.MetaAggregator.MetaLogBuffer.SetSubscriberWakeFilter(aggReaderName, nil)
|
||||
|
||||
gapStall := &gapStallReporter{scope: "aggregated", clientName: clientName, pathPrefix: req.PathPrefix}
|
||||
defer gapStall.close()
|
||||
@@ -1127,6 +1129,8 @@ func (fs *FilerServer) subscribeLocalMetadata(req *filer_pb.SubscribeMetadataReq
|
||||
localReaderName := fmt.Sprintf("localMeta:%s:%d:%d", clientName, req.ClientId, req.ClientEpoch)
|
||||
localFlushChan := fs.filer.LocalMetaLogBuffer.RegisterFlushSubscriber(localNotifyName)
|
||||
defer fs.filer.LocalMetaLogBuffer.UnregisterFlushSubscriber(localNotifyName)
|
||||
fs.filer.LocalMetaLogBuffer.SetSubscriberWakeFilter(localReaderName, subscriptionWakeFilter(req))
|
||||
defer fs.filer.LocalMetaLogBuffer.SetSubscriberWakeFilter(localReaderName, nil)
|
||||
|
||||
gapStall := &gapStallReporter{scope: "local", clientName: clientName, pathPrefix: req.PathPrefix}
|
||||
defer gapStall.close()
|
||||
@@ -1305,11 +1309,32 @@ func (fs *FilerServer) subscribeLocalMetadata(req *filer_pb.SubscribeMetadataReq
|
||||
|
||||
}
|
||||
|
||||
// subscriptionWakeFilter lets a reader sleep through appends in directories
|
||||
// the subscription cannot match; it reads them on its next poll instead.
|
||||
func subscriptionWakeFilter(req *filer_pb.SubscribeMetadataRequest) func(key []byte) bool {
|
||||
if req.PathPrefix == "" {
|
||||
return nil
|
||||
}
|
||||
return func(key []byte) bool {
|
||||
return filer_pb.MetadataDirectoryMayMatchSubscription(key, req.PathPrefix, req.PathPrefixes, req.Directories)
|
||||
}
|
||||
}
|
||||
|
||||
func eachLogEntryFn(ctx context.Context, req *filer_pb.SubscribeMetadataRequest, sender metadataStreamSender, eachEventNotificationFn func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64, mutationTsNs int64) error, filtered *int64) log_buffer.EachLogEntryFuncType {
|
||||
// A shallow scan of the path fields skips unmarshaling chunk-heavy events
|
||||
// this subscriber would filter out anyway; scan surprises fall back to the
|
||||
// full decode. Only a delivery resets the shared unsynced-events counter.
|
||||
prefilter := req.PathPrefix != "" || len(req.PathPrefixes) > 0 || len(req.Directories) > 0
|
||||
mayMatch := subscriptionWakeFilter(req)
|
||||
skip := func(data []byte) bool {
|
||||
if mayMatch != nil {
|
||||
if dir, target, ok := filer_pb.ScanMetadataEventDirectories(data); ok && !mayMatch(dir) && (len(target) == 0 || !mayMatch(target)) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
skeleton, ok := filer_pb.ScanMetadataEventSkeleton(data)
|
||||
return ok && !filer_pb.MetadataEventMatchesSubscription(skeleton, req.PathPrefix, req.PathPrefixes, req.Directories)
|
||||
}
|
||||
return func(logEntry *filer_pb.LogEntry) (bool, error) {
|
||||
// A cancelled context ends the pass here: neither loop checks it
|
||||
// per entry, and the send path alone cannot cover filtered entries.
|
||||
@@ -1317,8 +1342,7 @@ func eachLogEntryFn(ctx context.Context, req *filer_pb.SubscribeMetadataRequest,
|
||||
return true, nil
|
||||
}
|
||||
if prefilter {
|
||||
if skeleton, ok := filer_pb.ScanMetadataEventSkeleton(logEntry.Data); ok &&
|
||||
!filer_pb.MetadataEventMatchesSubscription(skeleton, req.PathPrefix, req.PathPrefixes, req.Directories) {
|
||||
if skip(logEntry.Data) {
|
||||
*filtered++
|
||||
if *filtered > MaxUnsyncedEvents {
|
||||
if err := sender.Send(&filer_pb.SubscribeMetadataResponse{
|
||||
|
||||
@@ -158,3 +158,33 @@ func TestEachLogEntryFnFilteredHeartbeatUsesStreamPosition(t *testing.T) {
|
||||
t.Fatalf("heartbeat ts = %+v, want stream position 101", sender.sent)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEachLogEntryFnPrefilterKeepsMovesIntoPrefix(t *testing.T) {
|
||||
req := &filer_pb.SubscribeMetadataRequest{PathPrefix: "/data/pvc-7/"}
|
||||
var delivered []string
|
||||
var unsyncedEvents int64
|
||||
fn := eachLogEntryFn(context.Background(), req, &recordingSender{}, func(dirPath string, eventNotification *filer_pb.EventNotification, tsNs int64, mutationTsNs int64) error {
|
||||
delivered = append(delivered, eventNotification.NewParentPath)
|
||||
return nil
|
||||
}, &unsyncedEvents)
|
||||
|
||||
for i, target := range []string{"/data/pvc-7", "/data/pvc-1/sub"} {
|
||||
data, err := proto.Marshal(&filer_pb.SubscribeMetadataResponse{
|
||||
Directory: "/data/pvc-1",
|
||||
EventNotification: &filer_pb.EventNotification{
|
||||
OldEntry: &filer_pb.Entry{Name: "a.log"},
|
||||
NewEntry: &filer_pb.Entry{Name: "a.log"},
|
||||
NewParentPath: target,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := fn(&filer_pb.LogEntry{TsNs: int64(i + 1), Data: data}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if len(delivered) != 1 || delivered[0] != "/data/pvc-7" {
|
||||
t.Fatalf("only the move into the prefix must reach the notification fn, got %v", delivered)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user