Files
seaweedfs/seaweed-block/core/authority/directive.go
T
wzcheng99andClaude Opus 4.7 5c663b8c83 P14 S2: bounded authority publication route for assignment truth
Introduces the core/authority/ package — the system-owned publication
route where Epoch and EndpointVersion are authored, not relayed. The
publisher is the only production code that constructs an
adapter.AssignmentInfo with a non-zero Epoch.

Contract boundaries (load-bearing):
  * S2 owns publication of authority truth.
  * S2 does NOT own failover policy.
  * Engine and adapter remain consumers, not producers.

Design:
  * Publisher.Run consumes AssignmentAsk values from an injected
    Directive and fans out AssignmentInfo to subscribers keyed by
    (VolumeID, ReplicaID) — not by ReplicaID alone, which would
    multiplex different volumes that share a replica name.
  * AssignmentAsk carries no Epoch and no EndpointVersion. The
    publisher mints both from the intent enum (Bind | RefreshEndpoint
    | Reassign). Retirement (Remove) is deliberately deferred.
  * No public verb accepts a preformed AssignmentInfo. The former
    PublishAssignment relay verb was removed from the design before
    code — the publisher is a minter, not a pass-through.
  * Directive is the seam where P14 S3's failover policy will later
    plug in without reaching into the publisher. S2 ships only
    StaticDirective.
  * Bridge forwards publisher output to adapter.OnAssignment — the
    adapter remains the one and only consumer entry point; no second
    mutation ingress was added.

Tests (core/authority/authority_test.go):
  * Intent semantics: Bind mints epoch=1 ev=1; RefreshEndpoint keeps
    epoch and bumps ev (no-op on unchanged addrs); Reassign bumps
    epoch and resets ev=1; each invalid precondition is rejected.
  * Fan-out keyed by (VolumeID, ReplicaID): two subs on the same key
    both receive; a sub on a different volume for the same replica
    name does NOT receive — regression for the narrow-key bug.
  * Late-subscriber catch-up via LastPublished.
  * Run loop drives Directive until ctx cancel; rejected asks are
    logged and loop continues.
  * Bridge forwards to AssignmentConsumer.
  * Closure target integration test: Publisher -> Bridge -> real
    VolumeReplicaAdapter reaches Mode == ModeHealthy without any
    harness.assign call.
  * Non-forgeability structural test walks core/ and fails if any
    production file outside an explicit allowlist (authority /
    calibration / conformance / schema) constructs an AssignmentInfo
    with a non-zero Epoch.

Docs:
  * docs/p14-s2-audit.md — scoped audit of existing producers /
    publishers / consumers / hidden-operator-path smells / V2 port
    candidates.
  * docs/p14-s2-design.md — bounded design: load-bearing boundaries,
    closure target, package layout, AssignmentAsk shape, authoring
    rules table, publication API, Bridge model, V2 port plan,
    explicit non-goals and acceptance criteria.
  * docs/p14-s2-closure.md — what S2 closed, what stayed out, S2/S3
    boundary table, verification recipe.

No changes to engine, adapter, transport, ops, storage, or sparrow
wiring. The authority route is additive.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-17 17:13:12 -07:00

169 lines
5.2 KiB
Go

