Test delete intent work at full manifest geometry

This commit is contained in:
pingqiu committed 2026-12-02 07:03:41 -08:00
1 parent eaf456d5e0
commit ae0af6eed7
1 file changed
+251
@@ -0,0 +1,251 @@
package chunkstore
import (
"bytes"
"context"
"errors"
"fmt"
"path"
"sort"
"sync"
"testing"
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/filerpb"
"github.com/seaweedfs/seaweed-block/core/frontend/chunkstore/layout"
)
type intentCost struct {
mu sync.Mutex
lists, roots, segments, pages, data, headWrites int
rootBytes, segmentBytes, pageBytes int
deletes map[string]int
}
type intentCostFiler struct {
Filer
cost *intentCost
}
func (f *intentCostFiler) ListEntries(context.Context, string, string, string, uint32) ([]*filerpb.Entry, error) {
f.cost.mu.Lock()
f.cost.lists++
f.cost.mu.Unlock()
return nil, errors.New("counting control: intent driver enumerated the namespace")
}
func (f *intentCostFiler) UpdateEntry(ctx context.Context, dir string, e *filerpb.Entry, expected map[string][]byte) error {
err := f.Filer.UpdateEntry(ctx, dir, e, expected)
if err == nil && e.Name == layout.HeadName {
f.cost.mu.Lock()
f.cost.headWrites++
f.cost.mu.Unlock()
}
return err
}
func (f *intentCostFiler) DeleteEntry(ctx context.Context, dir, name string, expected map[string][]byte) error {
err := f.Filer.DeleteEntry(ctx, dir, name, expected)
if err == nil {
f.cost.mu.Lock()
f.cost.deletes[path.Join(dir, name)]++
f.cost.mu.Unlock()
}
return err
}
type intentCostBlobs struct {
Blobs
cost *intentCost
}
func (b *intentCostBlobs) Get(ctx context.Context, fid string) ([]byte, error) {
raw, err := b.Blobs.Get(ctx, fid)
if err != nil {
return nil, err
}
b.cost.mu.Lock()
defer b.cost.mu.Unlock()
switch {
case bytes.HasPrefix(raw, []byte("SWBROOT")):
b.cost.roots++
b.cost.rootBytes += len(raw)
case bytes.HasPrefix(raw, []byte("SWBSEG")):
b.cost.segments++
b.cost.segmentBytes += len(raw)
case bytes.HasPrefix(raw, []byte(`{"version":1,"volume_id":`)):
b.cost.pages++
b.cost.pageBytes += len(raw)
default:
b.cost.data++
}
return raw, nil
}
// This is a counting fake, not a payload durability fixture. It materializes
// all N canonical current references and their root/segments, but no live data
// payloads. Only the deleted snapshot's two candidate payloads are necessary.
func largeIntentCatalog(t *testing.T, n int) (*BCatalog, VolumeRef, *intentCost) {
t.Helper()
ctx := context.Background()
c, err := NewBCatalog(NewMemFiler(), newFakeBlobs(), "/blocks")
if err != nil {
t.Fatal(err)
}
v := newRef(t, "scale")
if _, err := c.CreateVolume(ctx, v, layout.Geometry{SizeBytes: uint64(n) * (4 << 20), BlockSize: 4096, ChunkBytes: 4 << 20}); err != nil {
t.Fatal(err)
}
s, err := c.Store(v)
if err != nil {
t.Fatal(err)
}
putManifest := func(refs map[uint64]layout.Ref) (layout.RootName, []layout.SegName) {
t.Helper()
root, segs, err := layout.Build(refs)
if err != nil {
t.Fatal(err)
}
var names []layout.SegName
for name, raw := range segs {
if err := layout.EnsureObject(ctx, s, name.Path(), raw, "fixture"); err != nil {
t.Fatal(err)
}
names = append(names, name)
}
rn, raw, err := layout.NameRoot(root)
if err != nil {
t.Fatal(err)
}
if err := layout.EnsureObject(ctx, s, rn.Path(), raw, "fixture"); err != nil {
t.Fatal(err)
}
return rn, names
}
refs := make(map[uint64]layout.Ref, n)
for i := range n {
n := layout.NameChunk(uint64(i), []byte("live payload not materialized"))
refs[uint64(i)] = layout.Ref{Owner: v.Name, OwnerID: v.ID, Chunk: n}
}
current, _ := putManifest(refs)
old := map[uint64]layout.Ref{}
var chunks []layout.ChunkName
for _, i := range []uint64{2, 3} {
raw := []byte(fmt.Sprintf("old chunk %d", i))
n := layout.NameChunk(i, raw)
if err := layout.EnsureObject(ctx, s, n.Path(), raw, "fixture"); err != nil {
t.Fatal(err)
}
old[i] = layout.Ref{Owner: v.Name, OwnerID: v.ID, Chunk: n}
chunks = append(chunks, n)
}
snapshot, oldSegments := putManifest(old)
h, _, err := s.ReadHead(ctx)
if err != nil {
t.Fatal(err)
}
h.Version, h.Epoch, h.Holder, h.ClaimID = layout.HeadVersion1, cleanHolder.Epoch, cleanHolder.Holder, cleanHolder.ClaimID
h.Current = &current
h.Snapshots = map[string]layout.Snapshot{"old": {Root: snapshot, Gen: h.Gen + 1}}
if err := s.UpdateHead(ctx, h.Gen, h, layout.CauseCommit); err != nil {
t.Fatal(err)
}
h, _, err = s.ReadHead(ctx)
if err != nil {
t.Fatal(err)
}
p := layout.ListPage{VolumeID: v.ID, Kind: layout.ListSnapshotDeleteIntent, Producer: "delete-old", Generation: h.Gen + 1, Pages: 1,
Target: &layout.SnapshotTarget{Name: "old", Gen: h.Snapshots["old"].Gen}, Entries: []string{chunks[1].Path()}}
tail, err := layout.StageSnapshotDeletePage(ctx, s, h.Gen, p)
if err != nil {
t.Fatal(err)
}
p.Pages, p.Next = 2, &tail
p.Entries = []string{snapshot.Path(), oldSegments[0].Path(), chunks[0].Path()}
sort.Strings(p.Entries)
head, err := layout.StageSnapshotDeletePage(ctx, s, h.Gen, p)
if err != nil {
t.Fatal(err)
}
if _, err := layout.RunDeleteSnapshotWithIntent(ctx, s, h.Gen, layout.ListCursor{Ref: head, Kind: p.Kind, Producer: p.Producer, Generation: p.Generation, Target: p.Target}); err != nil {
t.Fatal(err)
}
cost := &intentCost{deletes: map[string]int{}}
c.f, c.b = &intentCostFiler{Filer: c.f, cost: cost}, &intentCostBlobs{Blobs: c.b, cost: cost}
return c, v, cost
}
// Prediction before measurement: fixed two-page work needs at most three root
// GETs, two candidate-shard GETs and two page GETs, independent of N. Root bytes
// still grow with ceil(N/ShardSize); the existing format cost is reported, not
// hidden by constant request counts. All four candidate deletions occur once.
func TestCatalogIntentCountingAt262144Entries(t *testing.T) {
for _, n := range []int{1024, 16384, 262144} {
t.Run(fmt.Sprintf("entries_%d", n), func(t *testing.T) {
c, v, cost := largeIntentCatalog(t, n)
for page := 0; page < 2; page++ {
if pending, err := c.CleanSnapshotDeletePage(context.Background(), v); err != nil || pending != (page == 0) {
t.Fatalf("page %d: pending=%v err=%v", page, pending, err)
}
var err error
c, err = NewBCatalog(c.f, c.b, c.base)
if err != nil {
t.Fatal(err)
}
}
t.Logf("entries=%d list_calls=%d roots=%d root_bytes=%d segments=%d segment_bytes=%d pages=%d page_bytes=%d payload_gets=%d deletes=%d cursor_writes=%d", n, cost.lists, cost.roots, cost.rootBytes, cost.segments, cost.segmentBytes, cost.pages, cost.pageBytes, cost.data, len(cost.deletes), cost.headWrites)
if cost.lists != 0 || cost.roots > 3 || cost.segments > 2 || cost.pages != 2 || cost.data != 0 || len(cost.deletes) != 4 || cost.headWrites != 2 {
t.Fatalf("counting prediction violated: %+v", cost)
}
for object, count := range cost.deletes {
if count != 1 {
t.Fatalf("completed deletion repeated: %s count=%d", object, count)
}
}
})
}
}
func TestCatalogIntentInterruptedCountingAt262144Entries(t *testing.T) {
for _, afterWrite := range []bool{false, true} {
t.Run(fmt.Sprintf("lost_reply_%v", afterWrite), func(t *testing.T) {
ctx := context.Background()
c, v, cost := largeIntentCatalog(t, 262144)
fault := &intentCursorFault{Filer: c.f, armed: true, afterWrite: afterWrite, deletes: map[string]int{}}
c.f = fault
pending, err := c.CleanSnapshotDeletePage(ctx, v)
if afterWrite {
if err != nil || !pending {
t.Fatalf("lost cursor reply: pending=%v err=%v", pending, err)
}
} else if !errors.Is(err, errIntentCursorInterrupted) {
t.Fatalf("cursor interruption not reported: %v", err)
}
// Root, segment and first chunk must be deleted before any cursor
// advance, including an update whose reply is lost.
if len(cost.deletes) != 3 || cost.headWrites != map[bool]int{false: 0, true: 1}[afterWrite] {
t.Fatalf("effects/cursor order: deletes=%v writes=%d", cost.deletes, cost.headWrites)
}
c, err = NewBCatalog(fault, c.b, c.base)
if err != nil {
t.Fatal(err)
}
if !afterWrite {
if pending, err := c.CleanSnapshotDeletePage(ctx, v); err != nil || !pending {
t.Fatalf("retry unfinished page: pending=%v err=%v", pending, err)
}
}
if pending, err := c.CleanSnapshotDeletePage(ctx, v); err != nil || pending {
t.Fatalf("finish tail: pending=%v err=%v", pending, err)
}
t.Logf("lost_reply=%v roots=%d segments=%d pages=%d deletes=%d cursor_writes=%d", afterWrite, cost.roots, cost.segments, cost.pages, len(cost.deletes), cost.headWrites)
if cost.lists != 0 || cost.roots > 5 || cost.segments > 3 || cost.pages > 3 || cost.data != 0 || len(cost.deletes) != 4 || cost.headWrites != 2 {
t.Fatalf("interrupted counting prediction violated: %+v", cost)
}
for object, count := range cost.deletes {
if count != 1 {
t.Fatalf("completed deletion repeated: %s count=%d", object, count)
}
}
})
}
}