diff --git a/seaweed-block/core/authority/controller.go b/seaweed-block/core/authority/controller.go index 322aba42b6..1d25f4c9c9 100644 --- a/seaweed-block/core/authority/controller.go +++ b/seaweed-block/core/authority/controller.go @@ -492,7 +492,7 @@ func (c *TopologyController) decideVolume( if candidateAcceptable(current, serverIndex) { if endpointStale(current, currentLine) { - return refreshAsk(vol.VolumeID, current), "endpoint refresh", true + return refreshAsk(vol.VolumeID, current, currentLine), "endpoint refresh", true } if target, ok := chooseRebalanceCandidate(vol, current, serverIndex, currentLoad, projectedLoad, c.config.RebalanceSkew); ok { // A primary change goes out only with the promotion gate's @@ -520,7 +520,7 @@ func (c *TopologyController) decideVolume( // may be refreshed. if hold, superseded := c.primaryBootGraceLocked(vol.VolumeID, current, candidatePlaceable(current, serverIndex)); hold { if !superseded && endpointStale(current, currentLine) { - return refreshAsk(vol.VolumeID, current), "endpoint refresh (primary boot grace)", true + return refreshAsk(vol.VolumeID, current, currentLine), "endpoint refresh (primary boot grace)", true } return AssignmentAsk{}, "", false } @@ -546,7 +546,7 @@ func (c *TopologyController) decideVolume( // when the current replica is still reachable/eligible; this preserves // authority continuity without treating that replica as a failover candidate. if candidatePlaceable(current, serverIndex) && endpointStale(current, currentLine) { - return refreshAsk(vol.VolumeID, current), "endpoint refresh", true + return refreshAsk(vol.VolumeID, current, currentLine), "endpoint refresh", true } return AssignmentAsk{}, "", false } @@ -561,15 +561,16 @@ func endpointStale(current ReplicaCandidate, line AuthorityBasis) bool { } // refreshAsk re-issues the current primary's line at its reported -// addresses for its reported incarnation. -func refreshAsk(volumeID string, current ReplicaCandidate) AssignmentAsk { +// addresses for its reported incarnation, only while the volume's line is +// still line (the one the refresh was decided against). +func refreshAsk(volumeID string, current ReplicaCandidate, line AuthorityBasis) AssignmentAsk { return AssignmentAsk{ - VolumeID: volumeID, - ReplicaID: current.ReplicaID, - DataAddr: current.DataAddr, - CtrlAddr: current.CtrlAddr, - Intent: IntentRefreshEndpoint, - Incarnation: current.Incarnation, + VolumeID: volumeID, + ReplicaID: current.ReplicaID, + DataAddr: current.DataAddr, + CtrlAddr: current.CtrlAddr, + Intent: IntentRefreshEndpoint, + Incarnation: current.Incarnation, } } diff --git a/seaweed-block/core/authority/directive.go b/seaweed-block/core/authority/directive.go index 26c6ce1553..6e4159175e 100644 --- a/seaweed-block/core/authority/directive.go +++ b/seaweed-block/core/authority/directive.go @@ -79,19 +79,25 @@ type AssignmentAsk struct { // differs from the line's is a restarted primary: a new endpoint version // even at the same addresses. Incarnation string - // ExpectCurrent, when set, makes a reassign conditional on the volume's - // line being exactly this replica at this epoch when the publisher mints + // ExpectCurrent, when set, makes a reassign or an endpoint refresh + // conditional on the volume's line being exactly this replica at this + // epoch (and, when given, at these addresses) when the publisher mints // (under its lock): an operator's move never lands on a line it did not - // see. + // see, and a refresh decided against a line never rewrites the line a + // move put in its place. ExpectCurrent *ExpectedLine } -// ExpectedLine is the line a conditional reassign was decided against. +// ExpectedLine is the line a conditional reassign or refresh was decided +// against. DataAddr and CtrlAddr are checked only when set. type ExpectedLine struct { ReplicaID string Epoch uint64 + DataAddr string + CtrlAddr string } + // ErrExpectedLineMoved: a conditional reassign found the volume's line no // longer the one it was decided against; the publisher mints nothing. var ErrExpectedLineMoved = errors.New("authority: the volume's line moved since the move was decided") diff --git a/seaweed-block/core/authority/refresh_expected_line_test.go b/seaweed-block/core/authority/refresh_expected_line_test.go new file mode 100644 index 0000000000..ec41168492 --- /dev/null +++ b/seaweed-block/core/authority/refresh_expected_line_test.go @@ -0,0 +1,59 @@ +package authority + +import ( + "errors" + "testing" + + "github.com/seaweedfs/seaweed-block/core/adapter" +) + +// An endpoint refresh decided against a line (a restarted holder at the +// source addresses) is not applied to the line a move put in its place: a +// planned move keeps the replica id and changes the host, so without the +// condition the refresh would write the source's addresses into the moved +// line. The publisher refuses it under its lock; the moved line stays. The +// same refresh against the unchanged line is applied. +func TestRefreshIsConditionalOnTheLineItWasDecidedAgainst(t *testing.T) { + pub := NewPublisher(NewStaticDirective(nil)) + if err := pub.apply(AssignmentAsk{VolumeID: "v1", ReplicaID: "r1", DataAddr: "src-d", CtrlAddr: "src-c", Intent: IntentBind, Incarnation: "proc-a"}); err != nil { + t.Fatal(err) + } + decided, _ := pub.VolumeAuthorityLine("v1") + refresh := AssignmentAsk{VolumeID: "v1", ReplicaID: "r1", DataAddr: "src-d", CtrlAddr: "src-c", Intent: IntentRefreshEndpoint, Incarnation: "proc-b", + ExpectCurrent: &ExpectedLine{ReplicaID: decided.ReplicaID, Epoch: decided.Epoch, DataAddr: decided.DataAddr, CtrlAddr: decided.CtrlAddr}} + + // A planned move lands first: the same replica at the next epoch, on the + // target's addresses. + if err := pub.apply(AssignmentAsk{VolumeID: "v1", ReplicaID: "r1", DataAddr: "dst-d", CtrlAddr: "dst-c", Intent: IntentReassign, + Evidence: adapter.PrimaryEvidence{Kind: adapter.PrimaryKindPromotion, DurableLSN: 1, Basis: adapter.BasisPlanned}}); err != nil { + t.Fatalf("move: %v", err) + } + moved, _ := pub.VolumeAuthorityLine("v1") + + if err := pub.apply(refresh); !errors.Is(err, ErrExpectedLineMoved) { + t.Fatalf("stale refresh: err=%v; want ErrExpectedLineMoved", err) + } + if line, _ := pub.VolumeAuthorityLine("v1"); line.Epoch != moved.Epoch || line.EndpointVersion != moved.EndpointVersion || line.DataAddr != "dst-d" || line.CtrlAddr != "dst-c" { + t.Fatalf("line %+v after the stale refresh; want the moved line %+v unchanged", line, moved) + } + + // Decided against the current (moved) line, a refresh for a restarted + // target is applied. + fresh := AssignmentAsk{VolumeID: "v1", ReplicaID: "r1", DataAddr: "dst-d", CtrlAddr: "dst-c", Intent: IntentRefreshEndpoint, Incarnation: "proc-c", + ExpectCurrent: &ExpectedLine{ReplicaID: moved.ReplicaID, Epoch: moved.Epoch, DataAddr: moved.DataAddr, CtrlAddr: moved.CtrlAddr}} + if err := pub.apply(fresh); err != nil { + t.Fatalf("refresh against the current line: %v", err) + } + if line, _ := pub.VolumeAuthorityLine("v1"); line.Epoch != moved.Epoch || line.EndpointVersion != moved.EndpointVersion+1 || line.Incarnation != "proc-c" { + t.Fatalf("line %+v; want %d/%d naming proc-c", line, moved.Epoch, moved.EndpointVersion+1) + } +} + +// The topology controller's refresh names the line it was decided against. +func TestControllerRefreshNamesTheLineItWasDecidedAgainst(t *testing.T) { + line := AuthorityBasis{ReplicaID: "r1", Epoch: 3, DataAddr: "d", CtrlAddr: "c"} + ask := refreshAsk("v1", ReplicaCandidate{ReplicaID: "r1", DataAddr: "d", CtrlAddr: "c", Incarnation: "proc-b"}, line) + if e := ask.ExpectCurrent; e == nil || e.ReplicaID != "r1" || e.Epoch != 3 || e.DataAddr != "d" || e.CtrlAddr != "c" { + t.Fatalf("refresh expects %+v; want the line r1@3 at d/c", ask.ExpectCurrent) + } +} diff --git a/seaweed-block/core/host/master/rf1_restart_reissue_test.go b/seaweed-block/core/host/master/rf1_restart_reissue_test.go index 4326b39f74..49c08a82f8 100644 --- a/seaweed-block/core/host/master/rf1_restart_reissue_test.go +++ b/seaweed-block/core/host/master/rf1_restart_reissue_test.go @@ -112,3 +112,99 @@ func rf1ReissueCase(t *testing.T, first string) { time.Sleep(50 * time.Millisecond) } } + +// A refresh decided before a move (a holder restarted at the source after a +// lost ACK answer, the tick building its refresh) and applied after the +// operator's clean move to another host never rewrites the moved line: it is +// submitted through the normal publication path and refused there, so the +// line stays the target's, at the move's epoch. +func TestRF1RefreshDecidedBeforeACrossHostMoveIsNotApplied(t *testing.T) { + head := authority.WorkspaceHead{Gen: 9, Epoch: 1, Holder: "node-a/r1", RemoteLSN: 1, Detached: true} + h, err := New(Config{Listen: "127.0.0.1:0", AuthorityStoreDir: t.TempDir(), LifecycleStoreDir: t.TempDir(), WorkspaceRPC: true, LayoutCreateVolume: testLayoutCreateVolume, + WorkspaceHeadReader: func(context.Context, string) (authority.WorkspaceHead, error) { return head, nil }}) + if err != nil { + t.Fatal(err) + } + h.Start() + defer closeTestMaster(t, h) + conn, err := grpc.NewClient(h.Addr(), grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + t.Fatal(err) + } + defer conn.Close() + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + ws := control.NewWorkspaceServiceClient(conn) + if _, err = control.NewLifecycleServiceClient(conn).CreateVolume(ctx, &control.CreateVolumeRequest{VolumeId: "rt", SizeBytes: 1 << 20, ReplicationFactor: 1}); err != nil { + t.Fatal(err) + } + for _, n := range []lifecycle.NodeRegistration{{ServerID: "node-a", DataAddr: "127.0.0.1:9201", CtrlAddr: "127.0.0.1:9101"}, {ServerID: "node-b", DataAddr: "127.0.0.1:9202", CtrlAddr: "127.0.0.1:9102"}} { + if _, err = h.Lifecycle().Nodes.RegisterNode(n); err != nil { + t.Fatal(err) + } + } + src := &control.WorkspaceBinding{VolumeId: "rt", ServerId: "node-a", DataAddr: "127.0.0.1:19201", CtrlAddr: "127.0.0.1:19101"} + dst := &control.WorkspaceBinding{VolumeId: "rt", ServerId: "node-b", DataAddr: "127.0.0.1:19202", CtrlAddr: "127.0.0.1:19102"} + if _, err = ws.RegisterWorkspaceBinding(ctx, src); err != nil { + t.Fatal(err) + } + if _, err = ws.PlaceWorkspace(ctx, &control.WorkspacePlacement{VolumeId: "rt", ServerId: "node-a", ReplicaId: "r1"}); err != nil { + t.Fatal(err) + } + report := func(server string, b *control.WorkspaceBinding, incarnation string) { + t.Helper() + if _, err := control.NewObservationServiceClient(conn).ReportHeartbeat(ctx, &control.HeartbeatReport{ServerId: server, SentAt: timestamppb.Now(), Reachable: true, Eligible: true, + Slots: []*control.HeartbeatSlot{{VolumeId: "rt", ReplicaId: "r1", DataAddr: b.DataAddr, CtrlAddr: b.CtrlAddr, Incarnation: incarnation}}}); err != nil { + t.Fatal(err) + } + } + report("node-a", src, "proc-a") + if _, err = h.RunLifecycleProductTick(); err != nil { + t.Fatal(err) + } + line := waitAuthorityLine(t, h.Publisher(), "rt") + id := &control.WorkspaceIdentity{ServerId: "node-a", VolumeId: "rt", ReplicaId: "r1", Epoch: line.Epoch} + if _, err = ws.RenewWorkspaceLease(ctx, id); err != nil { + t.Fatal(err) + } + if _, err = ws.RevokeWorkspace(ctx, id); err != nil { + t.Fatal(err) + } + if _, err = ws.AckWorkspaceRevoked(ctx, &control.WorkspaceRevocationAck{Identity: id, HeadGen: 9}); err != nil { + t.Fatal(err) + } + + // The source holder restarts (a lost ACK answer, exit 75) and reports a + // new process at the source; the tick decides its refresh now. + report("node-a", src, "proc-b") + stale, restarted := h.restartedHolderRefresh(authority.AssignmentAsk{VolumeID: "rt", ReplicaID: "r1", DataAddr: src.DataAddr, CtrlAddr: src.CtrlAddr, Intent: authority.IntentBind}) + if !restarted { + t.Fatal("fixture: no refresh decided for the restarted source") + } + + // The operator's clean move lands first. + if _, err = ws.RegisterWorkspaceBinding(ctx, dst); err != nil { + t.Fatal(err) + } + grant, err := ws.MoveWorkspace(ctx, &control.WorkspaceMove{RequestId: "clean-1", VolumeId: "rt", ExpectedReplicaId: "r1", ExpectedEpoch: line.Epoch, + TargetReplicaId: "r1", TargetServerId: "node-b", TargetDataAddr: dst.DataAddr, TargetCtrlAddr: dst.CtrlAddr, Kind: "clean"}) + if err != nil || !grant.Issued { + t.Fatalf("clean move: %v %v", grant, err) + } + moved := waitAuthorityReplica(t, h.Publisher(), "rt", "r1") + for deadline := time.Now().Add(5 * time.Second); moved.Epoch != grant.NewEpoch && time.Now().Before(deadline); time.Sleep(20 * time.Millisecond) { + moved, _ = h.Publisher().VolumeAuthorityLine("rt") + } + if moved.Epoch != grant.NewEpoch || moved.DataAddr != dst.DataAddr { + t.Fatalf("moved line %+v; want r1@%d at the target", moved, grant.NewEpoch) + } + + // Then the stale refresh is submitted. + if err := h.ctrl.SubmitAssignmentAsk(stale); err != nil { + t.Fatal(err) + } + time.Sleep(500 * time.Millisecond) + if after, _ := h.Publisher().VolumeAuthorityLine("rt"); after.Epoch != moved.Epoch || after.EndpointVersion != moved.EndpointVersion || after.DataAddr != dst.DataAddr || after.CtrlAddr != dst.CtrlAddr { + t.Fatalf("line %+v after the stale refresh; want the moved line %+v (the target's addresses)", after, moved) + } +}