Files
seaweedfs/weed/mount/wfs_save.go
T
120d786818 mount: update a directory's times once, without retries (#13443)
* mount: update a directory's times once, without retries

touchDirMtimeCtime sent its UpdateEntry through saveEntry's retries, 1, 2
and 4 s apart. Every create, link, rename and removal in the directory
waits for that update and ignores how it went. In a directory many
writers share, the update of its entry conflicts in the filer store with
theirs (TiKV reports "write conflict"), and the call waited out the
backoffs: in one directory about 200 jobs shared, three mounts logged 3646
such retries in two hours, each holding a create or a rename for up to
about 7 s.

saveEntry takes a bestEffort flag: such an update is tried once and its
failure logged at V(1). touchDirMtimeCtime sets it; setattr and the xattr
calls keep their retries.

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

* mount: shorten the directory times comments

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:11:43 -08:00

131 lines
4.4 KiB
Go

package mount
import (
"context"
"fmt"
"syscall"
"time"
"unicode/utf8"
"github.com/seaweedfs/go-fuse/v2/fuse"
"google.golang.org/protobuf/proto"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
)
// saveEntry stores entry at path on the filer, retrying a failed update. A
// bestEffort update, whose outcome changes nothing for its caller, is tried
// once and its failure logged at V(1).
func (wfs *WFS) saveEntry(path util.FullPath, entry *filer_pb.Entry, bestEffort bool) (code fuse.Status) {
parentDir, _ := path.DirAndName()
wfs.mapPbIdFromLocalToFiler(entry)
defer wfs.mapPbIdFromFilerToLocal(entry)
request := &filer_pb.UpdateEntryRequest{
Directory: parentDir,
Entry: entry,
Signatures: []int32{wfs.signature},
}
glog.V(1).Infof("save entry %s", path)
var resp *filer_pb.UpdateEntryResponse
update := func() error {
var callErr error
resp, callErr = wfs.streamUpdateEntry(context.Background(), request)
return callErr
}
var err error
if bestEffort {
err = update()
} else {
err = retryMetadataFlushIf(context.Background(), update, isRetryableFilerError, func(nextAttempt, totalAttempts int, backoff time.Duration, err error) {
glog.Warningf("saveEntry %s: retrying UpdateEntry (attempt %d/%d) after %v: %v",
path, nextAttempt, totalAttempts, backoff, err)
})
}
if err != nil {
// Wrap with %w so grpcErrorToFuseStatus can still unwrap the gRPC status
// (e.g. codes.Canceled → ETIMEDOUT). Using %v would stringify the error and
// status.FromError would fall through to the default EIO.
err = fmt.Errorf("UpdateEntry dir %s: %w", path, err)
fuseStatus := grpcErrorToFuseStatus(err)
if bestEffort {
glog.V(1).Infof("saveEntry %s: %v (best effort, not retried)", path, err)
} else if fuseStatus == fuse.EIO {
glog.Errorf("saveEntry failed for %s: %v (returning EIO)", path, err)
} else {
glog.V(1).Infof("saveEntry failed for %s: %v (returning %v)", path, err, fuseStatus)
}
return fuseStatus
}
wfs.applySavedEntry(path, entry, resp)
return fuse.OK
}
// applySavedEntry publishes an acknowledged entry with filer-side uid/gid.
func (wfs *WFS) applySavedEntry(path util.FullPath, entry *filer_pb.Entry, resp *filer_pb.UpdateEntryResponse) {
parentDir, _ := path.DirAndName()
// The mutation is acknowledged; bring any open handle for this path up
// to the acknowledged state — a handle opened while this save was in
// flight holds an older entry, and advancing its version alone would
// fence out the events carrying what it lacks. A no-change update
// returns no event but still carries the log position it confirmed.
ackVersion := ackVersionTsNs(resp)
if inode, found := wfs.inodeToPath.GetInode(path); found {
if fh, fhFound := wfs.fhMap.FindFileHandle(inode); fhFound {
ackedEntry := proto.Clone(entry).(*filer_pb.Entry)
wfs.mapPbIdFromFilerToLocal(ackedEntry)
fh.installAckedEntry(ackedEntry, ackVersion, resp.GetLogSignature())
}
}
event := resp.GetMetadataEvent()
if event == nil {
event = metadataUpdateEvent(parentDir, entry)
if event != nil {
event.TsNs = ackVersion
}
}
if applyErr := wfs.applyLocalMetadataEvent(context.Background(), event); applyErr != nil {
glog.Warningf("saveEntry %s: best-effort metadata apply failed: %v", path, applyErr)
wfs.inodeToPath.InvalidateChildrenCache(util.FullPath(parentDir))
}
}
func (wfs *WFS) mapPbIdFromFilerToLocal(entry *filer_pb.Entry) {
if entry.Attributes == nil {
return
}
entry.Attributes.Uid, entry.Attributes.Gid = wfs.option.UidGidMapper.FilerToLocal(entry.Attributes.Uid, entry.Attributes.Gid)
}
func (wfs *WFS) mapPbIdFromLocalToFiler(entry *filer_pb.Entry) {
if entry.Attributes == nil {
return
}
entry.Attributes.Uid, entry.Attributes.Gid = wfs.option.UidGidMapper.LocalToFiler(entry.Attributes.Uid, entry.Attributes.Gid)
}
// checkName refuses a name that is not valid UTF-8 instead of rewriting it:
// proto string fields cannot carry it, and any rewrite maps different names
// to one entry.
func checkName(name string) (string, fuse.Status) {
if !utf8.ValidString(name) {
return name, fuse.Status(syscall.EILSEQ)
}
// The Linux FUSE kernel module enforces NAME_MAX=255 at the VFS layer.
// Return ENAMETOOLONG early to avoid creating entries that cannot be
// looked up via normal syscalls (stat, chmod, etc.).
if len(name) > 255 {
return name, fuse.Status(syscall.ENAMETOOLONG)
}
return name, fuse.OK
}