From e5391bea5431471cf8c5fc729b2d75ecc6a35231 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Thu, 19 Nov 2026 01:23:44 -0800 Subject: [PATCH] feat: capture chunk births under the candidate barrier --- .../core/frontend/chunkstore/catalog_test.go | 2 +- .../frontend/chunkstore/retention_test.go | 6 +++--- seaweed-block/core/storage/chunk_birth.go | 18 ++++++++++++++++++ seaweed-block/core/storage/chunk_layer.go | 19 ++++++++++++------- 4 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 seaweed-block/core/storage/chunk_birth.go diff --git a/seaweed-block/core/frontend/chunkstore/catalog_test.go b/seaweed-block/core/frontend/chunkstore/catalog_test.go index a81b2a262f..33ade710d3 100644 --- a/seaweed-block/core/frontend/chunkstore/catalog_test.go +++ b/seaweed-block/core/frontend/chunkstore/catalog_test.go @@ -285,7 +285,7 @@ func TestFork_HeadIsEpochZeroWithQualifiedKeys(t *testing.T) { t.Fatalf("chunk %d ref %+v, want @v1/%s with the same hash", idx, ref, src.Key) } } - if res.HeadGen != 1 || res.HeadEpoch != 0 || res.QualifiedRefs != 2 || res.SourceGen != h.Gen || res.RemoteLSN != 42 || res.Replayed { + if res.HeadGen != 1 || res.HeadEpoch != 0 || res.QualifiedRefs != 1 || res.ChunkCount != 2 || res.SourceGen != h.Gen || res.RemoteLSN != 42 || res.Replayed { t.Fatalf("result %+v", res) } v, ok, err := c.ReadVolume(ctx, "v2") diff --git a/seaweed-block/core/frontend/chunkstore/retention_test.go b/seaweed-block/core/frontend/chunkstore/retention_test.go index 17c5991523..765aee8490 100644 --- a/seaweed-block/core/frontend/chunkstore/retention_test.go +++ b/seaweed-block/core/frontend/chunkstore/retention_test.go @@ -372,7 +372,7 @@ func TestCatalog_UsageFromManifests(t *testing.T) { if u.LiveChunks != 2 || u.LiveBytes != 2*full || len(u.Snapshots) != 1 || u.Snapshots[0].ExclusiveBytes != 0 { t.Fatalf("shared chunks: %+v", u) } - if u.StoredBindings != 2 || u.StoredBytes != 2*full || u.StoredUpperBound { + if u.StoredBindings != 2+u.ManifestBindings || u.ManifestBindings != 1 || u.StoredBytes != 2*full+u.ManifestBytes || u.StoredUpperBound { t.Fatalf("stored: %+v; want 2 bindings, exact", u) } @@ -386,7 +386,7 @@ func TestCatalog_UsageFromManifests(t *testing.T) { // both of s1's chunks are now exclusive: the new HEAD uses e2 keys t.Fatalf("after HEAD moved: s1 exclusive %d, want %d", u.Snapshots[0].ExclusiveBytes, 2*full) } - if u.StoredBindings != 4 || u.StoredBytes != 4*full || u.StoredUpperBound { + if u.StoredBindings != 4+u.ManifestBindings || u.ManifestBindings != 2 || u.StoredBytes != 4*full+u.ManifestBytes || u.StoredUpperBound { t.Fatalf("stored after HEAD moved: %+v; want 4 bindings, exact", u) } @@ -399,7 +399,7 @@ func TestCatalog_UsageFromManifests(t *testing.T) { if err != nil { t.Fatal(err) } - if len(u.Snapshots) != 0 || u.StoredBindings != 4 || !u.StoredUpperBound { + if len(u.Snapshots) != 0 || u.StoredBindings != 4+u.ManifestBindings || !u.StoredUpperBound { t.Fatalf("after delete: %+v; want no snapshots, 4 bindings, upper bound", u) } } diff --git a/seaweed-block/core/storage/chunk_birth.go b/seaweed-block/core/storage/chunk_birth.go new file mode 100644 index 0000000000..6c580c43ca --- /dev/null +++ b/seaweed-block/core/storage/chunk_birth.go @@ -0,0 +1,18 @@ +package storage + +// The caller holds captureMu exclusive, with no layer state lock held. +// An owned-map max is the newest local write in that chunk; base fills do +// not advance it. Other engines use the cut frontier as an upper bound. +func (l *chunkLayerImpl) captureBirths(candidates map[uint64]bool, frontier uint64) map[uint64]uint64 { + out := make(map[uint64]uint64, len(candidates)) + l.s.mu.RLock() + defer l.s.mu.RUnlock() + for idx := range candidates { + birth := frontier + if l.usesOwnedMapV3() && l.s.v3.mapEntries[idx].LSN != 0 { + birth = l.s.v3.mapEntries[idx].LSN + } + out[idx] = birth + } + return out +} diff --git a/seaweed-block/core/storage/chunk_layer.go b/seaweed-block/core/storage/chunk_layer.go index d8d56fc664..e9b03fa840 100644 --- a/seaweed-block/core/storage/chunk_layer.go +++ b/seaweed-block/core/storage/chunk_layer.go @@ -780,11 +780,12 @@ func (l *chunkLayerImpl) MakeRemoteDurable(ctx context.Context, t uint64) error } type capturedChunk struct { - idx uint64 - data []byte - sum [32]byte - zero bool - key string // staged: already uploaded under this key (data is nil) + idx uint64 + data []byte + sum [32]byte + zero bool + key string // staged: already uploaded under this key (data is nil) + birth uint64 } // stagedChunk is a chunk pre-copied outside the barrier at chunk version @@ -907,6 +908,9 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch l.cutLSN = h baseHead := copyHead(l.head) l.stMu.Unlock() + // Candidates, mutation versions and births belong to the same barrier. + // Do not sample the owned map after COW writers are admitted again. + births := l.captureBirths(candidates, h) st.CandidateChunks = len(candidates) var copyErr error @@ -974,6 +978,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch } var uploads []upload for _, c := range captured { + c.birth = births[c.idx] ref, had := baseHead.Chunks[c.idx] switch { case c.zero && !had, had && !c.zero && ref.SHA256 == c.sum: @@ -986,7 +991,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch continue } if c.key != "" { // staged: uploaded before the barrier - next.Chunks[c.idx] = chunkRef{Key: c.key, SHA256: c.sum} + next.Chunks[c.idx] = chunkRef{Key: c.key, SHA256: c.sum, Encoding: "full", BirthLSN: c.birth} newKeys[c.idx] = c.key changed = append(changed, c.idx) st.StagedChunksUsed++ @@ -1019,7 +1024,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch return chunkCutResult{}, fmt.Errorf("storage: capture upload: %w: %w", errChunkRemoteUnavailable, err) } for _, u := range uploads { - next.Chunks[u.c.idx] = chunkRef{Key: u.key, SHA256: u.c.sum} + next.Chunks[u.c.idx] = chunkRef{Key: u.key, SHA256: u.c.sum, Encoding: "full", BirthLSN: u.c.birth} newKeys[u.c.idx] = u.key changed = append(changed, u.c.idx) st.UploadedChunks++