mount: truncate a manifest the cut goes through as the chunks it holds (#13042)

* mount: truncate a manifest the cut goes through as the chunks it holds

Truncating a file shortens every chunk the new size cuts through, a
manifest chunk included. A manifest's span only says where its chunks
lie: reads resolve the chunks inside it at their own offsets and sizes,
so the bytes past the cut stay readable. Once the file grows over them
again without writing them, by an extending truncate or a write past the
end, they come back where zeros belong.

Reproduced with weed mount on a local cluster: 700 lines appended with
one close() each, so the small-chunk fold put the first 256 into a
manifest; then a truncate to the first 100 lines and an ftruncate past
the old end. The extended range read 8,580 bytes of the cut lines
instead of zeros, through both mounts of the file. Since #12462 any file
grown by a few hundred small appends has manifests; before it, only
files of 10,000 chunks did.

A truncate now opens each manifest the cut goes through, nested ones
too, and cuts the chunks it holds. The manifest leaves the entry, and
the filer deletes it with the chunks past the cut when the entry is
stored. A manifest shortened by an earlier truncate first gives up the
chunks past its own span. If a manifest cannot be read, the truncate
fails with EIO and the file is left as it was.

* mount: keep a truncate's expanded chunks from merges, read its manifests with a token

Review follow-ups:

- The chunks a truncate takes out of a manifest are not in the handle's
  base, which lists the manifest. A merge onto a peer's entry took them for
  writes of the handle's, so after a peer cut the file shorter and the filer
  queued the rest for deletion, the handle could publish references to
  those chunks again; the gap check passed because plain chunks covered the
  range. Before the expansion, the manifest across the cut refused that
  merge. A truncate that expands a manifest now marks the handle as having
  rewritten its chunks, so the merge is refused when the peer changed
  chunks, at the next flush and when the peer's event reaches the handle.
- A truncate by path, with no open handle, read the manifest before its file
  id was registered for a filer-issued read token, so a mount reading with
  -volumeReadAuth sent the read without one and the truncate failed with
  EIO. The entry's chunks are registered under its path first.
- All manifest reads of one truncate share one one-minute deadline, instead
  of a minute each.

TestTruncateThroughManifestCutsItsChunks runs SetAttr with and without an
open handle: it fails without the registration (no handle) and without the
rewrite mark (open handle). TestMergeRefusesTruncateExpansionOverPeerTruncate
shows the merge that published a peer-dropped chunk, and its refusal.

* mount: answer a truncate interrupted while reading a manifest with EINTR

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>
This commit is contained in:
authored and GitHub committed 2026-11-26 22:47:52 -08:00
1 parent be8eb02ac0
commit 424c67ad06
3 files changed
+378 -1

No files matched your search

+116
View File
@@ -0,0 +1,116 @@
package mount
import (
"context"
"fmt"
"slices"
"time"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
"google.golang.org/protobuf/proto"
)
// maxTruncateManifestDepth bounds how far manifests in manifests are opened.
const maxTruncateManifestDepth = 4
// truncateManifestWait bounds all the manifest reads of one truncate, which
// hold the handle's locks.
const truncateManifestWait = time.Minute
// truncateManifestResolver reads the chunks of one manifest for a truncate.
// Tests replace it.
var truncateManifestResolver = func(ctx context.Context, wfs *WFS, manifest *filer_pb.FileChunk) ([]*filer_pb.FileChunk, error) {
return filer.ResolveOneChunkManifest(ctx, wfs.LookupFn(), manifest, wfs.CacheInvalidator())
}
// cutThroughManifests returns chunks with each manifest a cut at size goes
// through replaced by the chunks it holds (expandManifestsAt), and whether it
// replaced one. The manifest reads share one deadline. The chunks are first
// registered for filer-issued read tokens under path: a truncate by path, with
// no open handle, has not registered them, and a mount without a signing key
// reads a manifest only with such a token.
func (wfs *WFS) cutThroughManifests(ctx context.Context, path util.FullPath, chunks []*filer_pb.FileChunk, size int64) ([]*filer_pb.FileChunk, bool, error) {
if !slices.ContainsFunc(chunks, func(chunk *filer_pb.FileChunk) bool { return cutGoesThrough(chunk, size) }) {
return chunks, false, nil
}
wfs.readAuth.Track(path, chunks)
ctx, cancel := context.WithTimeout(ctx, truncateManifestWait)
defer cancel()
return expandManifestsAt(chunks, size, func(manifest *filer_pb.FileChunk) ([]*filer_pb.FileChunk, error) {
return truncateManifestResolver(ctx, wfs, manifest)
})
}
// expandManifestsAt returns chunks with every manifest that a cut at size goes
// through replaced by the chunks it holds, nested ones too. Cutting a manifest
// only shortens its span: reads resolve its chunks at their own sizes, so what
// lay past the cut would come back as data once the file grows over it again
// without writing it (a hole, an extending truncate). The chunks are cut
// instead. A manifest's chunks are first cut to its own span, since one
// shortened before this was in place still holds them whole. Manifests that
// lie wholly before or after the cut are left as they are. expanded reports
// whether a manifest was replaced.
func expandManifestsAt(chunks []*filer_pb.FileChunk, size int64, resolve func(*filer_pb.FileChunk) ([]*filer_pb.FileChunk, error)) (cut []*filer_pb.FileChunk, expanded bool, err error) {
for i, chunk := range chunks {
if !cutGoesThrough(chunk, size) {
if cut != nil {
cut = append(cut, chunk)
}
continue
}
if cut == nil {
cut = append(make([]*filer_pb.FileChunk, 0, len(chunks)), chunks[:i]...)
}
held, err := expandManifestAt(chunk, size, resolve, 1)
if err != nil {
return nil, false, err
}
cut = append(cut, held...)
}
if cut == nil {
return chunks, false, nil
}
return cut, true, nil
}
func expandManifestAt(manifest *filer_pb.FileChunk, size int64, resolve func(*filer_pb.FileChunk) ([]*filer_pb.FileChunk, error), depth int) ([]*filer_pb.FileChunk, error) {
if depth > maxTruncateManifestDepth {
return nil, fmt.Errorf("manifest %s: nested deeper than %d", manifest.GetFileIdString(), maxTruncateManifestDepth)
}
held, err := resolve(manifest)
if err != nil {
return nil, fmt.Errorf("manifest %s: %w", manifest.GetFileIdString(), err)
}
end := chunkSpanEnd(manifest)
var chunks []*filer_pb.FileChunk
for _, chunk := range held {
if chunk.Offset >= end {
continue
}
if chunkSpanEnd(chunk) > end {
chunk = proto.Clone(chunk).(*filer_pb.FileChunk)
chunk.Size = uint64(end - chunk.Offset)
}
if cutGoesThrough(chunk, size) {
nested, err := expandManifestAt(chunk, size, resolve, depth+1)
if err != nil {
return nil, err
}
chunks = append(chunks, nested...)
continue
}
chunks = append(chunks, chunk)
}
return chunks, nil
}
// cutGoesThrough reports whether chunk is a manifest a cut at size divides.
func cutGoesThrough(chunk *filer_pb.FileChunk, size int64) bool {
return chunk.IsChunkManifest && chunk.Offset < size && chunkSpanEnd(chunk) > size
}
func chunkSpanEnd(chunk *filer_pb.FileChunk) int64 {
return chunk.Offset + int64(chunk.Size)
}
+245
View File
@@ -0,0 +1,245 @@
package mount
import (
"context"
"errors"
"fmt"
"testing"
"time"
"github.com/seaweedfs/go-fuse/v2/fuse"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"
"github.com/seaweedfs/seaweedfs/weed/filer"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/util"
)
// manifestFixture keeps what each fake manifest holds, keyed by its file key.
type manifestFixture map[uint64][]*filer_pb.FileChunk
func (m manifestFixture) resolve(manifest *filer_pb.FileChunk) ([]*filer_pb.FileChunk, error) {
held, ok := m[manifest.Fid.FileKey]
if !ok {
return nil, errors.New("no such manifest")
}
return held, nil
}
func dataChunk(key uint64, offset int64, size uint64) *filer_pb.FileChunk {
return &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 1, FileKey: key, Cookie: 1}, Offset: offset, Size: size, ModifiedTsNs: int64(key)}
}
func manifestChunk(key uint64, offset int64, size uint64) *filer_pb.FileChunk {
return &filer_pb.FileChunk{Fid: &filer_pb.FileId{VolumeId: 9, FileKey: key, Cookie: 1}, Offset: offset, Size: size, IsChunkManifest: true}
}
func spans(chunks []*filer_pb.FileChunk) [][3]int64 {
var s [][3]int64
for _, c := range chunks {
manifest := int64(0)
if c.IsChunkManifest {
manifest = 1
}
s = append(s, [3]int64{c.Offset, int64(c.Size), manifest})
}
return s
}
// A cut through a manifest yields its chunks; manifests before and after the
// cut, and data chunks, stay as they are.
func TestExpandManifestsAtOpensOnlyTheManifestTheCutDivides(t *testing.T) {
fixture := manifestFixture{}
var first, second []*filer_pb.FileChunk
for i := 0; i < 4; i++ {
first = append(first, dataChunk(uint64(100+i), int64(i)*256, 256))
second = append(second, dataChunk(uint64(200+i), 1024+int64(i)*256, 256))
}
fixture[1] = first
fixture[2] = second
before := manifestChunk(1, 0, 1024)
divided := manifestChunk(2, 1024, 1024)
after := manifestChunk(3, 2048, 1024) // not in the fixture: must not be read
tail := dataChunk(300, 3072, 100)
chunks := []*filer_pb.FileChunk{before, divided, after, tail}
got, expanded, err := expandManifestsAt(chunks, 1500, fixture.resolve)
require.NoError(t, err)
require.True(t, expanded)
require.Equal(t, [][3]int64{{0, 1024, 1}, {1024, 256, 0}, {1280, 256, 0}, {1536, 256, 0}, {1792, 256, 0}, {2048, 1024, 1}, {3072, 100, 0}}, spans(got))
same, expanded, err := expandManifestsAt(chunks, 1024, fixture.resolve)
require.NoError(t, err)
require.False(t, expanded)
require.Equal(t, chunks, same, "a cut at a manifest's edge opens nothing")
}
// Nested manifests the cut divides are opened too; a manifest shortened
// earlier gives up the chunks past its span and cuts the one it ends in.
func TestExpandManifestsAtOpensNestedAndShortenedManifests(t *testing.T) {
fixture := manifestFixture{
10: {dataChunk(1, 0, 400), dataChunk(2, 400, 400)},
11: {dataChunk(3, 800, 400), dataChunk(4, 1200, 400)},
12: {manifestChunk(10, 0, 800), manifestChunk(11, 800, 800)},
}
got, _, err := expandManifestsAt([]*filer_pb.FileChunk{manifestChunk(12, 0, 1600)}, 1000, fixture.resolve)
require.NoError(t, err)
require.Equal(t, [][3]int64{{0, 800, 1}, {800, 400, 0}, {1200, 400, 0}}, spans(got))
// shortened to 1000 by an older truncate: chunk 4 is past it, chunk 3 ends at it
got, _, err = expandManifestsAt([]*filer_pb.FileChunk{manifestChunk(11, 800, 200)}, 900, fixture.resolve)
require.NoError(t, err)
require.Equal(t, [][3]int64{{800, 200, 0}}, spans(got))
require.Equal(t, uint64(400), fixture[11][0].Size, "the resolved chunks are not changed in place")
}
func TestExpandManifestsAtFailsWhenAManifestCannotBeRead(t *testing.T) {
_, _, err := expandManifestsAt([]*filer_pb.FileChunk{manifestChunk(5, 0, 1000)}, 500, manifestFixture{}.resolve)
require.Error(t, err)
loop := manifestFixture{6: {manifestChunk(6, 0, 1000)}}
_, _, err = expandManifestsAt([]*filer_pb.FileChunk{manifestChunk(6, 0, 1000)}, 500, loop.resolve)
require.ErrorContains(t, err, "nested deeper")
}
// manifestFileEntry is a 1200-byte file whose four 300-byte chunks (keys 1-4)
// sit in one manifest (key 9 on volume 9).
func manifestFileEntry() (*filer_pb.Entry, manifestFixture) {
var held []*filer_pb.FileChunk
for i := 0; i < 4; i++ {
held = append(held, dataChunk(uint64(i+1), int64(i)*300, 300))
}
entry := &filer_pb.Entry{Name: "file", Attributes: &filer_pb.FuseAttributes{Inode: 42, FileMode: 0644, Uid: 2000, Gid: 20, FileSize: 1200},
Chunks: []*filer_pb.FileChunk{manifestChunk(1, 0, 1200)}}
return entry, manifestFixture{1: held}
}
// A truncate through a manifest, with and without an open handle: the
// manifest's chunks are cut, its file id is registered for read tokens before
// it is read, all reads run under one deadline, and an open handle is marked
// as having rewritten its chunks.
func TestTruncateThroughManifestCutsItsChunks(t *testing.T) {
for _, open := range []bool{false, true} {
t.Run(fmt.Sprintf("open=%t", open), func(t *testing.T) {
wfs := newInvalidateTestWFS(t)
wfs.readAuth = newReadAuth(wfs)
current, fixture := manifestFileEntry()
fake := &attributeCASFiler{entry: proto.Clone(current).(*filer_pb.Entry)}
startFakeFiler(t, wfs, fake)
path := util.FullPath("/dir/file")
wfs.inodeToPath.Lookup(util.FullPath("/dir"), 1, true, false, 0, false)
wfs.inodeToPath.Lookup(path, 1, false, false, 42, true)
wfs.inodeToPath.MarkChildrenCached(util.FullPath("/dir"))
require.NoError(t, wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry("/dir", current), 1))
manifestFid := current.Chunks[0].GetFileIdString()
saved := truncateManifestResolver
t.Cleanup(func() { truncateManifestResolver = saved })
var reads int
truncateManifestResolver = func(ctx context.Context, wfs *WFS, manifest *filer_pb.FileChunk) ([]*filer_pb.FileChunk, error) {
reads++
deadline, ok := ctx.Deadline()
require.True(t, ok, "manifest read without a deadline")
require.LessOrEqual(t, time.Until(deadline), truncateManifestWait)
wfs.readAuth.mu.Lock()
tracked := wfs.readAuth.paths[manifestFid]
wfs.readAuth.mu.Unlock()
require.Equal(t, path, tracked, "manifest read before its id was registered for a read token")
return fixture.resolve(manifest)
}
var fh *FileHandle
var base *filer_pb.Entry
if open {
local := proto.Clone(current).(*filer_pb.Entry)
wfs.mapPbIdFromFilerToLocal(local)
fh = newFileHandle(wfs, 1, 42, local)
base = proto.Clone(local).(*filer_pb.Entry)
fh.baseEntry.Store(base)
fh.RememberPath(path)
wfs.fhMap.inode2fh[42], wfs.fhMap.fh2inode[1] = fh, 42
t.Cleanup(func() { fh.dirtyPages.Destroy() })
}
in := &fuse.SetAttrIn{}
in.NodeId, in.Valid, in.Size = 42, fuse.FATTR_SIZE, 450
require.Equal(t, fuse.OK, wfs.SetAttr(nil, in, &fuse.AttrOut{}))
require.Equal(t, 1, reads)
want := [][3]int64{{0, 300, 0}, {300, 150, 0}}
if open {
require.Equal(t, want, spans(fh.GetEntry().GetEntry().Chunks))
mark := fh.markSince(base)
require.True(t, mark.rewrote, "the expanded chunks must not be merged over a peer's chunk change")
require.Equal(t, int64(450), mark.truncatedTo)
return
}
fake.mu.Lock()
defer fake.mu.Unlock()
require.Equal(t, want, spans(fake.entry.Chunks))
require.Equal(t, uint64(450), fake.entry.Attributes.FileSize)
})
}
}
// Mount A cut a manifest's chunks to 200 bytes and has not published; mount B
// cut the file to 100 bytes and did, and the filer queued the rest for
// deletion. A's merge must not publish its chunks past B's cut: they are not
// writes of A's. The rewrite mark set when the truncate expanded the manifest
// refuses the merge, as a manifest across the cut did before.
func TestMergeRefusesTruncateExpansionOverPeerTruncate(t *testing.T) {
var held []*filer_pb.FileChunk
for i := 0; i < 4; i++ {
held = append(held, dataChunk(uint64(i+1), int64(i)*100, 100))
}
attrs := &filer_pb.FuseAttributes{Inode: 42, FileMode: 0644, FileSize: 400}
base := &filer_pb.Entry{Name: "file", Attributes: attrs, Chunks: []*filer_pb.FileChunk{manifestChunk(1, 0, 400)}}
local := proto.Clone(base).(*filer_pb.Entry)
local.Attributes.FileSize = 200
cut, expanded, err := expandManifestsAt(base.Chunks, 200, manifestFixture{1: held}.resolve)
require.NoError(t, err)
require.True(t, expanded)
local.Chunks = cut[:2] // the chunks past 200 bytes are dropped by the truncate
stored := proto.Clone(base).(*filer_pb.Entry)
stored.Attributes.FileSize = 100
stored.Chunks = []*filer_pb.FileChunk{proto.Clone(held[0]).(*filer_pb.FileChunk)}
_, ok := mergeContentEntry(stored, base, local, local, 200, true)
require.False(t, ok, "a truncate that expanded a manifest merged over a peer's truncate")
// without the mark, the merge publishes chunk 2, which B's truncate dropped
merged, ok := mergeContentEntry(stored, base, local, local, 200, false)
require.True(t, ok)
require.Contains(t, spans(merged.Chunks), [3]int64{100, 100, 0})
}
// An interrupt while the truncate reads a manifest answers EINTR, which the
// caller retries, not EIO.
func TestTruncateInterruptedReadingAManifestAnswersEINTR(t *testing.T) {
w := newInvalidateTestWFS(t)
entry := &filer_pb.Entry{
Name: "file",
Attributes: &filer_pb.FuseAttributes{Inode: 42, FileMode: 0644, FileSize: 1000},
Chunks: []*filer_pb.FileChunk{manifestChunk(7, 0, 1000)},
}
w.inodeToPath.Lookup("/dir", 1, true, false, 0, false)
w.inodeToPath.Lookup("/dir/file", 1, false, false, 42, true)
w.inodeToPath.MarkChildrenCached("/dir")
require.NoError(t, w.metaCache.InsertEntry(context.Background(), filer.FromPbEntry("/dir", entry), 1))
cancel := make(chan struct{})
saved := truncateManifestResolver
t.Cleanup(func() { truncateManifestResolver = saved })
truncateManifestResolver = func(ctx context.Context, _ *WFS, _ *filer_pb.FileChunk) ([]*filer_pb.FileChunk, error) {
close(cancel)
<-ctx.Done()
return nil, ctx.Err()
}
in := &fuse.SetAttrIn{}
in.NodeId, in.Valid, in.Size = 42, fuse.FATTR_SIZE, 500
require.Equal(t, fuse.EINTR, w.SetAttr(cancel, in, &fuse.AttrOut{}))
}
+17 -1
View File
@@ -145,13 +145,29 @@ func (wfs *WFS) SetAttr(cancel <-chan struct{}, input *fuse.SetAttrIn, out *fuse
}
oldFileSize := filer.FileSize(entry)
if size < oldFileSize {
// A manifest the cut goes through is cut as the chunks it holds.
cut, expanded, err := wfs.cutThroughManifests(ctx, path, entry.GetChunks(), int64(size))
if err != nil {
if ctx.Err() != nil {
return fuse.EINTR
}
glog.Warningf("%v truncate to %d: %v", path, size, err)
return fuse.EIO
}
if fh != nil {
fh.noteTruncate(size)
if expanded {
// Its chunks are not in the handle's base: a merge would take
// them for writes of this handle and put back the ones a
// peer's truncate dropped. Like a rewrite, they cannot go over
// a peer's chunk change.
fh.noteRewrite()
}
}
// fmt.Printf("truncate %v \n", fullPath)
var chunks []*filer_pb.FileChunk
var truncatedChunks []*filer_pb.FileChunk
for _, chunk := range entry.GetChunks() {
for _, chunk := range cut {
int64Size := int64(chunk.Size)
if chunk.Offset+int64Size > int64(size) {
// this chunk is truncated