layout: Start validates the local root against the HEAD its claim updates

Start checked the local root against its first read of HEAD, then
claimed through a transition that read HEAD again without the check: a
claim by another lineage, or a published position rising past the local
root, between the two reads was written over. The local-root check now
runs again inside the claim's transition, on the very HEAD the
conditional update replaces.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-24 18:47:27 -08:00
1 parent 7ed69b1635
commit 6e43955848
5 files changed
+55 -10

No files matched your search

@@ -48,11 +48,16 @@ func (h Head) Claim(me HolderID) (next Head, changed bool, err error) {
// RunClaim reads HEAD, applies Claim, waits for the old lease's end (the
// check runs after the read and before the update), and commits by one
// conditional update. An absent HEAD refuses.
func RunClaim(ctx context.Context, st HeadStore, lease LeaseWait, me HolderID) (Head, error) {
func RunClaim(ctx context.Context, st HeadStore, lease LeaseWait, me HolderID, pre ...func(Head) error) (Head, error) {
if lease == nil {
return Head{}, errors.New("layout: Claim needs a lease wait")
}
return runTransition(ctx, st, func(h Head) (Head, bool, error) {
for _, check := range pre {
if err := check(h); err != nil {
return Head{}, false, err
}
}
next, changed, err := h.Claim(me)
if err != nil || !changed {
return next, changed, err
@@ -83,8 +83,9 @@ func TestHolderFunctions_RefuseAStaleHolder(t *testing.T) {
}
// Start: an older line, another server at HEAD's epoch and another claim
// id are its cases in start_test.go (TestStart_Refusals and the same-host
// forced successor).
covered := map[string]bool{"RemoveUnreachable": true, "Start": true}
// forced successor); startCheck is Start's check, run again inside the
// claim (TestStart_ValidatesAgainstTheHeadItUpdates).
covered := map[string]bool{"RemoveUnreachable": true, "Start": true, "startCheck": true}
var missing []string
for fname, src := range packageSources(t, false) {
file, err := parser.ParseFile(token.NewFileSet(), fname, src, 0)
@@ -58,22 +58,39 @@ func Start(ctx context.Context, st HeadStore, lease LeaseWait, line HolderID, lo
if !found {
return Head{}, ErrHeadAbsent
}
if err := startCheck(h, line, local); err != nil {
return Head{}, err
}
if h.Epoch == line.Epoch && h.Holder == line.Holder && h.State == StateLive {
return h, nil // HEAD holds this root's claim id: it continues
}
// The claim re-checks the local root against the very HEAD it updates:
// another lineage's claim or a higher published position after the
// read above refuses, never writes over the newer HEAD.
me := HolderID{Epoch: line.Epoch, Holder: line.Holder, ClaimID: local.ClaimID}
return RunClaim(ctx, st, lease, me, func(h Head) error { return startCheck(h, line, local) })
}
// startCheck refuses a local root that cannot start on h: an old copy of
// HEAD's lineage (same claim id, position below HEAD's), at HEAD's epoch
// and server a root without HEAD's claim id, and above HEAD's epoch a
// non-fresh root of another lineage.
func startCheck(h Head, line HolderID, local LocalRoot) error {
if !local.Fresh && local.ClaimID == h.ClaimID && local.Position < h.Position && line.Epoch >= h.Epoch {
return Head{}, fmt.Errorf("%w (%w): the local root holds position %d, HEAD published %d: an old copy of this lineage", ErrStaleLocalRoot, ErrNotHolder, local.Position, h.Position)
return fmt.Errorf("%w (%w): the local root holds position %d, HEAD published %d: an old copy of this lineage", ErrStaleLocalRoot, ErrNotHolder, local.Position, h.Position)
}
if h.Epoch == line.Epoch && h.Holder == line.Holder && h.State == StateLive {
switch {
case h.ClaimID == local.ClaimID:
return h, nil
return nil
case local.Fresh:
return Head{}, fmt.Errorf("%w at epoch %d: %s", ErrMissingLocalRoot, h.Epoch, MissingLocalRootCure)
return fmt.Errorf("%w at epoch %d: %s", ErrMissingLocalRoot, h.Epoch, MissingLocalRootCure)
default:
return Head{}, fmt.Errorf("%w (%w): HEAD holds claim %q at epoch %d, the local root carries %q: %s", ErrStaleLocalRoot, ErrNotHolder, h.ClaimID, h.Epoch, local.ClaimID, StaleLocalRootCure)
return fmt.Errorf("%w (%w): HEAD holds claim %q at epoch %d, the local root carries %q: %s", ErrStaleLocalRoot, ErrNotHolder, h.ClaimID, h.Epoch, local.ClaimID, StaleLocalRootCure)
}
}
if line.Epoch > h.Epoch && !local.Fresh && local.ClaimID != h.ClaimID {
return Head{}, fmt.Errorf("%w (%w): the local root carries claim %q, HEAD's lineage is %q: %s", ErrStaleLocalRoot, ErrNotHolder, local.ClaimID, h.ClaimID, StaleLocalRootCure)
return fmt.Errorf("%w (%w): the local root carries claim %q, HEAD's lineage is %q: %s", ErrStaleLocalRoot, ErrNotHolder, local.ClaimID, h.ClaimID, StaleLocalRootCure)
}
return RunClaim(ctx, st, lease, me)
return nil
}
@@ -226,3 +226,25 @@ func TestStart_StaleRootNamesTheCure(t *testing.T) {
t.Fatalf("old directory after a move away and back: %v; want ErrStaleLocalRoot naming %q", err, StaleLocalRootCure)
}
}
// Start validates the local root against the very HEAD its claim updates:
// if HEAD changes after Start's first view (another lineage claims, or the
// published position rises past the local root), the claim is refused,
// never written over the newer HEAD.
func TestStart_ValidatesAgainstTheHeadItUpdates(t *testing.T) {
for name, change := range map[string]func(h *Head){
"another lineage claims": func(h *Head) { h.Epoch, h.Holder, h.ClaimID = 4, "s2/r1", "k-other" },
"published position rises": func(h *Head) { h.Position = 100 },
} {
t.Run(name, func(t *testing.T) {
h := startHead(3, "s1/r1", "k3")
h.Position = 50
st := &memHead{h: &h}
st.afterRead = func() { change(st.h) }
_, err := Start(context.Background(), st, &fakeLease{ended: true}, HolderID{Epoch: 5, Holder: "s1/r1"}, LocalRoot{ClaimID: "k3", Position: 50})
if !errors.Is(err, ErrStaleLocalRoot) || st.updates != 0 {
t.Fatalf("start after HEAD changed under it: %v (%d writes); want ErrStaleLocalRoot, nothing written", err, st.updates)
}
})
}
}
+1 -1
View File
@@ -1120,7 +1120,7 @@ Notes:
| `INV-SNAPK-RELEASE-OPERATOR-001` | The operator's command releases a target that is deleted (HEAD deleting or deleted, or its directory's VOLUME of another id), one whose live roots name no chunk of the source, or one without HEAD, after its tombstone. | M9 source HEAD, dependents | `core/frontend/chunkstore/layout` `delete.go` `OperatorReleaseDependent` | `TestOperatorReleaseDependent` | — (the snapshot kernel model is not in this repository) | ACTIVE |
| `INV-SNAPK-RELEASE-REFUSE-001` | The operator's command refuses a live target still naming the source and one whose HEAD appeared before its tombstone, and then writes nothing to the source. | M9 source HEAD, dependents | `core/frontend/chunkstore/layout` `delete.go` `OperatorReleaseDependent` | `TestOperatorReleaseDependent` | — (the snapshot kernel model is not in this repository) | ACTIVE |
| `INV-SNAPK-START-ROOT-001` | At its own epoch a holder continues only on the local root whose claim id HEAD holds; a fresh root with another id is refused as a missing local root naming the cure, an older one as stale and fenced; neither writes HEAD. | M9 start (before any transition) | `core/frontend/chunkstore/layout` `start.go` `Start` | `TestStart_OwnEpochFreshRootRefusedBeforeClaim`, `TestStart_OwnEpochSameRootContinues`, `TestStart_LostClaimReplyContinues`, `TestStart_SameHostForcedSuccessorRefusesTheRemountedOldRoot` | — (the snapshot kernel model is not in this repository) | ACTIVE |
| `INV-SNAPK-START-LINEAGE-001` | At an epoch above HEAD's a holder claims only with a fresh root (it restores from HEAD) or the root whose claim id HEAD holds; any other root holds older data and is refused as stale, HEAD unchanged. | M9 any → idle (Claim) | `core/frontend/chunkstore/layout` `start.go` `Start` | `TestStart_HigherEpochRefusesAnOlderLineage`, `TestStart_FreshRootAtHigherEpochClaims`, `TestStart_StaleRootNamesTheCure`, `TestStart_DeletingVolumeRefused` | — (the snapshot kernel model is not in this repository) | ACTIVE |
| `INV-SNAPK-START-LINEAGE-001` | At an epoch above HEAD's a holder claims only with a fresh root (it restores from HEAD) or the root whose claim id HEAD holds; any other root holds older data and is refused as stale, HEAD unchanged. | M9 any → idle (Claim) | `core/frontend/chunkstore/layout` `start.go` `Start` | `TestStart_HigherEpochRefusesAnOlderLineage`, `TestStart_FreshRootAtHigherEpochClaims`, `TestStart_StaleRootNamesTheCure`, `TestStart_DeletingVolumeRefused`, `TestStart_ValidatesAgainstTheHeadItUpdates` | — (the snapshot kernel model is not in this repository) | ACTIVE |
| `INV-SNAPK-START-POSITION-001` | A root carrying HEAD's claim id but synced below HEAD's published position is an old copy of that lineage and is refused as stale, at its own epoch and above, HEAD unchanged. | M9 start (before any transition) | `core/frontend/chunkstore/layout` `start.go` `Start` | `TestStart_OldCopyOfTheRightRootRefused`, `TestStart_FreshAcrossACrashContinuesTheRestore` | — (the snapshot kernel model is not in this repository) | ACTIVE |
| `INV-SNAPK-START-CLAIM-001` | A claim is made only by Start, at an epoch above HEAD's, with the local root's claim id; a source scan refuses any other caller of RunClaim. | M9 any → idle (Claim) | `core/frontend/chunkstore/layout` `start.go` `Start`; (source scan) | `TestStart_FreshRootAtHigherEpochClaims`, `TestStart_Refusals`, `TestStart_InterruptAtTheClaim`, `TestGuard_Controls` | — (the snapshot kernel model is not in this repository) | ACTIVE |
| `INV-SNAPK-STALE-HOLDER-001` | Every transition refuses an older epoch of the same server, the same epoch held by another server, and the same epoch and server with another claim id, in idle, snap and clean; a new transition must be added to the table. | M9 every transition | `core/frontend/chunkstore/layout` `Head.heldBy` and each transition | `TestTransitions_RefuseAStaleHolder` | — (the snapshot kernel model is not in this repository) | ACTIVE |