diff --git a/seaweed-block/core/authority/controller.go b/seaweed-block/core/authority/controller.go index 7d4b4efa81..27e32379c8 100644 --- a/seaweed-block/core/authority/controller.go +++ b/seaweed-block/core/authority/controller.go @@ -749,7 +749,8 @@ func sameAsk(a, b AssignmentAsk) bool { a.ReplicaID == b.ReplicaID && a.DataAddr == b.DataAddr && a.CtrlAddr == b.CtrlAddr && - a.Intent == b.Intent + a.Intent == b.Intent && + a.Incarnation == b.Incarnation } // plannedEvidence is a rebalance's evidence: the gate's probe of the target, diff --git a/seaweed-block/core/authority/restart_endpoint_version_test.go b/seaweed-block/core/authority/restart_endpoint_version_test.go index e8250ce0cd..4e51adef48 100644 --- a/seaweed-block/core/authority/restart_endpoint_version_test.go +++ b/seaweed-block/core/authority/restart_endpoint_version_test.go @@ -235,3 +235,55 @@ func TestPublisher_AskWithoutAProcessIsIssuedForTheReportedOne(t *testing.T) { t.Fatalf("the promoted process's first report re-issued its line: EV %d -> %d", line.EndpointVersion, got.EndpointVersion) } } + +// Held publication: a refresh for one process is queued and not yet +// published when a newer report names another process at the same +// address. The queued ask is replaced, so the line is issued for the +// process that reports now, not for one that cannot adopt it. +func TestTopologyController_QueuedRefreshIsReplacedForANewerProcess(t *testing.T) { + g := newBootGraceRig(t) + g.reissued = false + g.steadyPrimary() + g.submit(r1Slot("r1-a", "d1-new", "c1-new", true)) // r1-a moved: refresh queued, not consumed + g.now = g.now.Add(time.Second) + g.submit(r1Slot("r1-b", "d1-new", "c1-new", true)) // restarted before the refresh was published + ask := nextAskOrFail(t, g.ctrl) + if ask.Intent != IntentRefreshEndpoint || ask.Incarnation != "r1-b" || ask.DataAddr != "d1-new" { + t.Fatalf("published ask %+v; want the refresh for r1-b at d1-new", ask) + } + expectNoNext(t, g.ctrl) +} + +// The workspace writer keeps the line's process: after a lease grant is +// persisted through it, a reload still names the process the line was +// issued for, so the unchanged primary is not re-issued and a restarted +// one still is. +func TestWorkspaceLeaseWriteKeepsTheLinesIncarnationAcrossReload(t *testing.T) { + ctx := context.Background() + disk, dir := newTempStore(t) + now := time.Unix(1000, 0) + p := NewPublisher(nil, WithStore(disk), WithWorkspaceLeaseConfig(time.Second, func() time.Time { return now })) + if err := p.apply(AssignmentAsk{VolumeID: "v", ReplicaID: "r", DataAddr: "d", CtrlAddr: "c", Intent: IntentBind, Incarnation: "p1"}); err != nil { + t.Fatal(err) + } + if _, err := p.RenewWorkspaceLease(ctx, "v", "r", 1); err != nil { // a workspace write + t.Fatal(err) + } + _ = disk.Close() + again, err := NewFileAuthorityStore(dir) + if err != nil { + t.Fatal(err) + } + defer again.Close() + q := NewPublisher(nil, WithStore(again), WithWorkspaceLeaseConfig(time.Second, func() time.Time { return now })) + line, ok := q.VolumeAuthorityLine("v") + if !ok || line.Incarnation != "p1" || line.EndpointVersion != 1 { + t.Fatalf("reloaded after a workspace write: %+v; want issued for p1 at EV 1", line) + } + if err := q.apply(AssignmentAsk{VolumeID: "v", ReplicaID: "r", DataAddr: "d", CtrlAddr: "c", Intent: IntentRefreshEndpoint, Incarnation: "p1"}); err != nil { + t.Fatal(err) + } + if got, _ := q.LastPublished("v", "r"); got.EndpointVersion != 1 { + t.Fatalf("the unchanged primary was re-issued after reload: EV %d", got.EndpointVersion) + } +} diff --git a/seaweed-block/core/authority/workspace.go b/seaweed-block/core/authority/workspace.go index 5fff3e6ad4..ae8d074e53 100644 --- a/seaweed-block/core/authority/workspace.go +++ b/seaweed-block/core/authority/workspace.go @@ -403,7 +403,7 @@ func (p *Publisher) persistWorkspaceTimedLocked(volume string, s *WorkspaceState } i := line.info record := DurableRecord{VolumeID: volume, ReplicaID: i.ReplicaID, Epoch: i.Epoch, EndpointVersion: i.EndpointVersion, DataAddr: i.DataAddr, CtrlAddr: i.CtrlAddr, - WriteSeq: p.writeSeqByVolume[volume] + 1, Workspace: cloneWorkspace(s)} + WriteSeq: p.writeSeqByVolume[volume] + 1, Workspace: cloneWorkspace(s), Incarnation: i.Incarnation} start := time.Now() err = p.store.Put(record) write = time.Since(start) diff --git a/seaweed-block/core/host/master/line_incarnation_test.go b/seaweed-block/core/host/master/line_incarnation_test.go new file mode 100644 index 0000000000..8a24dac023 --- /dev/null +++ b/seaweed-block/core/host/master/line_incarnation_test.go @@ -0,0 +1,44 @@ +package master + +import ( + "testing" + "time" + + "github.com/seaweedfs/seaweed-block/core/adapter" + "github.com/seaweedfs/seaweed-block/core/authority" +) + +// Every line names the process of its primary, also when the ask that +// minted it named none: the initial bind of the product tick, and a direct +// reassign (as the heal, a planned or workspace move, or an operator's +// request submits it) record the incarnation the target reports. So the +// promoted process's first report re-issues nothing, and a restarted one +// is told apart from it. +func TestLinesNameTheirPrimarysProcess(t *testing.T) { + h := newBootGraceMaster(t, Config{}) + defer closeTestMaster(t, h) + if line := waitAuthorityLine(t, h.Publisher(), "vol-rf3"); line.ReplicaID != "r1" || line.Incarnation != "r1-a" { + t.Fatalf("initial line %+v; want r1 issued for r1-a", line) + } + ingestRF3Incarnations(t, h, readySlot("r1-a"), readySlot("r2-a"), readySlot("r3-a")) // fresh reports + if err := h.Controller().SubmitAssignmentAsk(authority.AssignmentAsk{ + VolumeID: "vol-rf3", ReplicaID: "r2", DataAddr: "127.0.0.1:19201", CtrlAddr: "127.0.0.1:19202", + Intent: authority.IntentReassign, Evidence: adapter.PrimaryEvidence{Kind: adapter.PrimaryKindForced}, + }); err != nil { + t.Fatal(err) + } + deadline := time.Now().Add(3 * time.Second) + for { + line, ok := h.Publisher().VolumeAuthorityLine("vol-rf3") + if ok && line.ReplicaID == "r2" { + if line.Incarnation != "r2-a" { + t.Fatalf("direct reassign: line %+v; want it issued for r2-a", line) + } + return + } + if time.Now().After(deadline) { + t.Fatalf("the direct reassign was not published: %+v", line) + } + time.Sleep(10 * time.Millisecond) + } +} diff --git a/seaweed-block/docs/design/invariants.md b/seaweed-block/docs/design/invariants.md index 1aa5bd64a8..d0e1b2e72c 100644 --- a/seaweed-block/docs/design/invariants.md +++ b/seaweed-block/docs/design/invariants.md @@ -117,7 +117,7 @@ flowchart LR | From | To | Event and guard | Enforced in | Protects | |---|---|---|---|---| | Unbound | Bound (epoch 1, endpoint version 1) | `IntentBind`; no slot of this volume holds a present line (checked per volume, also after a restart); to move to another slot use `IntentReassign` | `core/authority/authority.go` `applyWithWorkspaceMove`, `boundReplicaForVolume`; `core/authority/controller.go` `decideVolume` (binds only a volume with no line) | `INV-AUTH-ONE-PRIMARY-001`, `INV-AUTH-PERSIST-BEFORE-PUBLISH-001` | -| Bound | Bound (endpoint version + 1) | `IntentRefreshEndpoint`; same epoch; a no-op when the addresses are unchanged | `core/authority/authority.go` `applyWithWorkspaceMove` | `INV-AUTH-PERSIST-BEFORE-PUBLISH-001` | +| Bound | Bound (endpoint version + 1) | `IntentRefreshEndpoint`; same epoch; new addresses, or the same addresses for another primary process (the line's incarnation differs); a no-op when both are unchanged | `core/authority/authority.go` `applyWithWorkspaceMove` | `INV-AUTH-PERSIST-BEFORE-PUBLISH-001`, `INV-AUTH-RESTART-REISSUE-001` | | Bound | Reassigned (per-volume maximum epoch + 1) | `IntentReassign`; some earlier publication exists; an enrolled workspace volume changes only through a guarded move | `core/authority/authority.go` `applyWithWorkspaceMove`, `maxPublishedEpochForVolume` | `INV-AUTH-EPOCH-MINT-001`, `INV-AUTH-ONE-PRIMARY-001` | | any | (store write first) | with an authority store configured, every transition is written to the store before memory changes or subscribers are notified; a failed write publishes nothing; a write whose outcome is unknown (the directory fsync after the rename failed) fail-stops blockmaster, so no caller is told it failed | `core/authority/authority.go` `applyWithWorkspaceMove` | `INV-AUTH-PERSIST-BEFORE-PUBLISH-001` | | any (workspace) | Uncertain | a failed workspace authority write, including a lease extension; later changes for that volume, and renewals that would issue a new or extended grant, are refused until restart (a Revoking lease still gets its bounded drain reply) | `core/authority/authority.go` `applyWithWorkspaceMove`; `core/authority/workspace.go` `persistWorkspaceLocked`; `core/authority/workspace_lease_view.go` `RenewWorkspaceLease`, `extendWorkspaceLease` | `INV-AUTH-UNCERTAIN-001` |