fix(storage): fence prior incarnation CAS before reopening I/O

This commit is contained in:
pingqiu committed 2026-11-21 12:16:46 -08:00
1 parent 746b6d59f7
commit 2edbee16b7
9 files changed
+81 -36

No files matched your search

@@ -462,20 +462,18 @@ func (p *DurableProvider) newChunkLayer(ctx context.Context, volumeID string, h
// restore here, so HEAD is never claimed for a mis-ordered ship.
return h.backend.seedAndRebase(func() error { return ws.SeedChunkFrontier(head.RemoteLSN) })
}
claimStart := time.Now()
log.Printf("durable: HEAD claim for volume=%s starting: target epoch=%d holder=%q", volumeID, cfg.Epoch, cfg.Holder)
head, err := storage.ClaimChunkHead(ctx, remote, cfg)
if err != nil {
log.Printf("durable: HEAD claim for volume=%s failed after %s: %v", volumeID, time.Since(claimStart), err)
return nil, fmt.Errorf("durable: restore HEAD claim: %w", err)
}
claimed := "claimed"
if head.Gen == 0 {
claimed = "no HEAD to claim (new lineage)"
}
log.Printf("durable: HEAD claim for volume=%s done in %s: gen=%d epoch=%d holder=%q, %s", volumeID, time.Since(claimStart), head.Gen, head.Epoch, head.Holder, claimed)
cfg.RestoreGuard = &storage.ChunkHeadVersion{Gen: head.Gen, Epoch: head.Epoch}
}
// INV-WS-REOPEN-FENCE-001: local-root reopens claim too, without
// reseeding their acknowledged WAL or replacing it with remote absence.
claimStart := time.Now()
log.Printf("durable: HEAD claim for volume=%s starting: target epoch=%d holder=%q", volumeID, cfg.Epoch, cfg.Holder)
head, err := storage.ClaimChunkHead(ctx, remote, cfg)
if err != nil {
log.Printf("durable: HEAD claim for volume=%s failed after %s: %v", volumeID, time.Since(claimStart), err)
return nil, fmt.Errorf("durable: restore HEAD claim: %w", err)
}
log.Printf("durable: HEAD claim for volume=%s done in %s: gen=%d epoch=%d holder=%q", volumeID, time.Since(claimStart), head.Gen, head.Epoch, head.Holder)
cfg.RestoreGuard = &storage.ChunkHeadVersion{Gen: head.Gen, Epoch: head.Epoch}
layer, attachErr := storage.AttachChunkLayer(ws, remote, cfg)
if attachErr == nil {
@@ -250,7 +250,7 @@ func TestHeadBeltRetryOfUnconfirmedClaimStillCools(t *testing.T) {
if _, err := ClaimChunkHead(retryCtx, f, ChunkLayerConfig{Epoch: 2, Holder: "new", HeadCoolingWindow: chunkHeadOperationTimeout + chunkHeadSettleTime}); err != nil {
t.Fatal(err)
}
if time.Since(start) < chunkHeadOperationTimeout+chunkHeadSettleTime || f.stats().cas != 1 {
t.Fatal("retry bypassed cooling or replayed a settled claim")
if time.Since(start) < chunkHeadOperationTimeout+chunkHeadSettleTime || f.stats().cas != 2 {
t.Fatal("retry bypassed cooling or failed to fence the previous incarnation")
}
}
+4 -13
View File
@@ -47,19 +47,10 @@ func ClaimChunkHead(ctx context.Context, remote ChunkRemote, cfg ChunkLayerConfi
if err := allowed(); err != nil {
return ChunkHead{}, err
}
// No HEAD is a new volume, not a restore: there is no old holder to
// fence. The first content cut keeps exclusive-create semantics.
if head.Gen == 0 {
return head, nil
}
if head.Gen != 0 && head.Epoch == cfg.Epoch && head.Holder == cfg.Holder && !chunkHeadIsDetached(head) && head.DetachedBy == nil {
// A previous claim may have committed but timed out during cooling.
// An identical root alone does not prove that wait completed.
if err := confirmChunkTakeover(ctx, remote, head, cfg.HeadCoolingWindow); err != nil {
return ChunkHead{}, err
}
return head, allowed() // idempotent restart or settled prior claim
}
// INV-WS-HEAD-BELT-001: even the same owner must replace the generation
// before serving. A killed incarnation may have left a CAS in flight;
// readback alone cannot invalidate that request's original precondition.
// An empty HEAD also needs an exclusive create before an old first cut.
next := copyHead(head)
next.Epoch, next.Holder = cfg.Epoch, cfg.Holder
// A new holder's takeover starts serving the same image as active. The
@@ -75,13 +75,13 @@ func TestClaimChunkHeadRefusesStaleAuthorityAndAdmissionDrift(t *testing.T) {
if err == nil || after.Gen != before.Gen || after.Epoch != 3 {
t.Fatalf("drift published: head=%+v err=%v", after, err)
}
// An identical already-settled claim is idempotent and creates no new gen.
// Reopening the same owner preserves the image but fences its old generation.
if _, err := ClaimChunkHead(context.Background(), remote, ChunkLayerConfig{Epoch: 3, Holder: "current"}); err != nil {
t.Fatal(err)
}
final, _ := remote.GetHead(context.Background())
if final.Gen != before.Gen {
t.Fatal("idempotent claim advanced HEAD")
if final.Gen != before.Gen+1 || final.RemoteLSN != before.RemoteLSN {
t.Fatal("same-owner reopen did not replace only the generation")
}
}
+5 -5
View File
@@ -172,10 +172,9 @@ stateDiagram-v2
}
state "HEAD holder" as H {
[*] --> Attaching
Attaching --> Claiming: fresh local root
Attaching --> Holder: existing root, HEAD is this holder's
Attaching --> Claiming: fresh or same-holder local root
Attaching --> Refused: existing root, HEAD published by another holder
Claiming --> Cooling: CAS to this epoch and holder, state active
Claiming --> Cooling: replace generation (or exclusive create), same image
Cooling --> Holder: configured window elapsed (zero for single filer), exact HEAD reread
Cooling --> HolderFenced: publication displaced
Claiming --> HolderFenced: HEAD epoch higher, or another holder at this epoch
@@ -194,8 +193,9 @@ stateDiagram-v2
| Fenced | Drained, Acked | the stop callback succeeded; only then, and only when revoked, the acknowledgement is sent | `core/host/volume/workspace_lease.go` `drain` | `INV-WS-REVOKE-ACK-001` |
| Attaching | Refused | an existing local root whose HEAD another holder has published (`ErrChunkStaleLocalRoot`) | `core/frontend/durable/provider_chunk.go` `newChunkLayer` | `INV-WS-STALE-LOCAL-ROOT-001` |
| (startup) | Refused | `--chunk-filer` is not the only filer of its group: a master lists another filer or several, or no master lists it within 60 s (`ErrNotSingleFiler`) | `core/frontend/chunkstore/single_filer.go` `CheckSingleFiler`; `cmd/blockvolume/chunk_remote.go` `newChunkWiring` | `INV-WS-SINGLE-FILER-001` |
| Claiming | Holder | HEAD epoch not above the master-issued epoch, no other holder at the same epoch; the local frontier is seeded from HEAD before the CAS; I/O opens only after the configured cooling window (zero at one serialization point) and an exact generation/epoch/holder readback | `core/storage/chunk_restore.go` `ClaimChunkHead`; `core/frontend/durable/provider_chunk.go` `newChunkLayer` | `INV-WS-CLAIM-BEFORE-SERVE-001`, `INV-WS-HEAD-CAS-001` |
| Claiming | Holder, or not installed | unknown CAS outcome: success only if a re-read shows the exact proposal; never a blind replay | `core/storage/chunk_restore.go` `ClaimChunkHead` | `INV-WS-CAS-UNKNOWN-001` |
| Claiming | Holder | HEAD epoch not above the master-issued epoch, no other holder at the same epoch; fresh roots seed from HEAD before the CAS; existing roots retain local WAL; every incarnation replaces the generation (or exclusive-creates HEAD); I/O opens only after the configured cooling window (zero at one serialization point) and an exact generation/epoch/holder readback | `core/storage/chunk_restore.go` `ClaimChunkHead`; `core/frontend/durable/provider_chunk.go` `newChunkLayer` | `INV-WS-CLAIM-BEFORE-SERVE-001`, `INV-WS-HEAD-CAS-001`, `INV-WS-REOPEN-FENCE-001` |
| Claiming | Holder, or not installed | unknown CAS outcome: success only if a re-read shows the exact proposal; never a blind replay | `core/storage/chunk_restore.go` `ClaimChunkHead` | `INV-WS-REOPEN-FENCE-001` | Before I/O, every reopened holder replaces the observed HEAD generation, including the same epoch/holder and an empty HEAD. A previous incarnation's delayed CAS cannot publish afterward. Local acknowledged WAL is retained. | M4 Claiming -> Holder | `core/storage/chunk_restore.go` `ClaimChunkHead`; `core/frontend/durable/provider_chunk.go` `newChunkLayer` | `TestClaimChunkHeadSameOwnerReopenRejectsKilledProcessCAS`, `TestHeadBeltSameOwnerReopenPreservesAcknowledgedWrites` (`core/storage/chunk_reopen_belt_test.go`); `TestLocalReopenClaimsBeforeServing` (`core/frontend/durable/provider_reopen_belt_test.go`) | `HeadReopenBelt` `OldCASCannotLandAfterReopen` | ACTIVE |
| `INV-WS-CAS-UNKNOWN-001` |
| Holder | Holder | every publish is a cut (M5) | `core/storage/chunk_layer.go` `captureLocked` | `INV-WS-HEAD-CAS-001`, `INV-WS-HEAD-MONOTONE-001` |
| Holder | Detached | `Detach` publishes the final cut with state `detached` in one CAS | `core/storage/chunk_layer.go` `chunkLayerImpl.Detach`, `captureLocked` | `INV-WS-CLEAN-MOVE-001` |
| Holder | HolderFenced | a conflicting HEAD owner (including a regressed epoch) or a displaced publication is observed; local data and unresolved cut intents remain | `core/storage/chunk_layer.go` `supersededBy`; `core/frontend/chunkstore/chunk_remote.go` `CASHead` | `INV-WS-HEAD-CAS-001` |
@@ -0,0 +1,29 @@
--------------------------- MODULE HeadReopenBelt ---------------------------
\* One same-owner reopen, one server-held old CAS, one new acknowledged image.
\* The single serialization point evaluates a CAS guard at application time.
EXTENDS Naturals, TLC
CONSTANT ClaimWrites
VARIABLES gen, image, pending, sent, reopened, late, acked, published
vars == <<gen, image, pending, sent, reopened, late, acked, published>>
Init == /\ gen = 1 /\ image = 0 /\ pending = FALSE /\ sent = FALSE
/\ reopened = FALSE /\ late = FALSE /\ acked = FALSE /\ published = FALSE
SendOld == /\ ~sent /\ ~reopened /\ sent' = TRUE /\ pending' = TRUE
/\ UNCHANGED <<gen, image, reopened, late, acked, published>>
Reopen == /\ sent /\ ~reopened /\ reopened' = TRUE
/\ gen' = IF ClaimWrites THEN gen + 1 ELSE gen
/\ UNCHANGED <<image, pending, sent, late, acked, published>>
ApplyOld == /\ pending /\ pending' = FALSE
/\ gen' = IF gen = 1 THEN 2 ELSE gen
/\ image' = IF gen = 1 THEN 1 ELSE image
/\ late' = (late \/ (gen = 1 /\ reopened))
/\ UNCHANGED <<sent, reopened, acked, published>>
AckNew == /\ reopened /\ ~acked /\ acked' = TRUE
/\ UNCHANGED <<gen, image, pending, sent, reopened, late, published>>
PublishNew == /\ acked /\ ~published /\ published' = TRUE
/\ gen' = gen + 1 /\ image' = 2
/\ UNCHANGED <<pending, sent, reopened, late, acked>>
Next == SendOld \/ Reopen \/ ApplyOld \/ AckNew \/ PublishNew
Spec == Init /\ [][Next]_vars
OldCASCannotLandAfterReopen == ~late
PublishedAcknowledgedImage == published => image = 2
=============================================================================
+21
View File
@@ -572,3 +572,24 @@ HEAD-read deadline/cancellation controls in
controls in `TestWorkspaceUncertaintyPrecedesRequestRefusal`. Actual request
cancellation retains its own controls, including a conflicting dependency status.
These are reply-classification claims, not authority-durability or delivery proof.
### Same-owner process reopen belt
`HeadReopenBelt.tla` checks M4 Claiming -> Holder and
`INV-WS-REOPEN-FENCE-001` at one HEAD serialization point. One old server-held
CAS, one reopen and one new acknowledged image are interleaved. Claim replaces
the generation while preserving the observed image; the old CAS evaluates its
original generation when applied. This abstracts storage I/O and durability;
native tests cover WAL recovery and acknowledged bytes.
TLC 2.19, tool SHA256
`936a262061c914694dfd669a543be24573c45d5aa0ff20a8b96b23d01e050e88`,
2026-11-21: `cfg/HeadReopenBelt.cfg` exhausted 14 generated / 12 distinct states,
depth 6, queue empty. `OldCASCannotLandAfterReopen` and
`PublishedAcknowledgedImage` hold. `cfg/HeadReopenBelt_no_claim.cfg` violates
`OldCASCannotLandAfterReopen` at depth 4 (5 distinct states). That negative maps
to `TestClaimChunkHeadSameOwnerReopenRejectsKilledProcessCAS` and the real
cut/recovered-WAL control `TestHeadBeltSameOwnerReopenPreservesAcknowledgedWrites`;
`TestLocalReopenClaimsBeforeServing` covers the provider existing-root path.
The finite model checks one nonempty base; the native/public claim test also
covers an empty root with a delayed first-cut exclusive create.
@@ -0,0 +1,3 @@
CONSTANT ClaimWrites = TRUE
SPECIFICATION Spec
INVARIANTS OldCASCannotLandAfterReopen PublishedAcknowledgedImage
@@ -0,0 +1,3 @@
CONSTANT ClaimWrites = FALSE
SPECIFICATION Spec
INVARIANTS OldCASCannotLandAfterReopen PublishedAcknowledgedImage