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:
Chris LuandClaude Opus 5.5 authored and GitHub committed 2026-11-15 20:53:41 -08:00
1 parent f13b763552
commit 8e15110b4f
10 files changed
+414 -18

No files matched your search

+42 -16
View File
@@ -194,8 +194,11 @@ type LogBuffer struct {
subscribers map[string]*subscription // subscriberID -> shared notification channel
// Notified only when a flush lands, for readers that cannot act on an append
flushSubscribers map[string]*subscription
isStopping *atomic.Bool
shutdownCh chan struct{} // closed by ShutdownLogBuffer to wake blocked subscribers
// wakeFilters skip a subscriber's append wake-up when the entry's partition
// key cannot interest it; it still reads the entry on its next poll.
wakeFilters map[string]func(key []byte) bool
isStopping *atomic.Bool
shutdownCh chan struct{} // closed by ShutdownLogBuffer to wake blocked subscribers
// flushDroppedBytes counts bytes the flushFn gave up on during shutdown.
// loopFlush reads it after each window so an abandoned flush does not
// advance the flushed watermark or wake subscribers as if it had landed.
@@ -232,6 +235,7 @@ func NewLogBuffer(name string, flushInterval time.Duration, flushFn LogFlushFunc
notifyFn: notifyFn,
subscribers: make(map[string]*subscription),
flushSubscribers: make(map[string]*subscription),
wakeFilters: make(map[string]func(key []byte) bool),
flushChan: make(chan *dataToFlush, flushQueueDepth),
isStopping: new(atomic.Bool),
shutdownCh: make(chan struct{}),
@@ -259,6 +263,7 @@ func NewLogBuffer(name string, flushInterval time.Duration, flushFn LogFlushFunc
type subscription struct {
notifyChan chan struct{}
refCount int
wants func(key []byte) bool
}
func registerSubscription(subscriptions map[string]*subscription, subscriberID string) chan struct{} {
@@ -292,7 +297,9 @@ func (logBuffer *LogBuffer) RegisterSubscriber(subscriberID string) chan struct{
logBuffer.subscribersMu.Lock()
defer logBuffer.subscribersMu.Unlock()
return registerSubscription(logBuffer.subscribers, subscriberID)
notifyChan := registerSubscription(logBuffer.subscribers, subscriberID)
logBuffer.subscribers[subscriberID].wants = logBuffer.wakeFilters[subscriberID]
return notifyChan
}
// UnregisterSubscriber removes a subscriber and closes its notification channel
@@ -374,23 +381,42 @@ func (logBuffer *LogBuffer) IsOffsetInMemory(offset int64) bool {
return false
}
// notifySubscribers sends notifications to all registered subscribers
// Non-blocking: uses select with default to avoid blocking on full channels
func (logBuffer *LogBuffer) notifySubscribers() {
// SetSubscriberWakeFilter limits append wake-ups of the subscriber registered
// as subscriberID to entries whose partition key satisfies wants; a nil wants
// removes the filter. Flushes and keyless appends still wake every subscriber.
func (logBuffer *LogBuffer) SetSubscriberWakeFilter(subscriberID string, wants func(key []byte) bool) {
logBuffer.subscribersMu.Lock()
defer logBuffer.subscribersMu.Unlock()
if wants == nil {
delete(logBuffer.wakeFilters, subscriberID)
} else {
logBuffer.wakeFilters[subscriberID] = wants
}
if sub, ok := logBuffer.subscribers[subscriberID]; ok {
sub.wants = wants
}
}
// NotifySubscribers wakes the subscribers interested in key, for entries that
// also concern a key other than the partition key they were appended under.
func (logBuffer *LogBuffer) NotifySubscribers(key []byte) {
logBuffer.notifySubscribers(key)
}
// notifySubscribers wakes the subscribers interested in key, or all of them
// for an empty key. Non-blocking: a full channel already holds a wake-up.
func (logBuffer *LogBuffer) notifySubscribers(key []byte) {
logBuffer.subscribersMu.RLock()
defer logBuffer.subscribersMu.RUnlock()
if len(logBuffer.subscribers) == 0 {
return // No subscribers, skip notification
}
for _, sub := range logBuffer.subscribers {
if len(key) > 0 && sub.wants != nil && !sub.wants(key) {
continue
}
select {
case sub.notifyChan <- struct{}{}:
// Notification sent successfully
default:
// Channel full - subscriber hasn't consumed previous notification yet
// This is OK because one notification is sufficient to wake the subscriber
}
}
}
@@ -468,7 +494,7 @@ func (logBuffer *LogBuffer) AddLogEntryToBuffer(logEntry *filer_pb.LogEntry) err
logBuffer.notifyFn()
}
// Notify all registered subscribers instantly (<1ms latency)
logBuffer.notifySubscribers()
logBuffer.notifySubscribers(logEntry.Key)
}
}()
@@ -607,7 +633,7 @@ func (logBuffer *LogBuffer) AddDataToBufferAndGetTs(partitionKey, data []byte, p
logBuffer.notifyFn()
}
// Notify all registered subscribers instantly (<1ms latency)
logBuffer.notifySubscribers()
logBuffer.notifySubscribers(partitionKey)
}
}()
@@ -823,7 +849,7 @@ func (logBuffer *LogBuffer) loopFlush() {
if logBuffer.notifyFn != nil {
logBuffer.notifyFn()
}
logBuffer.notifySubscribers()
logBuffer.notifySubscribers(nil)
logBuffer.notifyFlushSubscribers()
// Signal completion if there's a callback channel
@@ -0,0 +1,97 @@
package log_buffer
import (
"bytes"
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
)
func woken(ch <-chan struct{}) bool {
select {
case <-ch:
return true
case <-time.After(50 * time.Millisecond):
return false
}
}
func TestSubscriberWakeFilter(t *testing.T) {
flushFn := func(*LogBuffer, time.Time, time.Time, []byte, int64, int64) {}
logBuffer := NewLogBuffer("test", time.Minute, flushFn, nil, nil)
defer logBuffer.ShutdownLogBuffer()
wantsA := func(key []byte) bool { return bytes.Equal(key, []byte("a")) }
// set before registration, the way a reader is set up ahead of its loop
logBuffer.SetSubscriberWakeFilter("filtered", wantsA)
filtered := logBuffer.RegisterSubscriber("filtered")
defer logBuffer.UnregisterSubscriber("filtered")
all := logBuffer.RegisterSubscriber("all")
defer logBuffer.UnregisterSubscriber("all")
late := logBuffer.RegisterSubscriber("late")
defer logBuffer.UnregisterSubscriber("late")
logBuffer.SetSubscriberWakeFilter("late", wantsA)
if err := logBuffer.AddDataToBuffer([]byte("b"), []byte("v"), 0); err != nil {
t.Fatal(err)
}
if woken(filtered) || woken(late) {
t.Fatal("an append under another key must not wake filtered subscribers")
}
if !woken(all) {
t.Fatal("an unfiltered subscriber must wake on every append")
}
if err := logBuffer.AddDataToBuffer([]byte("a"), []byte("v"), 0); err != nil {
t.Fatal(err)
}
if !woken(filtered) || !woken(late) || !woken(all) {
t.Fatal("an append under a wanted key must wake every subscriber")
}
logBuffer.NotifySubscribers([]byte("a"))
if !woken(filtered) {
t.Fatal("NotifySubscribers must wake subscribers that want the key")
}
logBuffer.ForceFlush()
if !woken(filtered) || !woken(late) {
t.Fatal("a flush must wake filtered subscribers")
}
logBuffer.SetSubscriberWakeFilter("late", nil)
if err := logBuffer.AddDataToBuffer([]byte("b"), []byte("v"), 0); err != nil {
t.Fatal(err)
}
if !woken(late) {
t.Fatal("clearing the filter must restore wake-ups")
}
}
func TestSubscriberWakeFilterStillDelivers(t *testing.T) {
flushFn := func(*LogBuffer, time.Time, time.Time, []byte, int64, int64) {}
logBuffer := NewLogBuffer("test", time.Minute, flushFn, nil, nil)
defer logBuffer.ShutdownLogBuffer()
logBuffer.SetSubscriberWakeFilter("reader", func([]byte) bool { return false })
start := NewMessagePosition(time.Now().UnixNano(), -2)
got := make(chan []byte, 1)
go logBuffer.LoopProcessLogData("reader", start, 0, func() bool { return true }, func(logEntry *filer_pb.LogEntry) (bool, error) {
got <- bytes.Clone(logEntry.Data)
return true, nil
})
time.Sleep(50 * time.Millisecond)
if err := logBuffer.AddDataToBuffer([]byte("unwanted"), []byte("v"), 0); err != nil {
t.Fatal(err)
}
select {
case data := <-got:
if string(data) != "v" {
t.Fatalf("got %q", data)
}
case <-time.After(2 * notificationHealthCheckInterval):
t.Fatal("a reader that sleeps through an append must still read it on its next poll")
}
}