Merge pull request #948 from seaweedfs/fix/b1-candidate-followups-2

storage: touched-set bookkeeping: attach allocation, drained pause, status
This commit is contained in:
Ping Qiu authored and GitHub committed 2026-12-02 06:24:50 -08:00
commit 14016ef705
2 files changed
+149 -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,122 @@ 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() { // bounded, as mustAttach's
done := make(chan struct{})
go func() { _ = layer.Close(); close(done) }()
select {
case <-done:
case <-time.After(ctLocalBound):
t.Errorf("chunk layer Close did not return within %v", ctLocalBound)
}
})
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")
}
})
}
}
// The status's DirtyChunks is the exact union of the live candidates and a
// cut's in-flight ones while its upload runs (a chunk in both counts once,
// one only in flight still counts), and the in-flight part leaves the count
// once that cut's HEAD installs.
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: in flight only, once flushed
w.write(t, s, 3*ctChunkBlocks, 0x33) // chunk 3: in flight, and written again below
entered, release := holdPuts(t, f)
t.Cleanup(release)
done := startCapture(t, layer)
waitKey(t, entered, "chunk upload")
w.write(t, s, 3*ctChunkBlocks+1, 0x3b) // chunk 3 again: live and in flight
w.write(t, s, 4*ctChunkBlocks, 0x44) // chunk 4: live only
mustSyncFlush(t, s) // chunk 2's write leaves the dirty map
during := l.Stats().DirtyChunks
release()
if err := waitErr(t, done, "capture"); err != nil {
t.Fatal(err)
}
after := l.Stats().DirtyChunks
if during != 3 {
t.Fatalf("during the cut's upload: %d dirty chunks, want 3 (chunk 2 in flight, 3 in both, 4 live)", during)
}
if after != 2 {
t.Fatalf("after the cut's HEAD installed: %d dirty chunks, want 2 (chunks 3 and 4, written since)", after)
}
}
// A pre-copy cut keeps its frontend drained through the candidate step, so
// every walk it makes there is pause work, charged exactly once: over the
// same drained cut with nothing to walk, k dirty chunks add 2k (the
// dirty-set visit and the judging of each), and a BASE's whole-volume pass
// over N chunks adds 2N (the pass and the judging of each). A walk charged
// before it ran, or charged twice, misses the exact delta.
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 })
cut := func(what string) int64 {
t.Helper()
r, err := l.Cut(ctTimeout(t))
if err != nil {
t.Fatalf("%s: %v", what, err)
}
return r.Stats.PauseCandidateOps
}
plain := cut("a drained cut with nothing to walk")
const k = 3
l.stMu.Lock()
for i := uint64(0); i < k; i++ {
l.dirty[i] = true
}
l.stMu.Unlock()
if got, want := cut("k dirty chunks")-plain, int64(2*k); got != want {
t.Fatalf("a drained cut over %d dirty chunks charged %d more pause operations, want %d", k, got, want)
}
l.stMu.Lock()
l.allDirty = true // as after a BASE: every chunk is walked
l.stMu.Unlock()
if got, want := cut("a whole-volume pass")-plain, 2*int64(l.numChunks); got != want {
t.Fatalf("a drained cut's whole-volume pass charged %d more pause operations, want %d (the pass and the judging, %d chunks each)", got, want, l.numChunks)
}
if got := cut("nothing to walk again"); got != plain {
t.Fatalf("a drained cut with nothing to walk after them charged %d, want %d (nothing carried over)", got, plain)
}
}
+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