mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
T4b-3: ReplicaPeer.Barrier + Fence wrappers (peer-level seam)
New leaf task introduced by QA round-23 §0-B review. T4b-3 originally
planned for ReplicaPeer.Fence-only; QA's peer-state-mutation locality
check surfaced that DurabilityCoordinator must consume peer.Barrier
(not peer.executor.Barrier) so the error → Invalidate translation
lives at the per-peer execution layer — matching V2's WALShipper-
internal markDegraded locality. T4b-3 now ships both Barrier + Fence
wrappers as a leaf before the T4b-4 DurabilityCoordinator muscle port.
Invariant pinned: INV-REPL-BARRIER-FAILURE-DEGRADES-PEER at the V3
per-peer layer, mirroring T4a-3's Ship → Invalidate translation.
Changes:
core/transport/executor.go
Extract FenceSync(replicaID, lineage) error — synchronous fence
exchange returning a descriptive error on any dial / send /
recv / lineage-mismatch failure. Refactored existing async
doFence to wrap FenceSync + adapt the error to the callback-
based FenceResult. Behavior unchanged for existing async
callers (engine recovery path); T4b-3 peer wrapper gets a
clean sync surface.
core/replication/peer.go
New ReplicaPeer.Barrier(ctx, targetLSN):
- Passes peer.lineage UNCHANGED to executor.Barrier — does
NOT override lineage.TargetLSN with the caller's targetLSN.
Replica's acceptMutationLineage rejects same-authority
messages that change TargetLSN, so override would self-
reject. Caller's targetLSN is coordinator-layer-only
(compared against ack.AchievedLSN for mode arithmetic).
- MUST NOT gate on state == Degraded per V2 "always attempt
BarrierAll" invariant (architect §4.3 note in G-1 doc).
Only post-Close gate applies.
- On transport error: p.Invalidate(reason) + state=Degraded
(INV-REPL-BARRIER-FAILURE-DEGRADES-PEER pin).
New ReplicaPeer.Fence(ctx, lineage):
- Takes caller-supplied lineage (not peer-owned) because
fence semantic is "confirm this specific authority tuple"
— coordinator or recovery path chooses which lineage.
- Delegates to executor.FenceSync.
- Same error → Invalidate translation.
Tests (5 required per QA spec):
- TestReplicaPeer_Barrier_Happy — round-trip against real
ReplicaListener; ack.Lineage == peer.lineage byte-exact;
state remains Healthy
- TestReplicaPeer_Barrier_ErrorPropagates_MarksDegraded —
unreachable replica; Barrier returns error; peer transitions
Healthy → Degraded. INV-REPL-BARRIER-FAILURE-DEGRADES-PEER
pin at peer layer.
- TestReplicaPeer_Fence_Happy — fence exchange at peer.lineage
against real replica; state stays Healthy
- TestReplicaPeer_Barrier_AfterClose_Errors — post-Close call
returns 'closed' error; no panic
- TestReplicaPeer_Fence_AfterClose_Errors — post-Close call
returns 'closed' error; no panic
Verification: full 21-package repo suite green. G-2 tri-line godoc
on Barrier, Fence (Called by / Owns / Borrows) + FenceSync on
executor. G-3 cumulative. Existing T4b-2 Fence tests pass after the
doFence → FenceSync refactor (behavior preserved).
Third task in T4b batch. Next: T4b-4 DurabilityCoordinator (muscle
port; G-1 pre-signed by QA in revised artifact at sw-block/design/
v3-phase-15-t4b-4-g1-v2-read.md) against the peer.Barrier surface.
This commit is contained in:
1 parent
4b16a2e759
commit
1c6dc56107
3 files changed
+267
-46
No files matched your search
@@ -175,6 +175,89 @@ func (p *ReplicaPeer) ShipEntry(ctx context.Context, lineage transport.RecoveryL
|
||||
return nil
|
||||
}
|
||||
|
||||
// Barrier runs a per-peer durability barrier and returns the
|
||||
// replica's validated ack. On any transport / lineage-mismatch /
|
||||
// short-payload error, translates error → peer.Invalidate +
|
||||
// state=Degraded per INV-REPL-BARRIER-FAILURE-DEGRADES-PEER, then
|
||||
// surfaces the error to the caller. This preserves V2's
|
||||
// per-peer-execution-layer locality (V2 WALShipper.Barrier mutated
|
||||
// peer state internally via failBarrier → markDegraded; V3 puts
|
||||
// that mutation at the V2-faithful V3 location: ReplicaPeer.Barrier).
|
||||
//
|
||||
// MUST NOT gate the attempt on p.state == Degraded. The V2 "always
|
||||
// attempt BarrierAll, even when all shippers are Disconnected or
|
||||
// Degraded" invariant applies: an in-flight barrier may succeed
|
||||
// and transition the peer back to a healthy posture; gating it
|
||||
// out would make Degraded a one-way trap. Only the post-Close
|
||||
// gate applies.
|
||||
//
|
||||
// Wire payload: sends MsgBarrierReq with the peer's registered
|
||||
// lineage (unchanged from session registration). The caller-supplied
|
||||
// targetLSN is passed through to BlockExecutor.Barrier but does NOT
|
||||
// overwrite lineage.TargetLSN — replica's acceptMutationLineage
|
||||
// requires same-authority messages to carry the same TargetLSN as
|
||||
// the session was registered with, so overriding would self-reject.
|
||||
// Caller uses targetLSN at the coordinator layer (comparing against
|
||||
// ack.AchievedLSN) to decide per-mode durability arithmetic.
|
||||
//
|
||||
// Called by: DurabilityCoordinator.SyncLocalAndReplicas (T4b-4)
|
||||
// per-peer fan-out.
|
||||
// Owns: the error → Invalidate translation (peer state mutation).
|
||||
// Borrows: ctx + targetLSN from caller; lineage is read from peer's
|
||||
// own registered state.
|
||||
func (p *ReplicaPeer) Barrier(ctx context.Context, targetLSN uint64) (transport.BarrierAck, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return transport.BarrierAck{}, err
|
||||
}
|
||||
p.mu.Lock()
|
||||
if p.closed {
|
||||
p.mu.Unlock()
|
||||
return transport.BarrierAck{}, fmt.Errorf("replication: Barrier: peer %s closed", p.target.ReplicaID)
|
||||
}
|
||||
lineage := p.lineage
|
||||
p.mu.Unlock()
|
||||
|
||||
ack, err := p.executor.Barrier(p.target.ReplicaID, lineage, targetLSN)
|
||||
if err != nil {
|
||||
p.Invalidate(fmt.Sprintf("barrier error: %v", err))
|
||||
return transport.BarrierAck{}, err
|
||||
}
|
||||
return ack, nil
|
||||
}
|
||||
|
||||
// Fence issues a synchronous fence exchange against this peer using
|
||||
// the caller-supplied lineage. Returns nil on success or a
|
||||
// descriptive error on any failure. On error, marks the peer
|
||||
// Degraded via Invalidate (parallel to Barrier).
|
||||
//
|
||||
// Lineage is caller-supplied (not peer-owned) because fence is used
|
||||
// to confirm a SPECIFIC authority tuple was received — typically
|
||||
// when the coordinator wants to verify an old lineage was acked
|
||||
// before advancing. The caller knows which lineage matters; the
|
||||
// peer doesn't drive the choice.
|
||||
//
|
||||
// Called by: DurabilityCoordinator (T4b-4) on quorum-loss recovery;
|
||||
// future T4c recovery pipeline.
|
||||
// Owns: error → Invalidate translation on failure.
|
||||
// Borrows: ctx + lineage from caller.
|
||||
func (p *ReplicaPeer) Fence(ctx context.Context, lineage transport.RecoveryLineage) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
p.mu.Lock()
|
||||
if p.closed {
|
||||
p.mu.Unlock()
|
||||
return fmt.Errorf("replication: Fence: peer %s closed", p.target.ReplicaID)
|
||||
}
|
||||
p.mu.Unlock()
|
||||
|
||||
if err := p.executor.FenceSync(p.target.ReplicaID, lineage); err != nil {
|
||||
p.Invalidate(fmt.Sprintf("fence error: %v", err))
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Invalidate marks the peer Degraded and logs the reason. No state
|
||||
// transitions beyond Healthy ↔ Degraded in T4a; CatchingUp /
|
||||
// Rebuilding / NeedsRebuild arrive at T4c. Calling Invalidate on an
|
||||
|
||||
@@ -228,3 +228,136 @@ func TestReplicaPeer_NewReplicaPeer_RejectsZeroAuthority(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// --- T4b-3 wrapper tests (5 per QA spec) ---
|
||||
|
||||
// TestReplicaPeer_Barrier_Happy — round-trip through a real replica
|
||||
// listener. Peer is Healthy before and after; ack carries the peer's
|
||||
// registered lineage byte-exact.
|
||||
func TestReplicaPeer_Barrier_Happy(t *testing.T) {
|
||||
peer, _, _ := setupPeerWithRealReplica(t)
|
||||
if peer.State() != ReplicaHealthy {
|
||||
t.Fatalf("precondition: expected Healthy, got %s", peer.State())
|
||||
}
|
||||
|
||||
ack, err := peer.Barrier(context.Background(), 42)
|
||||
if err != nil {
|
||||
t.Fatalf("Barrier: %v", err)
|
||||
}
|
||||
if !ack.Success {
|
||||
t.Fatal("ack.Success=false")
|
||||
}
|
||||
// Echo must match the peer's registered lineage (not the caller's
|
||||
// targetLSN — that's coordinator-layer only).
|
||||
if ack.Lineage != peer.lineage {
|
||||
t.Fatalf("ack.Lineage %+v != peer.lineage %+v", ack.Lineage, peer.lineage)
|
||||
}
|
||||
if peer.State() != ReplicaHealthy {
|
||||
t.Fatalf("Barrier success must leave peer Healthy, got %s", peer.State())
|
||||
}
|
||||
}
|
||||
|
||||
// TestReplicaPeer_Barrier_ErrorPropagates_MarksDegraded — the
|
||||
// INV-REPL-BARRIER-FAILURE-DEGRADES-PEER pin. Transport-level
|
||||
// barrier failure must flow through to peer.Invalidate +
|
||||
// state=Degraded (V2-faithful per §0-B locality; preserves V2's
|
||||
// WALShipper-internal markDegraded contract at the V3 peer wrapper
|
||||
// layer).
|
||||
func TestReplicaPeer_Barrier_ErrorPropagates_MarksDegraded(t *testing.T) {
|
||||
// Reserve a port then release it → guaranteed unreachable.
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
deadAddr := ln.Addr().String()
|
||||
_ = ln.Close()
|
||||
|
||||
primary := storage.NewBlockStore(64, 4096)
|
||||
exec := transport.NewBlockExecutor(primary, deadAddr)
|
||||
target := ReplicaTarget{
|
||||
ReplicaID: "r-dead-barrier",
|
||||
DataAddr: deadAddr,
|
||||
ControlAddr: deadAddr,
|
||||
Epoch: 4,
|
||||
EndpointVersion: 2,
|
||||
}
|
||||
peer, err := NewReplicaPeer(target, exec)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = peer.Close() })
|
||||
|
||||
if peer.State() != ReplicaHealthy {
|
||||
t.Fatalf("precondition: Healthy, got %s", peer.State())
|
||||
}
|
||||
|
||||
ack, err := peer.Barrier(context.Background(), 100)
|
||||
if err == nil {
|
||||
t.Fatal("Barrier against unreachable replica should error")
|
||||
}
|
||||
if ack.Success {
|
||||
t.Fatal("ack.Success should be false on error path")
|
||||
}
|
||||
if peer.State() != ReplicaDegraded {
|
||||
t.Fatalf("expected ReplicaDegraded after Barrier failure, got %s", peer.State())
|
||||
}
|
||||
// Error shape: should contain "barrier" and the underlying
|
||||
// transport / dial failure signal.
|
||||
if !strings.Contains(err.Error(), "barrier") {
|
||||
t.Fatalf("expected 'barrier' in error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestReplicaPeer_Fence_Happy — fence exchange at caller-supplied
|
||||
// lineage against a real replica. Peer stays Healthy on success.
|
||||
func TestReplicaPeer_Fence_Happy(t *testing.T) {
|
||||
peer, _, _ := setupPeerWithRealReplica(t)
|
||||
|
||||
// Use the peer's registered lineage so the replica's
|
||||
// acceptMutationLineage rule passes (same-authority +
|
||||
// same TargetLSN).
|
||||
fenceLineage := peer.lineage
|
||||
|
||||
if err := peer.Fence(context.Background(), fenceLineage); err != nil {
|
||||
t.Fatalf("Fence: %v", err)
|
||||
}
|
||||
if peer.State() != ReplicaHealthy {
|
||||
t.Fatalf("Fence success must leave peer Healthy, got %s", peer.State())
|
||||
}
|
||||
}
|
||||
|
||||
// TestReplicaPeer_Barrier_AfterClose_Errors — post-Close Barrier
|
||||
// returns error without panic. No state mutation (peer is already
|
||||
// Unknown after Close).
|
||||
func TestReplicaPeer_Barrier_AfterClose_Errors(t *testing.T) {
|
||||
peer, _, _ := setupPeerWithRealReplica(t)
|
||||
if err := peer.Close(); err != nil {
|
||||
t.Fatalf("Close: %v", err)
|
||||
}
|
||||
|
||||
_, err := peer.Barrier(context.Background(), 1)
|
||||
if err == nil {
|
||||
t.Fatal("Barrier on closed peer must error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "closed") {
|
||||
t.Fatalf("expected 'closed' in error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestReplicaPeer_Fence_AfterClose_Errors — post-Close Fence returns
|
||||
// error without panic.
|
||||
func TestReplicaPeer_Fence_AfterClose_Errors(t *testing.T) {
|
||||
peer, _, _ := setupPeerWithRealReplica(t)
|
||||
fenceLineage := peer.lineage
|
||||
if err := peer.Close(); err != nil {
|
||||
t.Fatalf("Close: %v", err)
|
||||
}
|
||||
|
||||
err := peer.Fence(context.Background(), fenceLineage)
|
||||
if err == nil {
|
||||
t.Fatal("Fence on closed peer must error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "closed") {
|
||||
t.Fatalf("expected 'closed' in error, got: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -97,6 +97,53 @@ func (e *BlockExecutor) Fence(replicaID string, sessionID, epoch, endpointVersio
|
||||
// See Fence() godoc for why this is non-zero.
|
||||
const fenceSentinelTargetLSN uint64 = 1
|
||||
|
||||
// FenceSync performs a synchronous fence exchange against the replica:
|
||||
// dial a fresh conn, send MsgBarrierReq at the given lineage, read
|
||||
// MsgBarrierResp, validate the full-lineage echo, and return. Returns
|
||||
// nil on success or a descriptive error on any dial / send / recv /
|
||||
// lineage-mismatch failure.
|
||||
//
|
||||
// Does NOT go through the session registry — fence is a single barrier
|
||||
// exchange, not a recovery session. Does NOT fire OnFenceComplete; the
|
||||
// async entry point Fence() wraps FenceSync and handles the callback.
|
||||
//
|
||||
// Called by: BlockExecutor.doFence (async wrapper for legacy callback-
|
||||
// driven callers like the engine's recovery path) and
|
||||
// ReplicaPeer.Fence (T4b-3 wrapper for sync-style callers like
|
||||
// DurabilityCoordinator).
|
||||
// Owns: per-call conn dial + deadline; lineage validation against the
|
||||
// request lineage (round-21 uniform rule).
|
||||
// Borrows: lineage from caller.
|
||||
func (e *BlockExecutor) FenceSync(replicaID string, lineage RecoveryLineage) error {
|
||||
conn, err := net.DialTimeout("tcp", e.replicaAddr, 2*time.Second)
|
||||
if err != nil {
|
||||
return fmt.Errorf("fence dial: %w", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
if err := sendBarrierReq(conn, lineage, recoveryConnTimeout); err != nil {
|
||||
return fmt.Errorf("fence barrier send: %w", err)
|
||||
}
|
||||
resp, err := recvBarrierResp(conn, recoveryConnTimeout)
|
||||
if err != nil {
|
||||
// Short / zero-valued / field-order-malformed payloads are
|
||||
// rejected at the T4b-1 decode layer as errors; they surface
|
||||
// here without silent-accept (architect round 22 uniform rule
|
||||
// at the first ack-consuming surface).
|
||||
return fmt.Errorf("fence barrier resp: %w", err)
|
||||
}
|
||||
// T4b-2 round-22 lineage validation: the replica's echoed lineage
|
||||
// MUST match the lineage we sent.
|
||||
if resp.Lineage != lineage {
|
||||
log.Printf("executor: fence lineage mismatch replica=%s expected=%+v actual=%+v",
|
||||
replicaID, lineage, resp.Lineage)
|
||||
return fmt.Errorf("fence barrier resp lineage mismatch: expected=%+v actual=%+v",
|
||||
lineage, resp.Lineage)
|
||||
}
|
||||
log.Printf("executor: fence complete replica=%s epoch=%d", replicaID, lineage.Epoch)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *BlockExecutor) doFence(replicaID string, lineage RecoveryLineage) {
|
||||
result := adapter.FenceResult{
|
||||
ReplicaID: replicaID,
|
||||
@@ -104,54 +151,12 @@ func (e *BlockExecutor) doFence(replicaID string, lineage RecoveryLineage) {
|
||||
Epoch: lineage.Epoch,
|
||||
EndpointVersion: lineage.EndpointVersion,
|
||||
}
|
||||
|
||||
conn, err := net.DialTimeout("tcp", e.replicaAddr, 2*time.Second)
|
||||
if err != nil {
|
||||
if err := e.FenceSync(replicaID, lineage); err != nil {
|
||||
result.Success = false
|
||||
result.FailReason = fmt.Sprintf("fence dial: %v", err)
|
||||
e.fireFenceComplete(result)
|
||||
return
|
||||
result.FailReason = err.Error()
|
||||
} else {
|
||||
result.Success = true
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
if err := sendBarrierReq(conn, lineage, recoveryConnTimeout); err != nil {
|
||||
result.Success = false
|
||||
result.FailReason = fmt.Sprintf("fence barrier send: %v", err)
|
||||
e.fireFenceComplete(result)
|
||||
return
|
||||
}
|
||||
resp, err := recvBarrierResp(conn, recoveryConnTimeout)
|
||||
if err != nil {
|
||||
// Short / zero-valued / field-order-malformed payloads are
|
||||
// rejected at the T4b-1 decode layer as errors, so they
|
||||
// surface here and fail the fence — NOT a silent accept.
|
||||
// Covers the uniform-rule failure class at the first
|
||||
// existing consumer of the extended BarrierResponse wire
|
||||
// (architect round 22).
|
||||
result.Success = false
|
||||
result.FailReason = fmt.Sprintf("fence barrier resp: %v", err)
|
||||
e.fireFenceComplete(result)
|
||||
return
|
||||
}
|
||||
// T4b-2 round-22 lineage validation: the replica's echoed lineage
|
||||
// MUST match the lineage we sent. This is the "valid-decode but
|
||||
// wrong-session" case — decode was fine, but the ack is for a
|
||||
// different authority tuple than the one we're awaiting.
|
||||
// AchievedLSN is still not semantically meaningful for fence;
|
||||
// the validation is purely about confirming the replica acked
|
||||
// OUR request, not some concurrent / stale one.
|
||||
if resp.Lineage != lineage {
|
||||
result.Success = false
|
||||
result.FailReason = fmt.Sprintf("fence barrier resp lineage mismatch: expected=%+v actual=%+v",
|
||||
lineage, resp.Lineage)
|
||||
log.Printf("executor: fence lineage mismatch replica=%s expected=%+v actual=%+v",
|
||||
replicaID, lineage, resp.Lineage)
|
||||
e.fireFenceComplete(result)
|
||||
return
|
||||
}
|
||||
|
||||
result.Success = true
|
||||
log.Printf("executor: fence complete replica=%s epoch=%d", replicaID, lineage.Epoch)
|
||||
e.fireFenceComplete(result)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user