feat: capture chunk births under the candidate barrier

This commit is contained in:
pingqiu committed 2026-11-19 01:23:44 -08:00
1 parent f92e6da171
commit e5391bea54
4 files changed
+34 -11

No files matched your search

@@ -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")
@@ -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)
}
}
+18
View File
@@ -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
}
+12 -7
View File
@@ -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++