diff --git a/seaweed-block/core/authority/authority.go b/seaweed-block/core/authority/authority.go index 9ef074ccbe..b1574e359c 100644 --- a/seaweed-block/core/authority/authority.go +++ b/seaweed-block/core/authority/authority.go @@ -120,6 +120,11 @@ type Publisher struct { nextSubID atomic.Uint64 + // incarnationOf is the latest process incarnation a replica reports + // (SetIncarnationSource); its own lock, never taken under mu. + incarnationMu sync.RWMutex + incarnationOf func(volumeID, replicaID string) string + mu sync.Mutex state map[subKey]assignmentState subs map[subKey][]*subscription @@ -465,6 +470,15 @@ func (p *Publisher) closeAllSubscriptions() { // production code that constructs an adapter.AssignmentInfo with a // non-zero Epoch. The structural non-forgeability test in // authority_test.go enforces this boundary at repo scope. +// SetIncarnationSource gives the publisher the latest process incarnation +// each replica reports, for asks that do not name one (see +// AssignmentAsk.Incarnation). nil (the default) records what asks name. +func (p *Publisher) SetIncarnationSource(of func(volumeID, replicaID string) string) { + p.incarnationMu.Lock() + p.incarnationOf = of + p.incarnationMu.Unlock() +} + func (p *Publisher) apply(ask AssignmentAsk) error { if p.publicationAdmission != nil { return p.publicationAdmission(ask, func() error { return p.applyWithWorkspaceMove(ask, nil) }) @@ -476,6 +490,19 @@ func (p *Publisher) applyWithWorkspaceMove(ask AssignmentAsk, grant *workspaceGr if err := validateAsk(ask); err != nil { return err } + // An ask built without the target's process (a heal, a planned move, a + // workspace move, an operator's request) records the one the target + // reports now, so its line is issued for that process and its first + // report re-issues nothing. Read before p.mu: the source is the + // observation store, which reads the publisher. + if ask.Incarnation == "" { + p.incarnationMu.RLock() + of := p.incarnationOf + p.incarnationMu.RUnlock() + if of != nil { + ask.Incarnation = of(ask.VolumeID, ask.ReplicaID) + } + } p.beforeWorkspaceGrantLock("publication") p.mu.Lock() diff --git a/seaweed-block/core/authority/restart_endpoint_version_test.go b/seaweed-block/core/authority/restart_endpoint_version_test.go index 89e2151376..e8250ce0cd 100644 --- a/seaweed-block/core/authority/restart_endpoint_version_test.go +++ b/seaweed-block/core/authority/restart_endpoint_version_test.go @@ -5,6 +5,8 @@ import ( "reflect" "testing" "time" + + "github.com/seaweedfs/seaweed-block/core/adapter" ) // A restarted primary process (a new incarnation at the same addresses) @@ -199,3 +201,37 @@ func TestWorkspaceRestartReissueTouchesOnlyTheEndpointVersion(t *testing.T) { t.Fatalf("the holder no longer renews at its epoch: %v", err) } } + +// An ask built outside the controller names no process (the heal after a +// failed fencing round, a planned or workspace move, an operator's +// request). The publisher issues its line for the process the target +// reports now, so that process's first report as primary re-issues +// nothing: one endpoint version, not a second right after the promotion. +func TestPublisher_AskWithoutAProcessIsIssuedForTheReportedOne(t *testing.T) { + pub := NewPublisher(NewStaticDirective(nil)) + reported := map[string]string{"r1": "p1", "r2": "q7"} + pub.SetIncarnationSource(func(volumeID, replicaID string) string { return reported[replicaID] }) + if err := pub.apply(AssignmentAsk{VolumeID: "v1", ReplicaID: "r1", DataAddr: "d1", CtrlAddr: "c1", Intent: IntentBind}); err != nil { + t.Fatal(err) + } + if got, _ := pub.LastPublished("v1", "r1"); got.Incarnation != "p1" { + t.Fatalf("bind without a process: issued for %q; want the reported p1", got.Incarnation) + } + heal := AssignmentAsk{VolumeID: "v1", ReplicaID: "r2", DataAddr: "d2", CtrlAddr: "c2", Intent: IntentReassign, + Evidence: adapter.PrimaryEvidence{Kind: adapter.PrimaryKindForced}} + if err := pub.apply(heal); err != nil { + t.Fatal(err) + } + line, _ := pub.LastPublished("v1", "r2") + if line.Incarnation != "q7" { + t.Fatalf("reassign without a process: issued for %q; want the reported q7", line.Incarnation) + } + // q7's first report: the controller's refresh names q7, the line's own + // process, and the publisher re-issues nothing. + if err := pub.apply(AssignmentAsk{VolumeID: "v1", ReplicaID: "r2", DataAddr: "d2", CtrlAddr: "c2", Intent: IntentRefreshEndpoint, Incarnation: "q7"}); err != nil { + t.Fatal(err) + } + if got, _ := pub.LastPublished("v1", "r2"); got.EndpointVersion != line.EndpointVersion { + t.Fatalf("the promoted process's first report re-issued its line: EV %d -> %d", line.EndpointVersion, got.EndpointVersion) + } +} diff --git a/seaweed-block/core/host/master/host.go b/seaweed-block/core/host/master/host.go index 59c5a0754f..ce28059953 100644 --- a/seaweed-block/core/host/master/host.go +++ b/seaweed-block/core/host/master/host.go @@ -292,6 +292,15 @@ func New(cfg Config) (*Host, error) { Sink: sink, Reader: boot.Publisher, }) + // An ask built outside the controller (a heal, a planned or workspace + // move, an operator's request) is issued for the process its target + // reports now. + boot.Publisher.SetIncarnationSource(func(volumeID, replicaID string) string { + if f, ok := obs.Store().SlotFact(volumeID, replicaID); ok { + return f.Incarnation + } + return "" + }) lifecycleStores, err := openLifecycleStores(cfg.LifecycleStoreDir) if err != nil { diff --git a/seaweed-block/core/host/volume/host.go b/seaweed-block/core/host/volume/host.go index 5d6efd239c..eef470d643 100644 --- a/seaweed-block/core/host/volume/host.go +++ b/seaweed-block/core/host/volume/host.go @@ -1093,6 +1093,18 @@ func (h *Host) applyFact(fact *control.AssignmentFact) { return } + // A line issued for another process of this replica is the dead + // process's: its sessions (endpoint version, session id) are the ones + // each replica still holds, and a session this process opened on it + // could equal one of them. Nothing is adopted or recorded (no + // assignment, watchdog, replica set or role) until the master re-issues + // the line for this process (a new endpoint version). + if inc := fact.GetLineIncarnation(); inc != "" && inc != h.incarnation { + h.log.Printf("blockvolume: volume %s line epoch=%d EV=%d was issued for process %s, this is %s: NOT adopting until it is re-issued", + h.cfg.VolumeID, fact.Epoch, fact.EndpointVersion, inc, h.incarnation) + return + } + h.recordSelfAssignment(fact) // A fact that awaits replica stores closes the frontend at once; one // that carries them all opens it only after the peers' expected stores @@ -1129,17 +1141,6 @@ func (h *Host) applyFact(fact *control.AssignmentFact) { } } - // A line issued for another process of this replica is the dead - // process's: its sessions (endpoint version, session id) are the ones - // each replica still holds, and a session this process opened on it - // could equal one of them. Nothing is adopted until the master - // re-issues the line for this process (a new endpoint version). - if inc := fact.GetLineIncarnation(); inc != "" && inc != h.incarnation { - h.log.Printf("blockvolume: volume %s line epoch=%d EV=%d was issued for process %s, this is %s: NOT adopting until it is re-issued", - h.cfg.VolumeID, fact.Epoch, fact.EndpointVersion, inc, h.incarnation) - return - } - // The target's own store must still show what the master probed before // it adopts the role (nothing is installed yet, so a refusal has no // effect to undo). diff --git a/seaweed-block/core/host/volume/line_incarnation_test.go b/seaweed-block/core/host/volume/line_incarnation_test.go index 5ed1fb45a5..53af433b84 100644 --- a/seaweed-block/core/host/volume/line_incarnation_test.go +++ b/seaweed-block/core/host/volume/line_incarnation_test.go @@ -28,6 +28,12 @@ func TestHost_ApplyFact_LineIssuedForAnotherProcessIsNotAdopted(t *testing.T) { if r.callCount.Load() != 0 || a.callCount.Load() != 0 { t.Fatalf("the dead process's line was adopted: replica sets %d, assignments %d", r.callCount.Load(), a.callCount.Load()) } + h.ownAssignmentMu.RLock() + recorded := h.ownAssignmentEpoch + h.ownAssignmentMu.RUnlock() + if recorded != 0 { + t.Fatalf("the dead process's line was recorded as this process's assignment (epoch %d)", recorded) + } h.applyFixtureFact(fact(2, "p2")) if r.callCount.Load() != 1 || a.callCount.Load() != 1 || a.lastInfo.EndpointVersion != 2 { t.Fatalf("the line re-issued for this process: replica sets %d, assignments %d (EV %d); want both at EV 2", r.callCount.Load(), a.callCount.Load(), a.lastInfo.EndpointVersion)