diff --git a/seaweed-block/core/frontend/chunkstore/head_readers_test.go b/seaweed-block/core/frontend/chunkstore/head_readers_test.go index b612cb859d..fc409a72f7 100644 --- a/seaweed-block/core/frontend/chunkstore/head_readers_test.go +++ b/seaweed-block/core/frontend/chunkstore/head_readers_test.go @@ -41,6 +41,7 @@ var headReaders = map[string]string{ "core/frontend/chunkstore/usage.go Usage -> ReadHead": "GAP: reports an empty image, no error (probe Usage)", "core/frontend/chunkstore/verify.go readHead -> LookupEntry": "finding ROOT_INVALID (probe Verify)", "core/frontend/chunkstore/layout/delete.go OperatorReleaseDependent -> ReadHead": "reads the source's HEAD (absent: refuses ErrHeadAbsent) and the target's: a target without HEAD (never created, or a restore that died between its VOLUME and its HEAD) gets a deleted HEAD written exclusively before the release (probe LayoutOperatorRelease)", + "core/frontend/chunkstore/layout/start.go Start -> ReadHead": "refuses ErrHeadAbsent and creates nothing (probe LayoutStart)", "core/frontend/chunkstore/layout/remove.go RemoveUnreachable -> ReadHead": "refuses ErrHeadAbsent and writes nothing to the objects (probe LayoutRemoval)", "core/frontend/chunkstore/layout/head.go runTransition -> ReadHead": "refuses ErrHeadAbsent and writes nothing; no layout transition creates a HEAD (probe LayoutTransition)", "core/frontend/chunkstore/verify.go run -> LookupEntry": "finding ROOT_INVALID (probe Verify)", @@ -308,6 +309,12 @@ func TestHeadReaders_AbsentHeadProbes(t *testing.T) { t.Fatalf("operator release with the source HEAD absent: %v; want ErrHeadAbsent", err) } }) + t.Run("LayoutStart", func(t *testing.T) { + st := &absentLayoutHead{} + if _, err := layout.Start(ctx, st, okClaimLease{}, layout.HolderID{Epoch: 1, Holder: "h"}, layout.LocalRoot{ClaimID: "k", Fresh: true}); !errors.Is(err, layout.ErrHeadAbsent) || st.updates != 0 { + t.Fatalf("start over an absent HEAD: %v, %d writes; want ErrHeadAbsent and none", err, st.updates) + } + }) t.Run("ForkReplay", func(t *testing.T) { f, b := headReaderForkFixture(t) c, err := NewCatalog(manifestFilerBlobs{f, gcBlobs{b}}, catBase) @@ -386,3 +393,7 @@ func (p *presentLayoutHead) UpdateHead(context.Context, uint64, layout.Head) err p.updates++ return nil } + +type okClaimLease struct{} + +func (okClaimLease) OldLeaseEnded(context.Context, uint64) error { return nil } diff --git a/seaweed-block/core/frontend/chunkstore/layout/claim.go b/seaweed-block/core/frontend/chunkstore/layout/claim.go index 73d8e2c350..d25869363b 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/claim.go +++ b/seaweed-block/core/frontend/chunkstore/layout/claim.go @@ -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 diff --git a/seaweed-block/core/frontend/chunkstore/layout/guard_test.go b/seaweed-block/core/frontend/chunkstore/layout/guard_test.go index 10c0d7c311..2c3d028485 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/guard_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout/guard_test.go @@ -123,14 +123,16 @@ func owners(m map[string]bool) []string { // - Lease, claim half: OldLeaseEnded is used only in RunClaim, and // RunClaim, when it is declared, uses it. // - Every declared exported Run function goes through runTransition. +// - Claim: RunClaim is used only by Start, which applies the local-root +// rules (own epoch: continue only on HEAD's claim id) before claiming. // - Release of a dependent: RunReleaseDependent is used only by // DeleteVolume (its own HEAD is deleting) and OperatorReleaseDependent, // which reads the target's HEAD before it releases. // - Pool: a name starting with Delete or Remove is used only in -// RemoveUnreachable, where every delete is preceded, in the same loop -// body, by the remover's own lease check (StillHeld), and in -// DeleteVolume, whose steps run in the delete's order: no holder, HEAD -// deleting, the dependents released, the objects deleted, HEAD +// RemoveUnreachable, whose only removal is DeleteMarked, each right +// after a gate on its Mark's error, and in DeleteVolume, whose steps run +// in the delete's order: no holder, HEAD deleting, the dependents +// released, the objects deleted (conditional on the volume id), HEAD // deleted; see removerOrderFindings and deleteOrderFindings. // // These are syntactic checks of this package's own files: they see any @@ -179,6 +181,11 @@ func guardFindings(code map[string]map[string]bool, decl, tests map[string]bool) bad = append(bad, fn+" is declared and does not go through runTransition") } } + for _, fn := range owners(code["RunClaim"]) { + if fn != "Start" { + bad = append(bad, fn+" uses RunClaim: a claim is made only by Start, which first applies the local-root rules") + } + } for _, fn := range owners(code["CreateDeletedHead"]) { if fn != "OperatorReleaseDependent" { bad = append(bad, fn+" uses CreateDeletedHead: only the operator's release writes a tombstone, for a target never created") @@ -472,6 +479,7 @@ func w(ctx context.Context, x g) error { return x.NoLiveLease(ctx) }`, want: "on "ignored mark": {file: "remove.go", src: removeVariant(t, "ignored"), want: "without a mark gate immediately before it"}, "one mark, two deletes": {file: "remove.go", src: removeVariant(t, "second"), want: "without a mark gate immediately before it"}, "unlisted removal method": {file: "remove.go", src: removeVariant(t, "other"), want: "without a mark gate immediately before it"}, + "claim outside Start": {src: `func w(ctx context.Context, st HeadStore, l LeaseWait, me HolderID) { _, _ = RunClaim(ctx, st, l, me) }`, want: "a claim is made only by Start"}, "tombstone outside the operator's release": {src: `func w(ctx context.Context, t TargetStore, h Head) { _ = t.CreateDeletedHead(ctx, h) }`, want: "only the operator's release writes a tombstone"}, "remover method value": {src: `type p interface{ RemoveName(string) } func w(x p) { f := x.RemoveName; f("o") }`, want: "only RemoveUnreachable (mark-gated) and DeleteVolume"}, diff --git a/seaweed-block/core/frontend/chunkstore/layout/stale_test.go b/seaweed-block/core/frontend/chunkstore/layout/stale_test.go index 290d2de3ec..6548fff1f5 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/stale_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout/stale_test.go @@ -81,7 +81,11 @@ func TestHolderFunctions_RefuseAStaleHolder(t *testing.T) { t.Errorf("RemoveUnreachable by %s: %v, %d writes; want ErrNotHolder and none", who, err, len(o.writes)) } } - covered := map[string]bool{"RemoveUnreachable": true} + // 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); 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) diff --git a/seaweed-block/core/frontend/chunkstore/layout/start.go b/seaweed-block/core/frontend/chunkstore/layout/start.go new file mode 100644 index 0000000000..f56254a4c1 --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/layout/start.go @@ -0,0 +1,96 @@ +package layout + +import ( + "context" + "errors" + "fmt" +) + +var ( + // ErrMissingLocalRoot: the holder starts at its own epoch on a fresh + // local root; it cannot prove it holds what it acknowledged. + ErrMissingLocalRoot = errors.New("layout: local root missing at the holder's own epoch") + // ErrStaleLocalRoot: the local root belongs to an earlier claim of this + // server (a same-host forced move published past it). + ErrStaleLocalRoot = errors.New("layout: local root is from an earlier claim") +) + +// StaleLocalRootCure is the way back a stale-root refusal names: after a +// clean stop the published image holds everything the old directory does. +const StaleLocalRootCure = "another root claimed this volume since this directory's last claim (for example a planned move away and back); after a clean stop the directory holds nothing the published image lacks: clear it and start again on a fresh root" + +// MissingLocalRootCure is the way back the refusal names. +const MissingLocalRootCure = "check for a wrong or unmounted data directory; if the local data is gone, a forced move (also to the same host) records the lost range and restarts from the published image" + +// LocalRoot is what a starting holder knows of its local data: the claim +// id its local root's marker carries (written before any claim, so it is +// durable before HEAD can name it), whether the root is fresh, and the +// position it holds synced. A root is fresh from its creation until the +// base of its restore from HEAD is durable, across any crash in between: +// reported non-fresh before that, its position below HEAD's would be +// refused as an old copy. +type LocalRoot struct { + ClaimID string + Fresh bool + Position uint64 // the position the local root holds synced +} + +// Start is the HEAD side of a holder's start on the line it was granted +// (epoch, server), with the claim id of its local root. At HEAD's epoch +// and server, the local root whose claim id HEAD holds continues (its own +// restart, or a claim whose reply was lost); a fresh root with another +// claim id is ErrMissingLocalRoot, an older root with another claim id is +// ErrStaleLocalRoot (fenced); neither writes HEAD. At a higher epoch the +// holder claims with its local claim id (RunClaim, which waits for the old +// lease) only if its root is fresh (it restores from HEAD) or carries +// HEAD's claim id (the continuing lineage); any other root holds data +// older than HEAD's image and is ErrStaleLocalRoot. An older epoch, +// another server at HEAD's epoch, a deleting volume and an absent HEAD +// are refused. +func Start(ctx context.Context, st HeadStore, lease LeaseWait, line HolderID, local LocalRoot) (Head, error) { + if local.ClaimID == "" { + return Head{}, errors.New("layout: start needs the local root's claim id") + } + h, found, err := st.ReadHead(ctx) + if err != nil { + return Head{}, err + } + 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 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 nil + case local.Fresh: + return fmt.Errorf("%w at epoch %d: %s", ErrMissingLocalRoot, h.Epoch, MissingLocalRootCure) + default: + 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 fmt.Errorf("%w (%w): the local root carries claim %q, HEAD's lineage is %q: %s", ErrStaleLocalRoot, ErrNotHolder, local.ClaimID, h.ClaimID, StaleLocalRootCure) + } + return nil +} diff --git a/seaweed-block/core/frontend/chunkstore/layout/start_test.go b/seaweed-block/core/frontend/chunkstore/layout/start_test.go new file mode 100644 index 0000000000..b2c4ef8b01 --- /dev/null +++ b/seaweed-block/core/frontend/chunkstore/layout/start_test.go @@ -0,0 +1,250 @@ +package layout + +import ( + "context" + "errors" + "strings" + "testing" +) + +// The start rules, carried from the store-independent tests of the same +// rule on the current store: a holder's local data is identified by the +// claim id its local root's marker carries, together with the epoch. + +func startHead(epoch uint64, holder, claim string) Head { + return Head{Gen: 7, VolumeID: idFor("v1"), Epoch: epoch, Holder: holder, ClaimID: claim, Activity: Activity{Kind: ActIdle}} +} + +func start(t *testing.T, h Head, line HolderID, local LocalRoot) (*memHead, Head, error) { + t.Helper() + st := &memHead{h: &h} + got, err := Start(context.Background(), st, &fakeLease{ended: true}, line, local) + return st, got, err +} + +// A fresh local root at the holder's own epoch cannot prove it holds the +// acknowledged suffix: refused before any claim, with the likely cause and +// the way back, HEAD unchanged; a retry on the same (still fresh) root is +// refused too. +func TestStart_OwnEpochFreshRootRefusedBeforeClaim(t *testing.T) { + h := startHead(3, "s1/r1", "k-old") + line := HolderID{Epoch: 3, Holder: "s1/r1"} + fresh := LocalRoot{ClaimID: "k-new", Fresh: true} + st, _, err := start(t, h, line, fresh) + if !errors.Is(err, ErrMissingLocalRoot) { + t.Fatalf("own-epoch fresh root: %v; want ErrMissingLocalRoot", err) + } + for _, words := range []string{"wrong or unmounted data directory", "forced move (also to the same host)"} { + if !strings.Contains(err.Error(), words) { + t.Fatalf("refusal %q lacks %q", err, words) + } + } + if st.updates != 0 || st.h.ClaimID != "k-old" || st.h.Epoch != 3 { + t.Fatalf("refused start changed HEAD: %+v (%d writes)", st.h, st.updates) + } + if _, err := Start(context.Background(), st, &fakeLease{ended: true}, line, fresh); !errors.Is(err, ErrMissingLocalRoot) || st.updates != 0 { + t.Fatalf("retry on the same fresh root: %v (%d writes)", err, st.updates) + } +} + +// The same root restarting at its own epoch continues: its marker is the +// claim id HEAD holds. No claim is written. +func TestStart_OwnEpochSameRootContinues(t *testing.T) { + st, got, err := start(t, startHead(3, "s1/r1", "k3"), HolderID{Epoch: 3, Holder: "s1/r1"}, LocalRoot{ClaimID: "k3"}) + if err != nil || st.updates != 0 || got.ClaimID != "k3" { + t.Fatalf("same-root restart: %+v %v (%d writes); want continue without a write", got, err, st.updates) + } +} + +// A fresh root whose claim was written but whose reply was lost restarts +// with the same marker: HEAD holds that claim id at this epoch, so it +// continues. +func TestStart_LostClaimReplyContinues(t *testing.T) { + st, got, err := start(t, startHead(5, "s1/r1", "k5"), HolderID{Epoch: 5, Holder: "s1/r1"}, LocalRoot{ClaimID: "k5", Fresh: true}) + if err != nil || st.updates != 0 || got.Epoch != 5 { + t.Fatalf("restart after a lost claim reply: %+v %v (%d writes)", got, err, st.updates) + } +} + +// After a same-host forced move the successor (epoch 4, its own claim id) +// holds HEAD; the old root (claim id of epoch 3, with a long unpublished +// suffix) remounted under epoch 4 is refused as stale and fenced; HEAD and +// its image are unchanged. +func TestStart_SameHostForcedSuccessorRefusesTheRemountedOldRoot(t *testing.T) { + rn := RootName{Hash: Sum([]byte("successor image"))} + h := startHead(4, "s1/r1", "k4") + h.Current = &rn + st, _, err := start(t, h, HolderID{Epoch: 4, Holder: "s1/r1"}, LocalRoot{ClaimID: "k3"}) + if !errors.Is(err, ErrStaleLocalRoot) || !errors.Is(err, ErrNotHolder) { + t.Fatalf("remounted old root: %v; want ErrStaleLocalRoot and fenced (ErrNotHolder)", err) + } + if st.updates != 0 || st.h.ClaimID != "k4" || *st.h.Current != rn { + t.Fatalf("refused start changed HEAD: %+v", st.h) + } +} + +// A fresh root at a higher issued epoch, on the same server or another, +// claims with its marker's claim id. +func TestStart_FreshRootAtHigherEpochClaims(t *testing.T) { + for _, server := range []string{"s1/r1", "s2/r1"} { + st, got, err := start(t, startHead(3, "s1/r1", "k3"), HolderID{Epoch: 4, Holder: server}, LocalRoot{ClaimID: "k4", Fresh: true}) + if err != nil || st.updates != 1 || got.Epoch != 4 || got.Holder != server || got.ClaimID != "k4" { + t.Fatalf("fresh root at epoch 4 on %s: %+v %v (%d writes)", server, got, err, st.updates) + } + } +} + +// An older line is fenced; the same epoch held by another server is not +// this holder's; an absent HEAD is refused (nothing is created). +func TestStart_Refusals(t *testing.T) { + if _, _, err := start(t, startHead(4, "s1/r1", "k4"), HolderID{Epoch: 3, Holder: "s1/r1"}, LocalRoot{ClaimID: "k3"}); !errors.Is(err, ErrStaleEpoch) { + t.Errorf("older line: %v; want ErrStaleEpoch", err) + } + if _, _, err := start(t, startHead(4, "s1/r1", "k4"), HolderID{Epoch: 4, Holder: "s2/r1"}, LocalRoot{ClaimID: "k9", Fresh: true}); !errors.Is(err, ErrNotHolder) { + t.Errorf("same epoch, another server: %v; want ErrNotHolder", err) + } + st := &memHead{} + if _, err := Start(context.Background(), st, &fakeLease{ended: true}, HolderID{Epoch: 1, Holder: "s1/r1"}, LocalRoot{ClaimID: "k1", Fresh: true}); !errors.Is(err, ErrHeadAbsent) || st.updates != 0 { + t.Errorf("absent HEAD: %v", err) + } + if _, _, err := start(t, startHead(3, "s1/r1", "k3"), HolderID{Epoch: 4, Holder: "s1/r1"}, LocalRoot{}); err == nil { + t.Error("start without a local claim id accepted") + } +} + +// A same-host forced move put a fresh root Y at epoch 4 (claim k4, its +// image published); the old root X (claim k3, data) is remounted while the +// master grants epoch 5: X's data predates Y's image, so it is refused as +// stale and fenced, HEAD unchanged. At a higher epoch only a fresh root, or +// the root whose claim id HEAD holds (the continuing lineage), claims. +func TestStart_HigherEpochRefusesAnOlderLineage(t *testing.T) { + rn := RootName{Hash: Sum([]byte("image published at epoch 4"))} + h := startHead(4, "s1/r1", "k4") + h.Current = &rn + st, _, err := start(t, h, HolderID{Epoch: 5, Holder: "s1/r1"}, LocalRoot{ClaimID: "k3"}) + if !errors.Is(err, ErrStaleLocalRoot) || !errors.Is(err, ErrNotHolder) { + t.Fatalf("old root at a higher epoch: %v; want ErrStaleLocalRoot, fenced", err) + } + if st.updates != 0 || st.h.ClaimID != "k4" || st.h.Epoch != 4 || *st.h.Current != rn { + t.Fatalf("refused start changed HEAD: %+v (%d writes)", st.h, st.updates) + } + // The continuing lineage (the root HEAD's claim id names) claims at the + // higher epoch, keeping its data. + st2, got, err := start(t, h, HolderID{Epoch: 5, Holder: "s1/r1"}, LocalRoot{ClaimID: "k4"}) + if err != nil || st2.updates != 1 || got.Epoch != 5 || got.ClaimID != "k4" { + t.Fatalf("continuing lineage at epoch 5: %+v %v (%d writes)", got, err, st2.updates) + } +} + +// A root carrying HEAD's claim id but synced only below HEAD's published +// position is an old copy of the right directory: refused as stale, at its +// own epoch and at a higher one, HEAD unchanged. +func TestStart_OldCopyOfTheRightRootRefused(t *testing.T) { + h := startHead(4, "s1/r1", "k4") + h.Position = 100 + for _, epoch := range []uint64{4, 5} { + st, _, err := start(t, h, HolderID{Epoch: epoch, Holder: "s1/r1"}, LocalRoot{ClaimID: "k4", Position: 99}) + if !errors.Is(err, ErrStaleLocalRoot) || st.updates != 0 { + t.Fatalf("root at position 99 under HEAD at 100, epoch %d: %v (%d writes); want ErrStaleLocalRoot, no write", epoch, err, st.updates) + } + st, _, err = start(t, h, HolderID{Epoch: epoch, Holder: "s1/r1"}, LocalRoot{ClaimID: "k4", Position: 100}) + if err != nil { + t.Fatalf("root at HEAD's position, epoch %d: %v", epoch, err) + } + } +} + +// Killed at the claim's HEAD write (before it applies, and applied with +// its reply lost), the holder restarts with the same local root: the claim +// id it writes is the one its marker already carries, so the re-run either +// claims or finds its own claim and continues; it is never refused. +func TestStart_InterruptAtTheClaim(t *testing.T) { + for _, fresh := range []bool{true, false} { + local := LocalRoot{ClaimID: "k3", Fresh: fresh} + for _, after := range []bool{false, true} { + h := startHead(3, "s1/r1", "k3") + if fresh { + h = startHead(3, "s1/r1", "k-previous") + local.ClaimID = "k-fresh" + } + st := &memHead{h: &h, killAt: 1, after: after} + line := HolderID{Epoch: 4, Holder: "s1/r1"} + if _, err := Start(context.Background(), st, &fakeLease{ended: true}, line, local); !errors.Is(err, errKilled) { + t.Fatalf("killed start: %v", err) + } + st.restart() + got, err := Start(context.Background(), st, &fakeLease{ended: true}, line, local) + if err != nil || got.Epoch != 4 || got.ClaimID != local.ClaimID { + t.Fatalf("restart (fresh %t, applied %t): %+v %v; want claimed or continued at epoch 4 with %q", fresh, after, got, err, local.ClaimID) + } + } + } +} + +// A fresh root stays fresh until its restore's base is durable: claimed at +// a higher epoch, crashed before the base is down, restarted at the same +// epoch with the same marker and still fresh, it continues the restore. +// Reported as non-fresh at position 0 under HEAD's published position, the +// same root would be refused as an old copy; the caller reports Fresh until +// the base is durable. +func TestStart_FreshAcrossACrashContinuesTheRestore(t *testing.T) { + h := startHead(3, "s1/r1", "k-prev") + h.Position = 50 + st := &memHead{h: &h} + line := HolderID{Epoch: 4, Holder: "s1/r1"} + if _, err := Start(context.Background(), st, &fakeLease{ended: true}, line, LocalRoot{ClaimID: "k4", Fresh: true}); err != nil { + t.Fatal(err) + } + got, err := Start(context.Background(), st, &fakeLease{ended: true}, line, LocalRoot{ClaimID: "k4", Fresh: true}) + if err != nil || got.ClaimID != "k4" || got.Epoch != 4 { + t.Fatalf("restart of a still-fresh root: %+v %v; want continue", got, err) + } + if _, err := Start(context.Background(), st, &fakeLease{ended: true}, line, LocalRoot{ClaimID: "k4", Position: 0}); !errors.Is(err, ErrStaleLocalRoot) { + t.Fatalf("the same root reported non-fresh at 0 under 50: %v; want ErrStaleLocalRoot", err) + } +} + +// A deleting volume does not start: at the holder's own epoch Start falls +// through to the claim, which refuses a deleting HEAD. +func TestStart_DeletingVolumeRefused(t *testing.T) { + h := startHead(3, "s1/r1", "k3") + h.State = StateDeleting + for _, line := range []HolderID{{Epoch: 3, Holder: "s1/r1"}, {Epoch: 4, Holder: "s1/r1"}} { + st, _, err := start(t, h, line, LocalRoot{ClaimID: "k3"}) + if !errors.Is(err, ErrVolumeDeleting) || st.updates != 0 { + t.Fatalf("start at epoch %d of a deleting volume: %v (%d writes); want ErrVolumeDeleting", line.Epoch, err, st.updates) + } + } +} + +// A planned move away and back finds the old directory on the first host: +// another root claimed in between, so it is refused as stale, and the +// error names the cure. +func TestStart_StaleRootNamesTheCure(t *testing.T) { + _, _, err := start(t, startHead(5, "s2/r1", "k5"), HolderID{Epoch: 6, Holder: "s1/r1"}, LocalRoot{ClaimID: "k3"}) + if !errors.Is(err, ErrStaleLocalRoot) || !strings.Contains(err.Error(), StaleLocalRootCure) { + 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) + } + }) + } +} diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index d438f2b839..9256f4453b 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -542,7 +542,7 @@ stateDiagram-v2 | idle | snap(s) | caller is the holder at HEAD's epoch (epoch, server and claim id); HEAD present; snap(s) again is a replay, any other activity is refused | head.go `Head.Begin`, `runTransition` | `INV-SNAPK-HEAD-WRITE-001`, `INV-SNAPK-BEGIN-001` | | snap(s) | idle | holder at HEAD's epoch (epoch, server and claim id); activity is snap(s); over idle it succeeds without a write and means only that no activity remains | head.go `Head.Abort` | `INV-SNAPK-ABORT-001` | | snap(s) | idle, current = root | holder at HEAD's epoch (epoch, server and claim id); activity is snap(s); the previous manifest builds to HEAD's current root; the new own chunks, every segment and the root are present | commit.go `Head.Commit`, `RunCommit` | `INV-SNAPK-COMMIT-REACH-001`, `INV-SNAPK-TXN-INTERRUPT-001` | -| any | idle at epoch e, claim id k | e above HEAD's epoch (the same epoch, server and claim id is a replay; the same epoch and server with another claim id is refused); every older holder's lease has ended, checked after the read and before the update | claim.go `Head.Claim`, `RunClaim` (`LeaseWait.OldLeaseEnded`) | `INV-SNAPK-CLAIM-LEASE-001`, `INV-SNAPK-CLAIM-ID-001` | +| any (Start) | idle at epoch e, claim id k | Start only: at HEAD's epoch it continues on the root carrying HEAD's claim id at or above HEAD's position, else refuses; above, a fresh root or the continuing lineage claims, validated against the HEAD the claim updates | start.go `Start`; claim.go `RunClaim` | `INV-SNAPK-START-ROOT-001`, `INV-SNAPK-START-LINEAGE-001`, `INV-SNAPK-START-POSITION-001` | | idle | clean(c) | holder at HEAD's epoch (epoch, server and claim id); activity idle (clean(c) again is a replay) | clean.go `Head.BeginClean` | `INV-SNAPK-CLEAN-EXCL-001` | | clean(c) | idle | holder at HEAD's epoch (epoch, server and claim id); activity clean(c); over idle it succeeds without a write | clean.go `Head.EndClean` | `INV-SNAPK-CLEAN-EXCL-001` | | clean(c) | clean(c), names removed | holder at HEAD's epoch (epoch, server and claim id) with clean(c) open (HEAD read once, after BeginClean); a name is deleted only if no root in HEAD (current or snapshot) names it in this volume's pool; each removal is a mark conditional on the plan's metadata, then a delete conditional on that mark | remove.go `RemoveUnreachable` | `INV-SNAPK-REMOVE-MARK-001` | @@ -553,7 +553,7 @@ stateDiagram-v2 ## Part B. Invariant ledger -360 rows: 289 ACTIVE, 67 STUB, 4 ARCHIVED; 15 kept from the internal ledger, 345 new. +364 rows: 293 ACTIVE, 67 STUB, 4 ARCHIVED; 15 kept from the internal ledger, 349 new. The "Machine / transition" column refers to Part A. Notes under a table record what a row does not cover and, for kept rows, what was narrowed from the internal ledger. @@ -1124,6 +1124,10 @@ Notes: | `INV-SNAPK-RELEASE-CALLERS-001` | Only the target's own delete (its HEAD already deleting) and the operator's command release a dependent; a source scan restricts the callers and requires the operator's read of the target. | M9 source HEAD, dependents | `core/frontend/chunkstore/layout` (source scan of `delete.go`) | `TestGuard_Controls` | — (the snapshot kernel model is not in this repository) | ACTIVE | | `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`, `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 | Notes: