Files
seaweedfs/weed/filer/filer_hardlink_chunks.go
T
c7fc40c76c filer, mount: read a manifest's chunks only within its span (#13048)
* filer, mount: read a manifest's chunks only within its span

Until #13042, a truncate through a manifest only shortened the manifest
chunk; the chunks it holds stayed whole. Reads resolve those chunks at
their own offsets and sizes, so once such a file grows over the cut again
without writing it, by an extending truncate or a write past the end, the
cut bytes come back where zeros belong. #13042 keeps new truncates from
leaving such manifests, but files a mount truncated between #12462 and
#13042 still have them.

Reproduced with weed mount on a local cluster: a mount from before #13042
appends 700 lines with one close() each, so the first 256 fold into a
manifest, and truncates the file to 100 lines; a mount from current master
then extends it past its old end with ftruncate. The extended range read
8,580 bytes of the cut lines instead of zeros, through that mount and
through the filer.

Reads now cut each manifest's chunks to the manifest's span:
ResolveChunkManifestForRead, used by NonOverlappingVisibleIntervals (filer
and S3 streaming, WebDAV, the mount's merge reads), the S3 ranged reads and
the mount's peer reads, and ChunkGroup for the mount's reads. A manifest
made by MaybeManifestize spans its chunks exactly, so only shortened ones
change. ResolveChunkManifest still returns the chunks whole: MinusChunks,
deletions and the other chunk-list comparisons need every chunk a manifest
references, or the chunks past the cut would never be deleted.

* sw-client: read a manifest's chunks only within its span

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

* java client: read a manifest's chunks only within its span

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

* filer: drop PR numbers from the manifest span comments

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

* filer: fold ResolveChunkManifestForRead into ResolveChunkManifest

ResolveChunkManifest takes cutToSpan: the read paths pass true, the
deletion and chunk-list comparison paths pass false.

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

* s3api: cut manifests to their span when flattening an object's chunks

A full-object SSE GET, CopyObject and UploadPartCopy flatten the
source's manifests into a plain chunk list, which drops each manifest's
span, so the chunks a truncate left past a shortened manifest came back
as object data.

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

* shell: purge a volume.fsck path only for chunks within a manifest's span

A chunk past a shortened manifest's span is still referenced but never
read, so a missing volume under it loses nothing. The purge decision now
uses the span-cut chunks while the inventory keeps every reference.

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

* shell: cut manifests to their span in fs.distribute_chunks

The entry is rewritten with the resolved chunks as a flat list or a new
manifest that spans them, so chunks past a shortened manifest's span
became readable again. The filer's update still deletes them, since it
compares against the old entry's uncut chunks.

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

* shell: verify only the chunks within a manifest's span in fs.verify

A chunk past a shortened manifest's span is never read, so a missing
needle under it failed the entry and, with -pruneEntries, deleted a file
whose readable bytes were intact.

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

* shell: mark the chunks past a manifest's span instead of allowing the visible ones

The purge gate now skips only the resolved chunks that the span cut drops,
so chunks that come from outside the manifests keep their purge behavior.

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

* shell: skip the purge for a missing past-span chunk on a listed volume

volume.fsck -findMissingChunksInFiler queued a path for deletion when a
listed volume's index lacked any chunk the uncut inventory recorded,
including a chunk past a shortened manifest's span that no read reaches.
Record those chunks per path during the filer walk and let only a missing
chunk within the span queue the purge. The path is still reported.

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-11-27 18:51:15 -08:00

76 lines
3.1 KiB
Go

package filer
import (
"bytes"
"context"
"errors"
"math"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/wdclient"
)
// hardLinkStillHeld reports whether replaced, the entry a write has just
// replaced at its path, was one of several hard links to a file another name
// still reads: replaced carried a hard link, its replacement is not that same
// link, and the file's shared record outlived the drop of replaced's name.
//
// Ask it after the write. The store drops the replaced name's link as part of
// the write, so the shared record is gone exactly when replaced was the last
// link, whatever link count replaced carried when it was read.
func (f *Filer) hardLinkStillHeld(ctx context.Context, replaced, replacement *Entry) bool {
return replacesHardLink(replaced, replacement) && !f.hardLinkIsOrphaned(ctx, replaced.HardLinkId)
}
// replacesHardLink reports whether replacement unlinks a hard-linked
// replaced; the same file written through one of its names does not: the
// store rewrote the shared record, and no name reads what the write left out.
func replacesHardLink(replaced, replacement *Entry) bool {
if replaced == nil || len(replaced.HardLinkId) == 0 {
return false
}
return replacement == nil || !bytes.Equal(replaced.HardLinkId, replacement.HardLinkId)
}
// KeepHardLinkedChunks takes out of garbage, the chunks a write is about to
// delete, every chunk of replaced's file while another hard link still reads
// it. The chunks come from the shared record, since replaced may be a stale
// row when its lookup could not read the record.
//
// When the record or the manifests cannot be read nothing is deleted: a
// leaked chunk can be collected later, a deleted one that a name still reads
// is lost.
func (f *Filer) KeepHardLinkedChunks(ctx context.Context, replaced, replacement *Entry, garbage []*filer_pb.FileChunk) []*filer_pb.FileChunk {
if len(garbage) == 0 || !replacesHardLink(replaced, replacement) {
return garbage
}
value, err := f.Store.KvGet(ctx, replaced.HardLinkId)
if errors.Is(err, ErrKvNotFound) {
return garbage
}
shared := &Entry{}
if err == nil {
err = shared.DecodeAttributesAndChunks(value)
}
if err == nil {
var kept []*filer_pb.FileChunk
kept, err = withoutChunksOf(ctx, f.MasterClient.GetLookupFileIdFunction(), f.MasterClient, append(shared.GetChunks(), replaced.GetChunks()...), garbage)
if err == nil {
return kept
}
}
glog.ErrorfCtx(ctx, "keep %d chunk(s) of %s, which another hard link may still read: %v", len(garbage), replaced.FullPath, err)
return nil
}
// withoutChunksOf returns garbage without chunks, without the manifests among
// them, and without every chunk those manifests list, nested ones included.
func withoutChunksOf(ctx context.Context, lookupFileIdFn wdclient.LookupFileIdFunctionType, invalidator CacheInvalidator, chunks, garbage []*filer_pb.FileChunk) ([]*filer_pb.FileChunk, error) {
data, manifests, err := ResolveChunkManifest(ctx, lookupFileIdFn, chunks, 0, math.MaxInt64, invalidator, false)
if err != nil {
return nil, err
}
return DoMinusChunks(garbage, append(data, manifests...)), nil
}