//go:build !windows package mount import ( "bytes" "context" "fmt" "sync" "testing" "time" "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" "github.com/seaweedfs/seaweedfs/weed/util" ) // A deferred create is published by the flush helper, after which the export // goes through and carries a clean handle with a base. func TestFlushForHandoverPublishesDeferredCreate(t *testing.T) { wfs := newHandoverWFS(t) wfs.option.WritebackCache = false // a plain mount: the flush is synchronous startFakeFiler(t, wfs, &fakeFilerServer{}) fh, _ := openUnpublishedCreate(t, wfs, util.FullPath("/dir/file"), nil) if err := wfs.ExportHandoverState(&bytes.Buffer{}); err == nil { t.Fatalf("export before the flush: %v", err) } ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second) defer cancel() if err := wfs.FlushForHandover(ctx); err != nil { t.Fatalf("flush: %v", err) } if fh.dirtyMetadata || fh.baseEntry.Load() == nil || fh.dirtyPages.HasPendingUploads() { t.Fatalf("handle still dirty: metadata %v, base %v", fh.dirtyMetadata, fh.baseEntry.Load()) } data := exportToBytes(t, wfs) dst := newHandoverWFS(t) dst.option.WritebackCache = false if err := dst.ImportHandoverState(bytes.NewReader(data)); err != nil { t.Fatalf("import: %v", err) } got := dst.fhMap.inode2fh[fh.inode] if got == nil || got.dirtyMetadata || got.baseEntry.Load() == nil { t.Fatalf("restored handle: %+v", got) } } func TestFlushForHandoverWaitsForBackgroundWork(t *testing.T) { wfs := newHandoverWFS(t) drain := make(chan struct{}) wfs.pendingAsyncFlush[7] = drain wfs.asyncCreates.Add(1) go func() { time.Sleep(50 * time.Millisecond) close(drain) time.Sleep(50 * time.Millisecond) wfs.asyncCreates.Add(-1) }() ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() start := time.Now() if err := wfs.FlushForHandover(ctx); err != nil { t.Fatalf("flush: %v", err) } if time.Since(start) < 90*time.Millisecond { t.Fatalf("returned after %v, before the background work finished", time.Since(start)) } } func TestFlushForHandoverHonoursDeadline(t *testing.T) { wfs := newHandoverWFS(t) wfs.pendingAsyncFlush[7] = make(chan struct{}) // never finishes ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond) defer cancel() if err := wfs.FlushForHandover(ctx); err == nil { t.Fatal("expected the deadline to end the wait") } } // The first run happens while requests are served: writes and flushes change // a handle's dirty state under its flush lock, and opens and closes change the // handle map, while the flush looks for what to publish. Meant for -race. func TestFlushForHandoverAlongsideWritesAndCloses(t *testing.T) { wfs := newHandoverWFS(t) plain := func(name string) *filer_pb.Entry { return &filer_pb.Entry{Name: name, Attributes: &filer_pb.FuseAttributes{FileMode: 0o644}} } now := time.Now().Unix() var written []*FileHandle for i := 0; i < 8; i++ { path := util.FullPath(fmt.Sprintf("/w%d", i)) inode := wfs.inodeToPath.Lookup(path, now, false, false, 0, true) fh, _, _, _ := wfs.fhMap.AcquireFileHandle(wfs, inode, plain(path.Name()), 1, 1, false) fh.RememberPath(path) written = append(written, fh) } churned := wfs.inodeToPath.Lookup("/churn", now, false, false, 0, true) stop := make(chan struct{}) var wg sync.WaitGroup wg.Add(2) go func() { // a write, and the flush that publishes it, on every handle defer wg.Done() for { for _, fh := range written { lock := wfs.fhLockTable.AcquireLock("write", fh.fh, util.ExclusiveLock) fh.dirtyMetadata, fh.dirtyContent = true, true fh.dirtyMetadata, fh.dirtyContent = false, false wfs.fhLockTable.ReleaseLock(fh.fh, lock) } select { case <-stop: return default: } } }() go func() { // another file opened and closed over and over defer wg.Done() for { _, id, _, _ := wfs.fhMap.AcquireFileHandle(wfs, churned, plain("churn"), 1, 1, false) wfs.ReleaseHandle(id) select { case <-stop: return default: } } }() ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() for i := 0; i < 300; i++ { if err := wfs.FlushForHandover(ctx); err != nil { t.Fatalf("flush %d: %v", i, err) } } close(stop) wg.Wait() for _, fh := range written { if fh.counter != 1 || fh.dirtyMetadata { t.Fatalf("%s: counter %d, dirty %v", fh.FullPath(), fh.counter, fh.dirtyMetadata) } } }