feat(storage): select and settle captured chunks through owned maps

This commit is contained in:
pingqiu committed 2026-11-17 07:57:01 -08:00
1 parent f4ed786d84
commit 3c6e025da8
3 files changed
+154 -5

No files matched your search

+39 -5
View File
@@ -134,6 +134,9 @@ func attachChunkLayerImpl(s *WALStore, remote chunkRemote, cfg chunkLayerConfig)
if remote == nil || cfg.Bitmap == nil || cfg.ChunkBlocks == 0 {
return nil, errors.New("storage: chunk layer needs a remote, a bitmap store and ChunkBlocks > 0")
}
if s.v3 != nil && s.v3.ownedMap != nil && uint64(cfg.ChunkBlocks) != s.v3.ownedMap.chunkBlocks {
return nil, fmt.Errorf("%w: chunk layer geometry differs from owned map", ErrMapInvalid)
}
if cfg.UploadConcurrency < 0 || cfg.UploadConcurrency > 64 {
return nil, errors.New("storage: upload concurrency must be between 1 and 64 (0 defaults to 8)")
}
@@ -336,12 +339,33 @@ func (l *chunkLayerImpl) noteAllMutated() {
}
}
// walChunks returns the chunks with entries in the WAL dirty map.
func (l *chunkLayerImpl) walChunks() map[uint64]bool {
// captureCandidates includes current WAL entries and impl3 owned-map dirt.
// UNKNOWN never certifies clean; a max LSN beyond HEAD must be captured even
// after the WAL entry has checkpointed and the legacy bitmap says clean.
func (l *chunkLayerImpl) captureCandidates() map[uint64]bool {
out := map[uint64]bool{}
for _, e := range l.s.dm.snapshot() {
out[l.chunkOf(e.LBA)] = true
}
if l.s.v3 != nil && l.s.v3.ownedMap != nil {
l.stMu.Lock()
base := l.remoteLSN
absent := map[uint64]bool{}
for idx := range l.absent {
if !l.chunkHasLocalLocked(idx) {
absent[idx] = true
}
}
l.stMu.Unlock()
l.s.mu.RLock()
for idx, e := range l.s.v3.mapEntries {
i := uint64(idx)
if !absent[i] && (e.Unknown || e.LSN > base) {
out[i] = true
}
}
l.s.mu.RUnlock()
}
return out
}
@@ -735,7 +759,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch
return chunkCutResult{}, fmt.Errorf("storage: capture sync: %w", err)
}
candidates := map[uint64]bool{}
for idx := range l.walChunks() {
for idx := range l.captureCandidates() {
candidates[idx] = true
}
l.stMu.Lock()
@@ -757,8 +781,15 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch
// it since staging (its version is unchanged) and HEAD is the one it
// was compared against.
var captured []capturedChunk
var capturedVersions map[uint64]uint64
if l.s.v3 != nil && l.s.v3.ownedMap != nil {
capturedVersions = make(map[uint64]uint64, len(candidates))
}
useStaged := map[uint64]bool{}
for idx := range candidates {
if capturedVersions != nil {
capturedVersions[idx] = l.chunkVersion(idx)
}
if sc, ok := staged[idx]; ok && sc.ver == l.chunkVersion(idx) && sc.headGen == l.head.Gen {
useStaged[idx] = true
captured = append(captured, sc.capturedChunk)
@@ -905,7 +936,8 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch
return chunkCutResult{}, errChunkLayerFenced
}
l.finishCut(baseHead, h, nil, nil) // nothing new: no CAS
l.dropIntents(ctx, intents) // superseded staged uploads only
l.persistCapturedMapV3(capturedVersions)
l.dropIntents(ctx, intents) // superseded staged uploads only
return chunkCutResult{H: h, Head: copyHead(baseHead), Noop: true, FullHash: all}, nil
}
if err := l.admit(); err != nil { // admission re-checked right before the CAS
@@ -934,6 +966,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch
if casErr == nil {
next.Gen = gen
l.finishCut(next, h, changed, newKeys)
l.persistCapturedMapV3(capturedVersions)
l.dropIntents(ctx, intents)
return l.cutDone(chunkCutResult{H: h, Head: copyHead(next), FullHash: all}, detach), nil
}
@@ -963,6 +996,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (res ch
if sameProposal(cur, next) {
st.CASResult = "resolved-by-reread"
l.finishCut(cur, h, changed, newKeys)
l.persistCapturedMapV3(capturedVersions)
l.dropIntents(ctx, intents)
return l.cutDone(chunkCutResult{H: h, Head: copyHead(cur), FullHash: all}, detach), nil
}
@@ -1428,7 +1462,7 @@ func (l *chunkLayerImpl) stage(ctx context.Context, st *chunkCutStats) (map[uint
staged := map[uint64]stagedChunk{}
var intents []string
for round := 0; round < chunkStageRounds; round++ {
wal := l.walChunks()
wal := l.captureCandidates()
l.stMu.Lock()
need := map[uint64]bool{}
consider := func(idx uint64) {
@@ -0,0 +1,65 @@
package storage
// persistCapturedMapV3 is called only after HEAD's publication is definite
// (or an authority-checked no-op), outside bmMu/stMu and the capture barrier.
// The capture's mutation versions were sampled under the quiesced barrier.
// Store.mu prevents a WAL append from slipping between the version check and
// the UNKNOWN update. A later write updates max LSN and invalidates proofs.
// A failed map publication leaves the old disk UNKNOWN conservative; a map
// persistence failure also latches the store, while the remote HEAD remains
// the successfully captured image. No remote durability claim is undone.
func (l *chunkLayerImpl) persistCapturedMapV3(versions map[uint64]uint64) {
s := l.s
v := s.v3
if v == nil || v.ownedMap == nil {
return
}
s.mu.Lock()
if s.stickyIOFault() != nil {
s.mu.Unlock()
return
}
var clear []uint64
for idx, version := range versions {
if idx < uint64(len(v.mapEntries)) && v.mapEntries[idx].Unknown && l.chunkVersion(idx) == version {
clear = append(clear, idx)
}
}
if len(clear) > 0 {
if err := v.mapProof.mutatedV3(); err != nil {
s.mu.Unlock()
return
}
for _, idx := range clear {
v.mapEntries[idx].Unknown = false
}
}
v.pub.descMu.Lock()
cp := v.pub.cur.CheckpointLSN
need := false
for i, e := range v.mapEntries {
if e != v.pub.mapCurrent.entries[i] {
need = true
break
}
}
v.pub.descMu.Unlock()
s.mu.Unlock()
if !need {
return
}
prepared, err := s.prepareOwnedMapCheckpointV3(cp)
if err != nil {
return
}
v.syncMu.Lock()
defer v.syncMu.Unlock()
// The ordinary merged-coverage and proof checks handle a concurrent
// checkpoint/Recover/append. Failure retires no root or page. A later cut
// retries a cache/disk difference even if UNKNOWN was already cleared in
// memory by this verified publication.
_, _ = v.pub.publishMapV3(&s.mu, nil, prepared, func(n *descriptorV3) error {
n.W = max(n.W, v.wack.Load())
return nil
})
}
@@ -0,0 +1,50 @@
package storage
import (
"bytes"
"testing"
)
func TestChunkLayerV3_CaptureUsesOwnedMapAfterCheckpoint(t *testing.T) {
s, _ := newMappedCheckpointStoreV3(t)
writeSynced(t, s, 5, 5)
if err := s.flusher.flushOnce(); err != nil {
t.Fatal(err)
}
if s.dm.len() != 0 {
t.Fatal("setup requires checkpointed data with no WAL dirty entry")
}
f := newFakeChunkRemote()
cfg := ctConfig(1)
// The legacy bitmap deliberately reports clean in the current HEAD period.
// The owned impl3 map, not this stale fixture, must still select local data.
if err := cfg.Bitmap.WriteHeader(chunkBitmapHeader{}); err != nil {
t.Fatal(err)
}
layer := mustAttach(t, s, f, cfg)
cut, err := layer.Cut(ctTimeout(t))
if err != nil {
t.Fatal(err)
}
image := f.imageOf(t, cut.Head)
if !bytes.Equal(image[5*ctBlockSize:6*ctBlockSize], blk(5)) {
t.Error("capture omitted checkpointed data named by the owned map")
}
es, _, err := readChunkLSNMapV3(s.v3.ownedMap.fd, s.v3.pub.current(), 4)
if err != nil {
t.Fatal(err)
}
for i, e := range es {
if e.Unknown {
t.Errorf("verified captured chunk%d still UNKNOWN", i)
}
}
again, err := layer.Cut(ctTimeout(t))
if err != nil {
t.Fatal(err)
}
if !again.Noop || again.Stats.CandidateChunks != 0 {
t.Errorf("verified clean cut reread chunks: noop=%v candidates=%d", again.Noop, again.Stats.CandidateChunks)
}
assertNoRemoteMisuse(t, f)
}