storage: the capture path writes an intent before uploading and settles it

A cut that uploads chunks now writes one intent naming exactly the keys
it will upload, at the cut's base HEAD generation with the layer's epoch
and holder. The intent is durable before the first upload; if it cannot
be written, nothing is uploaded. A cut that only drops chunks writes
none.

The cut deletes only its own intent, once its outcome is settled:
- after the HEAD CAS succeeds, or a HEAD re-read confirms its proposal;
- on a clean abort: a failed upload, a refused admission, a fence, or a
  HEAD that moved past the CAS's base.

An unknown CAS outcome keeps the intent with the pending proposal. The
next cut deletes it once that proposal is settled, whether it landed or
can never land. A failed delete never fails a settled cut; the next cut
retries it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-15 09:19:07 -08:00
1 parent 22b53ce945
commit 15e98ba7ee
2 files changed
+85 -21

No files matched your search

+81 -17
View File
@@ -72,14 +72,20 @@ type chunkLayerImpl struct {
// pendingBase is the version the unresolved CAS expected. While HEAD is
// still at it, that CAS may still land, so a read alone never settles it.
pendingBase chunkHeadVersion
remoteLSN uint64
hdr chunkBitmapHeader // last header written or loaded
dirty map[uint64]bool // chunks dirtied since the cut in progress or the last HEAD
inflight map[uint64]bool // chunks captured by a cut whose HEAD is not yet installed
allDirty bool // a BASE block landed: every chunk is dirty
hashMode bool // the dirty set is unknown: compare every chunk by hash
cutLSN uint64 // H of the latest cut taken (in flight or installed)
keySeq uint64
// pendingIntent is the intent of the unresolved proposal in pending;
// it is deleted once that proposal is settled.
pendingIntent string
// leftoverIntents are this layer's own intents whose delete failed
// after their cut was settled; every cut retries them.
leftoverIntents []string
remoteLSN uint64
hdr chunkBitmapHeader // last header written or loaded
dirty map[uint64]bool // chunks dirtied since the cut in progress or the last HEAD
inflight map[uint64]bool // chunks captured by a cut whose HEAD is not yet installed
allDirty bool // a BASE block landed: every chunk is dirty
hashMode bool // the dirty set is unknown: compare every chunk by hash
cutLSN uint64 // H of the latest cut taken (in flight or installed)
keySeq uint64
// Test-only scheduling hooks; nil in production.
testPresenceSnapshotHook func() // after a presence snapshot, before it is written
@@ -557,6 +563,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (chunkC
if err := l.resolvePending(ctx); err != nil {
return chunkCutResult{}, err
}
l.retryLeftoverIntents(ctx)
// Quiesced cut.
l.s.captureMu.Lock()
@@ -628,6 +635,11 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (chunkC
}
var changed []uint64
newKeys := map[uint64]string{}
type upload struct {
c capturedChunk
key string
}
var uploads []upload
for _, c := range captured {
ref, had := baseHead.Chunks[c.idx]
switch {
@@ -638,14 +650,32 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (chunkC
changed = append(changed, c.idx)
continue
}
key := l.newKey(c.idx)
if err := l.remote.Put(ctx, key, c.data); err != nil {
l.abortCut(all)
return chunkCutResult{}, fmt.Errorf("storage: capture upload chunk %d: %w: %w", c.idx, errChunkRemoteUnavailable, err)
uploads = append(uploads, upload{c: c, key: l.newKey(c.idx)})
}
// The intent names every key this cut uploads and is durable before
// the first upload: GC treats it as a root until the cut is settled.
var intent string
if len(uploads) > 0 {
keys := make([]string, len(uploads))
for i, u := range uploads {
keys[i] = u.key
}
next.Chunks[c.idx] = chunkRef{Key: key, SHA256: c.sum}
newKeys[c.idx] = key
changed = append(changed, c.idx)
id, err := l.remote.PutIntent(ctx, chunkIntent{Gen: baseHead.Gen, Epoch: l.cfg.Epoch, Holder: l.cfg.Holder, Keys: keys})
if err != nil {
l.abortCut(all)
return chunkCutResult{}, fmt.Errorf("storage: capture intent: %w: %w", errChunkRemoteUnavailable, err)
}
intent = id
}
for _, u := range uploads {
if err := l.remote.Put(ctx, u.key, u.c.data); err != nil {
l.abortCut(all)
l.dropIntent(ctx, intent)
return chunkCutResult{}, fmt.Errorf("storage: capture upload chunk %d: %w: %w", u.c.idx, errChunkRemoteUnavailable, err)
}
next.Chunks[u.c.idx] = chunkRef{Key: u.key, SHA256: u.c.sum}
newKeys[u.c.idx] = u.key
changed = append(changed, u.c.idx)
}
if !detach && len(changed) == 0 && h == baseHead.RemoteLSN && baseHead.Gen != 0 && !chunkHeadIsDetached(baseHead) {
l.finishCut(baseHead, h, nil, nil) // nothing new: no CAS
@@ -653,10 +683,12 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (chunkC
}
if err := l.admit(); err != nil { // admission re-checked right before the CAS
l.abortCut(all)
l.dropIntent(ctx, intent)
return chunkCutResult{}, err
}
if l.supersededBy(baseHead) {
l.abortCut(all)
l.dropIntent(ctx, intent)
l.stMu.Lock()
l.fenced = true
l.stMu.Unlock()
@@ -667,6 +699,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (chunkC
if casErr == nil {
next.Gen = gen
l.finishCut(next, h, changed, newKeys)
l.dropIntent(ctx, intent)
return l.cutDone(chunkCutResult{H: h, Head: copyHead(next), FullHash: all}, detach), nil
}
// Precondition or unknown outcome: resolve by re-reading HEAD, never by
@@ -676,6 +709,7 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (chunkC
p := copyHead(next)
l.pending = &p
l.pendingDetach, l.pendingCut, l.pendingBase = detach, chunkCutResult{H: h, FullHash: all}, expected
l.pendingIntent = intent // kept until the proposal is settled
l.stMu.Unlock()
l.abortCut(all)
}
@@ -693,9 +727,12 @@ func (l *chunkLayerImpl) captureLocked(ctx context.Context, detach bool) (chunkC
}
if sameProposal(cur, next) {
l.finishCut(cur, h, changed, newKeys)
l.dropIntent(ctx, intent)
return l.cutDone(chunkCutResult{H: h, Head: copyHead(cur), FullHash: all}, detach), nil
}
// HEAD moved past the CAS's base without it: it can never land.
l.abortCut(all)
l.dropIntent(ctx, intent)
if l.supersededBy(cur) {
l.stMu.Lock()
l.fenced = true
@@ -776,7 +813,7 @@ func sameProposal(cur, next chunkHead) bool {
// resolvePending re-reads HEAD when an earlier CAS outcome is unresolved.
func (l *chunkLayerImpl) resolvePending(ctx context.Context) error {
l.stMu.Lock()
pending, base := l.pending, l.pendingBase
pending, base, intent := l.pending, l.pendingBase, l.pendingIntent
l.stMu.Unlock()
if pending == nil {
return nil
@@ -806,9 +843,11 @@ func (l *chunkLayerImpl) resolvePending(ctx context.Context) error {
return fmt.Errorf("storage: capture: earlier CAS outcome still unresolved (%v): HEAD unchanged: %w", casErr, errChunkCASOutcomeUnknown)
}
}
// Settled: the proposal landed or can never land.
l.dropIntent(ctx, intent)
l.stMu.Lock()
defer l.stMu.Unlock()
l.pending = nil
l.pending, l.pendingIntent = nil, ""
wasDetach, cut := l.pendingDetach, l.pendingCut
l.pendingDetach, l.pendingCut, l.pendingBase = false, chunkCutResult{}, chunkHeadVersion{}
switch {
@@ -831,6 +870,31 @@ func (l *chunkLayerImpl) resolvePending(ctx context.Context) error {
return nil
}
// dropIntent deletes this layer's own intent id (none if empty) once its
// cut is settled. A failed delete is kept and retried by the next cut; it
// never fails a cut whose outcome is already settled.
func (l *chunkLayerImpl) dropIntent(ctx context.Context, id string) {
if id == "" {
return
}
if err := l.remote.DeleteIntent(ctx, id); err != nil {
l.stMu.Lock()
l.leftoverIntents = append(l.leftoverIntents, id)
l.stMu.Unlock()
}
}
// retryLeftoverIntents retries the deletes dropIntent could not make.
func (l *chunkLayerImpl) retryLeftoverIntents(ctx context.Context) {
l.stMu.Lock()
ids := l.leftoverIntents
l.leftoverIntents = nil
l.stMu.Unlock()
for _, id := range ids {
l.dropIntent(ctx, id)
}
}
// abortCut returns a failed cut's chunks to the dirty set.
func (l *chunkLayerImpl) abortCut(all bool) {
l.stMu.Lock()
@@ -128,8 +128,8 @@ func TestChunkIntent_FailedPutIntentUploadsNothing(t *testing.T) {
f.failPutIntent = errors.New("injected PutIntent failure")
f.mu.Unlock()
mark := len(f.stats().ops)
if _, err := layer.Cut(ctTimeout(t)); err == nil {
t.Fatal("cut succeeded although its intent could not be written")
if _, err := layer.Cut(ctTimeout(t)); !errors.Is(err, errChunkRemoteUnavailable) {
t.Fatalf("cut with a failed intent write: %v; want a remote-unavailable refusal", err)
}
if got := uploadedKeys(opsSince(f, mark)); len(got) != 0 {
t.Fatalf("chunks uploaded without an intent: %q", got)
@@ -154,8 +154,8 @@ func TestChunkIntent_FailedUploadDeletesIntent(t *testing.T) {
f.mu.Lock()
f.failPut = errors.New("injected upload failure")
f.mu.Unlock()
if _, err := layer.Cut(ctTimeout(t)); err == nil {
t.Fatal("cut succeeded although its upload failed")
if _, err := layer.Cut(ctTimeout(t)); !errors.Is(err, errChunkRemoteUnavailable) {
t.Fatalf("cut with a failed upload: %v; want a remote-unavailable refusal", err)
}
in := lastPutIntent(t, f)
if live := liveIntents(t, f); len(live) != 0 {