Files
seaweedfs/weed/filer/filechunks_minus_test.go
T
MapleWhiteZZZandMapleWhiteZZZ 76cae100de filer: leave the manifests an update keeps unread when finding its garbage (#13224)
MinusChunks finds the chunks an update drops by resolving every manifest of
the old entry and of the new one. A manifest both entries list is the same
needle on both sides: all it holds is held by both, so none of it can be
dropped, yet it was read from a volume server twice on every update. A file
whose older chunks are folded into manifests paid that on each chmod, utimes,
setxattr and append.

Manifests listed on both sides are now taken out of both lists before the
others are resolved. They are read only when something else the old entry
holds might still be held through one of them, and then subtracted, so the
result is the one resolving everything gives. An attribute change or an
append that keeps the manifests reads none of them, and a kept manifest
that cannot be read no longer fails an update that drops nothing.

Co-authored-by: MapleWhiteZZZ <18653045+MapleWhiteZZZ@users.noreply.github.com>
2026-12-01 12:00:31 -08:00

223 lines
7.7 KiB
Go

package filer
import (
"context"
"fmt"
"math"
"math/rand"
"net/http"
"net/http/httptest"
"sort"
"strings"
"sync/atomic"
"testing"
"google.golang.org/protobuf/proto"
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
)
// manifestVolume serves the manifests of a testManifestStore by file id, as a
// volume server would, and counts the reads.
type manifestVolume struct {
store *testManifestStore
reads atomic.Int64
url string
}
func newManifestVolume(t *testing.T) *manifestVolume {
t.Helper()
v := &manifestVolume{store: newTestManifestStore()}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
v.reads.Add(1)
data, ok := v.store.stored[strings.TrimPrefix(r.URL.Path, "/")]
if !ok {
http.NotFound(w, r)
return
}
w.Write(data)
}))
t.Cleanup(srv.Close)
v.url = srv.URL
return v
}
func (v *manifestVolume) lookup(ctx context.Context, fileId string) ([]string, error) {
return []string{v.url + "/" + fileId}, nil
}
// manifest stores a manifest holding chunks and returns its chunk.
func (v *manifestVolume) manifest(t *testing.T, chunks ...*filer_pb.FileChunk) *filer_pb.FileChunk {
t.Helper()
held := make([]*filer_pb.FileChunk, len(chunks))
for i, c := range chunks {
held[i] = proto.Clone(c).(*filer_pb.FileChunk)
}
filer_pb.BeforeEntrySerialization(held)
data, err := proto.Marshal(&filer_pb.FileChunkManifest{Chunks: held})
if err != nil {
t.Fatal(err)
}
// a manifest covers the span of what it holds
start, stop := int64(math.MaxInt64), int64(0)
for _, c := range chunks {
start, stop = min(start, c.Offset), max(stop, c.Offset+int64(c.Size))
}
v.store.nextID++
m := &filer_pb.FileChunk{
Fid: &filer_pb.FileId{VolumeId: 999, FileKey: uint64(v.store.nextID), Cookie: 7},
IsChunkManifest: true,
Offset: start,
Size: uint64(stop - start),
}
v.store.stored[m.GetFileIdString()] = data
return m
}
func ids(chunks []*filer_pb.FileChunk) []string {
out := make([]string, 0, len(chunks))
for _, c := range chunks {
out = append(out, ChunkIdentity(c.GetFileIdString()))
}
sort.Strings(out)
return out
}
// minusEveryManifest is MinusChunks as it was: every manifest on both sides
// resolved.
func minusEveryManifest(t *testing.T, v *manifestVolume, as, bs []*filer_pb.FileChunk) []*filer_pb.FileChunk {
t.Helper()
aData, aMeta, err := ResolveChunkManifest(context.Background(), v.lookup, as, 0, math.MaxInt64, nil, false)
if err != nil {
t.Fatal(err)
}
bData, bMeta, err := ResolveChunkManifest(context.Background(), v.lookup, bs, 0, math.MaxInt64, nil, false)
if err != nil {
t.Fatal(err)
}
return append(DoMinusChunks(aData, bData), DoMinusChunks(aMeta, bMeta)...)
}
func TestMinusChunksReadsOnlyTheManifestsAnUpdateChanges(t *testing.T) {
v := newManifestVolume(t)
d := func(i int) *filer_pb.FileChunk {
return testChunk(1, uint64(i), uint32(i), int64(i*100), 100, int64(i))
}
m1 := v.manifest(t, d(1), d(2), d(3))
m2 := v.manifest(t, d(4), d(5), d(6))
m3 := v.manifest(t, d(7), d(8))
cases := []struct {
name string
old, new []*filer_pb.FileChunk
want []*filer_pb.FileChunk
wantReads int64
everyReads int64 // what resolving every manifest costs
}{
{"attribute change", []*filer_pb.FileChunk{m1, m2, d(7)}, []*filer_pb.FileChunk{m1, m2, d(7)}, nil, 0, 4},
{"append", []*filer_pb.FileChunk{m1, m2, d(7)}, []*filer_pb.FileChunk{m1, m2, d(7), d(8)}, nil, 0, 4},
{"fold the tail", []*filer_pb.FileChunk{m1, m2, d(7), d(8)}, []*filer_pb.FileChunk{m1, m2, m3}, nil, 1, 5},
{"rewrite the tail", []*filer_pb.FileChunk{m1, m2, d(7)}, []*filer_pb.FileChunk{m1, m2, d(9)}, []*filer_pb.FileChunk{d(7)}, 2, 4},
{"truncate to 0", []*filer_pb.FileChunk{m1, m2, d(7)}, nil, []*filer_pb.FileChunk{d(1), d(2), d(3), d(4), d(5), d(6), d(7), m1, m2}, 2, 2},
{"a dropped chunk still held through a kept manifest", []*filer_pb.FileChunk{m1, d(2)}, []*filer_pb.FileChunk{m1}, nil, 1, 2},
{"a dropped manifest", []*filer_pb.FileChunk{m1, m2}, []*filer_pb.FileChunk{m1}, []*filer_pb.FileChunk{d(4), d(5), d(6), m2}, 2, 3},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
v.reads.Store(0)
got, err := MinusChunks(context.Background(), v.lookup, tc.old, tc.new, nil)
if err != nil {
t.Fatal(err)
}
reads := v.reads.Load()
v.reads.Store(0)
every := minusEveryManifest(t, v, tc.old, tc.new)
if g, w := ids(got), ids(tc.want); fmt.Sprint(g) != fmt.Sprint(w) {
t.Errorf("got %v, want %v", g, w)
}
if g, w := ids(got), ids(every); fmt.Sprint(g) != fmt.Sprint(w) {
t.Errorf("got %v, resolving every manifest gives %v", g, w)
}
if reads != tc.wantReads {
t.Errorf("read %d manifests, want %d", reads, tc.wantReads)
}
if r := v.reads.Load(); r != tc.everyReads {
t.Errorf("resolving every manifest read %d, test expects %d", r, tc.everyReads)
}
})
}
}
// Random old and new chunk lists, with manifests held by both, nested
// manifests and chunks listed twice: the delta is the one resolving every
// manifest gives.
func TestMinusChunksMatchesResolvingEveryManifest(t *testing.T) {
v := newManifestVolume(t)
rnd := rand.New(rand.NewSource(13))
data := make([]*filer_pb.FileChunk, 40)
for i := range data {
data[i] = testChunk(uint32(1+i%3), uint64(i+1), uint32(i+1), int64(i*100), 100, int64(i+1))
}
pick := func(from []*filer_pb.FileChunk, n int) []*filer_pb.FileChunk {
var out []*filer_pb.FileChunk
for i := 0; i < n; i++ {
out = append(out, from[rnd.Intn(len(from))])
}
return out
}
for iter := 0; iter < 400; iter++ {
var manifests []*filer_pb.FileChunk
for k := 0; k < 1+rnd.Intn(6); k++ {
held := pick(data, 1+rnd.Intn(6))
if len(manifests) > 0 && rnd.Intn(3) == 0 {
held = append(held, manifests[rnd.Intn(len(manifests))]) // nested
}
manifests = append(manifests, v.manifest(t, held...))
}
listed := func() []*filer_pb.FileChunk {
out := append(pick(data, rnd.Intn(6)), pick(manifests, rnd.Intn(4))...)
if rnd.Intn(5) == 0 {
// a listing with an empty span, which resolving skips
empty := proto.Clone(manifests[rnd.Intn(len(manifests))]).(*filer_pb.FileChunk)
empty.Size = 0
out = append(out, empty)
}
return out
}
old, cur := listed(), listed()
if rnd.Intn(4) == 0 {
cur = append(cur, old...) // an update that keeps everything
}
rnd.Shuffle(len(old), func(i, j int) { old[i], old[j] = old[j], old[i] })
rnd.Shuffle(len(cur), func(i, j int) { cur[i], cur[j] = cur[j], cur[i] })
got, err := MinusChunks(context.Background(), v.lookup, old, cur, nil)
if err != nil {
t.Fatal(err)
}
want := minusEveryManifest(t, v, old, cur)
if g, w := ids(got), ids(want); fmt.Sprint(g) != fmt.Sprint(w) {
t.Fatalf("iteration %d: old %v new %v: got %v, resolving every manifest gives %v", iter, ids(old), ids(cur), g, w)
}
}
}
// A manifest both sides keep is not read, so one that cannot be read does not
// fail an update that drops nothing; one that has to be read still does.
func TestMinusChunksUnreadableKeptManifest(t *testing.T) {
v := newManifestVolume(t)
d := func(i int) *filer_pb.FileChunk {
return testChunk(1, uint64(i), uint32(i), int64(i*100), 100, int64(i))
}
m1 := v.manifest(t, d(1), d(2))
delete(v.store.stored, m1.GetFileIdString())
got, err := MinusChunks(context.Background(), v.lookup, []*filer_pb.FileChunk{m1, d(3)}, []*filer_pb.FileChunk{m1, d(3), d(4)}, nil)
if err != nil || len(got) != 0 {
t.Fatalf("an update keeping the manifest: got %v, %v; want nothing, no error", ids(got), err)
}
if _, err := MinusChunks(context.Background(), v.lookup, []*filer_pb.FileChunk{m1, d(3)}, []*filer_pb.FileChunk{m1}, nil); err == nil {
t.Fatal("an update dropping a chunk the kept manifest might hold: want the read error")
}
}