// Package authority is the system-owned publication route for
// assignment truth. It owns the authorship of Epoch and
// EndpointVersion — never accepts them as input — and fans out
// authoritative AssignmentInfo values to subscribers keyed by
// (VolumeID, ReplicaID).
//
// The design note is docs/p14-s2-design.md. Three boundaries hold:
// 1. S2 owns publication of authority truth.
// 2. S2 does NOT own failover policy.
// 3. Engine and adapter remain consumers, not producers, of
// assignment truth.
package authority
import (
"context"
"errors"
"sync"
"time"
)
// AskIntent is the only control knob a Directive has. The publisher
// uses it to decide which authoritative field to advance and by how
// much. S2 supports exactly three intents. Retirement is deferred
// (see docs/p14-s2-design.md "What we are NOT building").
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.
IntentBind AskIntent = iota + 1
// IntentRefreshEndpoint: same volume/replica, new transport
// endpoints. Publisher keeps Epoch, bumps EndpointVersion.
// No-op if addresses are unchanged. Rejected if no prior
// publish exists.
IntentRefreshEndpoint
// IntentReassign: bump the authoritative epoch for an
// already-bound key. Publisher increments Epoch and resets
// EndpointVersion to 1. Rejected if no prior publish exists.
IntentReassign
)
// String returns a stable name for diagnostics.
func (i AskIntent) String() string {
switch i {
case IntentBind:
return "Bind"
case IntentRefreshEndpoint:
return "RefreshEndpoint"
case IntentReassign:
return "Reassign"
default:
return "Unknown"
}
}
// AssignmentAsk is the narrow input a Directive emits. It is
// deliberately narrower than adapter.AssignmentInfo: it carries no
// Epoch and no EndpointVersion. Those are authored by the publisher.
type AssignmentAsk struct {
VolumeID string
ReplicaID string
DataAddr string
CtrlAddr string
Intent AskIntent
}
// Directive is the input side of the authority route. A Directive
// emits one AssignmentAsk at a time. The publisher's Run loop calls
// Next repeatedly until ctx cancellation.
//
// The Directive interface is the seam where P14 S3's failover
// policy will later plug in. S2 ships only a trivial implementation:
// StaticDirective.
type Directive interface {
Next(ctx context.Context) (AssignmentAsk, error)
}
// StaticDirective emits a pre-wired sequence of AssignmentAsk
// values, then blocks on ctx cancellation. It is the trivial S2
// implementation used for tests and the sparrow closure target.
//
// StaticDirective carries no failover policy, no eligibility logic,
// no trigger logic. It is pure "replay these requests".
type StaticDirective struct {
mu sync.Mutex
asks []AssignmentAsk
idx int
}
// NewStaticDirective returns a Directive that emits asks in order.
// After the last ask, Next blocks until ctx cancellation.
func NewStaticDirective(asks []AssignmentAsk) *StaticDirective {
cp := make([]AssignmentAsk, len(asks))
copy(cp, asks)
return &StaticDirective{asks: cp}
}
// Append adds an ask to the tail. Safe for tests to call while a
// publisher Run loop is consuming.
func (s *StaticDirective) Append(ask AssignmentAsk) {
s.mu.Lock()
s.asks = append(s.asks, ask)
s.mu.Unlock()
}
// Next returns the next ask, or blocks until ctx is cancelled when
// the pre-wired sequence is exhausted. Returning ctx.Err() signals
// the publisher's Run to exit cleanly.
func (s *StaticDirective) Next(ctx context.Context) (AssignmentAsk, error) {
for {
s.mu.Lock()
if s.idx < len(s.asks) {
ask := s.asks[s.idx]
s.idx++
s.mu.Unlock()
return ask, nil
}
s.mu.Unlock()
// Exhausted. Block briefly and retry so Append can add more,
// but honor ctx cancellation promptly.
select {
case <-ctx.Done():
return AssignmentAsk{}, ctx.Err()
case <-time.After(10 * time.Millisecond):
}
}
}
// Errors returned by the publisher for rejected asks.
var (
ErrMissingVolumeID = errors.New("authority: AssignmentAsk missing VolumeID")
ErrMissingReplicaID = errors.New("authority: AssignmentAsk missing ReplicaID")
ErrMissingDataAddr = errors.New("authority: AssignmentAsk missing DataAddr")
ErrMissingCtrlAddr = errors.New("authority: AssignmentAsk missing CtrlAddr")
ErrMissingIntent = errors.New("authority: AssignmentAsk missing Intent")
ErrUnknownIntent = errors.New("authority: AssignmentAsk Intent not recognized")
ErrBindAlreadyBound = errors.New("authority: Bind on already-bound key")
ErrRefreshNotBound = errors.New("authority: RefreshEndpoint on unbound key")
ErrReassignNotBound = errors.New("authority: Reassign on unbound key")
)
func validateAsk(ask AssignmentAsk) error {
if ask.VolumeID == "" {
return ErrMissingVolumeID
}
if ask.ReplicaID == "" {
return ErrMissingReplicaID
}
if ask.DataAddr == "" {
return ErrMissingDataAddr
}
if ask.CtrlAddr == "" {
return ErrMissingCtrlAddr
}
if ask.Intent == 0 {
return ErrMissingIntent
}
if ask.Intent != IntentBind &&
ask.Intent != IntentRefreshEndpoint &&
ask.Intent != IntentReassign {
return ErrUnknownIntent
}
return nil
}