mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
- Fork's early replay of a completed request refuses (volume_deleted) a target whose HEAD is deleting, purging or deleted, also before its VOLUME is marked, like the later publication path. - Host.ImportLifecycleVolume registers a cluster-spec volume under the CreateVolume RPC's boundary (workspaceMu, reservation check through the create); blockmaster startup uses it, skipping (and logging) a reserved id, and the placement import skips reserved ids too. Unreserved ids keep importing idempotently. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
927 lines
35 KiB
Go
927 lines
35 KiB
Go
package master
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweed-block/core/adapter"
|
|
"github.com/seaweedfs/seaweed-block/core/authority"
|
|
"github.com/seaweedfs/seaweed-block/core/lifecycle"
|
|
"github.com/seaweedfs/seaweed-block/core/ops"
|
|
control "github.com/seaweedfs/seaweed-block/core/rpc/control"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
)
|
|
|
|
// adapter import is still needed: the AssignmentService fan-in
|
|
// uses adapter.AssignmentInfo as the delivery channel element
|
|
// type from publisher.Subscribe. No composite literal of
|
|
// adapter.AssignmentInfo is constructed on the master side —
|
|
// values flow through unchanged from publisher state.
|
|
|
|
// services is the master-side implementation of the control-plane
|
|
// gRPC services defined in core/rpc/proto/v3_control.proto.
|
|
//
|
|
// Boundary: every handler here converts wire messages to
|
|
// authority-layer types via HeartbeatToObservation or LastPublished
|
|
// reads; no handler constructs an AssignmentAsk or mints epochs.
|
|
type services struct {
|
|
control.UnimplementedObservationServiceServer
|
|
control.UnimplementedAssignmentServiceServer
|
|
control.UnimplementedEvidenceServiceServer
|
|
control.UnimplementedClusterEvidenceServiceServer
|
|
control.UnimplementedLifecycleServiceServer
|
|
control.UnimplementedFailbackServiceServer
|
|
control.UnimplementedWorkspaceServiceServer
|
|
|
|
host *Host
|
|
// seq is incremented per ReportHeartbeat handler invocation.
|
|
// gRPC runs each unary RPC on its own goroutine, so seq MUST
|
|
// be atomic. Diagnostic only — the client uses the sequence
|
|
// to correlate log lines, not to drive authority.
|
|
seq atomic.Uint64
|
|
}
|
|
|
|
func newServices(h *Host) *services {
|
|
return &services{host: h}
|
|
}
|
|
|
|
func (s *services) CreateVolume(ctx context.Context, req *control.CreateVolumeRequest) (*control.CreateVolumeResponse, error) {
|
|
stores := s.host.Lifecycle()
|
|
if stores == nil {
|
|
return nil, status.Error(codes.FailedPrecondition, "lifecycle store is not configured")
|
|
}
|
|
spec := lifecycleSpecFromWire(req)
|
|
// A reserved id (a chunk volume delete admitted or committed) is never
|
|
// created again; the reservation is checked under the lock its writer
|
|
// holds.
|
|
// held through the create (workspace_protocol.md: reservation checks
|
|
// and commits stay under workspaceMu).
|
|
s.host.workspaceMu.Lock()
|
|
defer s.host.workspaceMu.Unlock()
|
|
if stores.Deletions.Reserved(spec.VolumeID) {
|
|
return nil, status.Errorf(codes.FailedPrecondition, "create volume: %s is being deleted or was deleted; its id is never reused", spec.VolumeID)
|
|
}
|
|
if createVolumeHook != nil {
|
|
createVolumeHook(spec.VolumeID)
|
|
}
|
|
s.host.log.Printf("blockmaster: CreateVolume volume=%q protocol=%q replication_factor=%d pvc=%q namespace=%q", spec.VolumeID, spec.Protocol, spec.ReplicationFactor, spec.PVCName, spec.PVCNamespace)
|
|
rec, err := stores.Volumes.CreateVolume(spec)
|
|
if err != nil {
|
|
return nil, lifecycleError("create volume", err)
|
|
}
|
|
return &control.CreateVolumeResponse{
|
|
VolumeId: rec.Spec.VolumeID,
|
|
SizeBytes: rec.Spec.SizeBytes,
|
|
ReplicationFactor: int32(rec.Spec.ReplicationFactor),
|
|
Protocol: rec.Spec.Protocol,
|
|
FrontendTransport: rec.Spec.FrontendTransport,
|
|
PvcName: rec.Spec.PVCName,
|
|
PvcNamespace: rec.Spec.PVCNamespace,
|
|
PvcUid: rec.Spec.PVCUID,
|
|
PvName: rec.Spec.PVName,
|
|
}, nil
|
|
}
|
|
|
|
func (s *services) DeleteVolume(ctx context.Context, req *control.DeleteVolumeRequest) (*control.DeleteVolumeResponse, error) {
|
|
s.host.workspaceMu.Lock()
|
|
defer s.host.workspaceMu.Unlock()
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, workspaceError(err)
|
|
}
|
|
stores := s.host.Lifecycle()
|
|
if stores == nil {
|
|
return nil, status.Error(codes.FailedPrecondition, "lifecycle store is not configured")
|
|
}
|
|
if req.GetVolumeId() == "" {
|
|
return nil, status.Error(codes.InvalidArgument, "volume_id is required")
|
|
}
|
|
_, bound, err := stores.WorkspaceBindings.Get(req.GetVolumeId())
|
|
if err != nil {
|
|
return nil, lifecycleError("delete workspace bindings", err)
|
|
}
|
|
_, placed := stores.Placements.GetPlacement(req.GetVolumeId())
|
|
err = s.host.Publisher().RecordWorkspaceDeletion(ctx, req.GetVolumeId(), bound && placed, func() error {
|
|
if e := stores.Placements.DeletePlacement(req.GetVolumeId()); e != nil {
|
|
return e
|
|
}
|
|
if e := stores.Volumes.DeleteVolume(req.GetVolumeId()); e != nil {
|
|
return e
|
|
}
|
|
return stores.WorkspaceBindings.DeleteVolume(req.GetVolumeId())
|
|
})
|
|
if err != nil {
|
|
return nil, workspaceError(err)
|
|
}
|
|
return &control.DeleteVolumeResponse{}, nil
|
|
}
|
|
|
|
// MarkVolumeDeleting admits a chunk volume delete (sw-block ops chunk
|
|
// volume-delete). It holds workspaceMu, the lock every assignment,
|
|
// placement and binding write takes, and runs the mark inside
|
|
// RecordWorkspaceDeletion's check under the publisher lock: a volume with a
|
|
// live authority line (and no clean revocation ACK), a pending move or a
|
|
// queued placement is refused; otherwise the record is marked deleting, and
|
|
// from then on publication admission, the placement reconciler, the
|
|
// workload planner, binding/placement/move RPCs and CreateVolume all refuse
|
|
// it.
|
|
func (s *services) MarkVolumeDeleting(ctx context.Context, req *control.MarkVolumeDeletingRequest) (*control.MarkVolumeDeletingResponse, error) {
|
|
s.host.workspaceMu.Lock()
|
|
defer s.host.workspaceMu.Unlock()
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, workspaceError(err)
|
|
}
|
|
stores := s.host.Lifecycle()
|
|
if stores == nil {
|
|
return nil, status.Error(codes.FailedPrecondition, "lifecycle store is not configured")
|
|
}
|
|
vol := req.GetVolumeId()
|
|
if vol == "" {
|
|
return nil, status.Error(codes.InvalidArgument, "volume_id is required")
|
|
}
|
|
_, bound, err := stores.WorkspaceBindings.Get(vol)
|
|
if err != nil {
|
|
return nil, lifecycleError("mark volume deleting", err)
|
|
}
|
|
_, placed := stores.Placements.GetPlacement(vol)
|
|
var res lifecycle.DeletionReservation
|
|
var persistErr error
|
|
err = s.host.Publisher().RecordWorkspaceDeletion(ctx, vol, bound && placed, func() error {
|
|
res, persistErr = stores.Deletions.Admit(vol, req.GetToken())
|
|
return persistErr
|
|
})
|
|
switch {
|
|
case persistErr != nil:
|
|
// The admission check passed; the reservation write failed or its
|
|
// outcome is unknown (the store keeps it in memory, fail closed).
|
|
return nil, status.Errorf(codes.Unavailable, "mark volume deleting: %v", persistErr)
|
|
case err != nil:
|
|
return nil, status.Errorf(codes.FailedPrecondition, "mark volume deleting: %v", err)
|
|
}
|
|
_, known := stores.Volumes.GetVolume(vol)
|
|
s.host.log.Printf("blockmaster: MarkVolumeDeleting volume=%q known=%t state=%s", vol, known, res.State)
|
|
return &control.MarkVolumeDeletingResponse{Known: known, State: res.State}, nil
|
|
}
|
|
|
|
// ImportLifecycleVolume registers a desired volume from the cluster spec
|
|
// (blockmaster startup), under the same boundary as the CreateVolume RPC:
|
|
// a reserved id (a chunk volume delete admitted or committed) is skipped,
|
|
// never created again. imported is false for a skipped id.
|
|
func (h *Host) ImportLifecycleVolume(spec lifecycle.VolumeSpec) (imported bool, err error) {
|
|
stores := h.Lifecycle()
|
|
if stores == nil {
|
|
return false, fmt.Errorf("cluster volume import requires --lifecycle-store")
|
|
}
|
|
h.workspaceMu.Lock()
|
|
defer h.workspaceMu.Unlock()
|
|
if stores.Deletions.Reserved(spec.VolumeID) {
|
|
return false, nil
|
|
}
|
|
if _, err := stores.Volumes.CreateVolume(spec); err != nil {
|
|
return false, err
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// createVolumeHook is a test seam: it runs in CreateVolume after the
|
|
// reservation check, before the create.
|
|
var createVolumeHook func(vol string)
|
|
|
|
// AbortVolumeDeleting gives a delete's admission back (the delete found it
|
|
// could not proceed and undid its own effects). A committed deletion is
|
|
// never given back (FAILED_PRECONDITION).
|
|
func (s *services) AbortVolumeDeleting(ctx context.Context, req *control.MarkVolumeDeletingRequest) (*control.MarkVolumeDeletingResponse, error) {
|
|
return s.settleVolumeDeleting(ctx, req, "abort", (*lifecycle.DeletionReservationStore).Abort)
|
|
}
|
|
|
|
// CommitVolumeDeleting makes a delete's reservation permanent: the delete
|
|
// is about to take irreversible steps.
|
|
func (s *services) CommitVolumeDeleting(ctx context.Context, req *control.MarkVolumeDeletingRequest) (*control.MarkVolumeDeletingResponse, error) {
|
|
return s.settleVolumeDeleting(ctx, req, "commit", (*lifecycle.DeletionReservationStore).Commit)
|
|
}
|
|
|
|
func (s *services) settleVolumeDeleting(ctx context.Context, req *control.MarkVolumeDeletingRequest, op string,
|
|
apply func(*lifecycle.DeletionReservationStore, string, string) (lifecycle.DeletionReservation, error)) (*control.MarkVolumeDeletingResponse, error) {
|
|
s.host.workspaceMu.Lock()
|
|
defer s.host.workspaceMu.Unlock()
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, workspaceError(err)
|
|
}
|
|
stores := s.host.Lifecycle()
|
|
if stores == nil {
|
|
return nil, status.Error(codes.FailedPrecondition, "lifecycle store is not configured")
|
|
}
|
|
if req.GetVolumeId() == "" || req.GetToken() == "" {
|
|
return nil, status.Error(codes.InvalidArgument, "volume_id and token are required")
|
|
}
|
|
res, err := apply(stores.Deletions, req.GetVolumeId(), req.GetToken())
|
|
switch {
|
|
case errors.Is(err, lifecycle.ErrReservationCommitted), errors.Is(err, lifecycle.ErrReservationToken):
|
|
return nil, status.Errorf(codes.FailedPrecondition, "%s volume deleting: %v", op, err)
|
|
case err != nil:
|
|
return nil, status.Errorf(codes.Unavailable, "%s volume deleting: %v", op, err)
|
|
}
|
|
s.host.log.Printf("blockmaster: %s VolumeDeleting volume=%q state=%s", op, req.GetVolumeId(), res.State)
|
|
state := res.State
|
|
if state == lifecycle.ReservationAdmitted && len(res.Tokens) == 0 {
|
|
state = ""
|
|
}
|
|
return &control.MarkVolumeDeletingResponse{State: state}, nil
|
|
}
|
|
|
|
// liveVolumes is the desired volumes the placement reconciler may plan:
|
|
// every record whose id no delete has reserved.
|
|
func (st *LifecycleStores) liveVolumes() []lifecycle.VolumeRecord {
|
|
all := st.Volumes.ListVolumes()
|
|
out := all[:0:0]
|
|
for _, v := range all {
|
|
if !st.Deletions.Reserved(v.Spec.VolumeID) {
|
|
out = append(out, v)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func (s *services) ExecuteFailback(ctx context.Context, req *control.FailbackRequest) (*control.FailbackResponse, error) {
|
|
if !s.host.cfg.FailbackRuntimeRPC {
|
|
return nil, status.Error(codes.FailedPrecondition, "failback runtime RPC is disabled")
|
|
}
|
|
result, err := s.host.FailbackAuthorityRuntime().ExecuteFailback(ctx, authority.FailbackRuntimeRequest{
|
|
VolumeID: req.GetVolumeId(),
|
|
ReplicaID: req.GetReplicaId(),
|
|
TargetDataAddr: req.GetTargetDataAddr(),
|
|
TargetCtrlAddr: req.GetTargetCtrlAddr(),
|
|
ExpectedCurrentReplicaID: req.GetExpectedCurrentReplicaId(),
|
|
ExpectedCurrentEpoch: req.GetExpectedCurrentEpoch(),
|
|
AckEligible: req.GetAckEligible(),
|
|
FrontendFencedBeforeFailback: req.GetFrontendFencedBeforeFailback(),
|
|
DurableFrontierCovered: req.GetDurableFrontierCovered(),
|
|
NoCrossVolumeIdentityChange: req.GetNoCrossVolumeIdentityChange(),
|
|
EvidenceRefs: append([]string(nil), req.GetEvidenceRefs()...),
|
|
})
|
|
if err != nil {
|
|
return nil, status.Errorf(codes.FailedPrecondition, "execute failback: %v", err)
|
|
}
|
|
return &control.FailbackResponse{
|
|
FailbackStarted: result.FailbackStarted,
|
|
AuthorityEpochAdvanced: result.AuthorityEpochAdvanced,
|
|
SinglePrimaryAfterFailback: result.SinglePrimaryAfterFailback,
|
|
PublishTargetSwappedAfterFailback: result.PublishTargetSwappedAfterFailback,
|
|
NoStorageMutation: result.NoStorageMutation,
|
|
NoCrossVolumeIdentityChange: result.NoCrossVolumeIdentityChange,
|
|
EvidenceRefs: append([]string(nil), result.EvidenceRefs...),
|
|
}, nil
|
|
}
|
|
|
|
// ReportHeartbeat receives a volume's observation. It validates
|
|
// field presence (rejects malformed reports), translates to
|
|
// authority.HeartbeatMessage, and calls the observation host's
|
|
// Ingest path.
|
|
func (s *services) ReportHeartbeat(ctx context.Context, r *control.HeartbeatReport) (*control.HeartbeatAck, error) {
|
|
if err := validateHeartbeat(r); err != nil {
|
|
return nil, err
|
|
}
|
|
msg := heartbeatFromWire(r)
|
|
if err := s.host.ingestHeartbeatRecordingHealth(msg); err != nil {
|
|
return nil, fmt.Errorf("ingest: %w", err)
|
|
}
|
|
return &control.HeartbeatAck{
|
|
AcceptedAt: timestamppb.Now(),
|
|
Sequence: s.seq.Add(1),
|
|
}, nil
|
|
}
|
|
|
|
func lifecycleSpecFromWire(req *control.CreateVolumeRequest) lifecycle.VolumeSpec {
|
|
if req == nil {
|
|
return lifecycle.VolumeSpec{}
|
|
}
|
|
return lifecycle.VolumeSpec{
|
|
VolumeID: req.GetVolumeId(),
|
|
SizeBytes: req.GetSizeBytes(),
|
|
ReplicationFactor: int(req.GetReplicationFactor()),
|
|
Protocol: req.GetProtocol(),
|
|
FrontendTransport: req.GetFrontendTransport(),
|
|
PVCName: req.GetPvcName(),
|
|
PVCNamespace: req.GetPvcNamespace(),
|
|
PVCUID: req.GetPvcUid(),
|
|
PVName: req.GetPvName(),
|
|
}
|
|
}
|
|
|
|
func lifecycleError(op string, err error) error {
|
|
if errors.Is(err, lifecycle.ErrInvalidVolumeSpec) {
|
|
return status.Errorf(codes.InvalidArgument, "%s: %v", op, err)
|
|
}
|
|
if errors.Is(err, lifecycle.ErrVolumeConflict) {
|
|
return status.Errorf(codes.AlreadyExists, "%s: %v", op, err)
|
|
}
|
|
|
|
return status.Errorf(codes.Internal, "%s: %v", op, err)
|
|
}
|
|
|
|
// SubscribeAssignments is VOLUME-SCOPED, not replica-slot-scoped.
|
|
// On subscribe, master opens a publisher subscription on EVERY
|
|
// accepted-topology replica slot for the volume and fans the
|
|
// deliveries into this stream. The subscriber thus sees the CURRENT
|
|
// volume-authority line, regardless of which replica it targets.
|
|
//
|
|
// Why not per-(vol, rid) subscription:
|
|
//
|
|
// After failover r1@1 -> r2@2, the per-(vol, r1) subscriber would
|
|
// receive r1@1 catch-up and nothing after. The volume process
|
|
// holding replica r1 would never learn that the volume's
|
|
// authority has moved to r2. It would keep treating r1@1 as
|
|
// current. When a real data-path frontend later binds, that
|
|
// stale projection becomes a split/out-of-control risk.
|
|
//
|
|
// Request still carries replica_id — it IDENTIFIES the caller for
|
|
// the volume side's "am I still the current replica?" check. The
|
|
// master does not gate subscription on replica_id; it gates on
|
|
// volume_id and fans in ALL replica slots.
|
|
//
|
|
// The volume side (core/host/volume/host.go:streamOnce) filters
|
|
// received facts by self.ReplicaID before calling
|
|
// decodeAssignmentFact: facts for OTHER replicas are recorded as
|
|
// "I've been superseded" events and NOT fed into the local
|
|
// adapter.
|
|
//
|
|
// Dedupe: within one stream, we suppress deliveries whose
|
|
// lexicographic (Epoch, EndpointVersion) is NOT strictly greater
|
|
// than the last pair we sent (epoch-only dedupe would drop valid
|
|
// RefreshEndpoint).
|
|
//
|
|
// RefreshEndpoint is same-epoch by design: it keeps Epoch and
|
|
// bumps EndpointVersion (publisher contract). Epoch-only dedupe
|
|
// would silently drop a legitimate r1@1,EV=2 after r1@1,EV=1 had
|
|
// already been sent — breaking the endpoint refresh route on the
|
|
// product RPC before consumers see it. Lex ordering keeps same-line
|
|
// catch-up and forward-progress safe:
|
|
//
|
|
// - (Epoch, EV) strictly greater than last-sent: send.
|
|
// - equal pair (identical duplicate): drop.
|
|
// - lex smaller pair (stale late fan-in re-delivery): drop —
|
|
// this is the no-reverse guarantee.
|
|
func (s *services) SubscribeAssignments(req *control.SubscribeRequest, stream control.AssignmentService_SubscribeAssignmentsServer) error {
|
|
if req.VolumeId == "" {
|
|
return fmt.Errorf("subscribe: volume_id required")
|
|
}
|
|
pub := s.host.boot.Publisher
|
|
ctx := stream.Context()
|
|
|
|
// Look up accepted topology for this volume to find every
|
|
// replica slot. Subscribe to each. If the volume is not in
|
|
// accepted topology, there is nothing to subscribe to —
|
|
// return an error rather than streaming an empty line set.
|
|
slots := s.host.replicaSlotsFor(req.VolumeId)
|
|
if len(slots) == 0 {
|
|
return fmt.Errorf("subscribe: volume %q not in accepted topology", req.VolumeId)
|
|
}
|
|
|
|
type recv struct {
|
|
ch <-chan adapter.AssignmentInfo
|
|
cancel func()
|
|
}
|
|
recvs := make([]recv, 0, len(slots))
|
|
for _, rid := range slots {
|
|
ch, cancel := pub.Subscribe(req.VolumeId, rid)
|
|
recvs = append(recvs, recv{ch: ch, cancel: cancel})
|
|
}
|
|
defer func() {
|
|
for _, r := range recvs {
|
|
r.cancel()
|
|
}
|
|
}()
|
|
|
|
// Fan-in via one forwarding goroutine per channel, merged
|
|
// into a single ordered send channel.
|
|
merged := make(chan adapter.AssignmentInfo, len(recvs))
|
|
for _, r := range recvs {
|
|
go func(ch <-chan adapter.AssignmentInfo) {
|
|
for v := range ch {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case merged <- v:
|
|
}
|
|
}
|
|
}(r.ch)
|
|
}
|
|
|
|
var (
|
|
lastEpoch uint64
|
|
lastEV uint64
|
|
sentAny bool
|
|
)
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil
|
|
case info, ok := <-merged:
|
|
if !ok {
|
|
return nil
|
|
}
|
|
// Lex dedupe on (Epoch, EndpointVersion). First
|
|
// delivery on a fresh subscription always fires
|
|
// (current-line catch-up). After that, strictly
|
|
// greater pair is required.
|
|
if sentAny {
|
|
cur := info.Epoch
|
|
if cur < lastEpoch {
|
|
continue
|
|
}
|
|
if cur == lastEpoch && info.EndpointVersion <= lastEV {
|
|
continue
|
|
}
|
|
}
|
|
// Generation derivation requires each of
|
|
// Epoch and EndpointVersion to fit in uint32; aliasing at
|
|
// the 2^32 boundary would silently collapse distinct
|
|
// authority lines onto the same generation and break the
|
|
// monotonic stale-replay guard on the volume side. We
|
|
// assert + skip rather than panic: the fact has no valid
|
|
// generation, so we can't send it, but the master should
|
|
// keep running so operators can page + fix upstream.
|
|
if reason, ok := generationFitsUint32(info.Epoch, info.EndpointVersion); !ok {
|
|
fmt.Printf("services: volume %s replica %s: %s; skipping fact\n",
|
|
info.VolumeID, info.ReplicaID, reason)
|
|
continue
|
|
}
|
|
|
|
lastEpoch = info.Epoch
|
|
lastEV = info.EndpointVersion
|
|
sentAny = true
|
|
fact := &control.AssignmentFact{
|
|
VolumeId: info.VolumeID,
|
|
ReplicaId: info.ReplicaID,
|
|
Epoch: info.Epoch,
|
|
EndpointVersion: info.EndpointVersion,
|
|
DataAddr: info.DataAddr,
|
|
CtrlAddr: info.CtrlAddr,
|
|
}
|
|
// Populate the peer set from publisher LastPublished
|
|
// queries for every non-self replica slot in this volume's
|
|
// topology. peer_set_generation is derived
|
|
// from the authoritative line's (Epoch, EndpointVersion)
|
|
// packed into one uint64 as (epoch<<32 | ev). Assertion
|
|
// above guarantees each fits in uint32 so no aliasing.
|
|
// Monotonic across process lifetimes because Epoch/EV are
|
|
// durable authority facts preserved across master restart.
|
|
fact.Peers = s.collectPeers(req.VolumeId, info)
|
|
fact.PeerSetGeneration = (info.Epoch << 32) | info.EndpointVersion
|
|
if err := stream.Send(fact); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// QueryVolumeStatus is a read-only projection surface. Reports the
|
|
// publisher's current authority line plus controller evidence. Mode,
|
|
// probe, and session facts are not returned by this surface.
|
|
func (s *services) QueryVolumeStatus(ctx context.Context, req *control.StatusRequest) (*control.StatusResponse, error) {
|
|
if req.VolumeId == "" {
|
|
return nil, fmt.Errorf("status: volume_id required")
|
|
}
|
|
pub := s.host.boot.Publisher
|
|
ctrl := s.host.ctrl
|
|
|
|
resp := &control.StatusResponse{VolumeId: req.VolumeId}
|
|
|
|
if line, ok := pub.VolumeAuthorityLine(req.VolumeId); ok {
|
|
resp.Assigned = line.Assigned
|
|
resp.ReplicaId = line.ReplicaID
|
|
resp.Epoch = line.Epoch
|
|
resp.EndpointVersion = line.EndpointVersion
|
|
replicaIDs := s.host.replicaSlotsFor(req.VolumeId)
|
|
if len(replicaIDs) == 0 && line.ReplicaID != "" {
|
|
replicaIDs = []string{line.ReplicaID}
|
|
}
|
|
replicaIDs = primaryFirstReplicaIDs(replicaIDs, line.ReplicaID)
|
|
resp.Frontends = statusFrontendsForAssignedVolume(s.host.obs, req.VolumeId, replicaIDs, line.Assigned)
|
|
resp.UnhealthyReplicas = statusUnhealthyReplicas(s.host.obs, req.VolumeId, replicaIDs)
|
|
}
|
|
if ev, ok := ctrl.LastUnsupported(req.VolumeId); ok {
|
|
resp.LastUnsupportedReason = ev.Reason
|
|
}
|
|
if ev, ok := ctrl.LastConvergenceStuck(req.VolumeId); ok {
|
|
resp.LastConvergenceStuckAt = ev.StuckAt.UTC().Format(time.RFC3339Nano)
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
func statusFrontendsForAssignedLine(obs *authority.ObservationHost, volumeID, replicaID string, assigned bool) []*control.FrontendTarget {
|
|
return statusFrontendsForAssignedVolume(obs, volumeID, []string{replicaID}, assigned)
|
|
}
|
|
|
|
func primaryFirstReplicaIDs(replicaIDs []string, primaryReplicaID string) []string {
|
|
if primaryReplicaID == "" || len(replicaIDs) == 0 {
|
|
return replicaIDs
|
|
}
|
|
out := make([]string, 0, len(replicaIDs))
|
|
seenPrimary := false
|
|
for _, replicaID := range replicaIDs {
|
|
if replicaID == primaryReplicaID {
|
|
if !seenPrimary {
|
|
out = append(out, replicaID)
|
|
seenPrimary = true
|
|
}
|
|
continue
|
|
}
|
|
}
|
|
if !seenPrimary {
|
|
out = append(out, primaryReplicaID)
|
|
}
|
|
for _, replicaID := range replicaIDs {
|
|
if replicaID == "" || replicaID == primaryReplicaID {
|
|
continue
|
|
}
|
|
out = append(out, replicaID)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func statusFrontendsForAssignedVolume(obs *authority.ObservationHost, volumeID string, replicaIDs []string, assigned bool) []*control.FrontendTarget {
|
|
if !assigned || obs == nil {
|
|
return nil
|
|
}
|
|
out := make([]*control.FrontendTarget, 0, len(replicaIDs))
|
|
seen := map[string]struct{}{}
|
|
for _, replicaID := range replicaIDs {
|
|
if replicaID == "" {
|
|
continue
|
|
}
|
|
slot, ok := obs.Store().SlotFact(volumeID, replicaID)
|
|
if !ok {
|
|
continue
|
|
}
|
|
for _, ft := range frontendTargetsToWire(slot.Frontends) {
|
|
key := frontendTargetKey(ft)
|
|
if _, ok := seen[key]; ok {
|
|
continue
|
|
}
|
|
seen[key] = struct{}{}
|
|
out = append(out, ft)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func frontendTargetKey(ft *control.FrontendTarget) string {
|
|
if ft == nil {
|
|
return ""
|
|
}
|
|
return fmt.Sprintf("%s|%s|%s|%s|%s|%d|%d", ft.GetProtocol(), normalizedFrontendTransport(ft.GetProtocol(), ft.GetTransport()), ft.GetAddr(), ft.GetIqn(), ft.GetNqn(), ft.GetLun(), ft.GetNsid())
|
|
}
|
|
|
|
func normalizedFrontendTransport(protocol, transport string) string {
|
|
if protocol == "nvme" && transport == "" {
|
|
return "tcp"
|
|
}
|
|
return transport
|
|
}
|
|
|
|
// validateHeartbeat rejects reports that are missing required
|
|
// fields. Malformed reports MUST NOT reach IngestHeartbeat — fail
|
|
// closed at the RPC boundary.
|
|
//
|
|
// Checks:
|
|
// - non-nil report
|
|
// - non-empty ServerID
|
|
// - valid SentAt (not nil, not zero-protobuf)
|
|
// - non-nil slot
|
|
// - non-empty VolumeID / ReplicaID per slot
|
|
// - non-empty DataAddr / CtrlAddr per slot. Every slot carried
|
|
// over the wire represents a locally-hosted replica and MUST
|
|
// name its data and control endpoints. Empty endpoints are
|
|
// observation-layer poison (placement / failover would target
|
|
// a replica with no way to reach it), so we reject at the
|
|
// RPC boundary instead of silently letting the observation
|
|
// layer filter it.
|
|
func validateHeartbeat(r *control.HeartbeatReport) error {
|
|
if r == nil {
|
|
return errors.New("heartbeat: nil report")
|
|
}
|
|
if r.ServerId == "" {
|
|
return errors.New("heartbeat: empty ServerID")
|
|
}
|
|
if r.SentAt == nil || !r.SentAt.IsValid() {
|
|
return errors.New("heartbeat: invalid SentAt")
|
|
}
|
|
seen := make(map[string]int, len(r.Slots))
|
|
for i, s := range r.Slots {
|
|
if s == nil {
|
|
return fmt.Errorf("heartbeat: slot[%d] is nil", i)
|
|
}
|
|
if s.VolumeId == "" || s.ReplicaId == "" {
|
|
return fmt.Errorf("heartbeat: slot[%d] missing VolumeID or ReplicaID", i)
|
|
}
|
|
key := s.VolumeId + "/" + s.ReplicaId
|
|
if j, dup := seen[key]; dup {
|
|
return fmt.Errorf("heartbeat: slot[%d] repeats slot[%d] (%s)", i, j, key)
|
|
}
|
|
seen[key] = i
|
|
if s.DataAddr == "" || s.CtrlAddr == "" {
|
|
return fmt.Errorf("heartbeat: slot[%d] (%s/%s) missing DataAddr or CtrlAddr", i, s.VolumeId, s.ReplicaId)
|
|
}
|
|
for j, ft := range s.Frontends {
|
|
if ft == nil {
|
|
return fmt.Errorf("heartbeat: slot[%d].frontends[%d] is nil", i, j)
|
|
}
|
|
if ft.Protocol == "" || ft.Addr == "" {
|
|
return fmt.Errorf("heartbeat: slot[%d].frontends[%d] missing protocol or addr", i, j)
|
|
}
|
|
switch ft.Protocol {
|
|
case "iscsi":
|
|
if ft.Transport != "" {
|
|
return fmt.Errorf("heartbeat: slot[%d].frontends[%d] iscsi transport must be empty", i, j)
|
|
}
|
|
if ft.Iqn == "" {
|
|
return fmt.Errorf("heartbeat: slot[%d].frontends[%d] iscsi missing iqn", i, j)
|
|
}
|
|
case "nvme":
|
|
if ft.Transport != "" && ft.Transport != "tcp" && ft.Transport != "rdma" {
|
|
return fmt.Errorf("heartbeat: slot[%d].frontends[%d] invalid nvme transport %q", i, j, ft.Transport)
|
|
}
|
|
if ft.Nqn == "" {
|
|
return fmt.Errorf("heartbeat: slot[%d].frontends[%d] nvme missing nqn", i, j)
|
|
}
|
|
default:
|
|
return fmt.Errorf("heartbeat: slot[%d].frontends[%d] unsupported protocol %q", i, j, ft.Protocol)
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// generationFitsUint32 reports whether the (epoch, endpointVersion)
|
|
// pair can be safely packed into one uint64 as `(epoch<<32) | ev`.
|
|
// Returns (reason, false) when either exceeds 2^32-1 — in that case
|
|
// SubscribeAssignments skips the fact rather than silently alias
|
|
// distinct authority lines onto the same peer_set_generation.
|
|
//
|
|
// Paired with TestGenerationFitsUint32_* pins in services_test.go.
|
|
// Named as a pure function so tests can exercise boundary cases
|
|
// without driving a full gRPC stream.
|
|
func generationFitsUint32(epoch, endpointVersion uint64) (reason string, ok bool) {
|
|
const uint32Max = uint64(1) << 32
|
|
if epoch >= uint32Max {
|
|
return fmt.Sprintf("epoch %d exceeds uint32 — peer_set_generation would alias", epoch), false
|
|
}
|
|
if endpointVersion >= uint32Max {
|
|
return fmt.Sprintf("endpoint_version %d exceeds uint32 — peer_set_generation would alias", endpointVersion), false
|
|
}
|
|
return "", true
|
|
}
|
|
|
|
// collectPeers builds the AssignmentFact.peers list for a volume.
|
|
// Topology membership is the allow-list — only declared slot
|
|
// members can appear in peers (never accumulate from arbitrary
|
|
// observed nodes).
|
|
//
|
|
// Per-slot resolution:
|
|
//
|
|
// 1. AUTHORITY path: for slots whose authority is published (the
|
|
// bound replica + any historically-bound replicas with retained
|
|
// records), pull DataAddr/CtrlAddr from `LastPublished` (this
|
|
// is the freshest authoritative addr — refreshed via
|
|
// IntentRefreshEndpoint).
|
|
//
|
|
// 2. OBSERVATION path: for slots that ARE declared in topology
|
|
// but have NO published authority line (the common case for
|
|
// supporting replicas — Publisher.apply only mints for the
|
|
// bound replica), pull DataAddr/CtrlAddr from the observation
|
|
// store's freshest non-expired heartbeat. pub.state stays
|
|
// authority-only; observation does not mint presence records.
|
|
//
|
|
// Both paths stamp peer.Epoch / peer.EndpointVersion with the
|
|
// SUBSCRIBING PRIMARY's lineage, NOT the peer's own. Rationale:
|
|
// the descriptor's Epoch/EV become the live-ship lineage downstream
|
|
// at ReplicaPeer construction; live ships travel under primary's
|
|
// authority. Using a retained historical LastPublished epoch/EV
|
|
// for the peer would mis-stamp frames against the current
|
|
// authority and break the replica's apply gate.
|
|
//
|
|
// Fail-closed cases (slot is skipped, NOT silently substituted):
|
|
// - slot is the self replica (primary doesn't ship to itself)
|
|
// - slot has no published line AND no fresh observation
|
|
// - observation exists but reports empty DataAddr (unusable)
|
|
// - observation exists but is expired (now > ExpiresAt) — stale
|
|
// heartbeat addrs are unsafe for live dial
|
|
//
|
|
// Without this fallback, a cross-host replicated write would hang at
|
|
// fsync barrier because primary's peers list is empty and nothing
|
|
// fans out.
|
|
func (s *services) collectPeers(volumeID string, selfInfo adapter.AssignmentInfo) []*control.ReplicaDescriptor {
|
|
slots := s.host.replicaSlotsFor(volumeID)
|
|
if len(slots) == 0 {
|
|
return nil
|
|
}
|
|
var obsStore *authority.ObservationStore
|
|
if s.host.obs != nil {
|
|
obsStore = s.host.obs.Store()
|
|
}
|
|
return resolvePeers(slots, selfInfo, volumeID, s.host.boot.Publisher, obsStore)
|
|
}
|
|
|
|
// peerLookup is the narrow read interface resolvePeers needs from
|
|
// the publisher. *authority.Publisher satisfies it; tests substitute
|
|
// a stub.
|
|
type peerLookup interface {
|
|
LastPublished(volumeID, replicaID string) (adapter.AssignmentInfo, bool)
|
|
}
|
|
|
|
// peerObservation is the narrow read interface resolvePeers needs
|
|
// from the observation store. *authority.ObservationStore satisfies
|
|
// it; tests substitute a stub. nil is permitted (fail-closed: only
|
|
// path-1 published peers will resolve).
|
|
type peerObservation interface {
|
|
SlotFact(volumeID, replicaID string) (authority.SlotFact, bool)
|
|
}
|
|
|
|
// resolvePeers implements the peer-set construction policy. Pure
|
|
// function; testable without constructing a full Host.
|
|
//
|
|
// LINEAGE RULE: peer descriptor
|
|
// Epoch/EndpointVersion ALWAYS comes from the subscribing primary's
|
|
// own line, regardless of whether the peer's address came from
|
|
// authority publication (path 1) or observation (path 2).
|
|
// Rationale: the peer descriptor is consumed by the primary's
|
|
// ReplicaPeer constructor to stamp live-ship frames; live-ship is
|
|
// always under PRIMARY's lineage. A retained historical
|
|
// LastPublished line for the peer (e.g., r2 was previously bound
|
|
// before reassignment to r1) carries r2's old Epoch/EV — using
|
|
// those as the live-ship lineage would mis-stamp frames against
|
|
// the current authority and the replica's apply gate would reject.
|
|
// Authority/observation supplies ADDRESSES; subscriber lineage
|
|
// stamps EPOCH/EV.
|
|
//
|
|
// PATH RULE: path 1 (authority) is preferred over path 2
|
|
// (observation) when both have data, because authority has the
|
|
// live-current DataAddr/CtrlAddr (refreshed via IntentRefreshEndpoint),
|
|
// whereas observation may show a stale address from before a
|
|
// re-bind.
|
|
func resolvePeers(slots []string, selfInfo adapter.AssignmentInfo, volumeID string, pub peerLookup, obsStore peerObservation) []*control.ReplicaDescriptor {
|
|
peers := make([]*control.ReplicaDescriptor, 0, len(slots))
|
|
for _, rid := range slots {
|
|
if rid == selfInfo.ReplicaID {
|
|
continue
|
|
}
|
|
// Path 1: published authority line — use authority addrs,
|
|
// stamp subscriber lineage.
|
|
if info, ok := pub.LastPublished(volumeID, rid); ok {
|
|
peers = append(peers, &control.ReplicaDescriptor{
|
|
ReplicaId: info.ReplicaID,
|
|
Epoch: selfInfo.Epoch,
|
|
EndpointVersion: selfInfo.EndpointVersion,
|
|
DataAddr: info.DataAddr,
|
|
CtrlAddr: info.CtrlAddr,
|
|
})
|
|
continue
|
|
}
|
|
// Path 2: observation-only — use heartbeat addrs, stamp
|
|
// subscriber lineage. Same rationale as path 1.
|
|
if obsStore != nil {
|
|
if slot, ok := obsStore.SlotFact(volumeID, rid); ok {
|
|
peers = append(peers, &control.ReplicaDescriptor{
|
|
ReplicaId: rid,
|
|
Epoch: selfInfo.Epoch,
|
|
EndpointVersion: selfInfo.EndpointVersion,
|
|
DataAddr: slot.DataAddr,
|
|
CtrlAddr: slot.CtrlAddr,
|
|
})
|
|
continue
|
|
}
|
|
}
|
|
// Else: fail-closed skip. Slot is declared in topology but
|
|
// neither authority nor observation has a usable address yet
|
|
// (cold-start before heartbeat lands, or replica down). The
|
|
// next emission (after the supporting replica heartbeats in)
|
|
// will include it.
|
|
}
|
|
return peers
|
|
}
|
|
|
|
// heartbeatFromWire is the wire -> authority-layer translator.
|
|
// Field-copy only. This is the master-side analog of the volume's
|
|
// decodeAssignmentFact. Unlike the volume decode path, the master
|
|
// side is allowed to import core/authority freely.
|
|
func heartbeatFromWire(r *control.HeartbeatReport) authority.HeartbeatMessage {
|
|
slots := make([]authority.HeartbeatSlot, 0, len(r.Slots))
|
|
for _, s := range r.Slots {
|
|
slots = append(slots, authority.HeartbeatSlot{
|
|
VolumeID: s.VolumeId,
|
|
ReplicaID: s.ReplicaId,
|
|
DataAddr: s.DataAddr,
|
|
CtrlAddr: s.CtrlAddr,
|
|
Frontends: frontendTargetsFromWire(s.Frontends),
|
|
Reachable: s.Reachable,
|
|
ReadyForPrimary: s.ReadyForPrimary,
|
|
Eligible: s.Eligible,
|
|
Withdrawn: s.Withdrawn,
|
|
EvidenceScore: s.EvidenceScore,
|
|
Incarnation: s.Incarnation,
|
|
UnhealthyReason: s.UnhealthyReason,
|
|
LocalRoleClaim: authority.LocalRoleClaim(s.LocalRoleClaim),
|
|
})
|
|
}
|
|
return authority.HeartbeatMessage{
|
|
ServerID: r.ServerId,
|
|
SentAt: r.SentAt.AsTime(),
|
|
Reachable: r.Reachable,
|
|
Eligible: r.Eligible,
|
|
Slots: slots,
|
|
}
|
|
}
|
|
|
|
func frontendTargetsFromWire(in []*control.FrontendTarget) []authority.FrontendTargetFact {
|
|
if len(in) == 0 {
|
|
return nil
|
|
}
|
|
out := make([]authority.FrontendTargetFact, 0, len(in))
|
|
for _, ft := range in {
|
|
if ft == nil {
|
|
continue
|
|
}
|
|
out = append(out, authority.FrontendTargetFact{
|
|
Protocol: ft.Protocol,
|
|
Transport: normalizedFrontendTransport(ft.Protocol, ft.Transport),
|
|
Addr: ft.Addr,
|
|
IQN: ft.Iqn,
|
|
NQN: ft.Nqn,
|
|
LUN: ft.Lun,
|
|
NSID: ft.Nsid,
|
|
})
|
|
}
|
|
return out
|
|
}
|
|
|
|
func frontendTargetsToWire(in []authority.FrontendTargetFact) []*control.FrontendTarget {
|
|
if len(in) == 0 {
|
|
return nil
|
|
}
|
|
out := make([]*control.FrontendTarget, 0, len(in))
|
|
for _, ft := range in {
|
|
out = append(out, &control.FrontendTarget{
|
|
Protocol: ft.Protocol,
|
|
Transport: normalizedFrontendTransport(ft.Protocol, ft.Transport),
|
|
Addr: ft.Addr,
|
|
Iqn: ft.IQN,
|
|
Nqn: ft.NQN,
|
|
Lun: ft.LUN,
|
|
Nsid: ft.NSID,
|
|
})
|
|
}
|
|
return out
|
|
}
|
|
|
|
// statusUnhealthyReplicas lists the replicas whose fresh slot fact
|
|
// carries an explicit unhealthy reason (#128 rule 6).
|
|
func statusUnhealthyReplicas(obs *authority.ObservationHost, volumeID string, replicaIDs []string) []*control.ReplicaUnhealthy {
|
|
if obs == nil {
|
|
return nil
|
|
}
|
|
var out []*control.ReplicaUnhealthy
|
|
for _, replicaID := range replicaIDs {
|
|
if slot, ok := obs.Store().SlotFact(volumeID, replicaID); ok && slot.UnhealthyReason != "" {
|
|
out = append(out, &control.ReplicaUnhealthy{ReplicaId: replicaID, Reason: slot.UnhealthyReason})
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// ingestHeartbeatRecordingHealth ingests a heartbeat and records a
|
|
// replica_unhealthy / replica_unhealthy_cleared event for every slot
|
|
// whose unhealthy reason the accepted report changed (#128 rule 6). The
|
|
// transitions come from the accepted store mutation itself, so a refused
|
|
// report records nothing and expiry never fabricates a clear. healthMu
|
|
// keeps the events in accepted order across concurrent heartbeats.
|
|
func (h *Host) ingestHeartbeatRecordingHealth(msg authority.HeartbeatMessage) error {
|
|
h.healthMu.Lock()
|
|
defer h.healthMu.Unlock()
|
|
transitions, err := h.obs.IngestHeartbeatWithHealthTransitions(msg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, tr := range transitions {
|
|
if tr.New == "" {
|
|
h.events.append(ops.ClusterEvent{
|
|
VolumeID: tr.VolumeID, ReplicaID: tr.ReplicaID,
|
|
Type: "replica_unhealthy_cleared", Severity: "info",
|
|
Message: "replica reported healthy again", OldValue: tr.Old,
|
|
})
|
|
continue
|
|
}
|
|
h.events.append(ops.ClusterEvent{
|
|
VolumeID: tr.VolumeID, ReplicaID: tr.ReplicaID,
|
|
Type: "replica_unhealthy", Severity: "warning",
|
|
Message: "replica reported an explicit unhealthy fact: " + tr.New,
|
|
Reason: tr.New, OldValue: tr.Old, NewValue: tr.New,
|
|
})
|
|
}
|
|
return nil
|
|
}
|