mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
* 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>
285 lines
10 KiB
Go
285 lines
10 KiB
Go
package weed_server
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"math"
|
|
"os"
|
|
"slices"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/filer"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
|
"github.com/seaweedfs/seaweedfs/weed/wdclient"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
"google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
// scopeRetainsData is used at admission and again under the handler's path
|
|
// lock. A new chunk requires a live assignment proof; retaining one requires
|
|
// it still to be stored at the target. Inline bytes need no volume capability.
|
|
// Shared-store transactions remain a separate concern.
|
|
//
|
|
// Only the filer folds chunks into manifests for a keep-only scope. A request
|
|
// may keep a manifest stored at the target, sent as stored, but no other one.
|
|
// A kept manifest keeps its chunks referenced, so they are not read. The
|
|
// chunks a stored manifest the request drops holds count as stored at the
|
|
// target, and as identities an alias could hide behind: a truncate through a
|
|
// manifest, or a commit listing a manifest's chunks flat, retains them. An
|
|
// append keeps every stored chunk and drops no manifest. Reading the dropped
|
|
// manifests takes volume I/O, so readHeld is nil at admission. Then the checks
|
|
// that depend on their chunks are left to the handler, which always runs them
|
|
// again under its path lock.
|
|
func scopeRetainsData(stored, requested *filer_pb.Entry, appending bool, proven func(string) bool, readHeld func(manifests []*filer_pb.FileChunk) ([]*filer_pb.FileChunk, error)) error {
|
|
if err := scopeCheckEntryShape(requested); err != nil {
|
|
return err
|
|
}
|
|
if err := scopeCheckChunkRanges(stored.GetChunks()); err != nil {
|
|
return fmt.Errorf("stored entry: %w", err)
|
|
}
|
|
manifests := make(map[string]*filer_pb.FileChunk)
|
|
for _, chunk := range stored.GetChunks() {
|
|
if chunk.GetIsChunkManifest() {
|
|
manifests[chunk.GetFileIdString()] = chunk
|
|
}
|
|
}
|
|
var dropped []*filer_pb.FileChunk
|
|
if !appending {
|
|
kept := make(map[string]bool)
|
|
for _, chunk := range requested.GetChunks() {
|
|
if !chunk.GetIsChunkManifest() {
|
|
continue
|
|
}
|
|
fid := chunk.GetFileIdString()
|
|
if manifest, ok := manifests[fid]; ok && scopeSameChunk(manifest, chunk) {
|
|
kept[fid] = true
|
|
}
|
|
}
|
|
for _, chunk := range stored.GetChunks() {
|
|
if chunk.GetIsChunkManifest() && !kept[chunk.GetFileIdString()] {
|
|
dropped = append(dropped, chunk)
|
|
}
|
|
}
|
|
}
|
|
var held []*filer_pb.FileChunk
|
|
deferred := false
|
|
if len(requested.GetChunks()) > 0 && len(dropped) > 0 {
|
|
if readHeld == nil {
|
|
deferred = true
|
|
} else {
|
|
var err error
|
|
if held, err = readHeld(dropped); err != nil {
|
|
return &scopeManifestReadError{err: err}
|
|
}
|
|
}
|
|
}
|
|
known := make(map[string]bool, len(stored.GetChunks())+len(held))
|
|
manifestIds := make(map[string]bool)
|
|
identities := make(map[string]string, len(stored.GetChunks())+len(held))
|
|
for _, chunk := range slices.Concat(stored.GetChunks(), held) {
|
|
if chunk == nil {
|
|
continue
|
|
}
|
|
fid := chunk.GetFileIdString()
|
|
if chunk.GetIsChunkManifest() {
|
|
manifestIds[fid] = true
|
|
} else {
|
|
known[fid] = true
|
|
}
|
|
identity := filer.ChunkIdentity(fid)
|
|
if old, exists := identities[identity]; exists && old != fid {
|
|
// Existing aliases cannot safely be retained while one of their
|
|
// spellings might be queued for deletion.
|
|
identities[identity] = ""
|
|
} else {
|
|
identities[identity] = fid
|
|
}
|
|
}
|
|
for _, chunk := range requested.GetChunks() {
|
|
fid := chunk.GetFileIdString()
|
|
identity := filer.ChunkIdentity(fid)
|
|
if old, exists := identities[identity]; exists && old != fid {
|
|
return fmt.Errorf("chunk identity aliases are not allowed: %s", fid)
|
|
}
|
|
// Keep scoped alias refusal conservative even though unscoped garbage
|
|
// comparisons normalize equivalent needle spellings.
|
|
identities[identity] = fid
|
|
if chunk.GetIsChunkManifest() {
|
|
if manifest, ok := manifests[fid]; !ok || !scopeSameChunk(manifest, chunk) {
|
|
return fmt.Errorf("chunk %s: only a manifest stored at the target can be kept, unchanged", fid)
|
|
}
|
|
continue
|
|
}
|
|
if manifestIds[fid] {
|
|
return fmt.Errorf("chunk %s: a stored manifest cannot be kept as data", fid)
|
|
}
|
|
if !known[fid] && (proven == nil || !proven(fid)) {
|
|
if deferred {
|
|
// It may be held by a dropped manifest.
|
|
continue
|
|
}
|
|
return fmt.Errorf("%s %s", filer_pb.ChunkProofRefused, fid)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// scopeSameChunk reports whether a and b are equal chunks, whether each names
|
|
// its file id as a string or as a structure.
|
|
func scopeSameChunk(a, b *filer_pb.FileChunk) bool {
|
|
normalized := func(chunk *filer_pb.FileChunk) *filer_pb.FileChunk {
|
|
c := proto.Clone(chunk).(*filer_pb.FileChunk)
|
|
c.FileId, c.Fid = chunk.GetFileIdString(), nil
|
|
return c
|
|
}
|
|
return proto.Equal(normalized(a), normalized(b))
|
|
}
|
|
|
|
// scopeManifestReadError reports that the chunks held by the manifests stored
|
|
// at the target could not be read, so the retained data could not be checked.
|
|
type scopeManifestReadError struct {
|
|
err error
|
|
}
|
|
|
|
func (e *scopeManifestReadError) Error() string {
|
|
return fmt.Sprintf("the manifests stored at the target could not be read: %v", e.err)
|
|
}
|
|
|
|
func (e *scopeManifestReadError) Unwrap() error {
|
|
return e.err
|
|
}
|
|
|
|
// scopeManifestReader reads the chunks held by the stored manifests that a
|
|
// handler's request drops, for the retained-data check under its path lock.
|
|
// It keeps what it read: a handler that checks again inside its store
|
|
// transaction, against an entry it made sure is unchanged, does no volume I/O
|
|
// there.
|
|
type scopeManifestReader struct {
|
|
lookup wdclient.LookupFileIdFunctionType
|
|
invalidator filer.CacheInvalidator
|
|
manifests []*filer_pb.FileChunk
|
|
held []*filer_pb.FileChunk
|
|
}
|
|
|
|
func (fs *FilerServer) newScopeManifestReader() *scopeManifestReader {
|
|
return &scopeManifestReader{lookup: fs.lookupFileId, invalidator: fs.filer.MasterClient}
|
|
}
|
|
|
|
func (r *scopeManifestReader) read(ctx context.Context) func([]*filer_pb.FileChunk) ([]*filer_pb.FileChunk, error) {
|
|
return func(manifests []*filer_pb.FileChunk) ([]*filer_pb.FileChunk, error) {
|
|
if r.manifests != nil && slices.EqualFunc(r.manifests, manifests, scopeSameChunk) {
|
|
return r.held, nil
|
|
}
|
|
data, nested, err := filer.ResolveChunkManifest(ctx, r.lookup, manifests, 0, math.MaxInt64, r.invalidator, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
r.manifests = slices.Clone(manifests)
|
|
r.held = slices.Concat(data, nested)
|
|
return r.held, nil
|
|
}
|
|
}
|
|
|
|
// Chunk extents must fit the signed offsets used by readers and appenders.
|
|
func scopeCheckChunkRanges(chunks []*filer_pb.FileChunk) error {
|
|
for _, chunk := range chunks {
|
|
if chunk == nil {
|
|
return fmt.Errorf("nil chunks are not allowed")
|
|
}
|
|
if chunk.Offset < 0 || chunk.Size > uint64(math.MaxInt64-chunk.Offset) {
|
|
return fmt.Errorf("chunk range must fit a nonnegative signed offset")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Append has no replacement entry: its offset must preserve the stored data.
|
|
// Use the same refusals at admission and under the handler's path lock.
|
|
//
|
|
// An appended chunk is moved to the end of the file. A manifest cannot be
|
|
// moved: the chunks it holds keep their own offsets. So an append takes data
|
|
// chunks only, even a manifest the target stores, which a replacing commit
|
|
// may keep.
|
|
func scopeCheckAppendState(stored *filer_pb.Entry, chunks []*filer_pb.FileChunk) error {
|
|
if len(chunks) == 0 {
|
|
return nil
|
|
}
|
|
for _, chunk := range chunks {
|
|
if chunk.GetIsChunkManifest() {
|
|
return fmt.Errorf("manifest chunk %s cannot be appended", chunk.GetFileIdString())
|
|
}
|
|
}
|
|
if len(stored.GetContent()) > 0 {
|
|
return fmt.Errorf("chunks cannot be appended to an inline entry")
|
|
}
|
|
if stored != nil && stored.IsInRemoteOnly() {
|
|
return fmt.Errorf("chunks cannot be appended to a remote-only entry")
|
|
}
|
|
if stored.GetIsDirectory() || !os.FileMode(stored.GetAttributes().GetFileMode()).IsRegular() || stored.GetAttributes().GetSymlinkTarget() != "" {
|
|
return fmt.Errorf("chunks cannot be appended to a non-regular entry")
|
|
}
|
|
if err := scopeCheckChunkRanges(stored.GetChunks()); err != nil {
|
|
return err
|
|
}
|
|
extent := filer.TotalSize(stored.GetChunks())
|
|
if stored.GetAttributes().GetFileSize() > extent {
|
|
return fmt.Errorf("append offset is before the stored logical file end")
|
|
}
|
|
return scopeAppendChunkTotalFits(int64(extent), chunks)
|
|
}
|
|
|
|
// scopeKeepsHardLink refuses a write that changes a stored hard link's count
|
|
// or keeps its chunks on a name detached from it. Either way a later delete of
|
|
// this name would delete chunks that another name of the file still reads.
|
|
func scopeKeepsHardLink(stored, requested *filer_pb.Entry) error {
|
|
id := stored.GetHardLinkId()
|
|
if len(id) == 0 {
|
|
return nil
|
|
}
|
|
if bytes.Equal(requested.GetHardLinkId(), id) {
|
|
if requested.GetHardLinkCounter() != stored.GetHardLinkCounter() {
|
|
return fmt.Errorf("the link count of hard link %x cannot be changed", id)
|
|
}
|
|
return nil
|
|
}
|
|
if len(requested.GetChunks()) > 0 {
|
|
return fmt.Errorf("the chunks of hard link %x cannot be detached from it", id)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// checkScopeRetainedData runs the retained-data check under a handler's path
|
|
// lock. manifests reads what the stored manifests the request drops hold.
|
|
func checkScopeRetainedData(ctx context.Context, p string, stored *filer.Entry, requested *filer_pb.Entry, chunkAuth map[string]string, appending bool, manifests *scopeManifestReader) error {
|
|
if !scopeKeepOnly(ctx) {
|
|
return nil
|
|
}
|
|
var current *filer_pb.Entry
|
|
if stored != nil {
|
|
current = stored.ToProtoEntry()
|
|
}
|
|
if err := scopeCheckSSEState(current, requested, appending); err != nil {
|
|
return status.Errorf(codes.PermissionDenied, "filer: scoped SSE mutation refused: %v", err)
|
|
}
|
|
var proven func(string) bool
|
|
if authority, ok := ctx.Value(scopeWriteAuthorityKey{}).(*scopeWriteAuthority); ok {
|
|
if authority.sc.expired() {
|
|
return scopeExpiredError(authority.sc)
|
|
}
|
|
proven = authority.s.proofVerifier(ctx, authority.sc, chunkAuth, p)
|
|
}
|
|
if err := scopeRetainsData(current, requested, appending, proven, manifests.read(ctx)); err != nil {
|
|
var readErr *scopeManifestReadError
|
|
if errors.As(err, &readErr) {
|
|
return status.Errorf(codes.Unavailable, "filer: retained data of %s not checked: %v", p, err)
|
|
}
|
|
return status.Errorf(codes.PermissionDenied, "filer: retained data changed before commit: %v", err)
|
|
}
|
|
if err := scopeKeepsHardLink(current, requested); err != nil {
|
|
return status.Errorf(codes.PermissionDenied, "filer: %v", err)
|
|
}
|
|
return nil
|
|
}
|