fix(authority): Bind is genesis for the volume, not only the slot

The Publisher checked IntentBind against the (volume, replica) key only,
so Bind(v,r1) then Bind(v,r2) published two epoch-1 lines for one
volume. After a restart the file store reloads only the current slot,
so a Bind for an earlier slot was accepted the same way.

A Bind is now refused with ErrBindAlreadyBound when any slot of the
volume already holds an authority line. Moving to another slot stays
IntentReassign. Same-key double Bind and Bind of a fresh volume are
unchanged.

Fixes #183.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-11-16 21:26:33 -08:00
1 parent ca92a063e4
commit bdcc60d7a4
3 files changed
+133 -2

No files matched your search

+25
View File
@@ -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 {
@@ -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)
}
}
+4 -2
View File
@@ -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