From ddae3ac15ed5346b71fa954adde34bcc8645cc5b Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sun, 15 Nov 2026 18:39:35 -0800 Subject: [PATCH] test: pin failure joins range reuse and document manifest compatibility --- .../core/frontend/chunkstore/chunk_remote.go | 3 + .../core/frontend/chunkstore/manifest_test.go | 21 +++++++ .../storage/chunk_upload_parallel_test.go | 63 +++++++++++++++++++ seaweed-block/docs/chunk-manifests.md | 58 +++++++++++++++++ 4 files changed, 145 insertions(+) create mode 100644 seaweed-block/docs/chunk-manifests.md diff --git a/seaweed-block/core/frontend/chunkstore/chunk_remote.go b/seaweed-block/core/frontend/chunkstore/chunk_remote.go index 38d1e5e668..25d16aa34b 100644 --- a/seaweed-block/core/frontend/chunkstore/chunk_remote.go +++ b/seaweed-block/core/frontend/chunkstore/chunk_remote.go @@ -435,6 +435,9 @@ func headEntry(h storage.ChunkHead) (*filerpb.Entry, error) { } } if h.Segments != nil { + if err := validateRoot(h); err != nil { + return nil, err + } doc.Format = 2 doc.Chunks = nil doc.Segments = h.Segments diff --git a/seaweed-block/core/frontend/chunkstore/manifest_test.go b/seaweed-block/core/frontend/chunkstore/manifest_test.go index c8ab0ec751..58d39ea59c 100644 --- a/seaweed-block/core/frontend/chunkstore/manifest_test.go +++ b/seaweed-block/core/frontend/chunkstore/manifest_test.go @@ -183,6 +183,27 @@ func TestManifest_ReusesUnchangedRangeAndCrashKeepsOldRoot(t *testing.T) { if err != nil || gen != old.Gen+1 { t.Fatalf("normal CAS semantics: %d %v", gen, err) } + + published, err := r.GetHead(ctx) + if err != nil { + t.Fatal(err) + } + changed := published + changed.Chunks = make(map[uint64]storage.ChunkRef, len(published.Chunks)) + for idx, ref := range published.Chunks { + changed.Chunks[idx] = ref + } + if err = r.Put(ctx, "changed-range-zero", data); err != nil { + t.Fatal(err) + } + changed.Chunks[0] = storage.ChunkRef{Key: "changed-range-zero", SHA256: sha256.Sum256(data)} + rewritten, _, err := r.PrepareManifest(ctx, published, changed, published.ChunkBytes) + if err != nil { + t.Fatal(err) + } + if rewritten.Segments[0] == published.Segments[0] || rewritten.Segments[1] != published.Segments[1] { + t.Fatal("cut must rewrite only the changed range") + } } func TestManifest_UnreadablePublishedSegmentRefusesWholeGC(t *testing.T) { diff --git a/seaweed-block/core/storage/chunk_upload_parallel_test.go b/seaweed-block/core/storage/chunk_upload_parallel_test.go index c5b72a547b..f15df41ec5 100644 --- a/seaweed-block/core/storage/chunk_upload_parallel_test.go +++ b/seaweed-block/core/storage/chunk_upload_parallel_test.go @@ -2,7 +2,9 @@ package storage import ( "context" + "errors" "fmt" + "sync/atomic" "testing" "time" ) @@ -93,3 +95,64 @@ func TestChunkUploads_ConfiguredBoundAndCancellation(t *testing.T) { }) } } + +// A remote may ignore cancellation. Its owned upload must settle before +// failure cleanup can remove the intent; cancellation alone is not a join. +type unjoinedUploadRemote struct { + *fakeChunkRemote + calls atomic.Int32 + second chan struct{} + failed chan struct{} + release chan struct{} +} + +func (r *unjoinedUploadRemote) Put(ctx context.Context, key string, data []byte) error { + if r.calls.Add(1) == 1 { + <-r.second + close(r.failed) + return errFakeRemoteDown + } + close(r.second) + <-r.release + return r.fakeChunkRemote.Put(context.Background(), key, data) +} +func TestChunkUploads_FailureJoinsBeforeIntentCleanup(t *testing.T) { + s, _ := newChunkTestStore(t) + f := newFakeChunkRemote() + r := &unjoinedUploadRemote{fakeChunkRemote: f, second: make(chan struct{}), failed: make(chan struct{}), release: make(chan struct{})} + cfg := ctConfig(1) + cfg.FreshFromHead = true + cfg.UploadConcurrency = 2 + l := mustAttach(t, s, r, cfg) + w := newWriteLog() + w.write(t, s, 0, 0x21) + w.write(t, s, ctChunkBlocks, 0x22) + done := make(chan error, 1) + go func() { _, err := l.Cut(context.Background()); done <- err }() + select { + case <-r.failed: + case <-time.After(time.Second): + close(r.release) + <-done + t.Fatal("uploads did not overlap") + } + select { + case err := <-done: + close(r.release) + t.Fatalf("returned with owned upload still active: %v", err) + case <-time.After(20 * time.Millisecond): + } + if len(liveIntents(t, f)) == 0 { + close(r.release) + <-done + t.Fatal("intent removed before join") + } + close(r.release) + if err := <-done; !errors.Is(err, errFakeRemoteDown) { + t.Fatalf("lost upload failure: %v", err) + } + if f.stats().cas != 0 { + t.Fatal("failed batch published") + } + assertNoIntentsLeft(t, f) +} diff --git a/seaweed-block/docs/chunk-manifests.md b/seaweed-block/docs/chunk-manifests.md new file mode 100644 index 0000000000..311768c7bc --- /dev/null +++ b/seaweed-block/docs/chunk-manifests.md @@ -0,0 +1,58 @@ +# Chunk upload and manifest format + +`blockvolume --durable-impl=walstore-chunk` uploads dirty chunks in parallel. +`--chunk-upload-concurrency` defaults to 8 and accepts 1 through 64; 1 selects +serial upload. The setting applies to both capture and pre-copy. Segment size +is a format constant: each segment covers one logical GiB. + +HEAD format 1 remains readable. A volume's next cut upgrades it to format 2 in +one ordinary fenced root CAS (including an otherwise no-op cut); generation +advances once, with the ordinary epoch/holder/LSN checks. Old binaries cannot +read format 2: deploy format-aware readers and GC before publishing new cuts. +Downgrading a volume after its first format-2 cut is unsupported. + +A format-2 HEAD contains lineage, chunk geometry/count, and immutable segment +references, instead of a flat chunk map. Each segment contains a sparse map of +chunk indices and SHA-256 references; missing indices mean ZERO. Unchanged +ranges retain their exact segment keys. Data and segment keys are protected by +cut intents before upload. A root is published only after its objects exist; +an unknown root CAS retains the intents until reconciliation. + +Segment keys are owner-qualified (`@/seg/`); a segment's chunk +references retain their source namespace. Snapshot/fork copies only the root. +The fork's root alone keeps inherited segments/data live after deleting its +parent snapshot. There is no separate segment claim or segment publication +tombstone. Snapshot attempt tombstones and never-reused volume ids are +unchanged. Reclaimed immutable key bindings keep the existing GC marker that +prevents rebinding an old key. + +GC traces HEADs, snapshot attempts and copy intents through their verified +segments to data. It also marks segment objects themselves. A published +segment that cannot be read or verified refuses the entire mark, in dry-run +and apply; no chunks are deleted after an incomplete mark. Capture intents +protect prospective keys directly, including not-yet-uploaded segments. The +existing higher-epoch plus TTL rule, copy-target tombstone settlement, two +marks separated by grace, upload age floor and unbound-needle retention remain. +A collector adapter without blob reads can collect flat roots but refuses a +segmented root rather than omitting its children. + +## Locks and cancellation + +The existing cut/publication serialization is unchanged. Capture buffers and +staged versions are selected as before; upload workers acquire no WAL, +publication, shard or capture-state lock. Only the caller updates maps and +counters after joining workers. The worker queue mutex guards admission and +first error only, with no I/O under it. Failure cancels new admission and joins +all owned calls before clean-abort intent cleanup. A remote ignoring +cancellation can delay that join; it is never reported settled early. + +## Reproducible proxy measurements + +`go test -tags=manifest_perf ./core/frontend/chunkstore -run '^TestManifestPerf_' -count=1 -timeout=600s -v` + +This materializes and fsyncs immutable files, including all 10 GiB of the large +volume. Metadata uses the deterministic filer model. It compares 64 dirty +4 MiB chunks at concurrency 1/8 in alternating order (three samples each), and +flat versus segmented snapshot/fork operations (three samples each). Reported +root bytes exclude segment bytes, which are reported separately. It excludes +network latency, the block frontend, guest fsync and power-loss behavior.