* rename metadata events * fix subscription filter to use NewEntry.Name for rename path matching The server-side subscription filter constructed the new path using OldEntry.Name instead of NewEntry.Name when checking if a rename event's destination matches the subscriber's path prefix. This could cause events to be incorrectly filtered when a rename changes the file name. * fix bucket events to handle rename of bucket directories onBucketEvents only checked IsCreate and IsDelete. A bucket directory rename via AtomicRenameEntry now emits a single rename event (both OldEntry and NewEntry non-nil), which matched neither check. Handle IsRename by deleting the old bucket and creating the new one. * fix replicator to handle rename events across directory boundaries Two issues fixed: 1. The replicator filtered events by checking if the key (old path) was under the source directory. Rename events now use the old path as key, so renames from outside into the watched directory were silently dropped. Now both old and new paths are checked, and cross-boundary renames are converted to create or delete. 2. NewParentPath was passed to the sink without remapping to the sink's target directory structure, causing the sink to write entries at the wrong location. Now NewParentPath is remapped alongside the key. * fix filer sync to handle rename events crossing directory boundaries The early directory-prefix filter only checked resp.Directory (old parent). Rename events now carry the old parent as Directory, so renames from outside the source path into it were dropped before reaching the existing cross-boundary handling logic. Check both old and new directories against sourcePath and excludePaths so the downstream old-key/new-key logic can properly convert these to create or delete operations. * fix metadata event path matching * fix metadata event consumers for rename targets * Fix replication rename target keys Logical rename events now reach replication sinks with distinct source and target paths.\n\nHandle non-filer sinks as delete-plus-create on the translated target key, and make the rename fallback path create at the translated target key too.\n\nAdd focused tests covering non-filer renames, filer rename updates, and the fallback path.\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix filer sync rename path scoping Use directory-boundary matching instead of raw prefix checks when classifying source and target paths during filer sync.\n\nAlso apply excludePaths per side so renames across excluded boundaries downgrade cleanly to create/delete instead of being misclassified as in-scope updates.\n\nAdd focused tests for boundary matching and rename classification.\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix replicator directory boundary checks Use directory-boundary matching instead of raw prefix checks when deciding whether a source or target path is inside the watched tree or an excluded subtree.\n\nThis prevents sibling paths such as /foo and /foobar from being misclassified during rename handling, and preserves the earlier rename-target-key fix.\n\nAdd focused tests for boundary matching and rename classification across sibling/excluded directories.\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Fix etc-remote rename-out handling Use boundary-safe source/target directory membership when classifying metadata events under DirectoryEtcRemote.\n\nThis prevents rename-out events from being processed as config updates, while still treating them as removals where appropriate for the remote sync and remote gateway command paths.\n\nAdd focused tests for update/removal classification and sibling-prefix handling.\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Defer rename events until commit Queue logical rename metadata events during atomic and streaming renames and publish them only after the transaction commits successfully.\n\nThis prevents subscribers from seeing delete or logical rename events for operations that later fail during delete or commit.\n\nAlso serialize notification.Queue swaps in rename tests and add failure-path coverage.\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Skip descendant rename target lookups Avoid redundant target lookups during recursive directory renames once the destination subtree is known absent.\n\nThe recursive move path now inserts known-absent descendants directly, and the test harness exercises prefixed directory listing so the optimization is covered by a directory rename regression test.\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * Tighten rename review tests Return filer_pb.ErrNotFound from the bucket tracking store test stub so it follows the FilerStore contract, and add a webhook filter case for same-name renames across parent directories.\n\nCo-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix HardLinkId format verb in InsertEntryKnownAbsent error HardLinkId is a byte slice. %d prints each byte as a decimal number which is not useful for an identifier. Use %x to match the log line two lines above. * only skip descendant target lookup when source and dest use same store moveFolderSubEntries unconditionally passed skipTargetLookup=true for every descendant. This is safe when all paths resolve to the same underlying store, but with path-specific store configuration a child's destination may map to a different backend that already holds an entry at that path. Use FilerStoreWrapper.SameActualStore to check per-child and fall back to the full CreateEntry path when stores differ. * add nil and create edge-case tests for metadata event scope helpers * extract pathIsEqualOrUnder into util.IsEqualOrUnder Identical implementations existed in both replication/replicator.go and command/filer_sync.go. Move to util.IsEqualOrUnder (alongside the existing FullPath.IsUnder) and remove the duplicates. * use MetadataEventTargetDirectory for new-side directory in filer sync The new-side directory checks and sourceNewKey computation used message.NewParentPath directly. If NewParentPath were empty (legacy events, older filer versions during rolling upgrades), sourceNewKey would be wrong (/filename instead of /dir/filename) and the UpdateEntry parent path rewrite would panic on slice bounds. Derive targetDir once from MetadataEventTargetDirectory, which falls back to resp.Directory when NewParentPath is empty, and use it consistently for all new-side checks and the sink parent path.
398 lines
12 KiB
Go
398 lines
12 KiB
Go
package weed_server
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
)
|
|
|
|
// slowStream simulates a gRPC stream with configurable per-Send latency.
|
|
// It counts individual events including those packed inside batches.
|
|
type slowStream struct {
|
|
sendDelay time.Duration
|
|
sends int64 // number of stream.Send() calls
|
|
eventsSent int64 // total events (1 + len(Events) per Send)
|
|
}
|
|
|
|
func (s *slowStream) Send(msg *filer_pb.SubscribeMetadataResponse) error {
|
|
time.Sleep(s.sendDelay)
|
|
atomic.AddInt64(&s.sends, 1)
|
|
atomic.AddInt64(&s.eventsSent, 1+int64(len(msg.Events)))
|
|
return nil
|
|
}
|
|
|
|
type collectingStream struct {
|
|
messages []*filer_pb.SubscribeMetadataResponse
|
|
}
|
|
|
|
func (s *collectingStream) Send(msg *filer_pb.SubscribeMetadataResponse) error {
|
|
s.messages = append(s.messages, msg)
|
|
return nil
|
|
}
|
|
|
|
func makeEvent(dir, name string, tsNs int64) *filer_pb.SubscribeMetadataResponse {
|
|
return &filer_pb.SubscribeMetadataResponse{
|
|
Directory: dir,
|
|
TsNs: tsNs,
|
|
EventNotification: &filer_pb.EventNotification{
|
|
NewEntry: &filer_pb.Entry{
|
|
Name: name,
|
|
IsDirectory: false,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
// makeOldEvents creates events with timestamps far in the past (triggers batch mode).
|
|
func makeOldEvents(n int) []*filer_pb.SubscribeMetadataResponse {
|
|
baseTs := time.Now().Add(-time.Hour).UnixNano() // 1 hour ago → well past batchBehindThreshold
|
|
events := make([]*filer_pb.SubscribeMetadataResponse, n)
|
|
for i := range events {
|
|
events[i] = makeEvent("/bucket/dir", fmt.Sprintf("file%06d.txt", i), baseTs+int64(i))
|
|
}
|
|
return events
|
|
}
|
|
|
|
// makeRecentEvents creates events with timestamps close to now (sends one-by-one).
|
|
func makeRecentEvents(n int) []*filer_pb.SubscribeMetadataResponse {
|
|
baseTs := time.Now().UnixNano()
|
|
events := make([]*filer_pb.SubscribeMetadataResponse, n)
|
|
for i := range events {
|
|
events[i] = makeEvent("/bucket/dir", fmt.Sprintf("file%06d.txt", i), baseTs+int64(i))
|
|
}
|
|
return events
|
|
}
|
|
|
|
// TestPipelinedSenderThroughput compares direct (blocking) stream.Send with
|
|
// the pipelinedSender with adaptive batching.
|
|
//
|
|
// Simulates realistic backlog catch-up: the reader loads one log file at a time
|
|
// from a volume server (fileReadDelay per file), producing a burst of ~300
|
|
// events. The sender has per-Send gRPC overhead (sendDelay).
|
|
//
|
|
// - Direct: serial — each event: send one-by-one between file reads
|
|
// - Pipelined+batched: file I/O overlaps with batched sending
|
|
func TestPipelinedSenderThroughput(t *testing.T) {
|
|
const (
|
|
eventsPerFile = 300 // events in one minute-log file
|
|
numFiles = 7 // files to process
|
|
totalEvents = eventsPerFile * numFiles // 2100
|
|
fileReadDelay = 5 * time.Millisecond // volume server read per log file
|
|
sendDelay = 50 * time.Microsecond // gRPC round-trip per Send()
|
|
)
|
|
|
|
// Partition old events into file-sized bursts
|
|
files := make([][]*filer_pb.SubscribeMetadataResponse, numFiles)
|
|
baseTs := time.Now().Add(-time.Hour).UnixNano()
|
|
for f := 0; f < numFiles; f++ {
|
|
files[f] = make([]*filer_pb.SubscribeMetadataResponse, eventsPerFile)
|
|
for i := 0; i < eventsPerFile; i++ {
|
|
idx := f*eventsPerFile + i
|
|
files[f][i] = makeEvent("/bucket/dir", fmt.Sprintf("file%06d.txt", idx), baseTs+int64(idx))
|
|
}
|
|
}
|
|
|
|
// --- Direct (old behavior): read file, send events one-by-one, repeat ---
|
|
var directRate float64
|
|
t.Run("direct_send", func(t *testing.T) {
|
|
stream := &slowStream{sendDelay: sendDelay}
|
|
|
|
start := time.Now()
|
|
for _, file := range files {
|
|
time.Sleep(fileReadDelay) // read log file from volume server
|
|
for _, ev := range file {
|
|
if err := stream.Send(ev); err != nil {
|
|
t.Fatalf("send error: %v", err)
|
|
}
|
|
}
|
|
}
|
|
elapsed := time.Since(start)
|
|
|
|
directRate = float64(stream.eventsSent) / elapsed.Seconds()
|
|
t.Logf("direct: %d events %4d sends %v %6.0f events/sec",
|
|
stream.eventsSent, stream.sends, elapsed.Round(time.Millisecond), directRate)
|
|
})
|
|
|
|
// --- Pipelined + batched (new behavior): file reads overlap with batched sends ---
|
|
var batchedRate float64
|
|
t.Run("pipelined_batched_send", func(t *testing.T) {
|
|
stream := &slowStream{sendDelay: sendDelay}
|
|
sender := newPipelinedSender(stream, 1024, true)
|
|
|
|
start := time.Now()
|
|
for _, file := range files {
|
|
time.Sleep(fileReadDelay) // read log file from volume server
|
|
for _, ev := range file {
|
|
if err := sender.Send(ev); err != nil {
|
|
t.Fatalf("send error: %v", err)
|
|
}
|
|
}
|
|
}
|
|
if err := sender.Close(); err != nil {
|
|
t.Fatalf("close error: %v", err)
|
|
}
|
|
elapsed := time.Since(start)
|
|
|
|
batchedRate = float64(stream.eventsSent) / elapsed.Seconds()
|
|
t.Logf("pipelined+batch: %d events %4d sends %v %6.0f events/sec",
|
|
stream.eventsSent, stream.sends, elapsed.Round(time.Millisecond), batchedRate)
|
|
})
|
|
|
|
if directRate > 0 {
|
|
t.Logf("Speedup: %.1fx (pipelined+batched vs direct)", batchedRate/directRate)
|
|
}
|
|
}
|
|
|
|
func TestEachEventNotificationFnMatchesRenameTargetsForAllWatchTypes(t *testing.T) {
|
|
fs := &FilerServer{
|
|
option: &FilerOption{Host: pb.ServerAddress("127.0.0.1:8888")},
|
|
filer: &filer.Filer{Signature: 123},
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
req *filer_pb.SubscribeMetadataRequest
|
|
}{
|
|
{
|
|
name: "additional path prefix",
|
|
req: &filer_pb.SubscribeMetadataRequest{
|
|
ClientName: "test",
|
|
PathPrefix: "/data/",
|
|
PathPrefixes: []string{"/etc/remote"},
|
|
},
|
|
},
|
|
{
|
|
name: "directory watch",
|
|
req: &filer_pb.SubscribeMetadataRequest{
|
|
ClientName: "test",
|
|
PathPrefix: "/data/",
|
|
Directories: []string{"/etc/iam/identities"},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
stream := &collectingStream{}
|
|
eachEventFn := fs.eachEventNotificationFn(tt.req, stream, "client")
|
|
|
|
newDir := "/etc/remote"
|
|
if len(tt.req.Directories) > 0 {
|
|
newDir = tt.req.Directories[0]
|
|
}
|
|
err := eachEventFn("/tmp", &filer_pb.EventNotification{
|
|
OldEntry: &filer_pb.Entry{Name: "old"},
|
|
NewEntry: &filer_pb.Entry{Name: "new"},
|
|
NewParentPath: newDir,
|
|
}, time.Now().UnixNano())
|
|
if err != nil {
|
|
t.Fatalf("eachEventFn: %v", err)
|
|
}
|
|
if len(stream.messages) != 1 {
|
|
t.Fatalf("messages sent = %d, want 1", len(stream.messages))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestBatchingAdaptive verifies the adaptive behavior: old events are batched,
|
|
// recent events are sent one-by-one.
|
|
func TestBatchingAdaptive(t *testing.T) {
|
|
const numEvents = 500
|
|
|
|
t.Run("old_events_are_batched", func(t *testing.T) {
|
|
stream := &slowStream{sendDelay: 10 * time.Microsecond}
|
|
sender := newPipelinedSender(stream, 1024, true)
|
|
|
|
// Push all events at once (no read delay) so the sender can batch aggressively
|
|
for _, ev := range makeOldEvents(numEvents) {
|
|
sender.Send(ev)
|
|
}
|
|
sender.Close()
|
|
|
|
t.Logf("old events: %d events in %d sends (avg batch size: %.1f)",
|
|
stream.eventsSent, stream.sends, float64(stream.eventsSent)/float64(stream.sends))
|
|
|
|
if stream.sends >= int64(numEvents) {
|
|
t.Errorf("expected batching to reduce sends below %d, got %d", numEvents, stream.sends)
|
|
}
|
|
})
|
|
|
|
t.Run("recent_events_sent_individually", func(t *testing.T) {
|
|
stream := &slowStream{sendDelay: 10 * time.Microsecond}
|
|
sender := newPipelinedSender(stream, 1024, true)
|
|
|
|
for _, ev := range makeRecentEvents(numEvents) {
|
|
sender.Send(ev)
|
|
}
|
|
sender.Close()
|
|
|
|
t.Logf("recent events: %d events in %d sends (avg batch size: %.1f)",
|
|
stream.eventsSent, stream.sends, float64(stream.eventsSent)/float64(stream.sends))
|
|
|
|
if stream.sends != int64(numEvents) {
|
|
t.Errorf("expected 1:1 sends for recent events, got %d sends for %d events", stream.sends, numEvents)
|
|
}
|
|
})
|
|
}
|
|
|
|
// errorStreamImpl is a metadataStreamSender that returns an error after N sends.
|
|
type errorStreamImpl struct {
|
|
failAfter int
|
|
err error
|
|
count int64
|
|
}
|
|
|
|
func (s *errorStreamImpl) Send(msg *filer_pb.SubscribeMetadataResponse) error {
|
|
n := atomic.AddInt64(&s.count, 1)
|
|
if int(n) > s.failAfter {
|
|
return s.err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// TestPipelinedSenderErrorPropagation verifies that when stream.Send fails,
|
|
// the error propagates to pipelinedSender.Send callers and Close.
|
|
func TestPipelinedSenderErrorPropagation(t *testing.T) {
|
|
sendErr := fmt.Errorf("connection reset")
|
|
|
|
t.Run("send_returns_error", func(t *testing.T) {
|
|
// Stream fails after 5 successful sends
|
|
stream := &errorStreamImpl{failAfter: 5, err: sendErr}
|
|
sender := newPipelinedSender(stream, 4, true)
|
|
|
|
var lastErr error
|
|
for i := 0; i < 100; i++ {
|
|
ev := makeOldEvents(1)[0]
|
|
if err := sender.Send(ev); err != nil {
|
|
lastErr = err
|
|
break
|
|
}
|
|
}
|
|
|
|
if lastErr == nil {
|
|
t.Fatal("expected Send to return an error after stream failure")
|
|
}
|
|
t.Logf("Send returned error after stream failure: %v", lastErr)
|
|
})
|
|
|
|
t.Run("close_returns_error_if_not_consumed", func(t *testing.T) {
|
|
// Stream fails on the very first send — error surfaces via Close
|
|
// since Send may have already returned before the sender goroutine
|
|
// processes the message.
|
|
stream := &errorStreamImpl{failAfter: 0, err: sendErr}
|
|
sender := newPipelinedSender(stream, 1024, true)
|
|
|
|
ev := makeOldEvents(1)[0]
|
|
sender.Send(ev)
|
|
|
|
closeErr := sender.Close()
|
|
if closeErr == nil {
|
|
t.Log("Close returned nil (error was consumed by Send)")
|
|
} else {
|
|
t.Logf("Close returned error: %v", closeErr)
|
|
}
|
|
})
|
|
}
|
|
|
|
// TestPipelinedSingleVsParallelStreams shows 1 pipelined+batched stream vs
|
|
// N parallel pipelined+batched streams, using the realistic burst-read pattern.
|
|
func TestPipelinedSingleVsParallelStreams(t *testing.T) {
|
|
const (
|
|
numDirs = 10
|
|
filesPerDir = 7 // log files per directory
|
|
eventsPerFile = 300 // events per log file
|
|
totalEvents = numDirs * filesPerDir * eventsPerFile // 21000
|
|
fileReadDelay = 5 * time.Millisecond
|
|
sendDelay = 50 * time.Microsecond
|
|
)
|
|
|
|
// Generate partitioned OLD events grouped into file-sized bursts
|
|
baseTs := time.Now().Add(-time.Hour).UnixNano()
|
|
type logFile []*filer_pb.SubscribeMetadataResponse
|
|
// partitions[dir][file][event]
|
|
partitions := make([][]logFile, numDirs)
|
|
var allFiles []logFile
|
|
idx := 0
|
|
for d := 0; d < numDirs; d++ {
|
|
dir := fmt.Sprintf("/bucket/dir%03d", d)
|
|
for f := 0; f < filesPerDir; f++ {
|
|
file := make(logFile, eventsPerFile)
|
|
for i := 0; i < eventsPerFile; i++ {
|
|
file[i] = makeEvent(dir, fmt.Sprintf("file%06d.txt", idx), baseTs+int64(idx))
|
|
idx++
|
|
}
|
|
partitions[d] = append(partitions[d], file)
|
|
allFiles = append(allFiles, file)
|
|
}
|
|
}
|
|
|
|
// simulatePipeline: read files with I/O delay, push events, send via pipelinedSender
|
|
simulatePipeline := func(files []logFile) (eventsSent, sends int64, elapsed time.Duration, err error) {
|
|
stream := &slowStream{sendDelay: sendDelay}
|
|
sender := newPipelinedSender(stream, 1024, true)
|
|
|
|
start := time.Now()
|
|
outer:
|
|
for _, file := range files {
|
|
time.Sleep(fileReadDelay) // volume server read
|
|
for _, ev := range file {
|
|
if err = sender.Send(ev); err != nil {
|
|
break outer
|
|
}
|
|
}
|
|
}
|
|
if closeErr := sender.Close(); closeErr != nil && err == nil {
|
|
err = closeErr
|
|
}
|
|
elapsed = time.Since(start)
|
|
eventsSent = atomic.LoadInt64(&stream.eventsSent)
|
|
sends = atomic.LoadInt64(&stream.sends)
|
|
return
|
|
}
|
|
|
|
var singleRate float64
|
|
t.Run("1_pipelined_stream", func(t *testing.T) {
|
|
eventsSent, sends, elapsed, err := simulatePipeline(allFiles)
|
|
if err != nil {
|
|
t.Fatalf("pipeline error: %v", err)
|
|
}
|
|
singleRate = float64(eventsSent) / elapsed.Seconds()
|
|
t.Logf("1 stream: %5d events %4d sends %v %7.0f events/sec",
|
|
eventsSent, sends, elapsed.Round(time.Millisecond), singleRate)
|
|
})
|
|
|
|
var parallelRate float64
|
|
t.Run("10_pipelined_streams", func(t *testing.T) {
|
|
var totalEventsSent, totalSends int64
|
|
var wg sync.WaitGroup
|
|
|
|
start := time.Now()
|
|
for d := 0; d < numDirs; d++ {
|
|
wg.Add(1)
|
|
go func(files []logFile) {
|
|
defer wg.Done()
|
|
eventsSent, sends, _, _ := simulatePipeline(files)
|
|
atomic.AddInt64(&totalEventsSent, eventsSent)
|
|
atomic.AddInt64(&totalSends, sends)
|
|
}(partitions[d])
|
|
}
|
|
wg.Wait()
|
|
elapsed := time.Since(start)
|
|
|
|
parallelRate = float64(totalEventsSent) / elapsed.Seconds()
|
|
t.Logf("%d streams: %5d events %4d sends %v %7.0f events/sec",
|
|
numDirs, totalEventsSent, totalSends, elapsed.Round(time.Millisecond), parallelRate)
|
|
})
|
|
|
|
if singleRate > 0 && parallelRate > 0 {
|
|
t.Logf("Speedup: %.1fx (%d parallel pipelined streams vs 1)", parallelRate/singleRate, numDirs)
|
|
}
|
|
}
|