diff --git a/seaweed-block/core/authority/rebalance_expect_current_test.go b/seaweed-block/core/authority/rebalance_expect_current_test.go new file mode 100644 index 0000000000..19bd38c237 --- /dev/null +++ b/seaweed-block/core/authority/rebalance_expect_current_test.go @@ -0,0 +1,89 @@ +package authority + +import ( + "context" + "errors" + "testing" + "time" +) + +// A rebalance is decided against the line the controller read and waits in +// the queue until the publisher takes it. A line published directly in that +// window (an operator's failover) must not be overridden by the queued +// rebalance: the publisher refuses it because the line moved, and the line +// the operator published stays. +func TestAQueuedRebalanceIsNotMintedOverALineMovedSinceItWasDecided(t *testing.T) { + holder := &basisReaderHolder{} + ctrl := NewTopologyController(TopologyControllerConfig{RebalanceSkew: 1}, holder) + pub := NewPublisher(ctrl) + holder.set(pub) + for _, seed := range []AssignmentAsk{ + {VolumeID: "v1", ReplicaID: "v1r1", DataAddr: "d11", CtrlAddr: "c11", Intent: IntentBind}, + {VolumeID: "v2", ReplicaID: "v2r1", DataAddr: "d21", CtrlAddr: "c21", Intent: IntentBind}, + {VolumeID: "v3", ReplicaID: "v3r2", DataAddr: "d32", CtrlAddr: "c32", Intent: IntentBind}, + } { + if err := pub.apply(seed); err != nil { + t.Fatalf("seed %s: %v", seed.VolumeID, err) + } + } + // s1 carries two primaries, s3 none: the controller decides v1's + // rebalance to v1r3 against v1r1@1 and queues it (the publisher is not + // draining yet). + snap := ClusterSnapshot{ + CollectedRevision: 1, + Servers: []ServerObservation{ + {ServerID: "s1", Reachable: true, Eligible: true}, + {ServerID: "s2", Reachable: true, Eligible: true}, + {ServerID: "s3", Reachable: true, Eligible: true}, + }, + Volumes: []VolumeTopologySnapshot{ + clusterVolume("v1", observedAuthorityFrom(pub, "v1"), + candidate("v1r1", "s1", "d11", "c11", 10), + candidate("v1r2", "s2", "d12", "c12", 10), + candidate("v1r3", "s3", "d13", "c13", 30), + ), + clusterVolume("v2", observedAuthorityFrom(pub, "v2"), + candidate("v2r1", "s1", "d21", "c21", 10), + candidate("v2r2", "s2", "d22", "c22", 10), + candidate("v2r3", "s3", "d23", "c23", 10), + ), + clusterVolume("v3", observedAuthorityFrom(pub, "v3"), + candidate("v3r1", "s1", "d31", "c31", 10), + candidate("v3r2", "s2", "d32", "c32", 10), + candidate("v3r3", "s3", "d33", "c33", 10), + ), + }, + } + if err := ctrl.SubmitClusterSnapshot(snap); err != nil { + t.Fatal(err) + } + if d, ok := ctrl.DesiredFor("v1"); !ok || d.Ask.ReplicaID != "v1r3" || d.Reason != "rebalance" { + t.Fatalf("fixture: v1's desired %+v (ok=%v), want the rebalance to v1r3 queued", d, ok) + } + + // The operator's failover publishes v1r2 directly, at the next epoch. + operator := candidate("v1r2", "s2", "d12", "c12", 10).PromotionEvidence + if err := pub.apply(AssignmentAsk{VolumeID: "v1", ReplicaID: "v1r2", DataAddr: "d12", CtrlAddr: "c12", Intent: IntentReassign, Evidence: operator}); err != nil { + t.Fatalf("the operator's failover: %v", err) + } + moved, _ := pub.VolumeAuthorityLine("v1") + if moved.ReplicaID != "v1r2" || moved.Epoch != 2 { + t.Fatalf("fixture: line after the failover %+v, want v1r2@2", moved) + } + + // The publisher now takes the queued rebalance. + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + ask, err := ctrl.Next(ctx) + if err != nil || ask.VolumeID != "v1" || ask.ReplicaID != "v1r3" { + t.Fatalf("fixture: took %+v %v, want the queued rebalance of v1 to v1r3", ask, err) + } + applyErr := pub.apply(ask) + line, _ := pub.VolumeAuthorityLine("v1") + if line.ReplicaID != "v1r2" || line.Epoch != 2 { + t.Fatalf("the queued rebalance overrode the operator's failover: line %+v (apply: %v), want v1r2@2 kept", line, applyErr) + } + if !errors.Is(applyErr, ErrExpectedLineMoved) { + t.Fatalf("the stale rebalance was refused with %v, want ErrExpectedLineMoved", applyErr) + } +}