diff --git a/seaweed-block/core/authority/authority.go b/seaweed-block/core/authority/authority.go index 88e5a2c2b4..6556be2cc4 100644 --- a/seaweed-block/core/authority/authority.go +++ b/seaweed-block/core/authority/authority.go @@ -474,6 +474,15 @@ func (p *Publisher) applyWithWorkspaceMove(ask AssignmentAsk, grant *workspaceGr p.mu.Unlock() return ErrBindAlreadyBound } + // Bind is genesis for the VOLUME, not only for this slot: once + // any slot of the volume holds an authority line, a Bind for + // another slot would mint a second epoch-1 line beside it. + // Moving authority to another slot is IntentReassign. + if bound, ok := boundReplicaForVolume(p.state, ask.VolumeID); ok { + p.mu.Unlock() + return fmt.Errorf("%w: volume %s is already bound to replica %s; use Reassign", + ErrBindAlreadyBound, ask.VolumeID, bound) + } next = adapter.AssignmentInfo{ VolumeID: ask.VolumeID, ReplicaID: ask.ReplicaID, @@ -610,6 +619,22 @@ func safeInvokePublishObserver(observer func(PublishEvent), event PublishEvent) // treat that as "no per-volume authority line to advance". // // Must be called with p.mu held; reads p.state. +// boundReplicaForVolume reports whether any slot of volumeID holds a +// published authority line, and names one (the lowest replica id, so the +// error text is deterministic). Presence, not the epoch value, decides. +func boundReplicaForVolume(state map[subKey]assignmentState, volumeID string) (string, bool) { + bound, found := "", false + for k, st := range state { + if !st.present || k.volumeID != volumeID { + continue + } + if !found || k.replicaID < bound { + bound, found = k.replicaID, true + } + } + return bound, found +} + func maxPublishedEpochForVolume(state map[subKey]assignmentState, volumeID string) uint64 { var max uint64 for k, st := range state { diff --git a/seaweed-block/core/authority/bind_volume_genesis_test.go b/seaweed-block/core/authority/bind_volume_genesis_test.go new file mode 100644 index 0000000000..0e80e42f6e --- /dev/null +++ b/seaweed-block/core/authority/bind_volume_genesis_test.go @@ -0,0 +1,104 @@ +package authority + +import ( + "errors" + "testing" +) + +// #183: Bind is genesis for the volume. These cover the branches around +// the per-volume check; the Run-path reproducer is +// TestPublisher_RunRejectsSecondSlotGenesisBind. + +func bindAsk(volumeID, replicaID string) AssignmentAsk { + return AssignmentAsk{VolumeID: volumeID, ReplicaID: replicaID, DataAddr: "d-" + replicaID, CtrlAddr: "c-" + replicaID, Intent: IntentBind} +} + +func reassignAsk(volumeID, replicaID string) AssignmentAsk { + ask := bindAsk(volumeID, replicaID) + ask.Intent = IntentReassign + return ask +} + +func TestPublisher_BindSecondSlotOfBoundVolumeRejected(t *testing.T) { + pub := NewPublisher(NewStaticDirective(nil)) + if err := pub.apply(bindAsk("v1", "r1")); err != nil { + t.Fatalf("first Bind: %v", err) + } + if err := pub.apply(bindAsk("v1", "r2")); !errors.Is(err, ErrBindAlreadyBound) { + t.Fatalf("second-slot Bind: want ErrBindAlreadyBound, got %v", err) + } + if got, ok := pub.LastPublished("v1", "r2"); ok { + t.Fatalf("second slot was published: %+v", got) + } + if got, ok := pub.LastPublished("v1", "r1"); !ok || got.Epoch != 1 || got.EndpointVersion != 1 { + t.Fatalf("first slot line changed: %+v ok=%t", got, ok) + } +} + +func TestPublisher_BindOtherVolumeUnaffected(t *testing.T) { + pub := NewPublisher(NewStaticDirective(nil)) + if err := pub.apply(bindAsk("v1", "r1")); err != nil { + t.Fatalf("Bind v1: %v", err) + } + if err := pub.apply(bindAsk("v2", "r2")); err != nil { + t.Fatalf("Bind of a fresh volume rejected: %v", err) + } + if got, ok := pub.LastPublished("v2", "r2"); !ok || got.Epoch != 1 { + t.Fatalf("fresh volume line=%+v ok=%t want epoch 1", got, ok) + } +} + +func TestPublisher_ReassignToOtherSlotStillAllowedThenBindRejected(t *testing.T) { + pub := NewPublisher(NewStaticDirective(nil)) + if err := pub.apply(bindAsk("v1", "r1")); err != nil { + t.Fatalf("Bind: %v", err) + } + if err := pub.apply(reassignAsk("v1", "r2")); err != nil { + t.Fatalf("Reassign to another slot: %v", err) + } + if got, ok := pub.LastPublished("v1", "r2"); !ok || got.Epoch != 2 { + t.Fatalf("reassigned line=%+v ok=%t want epoch 2", got, ok) + } + for _, r := range []string{"r1", "r2", "r3"} { + if err := pub.apply(bindAsk("v1", r)); !errors.Is(err, ErrBindAlreadyBound) { + t.Fatalf("Bind %s after Reassign: want ErrBindAlreadyBound, got %v", r, err) + } + } + if _, ok := pub.LastPublished("v1", "r3"); ok { + t.Fatal("r3 was published by a Bind") + } +} + +// The file store keeps one record per volume, so after a restart only the +// current slot is in memory. A Bind for the earlier slot must still be +// refused: the volume, not the key, is bound. +func TestPublisher_BindAfterRestartRejectedForAnySlot(t *testing.T) { + dir := t.TempDir() + storeA, err := NewFileAuthorityStore(dir) + if err != nil { + t.Fatal(err) + } + pubA := NewPublisher(NewStaticDirective(nil), WithStore(storeA)) + if err := pubA.apply(bindAsk("v1", "r1")); err != nil { + t.Fatalf("Bind: %v", err) + } + if err := pubA.apply(reassignAsk("v1", "r2")); err != nil { + t.Fatalf("Reassign: %v", err) + } + _ = storeA.Close() + + storeB, err := NewFileAuthorityStore(dir) + if err != nil { + t.Fatal(err) + } + defer storeB.Close() + pubB := NewPublisher(NewStaticDirective(nil), WithStore(storeB)) + for _, r := range []string{"r1", "r2", "r3"} { + if err := pubB.apply(bindAsk("v1", r)); !errors.Is(err, ErrBindAlreadyBound) { + t.Fatalf("Bind %s after restart: want ErrBindAlreadyBound, got %v", r, err) + } + } + if got, ok := pubB.LastPublished("v1", "r2"); !ok || got.Epoch != 2 { + t.Fatalf("reloaded line=%+v ok=%t want r2 epoch 2", got, ok) + } +} diff --git a/seaweed-block/core/authority/directive.go b/seaweed-block/core/authority/directive.go index 958f10e3d2..f65349d76b 100644 --- a/seaweed-block/core/authority/directive.go +++ b/seaweed-block/core/authority/directive.go @@ -26,8 +26,10 @@ type AskIntent int const ( // IntentBind: first-time binding for (VolumeID, ReplicaID). - // Publisher mints Epoch=1, EndpointVersion=1. Rejected if a - // prior publish already exists for the key. + // Publisher mints Epoch=1, EndpointVersion=1. Rejected + // (ErrBindAlreadyBound) if a prior publish already exists for the + // key or for any other slot of the same volume: Bind is genesis + // for the volume; another slot is reached by IntentReassign. IntentBind AskIntent = iota + 1 // IntentRefreshEndpoint: same volume/replica, new transport