storage: touched-set bookkeeping: attach allocation, drained pause, status

Three accounting fixes for the touched-set candidates, each with a red:
the live touched set is allocated before the layer's hooks are published
(a v1.1 store's first map entry change otherwise allocated the bitmap
under the store's lock); a drained pre-copy cut charges its dirty-set
and whole-volume walks to the pause (they ran inside it, uncounted); and
the status's dirty count includes a cut's judged candidates until its
HEAD installs. The unknown-commit-outcome test now compares the remote
image byte for byte.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-12-02 04:41:29 -08:00
1 parent f54fc487d0
commit 7cd5240aea
2 files changed
+121 -3

No files matched your search

@@ -2,6 +2,7 @@ package storage
import (
"bytes"
"context"
"crypto/sha256"
"encoding/binary"
"errors"
@@ -325,7 +326,7 @@ func TestUnknownCommitOutcomeLosesNoChunk(t *testing.T) {
if _, err := l.Cut(ctTimeout(t)); !errors.Is(err, errChunkCASOutcomeUnknown) {
t.Fatalf("the injected cut answered %v, want its outcome unknown", err)
}
w.write(t, s, 5*ctChunkBlocks, 0xa5) // chunk 5, after the cut
last5 := w.write(t, s, 5*ctChunkBlocks, 0xa5) // chunk 5, after the cut
var err error
var last chunkCutResult
for i := 0; i < 3; i++ { // the next cuts settle the unknown outcome and publish
@@ -346,6 +347,15 @@ func TestUnknownCommitOutcomeLosesNoChunk(t *testing.T) {
t.Fatalf("chunk %d changed=%v after the unknown outcome and the next cut, want %v", idx, changed, want)
}
}
// Byte for byte: the remote is exactly the written image (the baseline
// with the three writes), not merely different in the right chunks.
if want := w.imageAt(last5); !bytes.Equal(img, want) {
for at := 0; at < len(want); at += ctBlockSize {
if !bytes.Equal(img[at:at+ctBlockSize], want[at:at+ctBlockSize]) {
t.Fatalf("the remote image differs from the written one at block %d", at/ctBlockSize)
}
}
}
}
// The candidate set's OR keeps the summary, so a set ORed back is listed in
@@ -600,3 +610,94 @@ func TestACutThatAbortsAfterTakingTheRequestReissuesIt(t *testing.T) {
t.Fatalf("the next cut left request generation %d pending", got)
}
}
// The live touched set exists once the layer attaches, before any write,
// on both store formats: a first map entry change only sets a bit (never
// allocates the bitmap under the store's lock). On v1.0 the record shadow's
// attach already seeds it; v1.1's record open does not, so it is allocated
// explicitly before the hooks are published.
func TestTheTouchedSetExistsAtAttach(t *testing.T) {
for _, format := range []string{"v1.0", "v1.1"} {
t.Run(format, func(t *testing.T) {
if format == "v1.1" {
withV11Format(t)
} else {
v10Only(t)
}
s, _ := newChunkTestStore(t)
// attachChunkLayer itself, not mustAttach: its reconcile wait reads
// Stats, whose candidate listing would seed the set.
layer, err := attachChunkLayer(s, newFakeChunkRemote(), ctConfig(1))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = layer.Close() })
l := layer.(*chunkLayerImpl)
l.unkMu.Lock()
allocated := l.touched != nil
l.unkMu.Unlock()
if !allocated {
t.Fatal("no touched set after attach: the first write would allocate it under the store lock")
}
})
}
}
// A chunk a cut judged a candidate stays in the status's DirtyChunks until
// that cut's HEAD installs, even after the flush checkpoints it out of the
// WAL dirty map while the cut's upload is in flight.
func TestStatsCountsACutsCandidatesUntilItsHeadInstalls(t *testing.T) {
s, _ := newChunkTestStore(t)
f := newFakeChunkRemote()
layer := mustAttach(t, s, f, ctConfig(1))
l := layer.(*chunkLayerImpl)
w := newWriteLog()
if _, err := l.Cut(ctTimeout(t)); err != nil { // the seed, and an empty touched set
t.Fatal(err)
}
w.write(t, s, 2*ctChunkBlocks, 0x22) // chunk 2
entered, release := holdPuts(t, f)
t.Cleanup(release)
done := startCapture(t, layer)
waitKey(t, entered, "chunk upload")
mustSyncFlush(t, s) // the write leaves the dirty map while the cut holds chunk 2
got := l.Stats().DirtyChunks
release()
if err := waitErr(t, done, "capture"); err != nil {
t.Fatal(err)
}
if got < 1 {
t.Fatalf("status during the cut's upload: %d dirty chunks, want chunk 2 counted", got)
}
}
// A pre-copy cut keeps its frontend drained through the candidate step, so
// every walk it makes there is pause work and is charged: a BASE's
// whole-volume pass (allDirty) is charged with the judging it feeds,
// compared with the same drained cut without it.
func TestADrainedCutChargesItsWholeVolumeWalkToThePause(t *testing.T) {
s, _ := newChunkTestStore(t)
l := mustAttach(t, s, newFakeChunkRemote(), ctConfig(1)).(*chunkLayerImpl)
if _, err := l.Cut(ctTimeout(t)); err != nil { // the seed, and an empty touched set
t.Fatal(err)
}
l.precopyPrepare = func(context.Context) (func(), error) { return func() {}, nil } // drained to the freeze
t.Cleanup(func() { l.precopyPrepare = nil })
plain, err := l.Cut(ctTimeout(t)) // drained, no whole-volume walk
if err != nil {
t.Fatal(err)
}
l.stMu.Lock()
l.allDirty = true // as after a BASE: every chunk is walked
l.stMu.Unlock()
walked, err := l.Cut(ctTimeout(t))
if err != nil {
t.Fatal(err)
}
// The whole-volume cut adds, inside the drained pause, the BASE pass (one
// visit per chunk) and the judging of every chunk that pass names (one
// per chunk): both are charged. Charging before the pass drops the first.
if got, want := walked.Stats.PauseCandidateOps-plain.Stats.PauseCandidateOps, 2*int64(l.numChunks); got < want {
t.Fatalf("a drained cut's whole-volume walk added %d pause candidate operations (%d -> %d), want at least %d (the pass and the judging, %d chunks each)", got, plain.Stats.PauseCandidateOps, walked.Stats.PauseCandidateOps, want, l.numChunks)
}
}
+19 -2
View File
@@ -475,6 +475,12 @@ func attachChunkLayerImpl(s *WALStore, remote chunkRemote, cfg chunkLayerConfig)
hooks.beforeMutation = l.beforeCOWMutation
hooks.beforeDestructive = l.holdCOWDestructive
// The live touched set exists before the hooks are published: the first
// map entry change (under WALStore.mu) only sets a bit, never allocates a
// volume-sized bitmap.
l.unkMu.Lock()
l.touchedLocked(false)
l.unkMu.Unlock()
if !s.chunk.CompareAndSwap(nil, hooks) {
l.cancel()
return nil, errors.New("storage: a chunk layer is already attached")
@@ -1826,7 +1832,6 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch
}
candidates, visits := l.candidatesFrom(members, h)
visits += int64(len(taken.summary)) // the summary words the walk read
l.pauseOp(int(visits)) // each step counts itself if it ever runs in the pause
for idx := range dirtyTaken {
visits++
candidates[idx] = true
@@ -1844,6 +1849,9 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch
}
}
l.stMu.Unlock()
// Charged after every walk (the touched set, the dirty set, a BASE's
// whole-volume pass): a drained pre-copy frontend is still paused here.
l.pauseOp(int(visits))
candidateOracleCompare(oracleWant, candidates) // tests only
// A chunk known to hold HEAD's bytes at its current version (a clean
// fill, or a compare by a cut that did not publish) is not compared
@@ -2899,7 +2907,16 @@ func (l *chunkLayerImpl) Stats() chunkLayerStats {
out.ReconcileDone = 0
l.stMu.Unlock()
out.DirtyChunks = len(l.captureCandidates())
dirty := l.captureCandidates()
// A cut's judged candidates stay counted until its HEAD installs: one the
// flush checkpointed out of the dirty map meanwhile is in neither source
// above. Read under stMu; the cut keeps its own taken set.
l.stMu.Lock()
for idx := range l.inflight {
dirty[idx] = true
}
l.stMu.Unlock()
out.DirtyChunks = len(dirty)
out.AbsentChunks = l.absentHydratable()
return out