Files
seaweedfs/weed/mount/filehandle_map.go
T
16a6a70a08 mount: keep delayed events about an earlier file off the next file at its name (#13445)
* mount: keep delayed events about an earlier file off the next file at its name

A writer that publishes a status file by writing NNNN.json.tmp-PID,
fsyncing it and renaming it over NNNN.json recreates the tmp name every
few seconds. Without -writebackCache a mount applies its own events from
the metadata subscription after the local apply (the events of a rename
stream carry the response time, not the log position, so the dedup ring
does not match them), and their invalidations reach whatever file sits at
the path by then. With about 200 such writers in one directory, two of
these events failed a round of the next file:

- the rename of round k moved round k+1's tmp file to NNNN.json. Its
  handle published under that name and was refused (EIO); the retry wrote
  into NNNN.json, and the rename of the tmp name failed with ENOENT.
- the create of round k became the entry and base of round k+1's file,
  and every flush of the round was refused (EIO).

Both hit the new file in the window in which Create had put its handle in
the map but not yet marked it an unpublished create: it did that
afterwards, holding no lock, so the event found a clean handle with a base
and took it for a published file. On master the second event, which
carries the inode number the creating mount stored, makes the handle give
the name up instead (replacedByAnotherFile) and marks it deleted: its
writes are never published, and close returns success.

- AcquireCreatedFileHandle puts a new handle in the state the create
  leaves it in before the handle is in the map. An existing handle, whose
  inode number outlived an earlier file, is installed under its lock.
- An invalidation carries which file its event is about: the inode number
  the file's creator stored and its creation time (hard links excluded).
  An open handle ignores a rename of another file, and any other event
  about an earlier one (another number, created before the file held). A
  later file at the name still reaches the handle.
- MovePath takes the inode number a rename event reports. A source that
  maps to another inode, while the table knows the renamed file by that
  number, holds another file and stays.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: judge another file by its creation time only for this mount's own events

A peer's file can take the name with an older creation time: renamed in
from another name, or created on a host whose clock runs behind. Dropping
its event left the name on the replaced file's inode. A peer's rename can
also name a file by a stored inode number another file here holds, when two
mounts made the same path within one second, and was refused.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mount: a delete of another file at the name leaves the open handle alone

Like a rename, a delete names the file it removed. A peer's delayed delete
of an earlier file at the name, logged by a filer other than the one behind
the handle's version, marked the held file deleted and dropped its writes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Chris Lu <chrislusf@users.noreply.github.com>
2026-12-05 11:12:29 -08:00

213 lines
6.9 KiB
Go

package mount
import (
"sync"
"sync/atomic"
"github.com/seaweedfs/seaweedfs/weed/util"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
)
type FileHandleToInode struct {
sync.RWMutex
inode2fh map[uint64]*FileHandle
fh2inode map[FileHandleId]uint64
}
func NewFileHandleToInode() *FileHandleToInode {
return &FileHandleToInode{
inode2fh: make(map[uint64]*FileHandle),
fh2inode: make(map[FileHandleId]uint64),
}
}
func (i *FileHandleToInode) GetFileHandle(fh FileHandleId) *FileHandle {
i.RLock()
defer i.RUnlock()
inode, found := i.fh2inode[fh]
if found {
return i.inode2fh[inode]
}
return nil
}
func (i *FileHandleToInode) FindFileHandle(inode uint64) (fh *FileHandle, found bool) {
i.RLock()
defer i.RUnlock()
fh, found = i.inode2fh[inode]
return
}
// MarkInodeRenamed sets isRenamed on any file handle associated with the
// given inode. This prevents the async flush from recreating a renamed
// file's metadata under its old path.
func (i *FileHandleToInode) MarkInodeRenamed(inode uint64) {
i.RLock()
defer i.RUnlock()
if fh, ok := i.inode2fh[inode]; ok {
fh.isRenamed = true
}
}
// AcquireFileHandle fully initializes a handle — entry and the log position it
// reflects — before exposing it in the map, and reports whether one already
// existed. An existing handle only gets its counter bumped: its entry belongs
// to whoever holds the handle lock, so callers install there (AcquireHandle)
// rather than under this one.
// Every open gets its own fh value over the shared FileHandle so that
// RELEASE — which identifies nothing else about the dying description — can
// scope per-open state (e.g. OFD lock owners) to the open that is closing.
//
// A handle whose counter already reached zero is draining its deferred flush:
// it stays in the map only so rename/unlink can still find it, and
// RemoveFileHandle will drop it (and every fh id minted for it) when the drain
// completes. Attaching here would hand the kernel an fh id that vanishes mid-
// open — reads on it return ENOENT, which the kernel reports to read() as EIO.
// Callers must treat a draining report as "wait for the pending flush, then
// retry the whole acquire", not as success.
func (i *FileHandleToInode) AcquireFileHandle(wfs *WFS, inode uint64, entry *filer_pb.Entry, versionTsNs int64, signature int32, appendMode ...bool) (*FileHandle, FileHandleId, bool, bool) {
return i.acquireFileHandle(wfs, inode, entry, versionTsNs, signature, len(appendMode) > 0 && appendMode[0], nil)
}
// AcquireCreatedFileHandle is AcquireFileHandle for the file Create just made.
// A new handle is in the state the create leaves it in before any other thread
// can find it: a deferred create dirty with no filer base (an unpublished
// create), an eager one clean on the entry the filer acknowledged. Setting that
// state after the handle was in the map left a window in which an event
// applied to the inode found a clean handle with a base and took it for a
// published file. An existing handle, whose inode number outlived an earlier
// file, comes back as from AcquireFileHandle; the caller installs the create
// under its lock.
func (i *FileHandleToInode) AcquireCreatedFileHandle(wfs *WFS, inode uint64, entry *filer_pb.Entry, deferred, appendMode bool) (*FileHandle, FileHandleId, bool, bool) {
return i.acquireFileHandle(wfs, inode, entry, 0, 0, appendMode, func(fh *FileHandle) {
fh.installCreated(entry, deferred)
})
}
// acquireFileHandle runs prepare, if given, on a new handle before the handle
// is in the map.
func (i *FileHandleToInode) acquireFileHandle(wfs *WFS, inode uint64, entry *filer_pb.Entry, versionTsNs int64, signature int32, appendMode bool, prepare func(fh *FileHandle)) (*FileHandle, FileHandleId, bool, bool) {
// Bind before publishing the handle or queuing any async close. Capture
// under the inode-table lock, but release it before taking the fhMap lock.
var writebackError *atomic.Bool
if wfs.option.WritebackCache {
wfs.inodeToPath.RLock()
state, found := wfs.writebackContentErrors.Load(inode)
if !found {
state, _ = wfs.writebackContentErrors.LoadOrStore(inode, new(atomic.Bool))
}
writebackError = state.(*atomic.Bool)
wfs.inodeToPath.RUnlock()
}
i.Lock()
defer i.Unlock()
fh, found := i.inode2fh[inode]
if !found {
fh = newFileHandle(wfs, FileHandleId(util.RandomUint64()), inode, entry)
fh.writebackContentError = writebackError
fh.fhs[fh.fh] = appendMode
fh.entryVersionTsNs.Store(versionTsNs)
fh.entryVersionSignature.Store(signature)
if prepare != nil {
prepare(fh)
}
i.inode2fh[inode] = fh
i.fh2inode[fh.fh] = inode
return fh, fh.fh, false, false
}
if fh.counter <= 0 {
return nil, 0, true, true
}
fh.counter++
openFh := FileHandleId(util.RandomUint64())
fh.fhs[openFh] = appendMode
i.fh2inode[openFh] = inode
return fh, openFh, true, false
}
func (i *FileHandleToInode) isAppendHandle(openFh FileHandleId) bool {
i.RLock()
defer i.RUnlock()
if fh := i.inode2fh[i.fh2inode[openFh]]; fh != nil {
return fh.fhs[openFh]
}
return false
}
func (i *FileHandleToInode) ReleaseByHandle(fh FileHandleId) *FileHandle {
i.Lock()
defer i.Unlock()
inode, found := i.fh2inode[fh]
if !found {
return nil
}
// The released id never resolves again: a duplicate RELEASE on it must
// not decrement the counter a second time.
delete(i.fh2inode, fh)
fhHandle, fhFound := i.inode2fh[inode]
if !fhFound {
return nil
}
delete(fhHandle.fhs, fh)
// If the counter is already <= 0, a prior Release already started the
// drain. Return nil to prevent double-processing.
if fhHandle.counter <= 0 {
return nil
}
fhHandle.counter--
if fhHandle.counter <= 0 {
if fhHandle.asyncFlushPending {
// Handle stays in fhMap so rename/unlink can still find it
// via FindFileHandle during the background drain.
return fhHandle
}
// Fence before a reopen can see the inode without this handle.
if fhHandle.dirtyContent && !fhHandle.isDeleted {
(&contentPublication{failure: fhHandle.writebackContentError}).abandon()
}
delete(i.inode2fh, inode)
for openFh := range fhHandle.fhs {
delete(i.fh2inode, openFh)
}
fhHandle.fhs = nil
return fhHandle
}
return nil
}
// RemoveFileHandle removes a handle from both maps. Called after an async
// drain completes to clean up the handle that was intentionally kept in the
// maps during the flush.
func (i *FileHandleToInode) RemoveFileHandle(fh FileHandleId, inode uint64) {
i.Lock()
defer i.Unlock()
if fhHandle, ok := i.inode2fh[inode]; ok {
for openFh := range fhHandle.fhs {
delete(i.fh2inode, openFh)
}
}
delete(i.inode2fh, inode)
delete(i.fh2inode, fh)
}
// openFhIds returns the live fh values minted for an inode's shared handle —
// one per still-open description.
func (i *FileHandleToInode) openFhIds(inode uint64) []FileHandleId {
i.RLock()
defer i.RUnlock()
fh := i.inode2fh[inode]
if fh == nil {
return nil
}
ids := make([]FileHandleId, 0, len(fh.fhs))
for id := range fh.fhs {
ids = append(ids, id)
}
return ids
}