Files
seaweedfs/weed/mount/weedfs_write.go
T
c96636ecd0 filer: fold chunk lists for scoped commits (#13281)
* filer: fold chunk lists for scoped commits

A filer serving a keep-only scoped token never folded chunk lists, and
refused any new chunk for an entry holding manifests, so scoped clients
kept their lists flat. On a store with a value size limit, such as
FoundationDB, a scoped file stopped fitting at about 1000 chunks.

The filer now folds scoped commits and reuses stored manifests as it does
for unscoped ones. Folding runs after the retained-data check, so the only
manifests a scoped commit stores are ones the filer wrote:

- a scoped request may keep a manifest stored at the target, sent as
  stored; any other manifest is refused, with or without a proof, and a
  stored manifest cannot be kept as a data chunk;
- the chunks held by the stored manifests count as stored at the target
  and join the identity map, so they may be retained flat but not under
  another spelling of their file id;
- those chunks are read under the handler's path lock, not at admission;
  admission still refuses a manifest the target does not store and leaves
  the checks that depend on the held chunks to the locked check. A
  manifest that cannot be read refuses the write as Unavailable. An update
  rechecking inside its store transaction reuses the chunks it already
  read, so it does no volume I/O there.

Appends to a folded entry are allowed under the same rules: an append
keeps every stored chunk, and the locked check refuses aliases of the
chunks the stored manifests hold.

* mount: explain why a scoped mount keeps chunk lists flat

A filer serving a scoped token now folds scoped commits itself and keeps
only the manifests it wrote. The mount's flat rule is unchanged; only the
reason given for it was out of date.

* filer: test a scoped truncate through a stored manifest

* filer: refuse manifest chunks in a scoped append

An append moves each chunk it carries to the end of the file. A manifest
cannot be moved that way: the chunks it holds keep their own offsets, so
the appended range would read no data and the manifest's bytes would still
read at their old place. Before this series a scoped request could carry no
manifest at all; now that a scoped commit may keep a stored manifest, an
append still takes data chunks only.

* filer: read only the stored manifests a scoped commit drops

The locked retained-data check read every stored manifest whenever a
scoped request had chunks, so each flush of a folded file and each append
cost one volume read per manifest under the path lock. The unscoped path
reads only the manifests a commit drops.

Read the same way for a keep-only scope: only the stored manifests the
request does not keep unchanged, and none for an append, which keeps every
stored chunk. A kept manifest keeps its chunks referenced, and garbage
comparisons match chunk identities, so another spelling of one of its
chunks cannot get the needle deleted; such a chunk now needs its own proof
like any new chunk. Truncating through a manifest and listing its chunks
flat still work, since both drop it.

* filer: find the dropped manifests of a scoped commit in one pass

Index the manifests the request keeps by file id instead of scanning the
request for each stored manifest, which compared and cloned chunks
quadratically in the number of manifests.

* filer: rewrap the scopeManifestReader comment

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

---------

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

125 lines
5.0 KiB
Go

package mount
import (
"fmt"
"io"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/operation"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
util_http "github.com/seaweedfs/seaweedfs/weed/util/http"
)
// shouldCacheUploadedChunk decides whether a just-uploaded chunk's bytes are
// put in the local chunk cache. The first chunk (small files) is always cached.
// Peer sharing needs every chunk cached so it can be served back on FetchChunk.
// A read-auth mount needs every chunk cached too: it holds no volume read key
// and reads a chunk with a filer-issued per-fid token the filer can mint only
// after the chunk's entry is published, so a chunk this handle just uploaded
// has no token until the next metadata flush; caching it lets a read-after-write
// of that chunk be served locally instead of failing on a token-less volume
// fetch. Gated on chunkCache != nil (-cacheCapacityMB=0 disables the cache).
func (wfs *WFS) shouldCacheUploadedChunk(offset int64) bool {
return wfs.chunkCache != nil && (offset == 0 || wfs.peerAnnouncer != nil || wfs.readAuth != nil)
}
// maybeManifestize folds a file's chunks into manifests as
// filer.MaybeManifestize does, except on a mount with a scoped filer token: a
// filer serving a scoped token stores only the manifests it wrote itself and
// refuses any other, since a manifest could name chunks the token may not
// reach. Such a mount keeps chunk lists flat and leaves folding to the filer;
// its later commits send the filer's manifests back as they were stored.
func (wfs *WFS) maybeManifestize(fullPath util.FullPath, chunks []*filer_pb.FileChunk) ([]*filer_pb.FileChunk, error) {
if wfs.chunkProofs != nil && wfs.filerTokenScoped() {
return chunks, nil
}
return filer.MaybeManifestize(wfs.saveDataAsChunk(fullPath), nil, chunks)
}
func (wfs *WFS) saveDataAsChunk(fullPath util.FullPath) filer.SaveDataAsChunkFunctionType {
// Backstop: FUSE entry points sanitize names before they reach
// inodeToPath, but async flush paths (e.g. writebackCache, handles whose
// RememberPath was set from an older code path) may still carry bytes
// that predate sanitization. Proto3 string fields require valid UTF-8,
// so scrub the full path once here before every AssignVolume call.
assignPath := fullPath.Sanitized()
return func(reader io.Reader, filename string, offset int64, tsNs int64, _ uint64) (chunk *filer_pb.FileChunk, err error) {
uploader, err := operation.NewUploader()
if err != nil {
return
}
// WantMd5 gives mount writes a real filer.ETag (server echoes Content-MD5 back).
uploadOption := &operation.UploadOption{
Filename: filename,
Cipher: wfs.option.Cipher,
IsInputCompressed: false,
MimeType: "",
PairMap: nil,
WantMd5: true,
}
if wfs.option.VolumeServerAccess == "filerProxy" {
// getCurrentFiler() can change on failover, so read it per attempt.
uploadOption.GenUploadUrl = func(host, fileId string) string {
return util_http.ProxyChunkUrl(string(wfs.getCurrentFiler()), fileId)
}
}
// A filer serving a scoped token stores the chunk only with the
// assignment proof it returns, so make room to keep it first.
if err = wfs.chunkProofs.reserve(1); err != nil {
glog.V(0).Infof("upload data %v: %v", filename, err)
return nil, fmt.Errorf("upload data: %w", err)
}
fileId, uploadResult, err, data := uploader.UploadWithRetry(
wfs,
&filer_pb.AssignVolumeRequest{
Count: 1,
Replication: wfs.option.Replication,
Collection: wfs.option.Collection,
TtlSec: wfs.option.TtlSec,
DiskType: string(wfs.option.DiskType),
DataCenter: wfs.option.DataCenter,
Path: assignPath,
},
uploadOption, reader,
)
if err != nil {
wfs.chunkProofs.unreserve(1)
glog.V(0).Infof("upload data %v: %v", filename, err)
return nil, fmt.Errorf("upload data: %w", err)
}
if uploadResult.Error != "" {
wfs.chunkProofs.unreserve(1)
glog.V(0).Infof("upload failure %v: %v", filename, err)
return nil, fmt.Errorf("upload result: %v", uploadResult.Error)
}
wfs.chunkProofs.keep(fileId, uploadResult.AssignAuth, 1)
// shouldCacheUploadedChunk explains when and why every uploaded
// chunk is cached (peer sharing, and read-auth read-after-write).
shouldCache := wfs.shouldCacheUploadedChunk(offset)
if shouldCache {
wfs.chunkCache.SetChunk(fileId, data)
}
// Announce every uploaded chunk so the tier-2 directory fills
// in as the file is written. Without this, the per-fetch
// announce path only bootstraps after someone else has already
// pulled a chunk via peer — which can't happen if nobody has
// told the directory who holds the chunk. Skip the announce
// when we couldn't cache (no point advertising bytes we can't
// actually serve back).
if wfs.peerAnnouncer != nil && shouldCache {
wfs.peerAnnouncer.EnqueueAnnounce(fileId)
}
chunk = uploadResult.ToPbFileChunk(fileId, offset, tsNs)
return chunk, nil
}
}