package mount import ( "context" "errors" "fmt" "sync" "time" "github.com/seaweedfs/seaweedfs/weed/glog" "github.com/seaweedfs/seaweedfs/weed/util" ) // FlushForHandover publishes everything a handover would otherwise refuse to // carry: every open handle with dirty pages, dirty metadata (a deferred create // is published here: its only copy in the meta cache is a placeholder that // does not cross) or a deferred close flush is flushed the way fsync flushes // it, and the closed files still draining in the background and the creates // still being sent are waited for. It adds no flush logic: each handle goes // through doFlush with allowAsync false. ctx bounds the wait; on expiry the // work already started carries on in the background, nothing is lost, and the // error says so. // // It is safe while requests are served, like an fsync of every open file from // another thread: run it once then, so the bulk of the uploads is not part of // the pause, and again after the FUSE server stopped reading requests, for // what became dirty in between. ExportHandoverState still refuses whatever is // left (a failed flush leaves its handle dirty, and a file closed during the // first run may still drain in the background). func (wfs *WFS) FlushForHandover(ctx context.Context) error { done := make(chan error, 1) go func() { done <- wfs.flushForHandover(ctx) }() select { case err := <-done: return err case <-ctx.Done(): return fmt.Errorf("flush before handover: %w", ctx.Err()) } } func (wfs *WFS) flushForHandover(ctx context.Context) error { // Closed files draining in the background first: their handle is still in // the map, and a new flush of it would race the drain. wfs.pendingAsyncFlushMu.Lock() drains := make([]chan struct{}, 0, len(wfs.pendingAsyncFlush)) for _, done := range wfs.pendingAsyncFlush { drains = append(drains, done) } wfs.pendingAsyncFlushMu.Unlock() for _, done := range drains { select { case <-done: case <-ctx.Done(): return ctx.Err() } } // Requests are still served during the first run, so each handle's state // is read under the lock its writers hold. The map's lock is only held to // list the handles: a handle's flush lock is never taken under it. type openHandle struct { fh *FileHandle // deferred is asyncFlushPending, read under the map's lock as RELEASE // reads it. A close sets it with no lock, so a flag missed here waits // for the run after the FUSE server stopped; the export refuses the // handle until then. deferred bool } wfs.fhMap.RLock() handles := make([]openHandle, 0, len(wfs.fhMap.inode2fh)) for _, fh := range wfs.fhMap.inode2fh { handles = append(handles, openHandle{fh, fh.asyncFlushPending}) } wfs.fhMap.RUnlock() var ( mu sync.Mutex errs []error wg sync.WaitGroup ) concurrency := wfs.option.ConcurrentWriters if concurrency <= 0 { concurrency = 16 } sem := make(chan struct{}, concurrency) for _, h := range handles { if ctx.Err() != nil { break } wg.Add(1) sem <- struct{}{} go func(h openHandle) { defer wg.Done() defer func() { <-sem }() if !h.deferred && !wfs.hasUnflushedState(h.fh) { return } // Closed since it was listed: its release, or the background // drain waited for above on the next run, deals with it. if !wfs.fhMap.isOpen(h.fh) { return } if status := wfs.doFlush(context.Background(), h.fh, unknownCallerId, unknownCallerId, false, metadataFlushTimeout); !status.Ok() { mu.Lock() errs = append(errs, fmt.Errorf("flush %s: status %v", h.fh.FullPath(), status)) mu.Unlock() } }(h) } wg.Wait() if err := ctx.Err(); err != nil { return err } // Creates sent in the background have no handle to wait on. for wfs.asyncCreates.Load() > 0 { select { case <-ctx.Done(): return ctx.Err() case <-time.After(10 * time.Millisecond): } } if len(errs) > 0 { glog.Warningf("flush before handover: %d handles failed to flush", len(errs)) } return errors.Join(errs...) } // hasUnflushedState reports whether fh has anything a flush would publish. It // waits for the handle's flush lock: writes, truncates and flushes change this // state under it. func (wfs *WFS) hasUnflushedState(fh *FileHandle) bool { lock := wfs.fhLockTable.AcquireLock("FlushForHandover", fh.fh, util.SharedLock) defer wfs.fhLockTable.ReleaseLock(fh.fh, lock) return fh.dirtyMetadata || fh.dirtyContent || fh.dirtyPages.HasPendingUploads() } // isOpen reports whether fh is still the handle of its inode with an open left. func (i *FileHandleToInode) isOpen(fh *FileHandle) bool { i.RLock() defer i.RUnlock() return i.inode2fh[fh.inode] == fh && fh.counter > 0 }