mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-05 10:37:23 +01:00
1646 lines
48 KiB
Go
1646 lines
48 KiB
Go
package adapter
|
|
|
|
// Completion oracle: recover(a,b) band — NOT recover(a) closure.
|
|
// See sw-block/design/recover-semantics-adjustment-plan.md §8.1.
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"sync"
|
|
"sync/atomic"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/seaweedfs/seaweed-block/core/engine"
|
|
)
|
|
|
|
// mockExecutor records commands and wires the session close callback.
|
|
type mockExecutor struct {
|
|
mu sync.Mutex
|
|
probeResults map[string]ProbeResult
|
|
commands []string
|
|
nextSession atomic.Uint64
|
|
onSessionStart OnSessionStart
|
|
onSessionClose OnSessionClose // wired by adapter
|
|
onDurableAck OnDurableAck
|
|
onFenceComplete OnFenceComplete
|
|
autoStart bool
|
|
autoClose bool
|
|
autoFence bool // fire OnFenceComplete(Success=true) on Fence()
|
|
catchUpErr error
|
|
rebuildErr error
|
|
fenceErr error
|
|
starts []startRecord
|
|
fences []fenceRecord
|
|
probeCalls []probeRecord // T4c-1: captures sessionID adapter mints for ProbeReplica
|
|
}
|
|
|
|
type probeRecord struct {
|
|
replicaID string
|
|
sessionID uint64
|
|
epoch uint64
|
|
endpointVersion uint64
|
|
}
|
|
|
|
type fenceRecord struct {
|
|
replicaID string
|
|
sessionID uint64
|
|
epoch uint64
|
|
endpointVersion uint64
|
|
}
|
|
|
|
type startRecord struct {
|
|
kind string
|
|
sessionID uint64
|
|
fromLSN uint64
|
|
targetLSN uint64
|
|
}
|
|
|
|
func newMockExecutor() *mockExecutor {
|
|
m := &mockExecutor{
|
|
probeResults: make(map[string]ProbeResult),
|
|
autoStart: true,
|
|
autoClose: true,
|
|
autoFence: true,
|
|
}
|
|
m.nextSession.Store(100)
|
|
return m
|
|
}
|
|
|
|
func (m *mockExecutor) SetOnSessionStart(fn OnSessionStart) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.onSessionStart = fn
|
|
}
|
|
|
|
func (m *mockExecutor) SetOnSessionClose(fn OnSessionClose) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.onSessionClose = fn
|
|
}
|
|
|
|
func (m *mockExecutor) SetOnDurableAck(fn OnDurableAck) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.onDurableAck = fn
|
|
}
|
|
|
|
func (m *mockExecutor) SetOnFenceComplete(fn OnFenceComplete) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.onFenceComplete = fn
|
|
}
|
|
|
|
func (m *mockExecutor) Fence(replicaID string, sessionID, epoch, endpointVersion uint64) error {
|
|
m.mu.Lock()
|
|
m.commands = append(m.commands, "Fence")
|
|
m.fences = append(m.fences, fenceRecord{
|
|
replicaID: replicaID, sessionID: sessionID,
|
|
epoch: epoch, endpointVersion: endpointVersion,
|
|
})
|
|
cb := m.onFenceComplete
|
|
auto := m.autoFence
|
|
errVal := m.fenceErr
|
|
m.mu.Unlock()
|
|
|
|
if errVal != nil {
|
|
return errVal
|
|
}
|
|
if auto && cb != nil {
|
|
// Sync callback: deterministic mock — no goroutine. Real
|
|
// transport is async; the unit-test mock fires inline so
|
|
// tests can read projection without arbitrary sleeps.
|
|
cb(FenceResult{
|
|
ReplicaID: replicaID,
|
|
SessionID: sessionID,
|
|
Epoch: epoch,
|
|
EndpointVersion: endpointVersion,
|
|
Success: true,
|
|
})
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// fireFence triggers the registered fence callback manually — used
|
|
// by tests that need to control fence outcome timing.
|
|
func (m *mockExecutor) fireFence(result FenceResult) {
|
|
m.mu.Lock()
|
|
cb := m.onFenceComplete
|
|
m.mu.Unlock()
|
|
if cb != nil {
|
|
cb(result)
|
|
}
|
|
}
|
|
|
|
func (m *mockExecutor) Probe(replicaID, dataAddr, ctrlAddr string, sessionID, epoch, endpointVersion uint64) ProbeResult {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.commands = append(m.commands, "Probe")
|
|
m.probeCalls = append(m.probeCalls, probeRecord{
|
|
replicaID: replicaID,
|
|
sessionID: sessionID,
|
|
epoch: epoch,
|
|
endpointVersion: endpointVersion,
|
|
})
|
|
if r, ok := m.probeResults[replicaID]; ok {
|
|
return r
|
|
}
|
|
return ProbeResult{
|
|
ReplicaID: replicaID,
|
|
Success: false,
|
|
EndpointVersion: endpointVersion,
|
|
TransportEpoch: epoch,
|
|
FailReason: "no mock result",
|
|
}
|
|
}
|
|
|
|
func (m *mockExecutor) StartCatchUp(replicaID string, sessionID, epoch, endpointVersion, fromLSN, targetLSN uint64) error {
|
|
m.mu.Lock()
|
|
m.commands = append(m.commands, "StartCatchUp")
|
|
startCb := m.onSessionStart
|
|
cb := m.onSessionClose
|
|
autoStart := m.autoStart
|
|
autoClose := m.autoClose
|
|
startErr := m.catchUpErr
|
|
m.starts = append(m.starts, startRecord{
|
|
kind: "StartCatchUp", sessionID: sessionID,
|
|
fromLSN: fromLSN, targetLSN: targetLSN,
|
|
})
|
|
m.mu.Unlock()
|
|
|
|
if startErr != nil {
|
|
return startErr
|
|
}
|
|
|
|
if autoStart && startCb != nil {
|
|
startCb(SessionStartResult{ReplicaID: replicaID, SessionID: sessionID})
|
|
}
|
|
|
|
// Simulate async completion: session succeeds after a short delay.
|
|
// Uses the adapter-assigned sessionID so the engine accepts the close.
|
|
if autoClose {
|
|
go func() {
|
|
time.Sleep(10 * time.Millisecond)
|
|
if cb != nil {
|
|
cb(SessionCloseResult{
|
|
ReplicaID: replicaID,
|
|
SessionID: sessionID,
|
|
Success: true,
|
|
AchievedLSN: targetLSN,
|
|
})
|
|
}
|
|
}()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *mockExecutor) StartRebuild(replicaID string, sessionID, epoch, endpointVersion, targetLSN uint64) error {
|
|
m.mu.Lock()
|
|
m.commands = append(m.commands, "StartRebuild")
|
|
startCb := m.onSessionStart
|
|
cb := m.onSessionClose
|
|
autoStart := m.autoStart
|
|
autoClose := m.autoClose
|
|
startErr := m.rebuildErr
|
|
m.starts = append(m.starts, startRecord{
|
|
kind: "StartRebuild", sessionID: sessionID,
|
|
targetLSN: targetLSN,
|
|
})
|
|
m.mu.Unlock()
|
|
|
|
if startErr != nil {
|
|
return startErr
|
|
}
|
|
|
|
if autoStart && startCb != nil {
|
|
startCb(SessionStartResult{ReplicaID: replicaID, SessionID: sessionID})
|
|
}
|
|
|
|
if autoClose {
|
|
go func() {
|
|
time.Sleep(10 * time.Millisecond)
|
|
if cb != nil {
|
|
cb(SessionCloseResult{
|
|
ReplicaID: replicaID,
|
|
SessionID: sessionID,
|
|
Success: true,
|
|
AchievedLSN: targetLSN,
|
|
})
|
|
}
|
|
}()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *mockExecutor) StartRecoverySession(
|
|
replicaID string,
|
|
sessionID, epoch, endpointVersion, targetLSN uint64,
|
|
contentKind engine.RecoveryContentKind,
|
|
policy engine.RecoveryRuntimePolicy,
|
|
) error {
|
|
if contentKind == engine.RecoveryContentFullExtent {
|
|
return m.StartRebuild(replicaID, sessionID, epoch, endpointVersion, targetLSN)
|
|
}
|
|
// T4d-3: bridge passes fromLSN=1 (StartRecoverySession doesn't
|
|
// carry fromLSN today; same convention as transport bridge).
|
|
return m.StartCatchUp(replicaID, sessionID, epoch, endpointVersion, 1, targetLSN)
|
|
}
|
|
|
|
func (m *mockExecutor) InvalidateSession(replicaID string, sessionID uint64, reason string) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.commands = append(m.commands, "InvalidateSession")
|
|
}
|
|
|
|
func (m *mockExecutor) PublishHealthy(replicaID string) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.commands = append(m.commands, "PublishHealthy")
|
|
}
|
|
|
|
func (m *mockExecutor) PublishDegraded(replicaID string, reason string) {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
m.commands = append(m.commands, "PublishDegraded")
|
|
}
|
|
|
|
func (m *mockExecutor) getCommands() []string {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
cp := make([]string, len(m.commands))
|
|
copy(cp, m.commands)
|
|
return cp
|
|
}
|
|
|
|
func (m *mockExecutor) latestSessionID(kind string) uint64 {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
for i := len(m.starts) - 1; i >= 0; i-- {
|
|
if m.starts[i].kind == kind {
|
|
return m.starts[i].sessionID
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func (m *mockExecutor) fireClose(result SessionCloseResult) {
|
|
m.mu.Lock()
|
|
cb := m.onSessionClose
|
|
m.mu.Unlock()
|
|
if cb != nil {
|
|
cb(result)
|
|
}
|
|
}
|
|
|
|
func (m *mockExecutor) fireStart(result SessionStartResult) {
|
|
m.mu.Lock()
|
|
cb := m.onSessionStart
|
|
m.mu.Unlock()
|
|
if cb != nil {
|
|
cb(result)
|
|
}
|
|
}
|
|
|
|
// ============================================================
|
|
// Phase 03 Constraint Tests
|
|
// ============================================================
|
|
|
|
// --- C8: Same facts must produce same decision ---
|
|
|
|
func TestC8_SameFactsSameDecision(t *testing.T) {
|
|
// Two adapters get the same facts. Both must reach the same decision.
|
|
exec1 := newMockExecutor()
|
|
exec2 := newMockExecutor()
|
|
a1 := NewVolumeReplicaAdapter(exec1)
|
|
a2 := NewVolumeReplicaAdapter(exec2)
|
|
|
|
assign := AssignmentInfo{
|
|
VolumeID: "vol1", ReplicaID: "r1",
|
|
Epoch: 1, EndpointVersion: 1,
|
|
DataAddr: "10.0.0.2:9333", CtrlAddr: "10.0.0.2:9334",
|
|
}
|
|
probe := ProbeResult{
|
|
ReplicaID: "r1", Success: true,
|
|
EndpointVersion: 1, TransportEpoch: 1,
|
|
ReplicaFlushedLSN: 50, PrimaryTailLSN: 10, PrimaryHeadLSN: 100,
|
|
}
|
|
|
|
a1.OnAssignment(assign)
|
|
a1.OnProbeResult(probe)
|
|
|
|
a2.OnAssignment(assign)
|
|
a2.OnProbeResult(probe)
|
|
|
|
p1 := a1.Projection()
|
|
p2 := a2.Projection()
|
|
|
|
if p1.RecoveryDecision != p2.RecoveryDecision {
|
|
t.Fatalf("C8: decision1=%s decision2=%s", p1.RecoveryDecision, p2.RecoveryDecision)
|
|
}
|
|
if p1.RecoveryDecision != engine.DecisionCatchUp {
|
|
t.Fatalf("C8: expected catch_up, got %s", p1.RecoveryDecision)
|
|
}
|
|
}
|
|
|
|
func TestAdapter_DurableAckCallback_ReachesEngineProgressFact(t *testing.T) {
|
|
exec := newMockExecutor()
|
|
a := NewVolumeReplicaAdapter(exec)
|
|
a.OnAssignment(AssignmentInfo{
|
|
VolumeID: "v1",
|
|
ReplicaID: "r1",
|
|
Epoch: 1,
|
|
EndpointVersion: 1,
|
|
DataAddr: "data",
|
|
CtrlAddr: "ctrl",
|
|
})
|
|
|
|
exec.mu.Lock()
|
|
cb := exec.onDurableAck
|
|
exec.mu.Unlock()
|
|
if cb == nil {
|
|
t.Fatal("NewVolumeReplicaAdapter did not wire optional durable ack callback")
|
|
}
|
|
cb(DurableAckResult{
|
|
ReplicaID: "r1",
|
|
SessionID: 7,
|
|
DurableLSN: 11,
|
|
PrimaryTailLSN: 5,
|
|
PrimaryHeadLSN: 20,
|
|
})
|
|
|
|
a.mu.Lock()
|
|
defer a.mu.Unlock()
|
|
if !a.state.Recovery.DurableAckKnown || a.state.Recovery.DurableAckR != 11 {
|
|
t.Fatalf("engine DurableAck=(%d known=%v), want (11 true)",
|
|
a.state.Recovery.DurableAckR, a.state.Recovery.DurableAckKnown)
|
|
}
|
|
}
|
|
|
|
func TestAdapter_DurableAckStall_StartsCatchUp(t *testing.T) {
|
|
exec := newMockExecutor()
|
|
exec.autoStart = false
|
|
exec.autoClose = false
|
|
a := newHealthyAdapterForDurableAck(t, exec)
|
|
|
|
cb := durableAckCallback(t, exec)
|
|
cb(DurableAckResult{ReplicaID: "r1", DurableLSN: 10, PrimaryTailLSN: 5, PrimaryHeadLSN: 20})
|
|
cb(DurableAckResult{ReplicaID: "r1", DurableLSN: 10, PrimaryTailLSN: 5, PrimaryHeadLSN: 25})
|
|
if hasAdapterCommand(a, "StartCatchUp") {
|
|
t.Fatal("durable ack lag must not start catch-up before the stalled evidence window closes")
|
|
}
|
|
|
|
cb(DurableAckResult{ReplicaID: "r1", DurableLSN: 10, PrimaryTailLSN: 5, PrimaryHeadLSN: 30})
|
|
|
|
start := latestStartRecord(t, exec, "StartCatchUp")
|
|
if start.fromLSN != 11 {
|
|
t.Fatalf("StartCatchUp.fromLSN=%d want durable ack R+1=11", start.fromLSN)
|
|
}
|
|
if start.targetLSN != 30 {
|
|
t.Fatalf("StartCatchUp frontier arg=%d want primary head 30", start.targetLSN)
|
|
}
|
|
}
|
|
|
|
func TestAdapter_DurableAckProgressingLag_DoesNotStartRecovery(t *testing.T) {
|
|
exec := newMockExecutor()
|
|
exec.autoStart = false
|
|
exec.autoClose = false
|
|
a := newHealthyAdapterForDurableAck(t, exec)
|
|
|
|
cb := durableAckCallback(t, exec)
|
|
cb(DurableAckResult{ReplicaID: "r1", DurableLSN: 10, PrimaryTailLSN: 5, PrimaryHeadLSN: 20})
|
|
cb(DurableAckResult{ReplicaID: "r1", DurableLSN: 11, PrimaryTailLSN: 5, PrimaryHeadLSN: 40})
|
|
cb(DurableAckResult{ReplicaID: "r1", DurableLSN: 12, PrimaryTailLSN: 5, PrimaryHeadLSN: 80})
|
|
|
|
if hasAdapterCommand(a, "StartCatchUp") || hasAdapterCommand(a, "StartRebuild") {
|
|
t.Fatalf("progressing lag must remain feeder-owned; commands=%v", a.CommandLog())
|
|
}
|
|
}
|
|
|
|
func TestAdapter_DurableAckBelowRetention_StartsRebuild(t *testing.T) {
|
|
exec := newMockExecutor()
|
|
exec.autoStart = false
|
|
exec.autoClose = false
|
|
_ = newHealthyAdapterForDurableAck(t, exec)
|
|
|
|
cb := durableAckCallback(t, exec)
|
|
cb(DurableAckResult{ReplicaID: "r1", DurableLSN: 4, PrimaryTailLSN: 5, PrimaryHeadLSN: 30})
|
|
|
|
start := latestStartRecord(t, exec, "StartRebuild")
|
|
if start.targetLSN != 30 {
|
|
t.Fatalf("StartRebuild frontier arg=%d want primary head 30", start.targetLSN)
|
|
}
|
|
}
|
|
|
|
func TestAdapter_PinUnderRetentionClose_DoesNotRetrySameLineage(t *testing.T) {
|
|
exec := newMockExecutor()
|
|
exec.autoClose = false
|
|
exec.probeResults["r1"] = ProbeResult{
|
|
ReplicaID: "r1",
|
|
Success: true,
|
|
EndpointVersion: 1,
|
|
TransportEpoch: 1,
|
|
ReplicaFlushedLSN: 10,
|
|
PrimaryTailLSN: 5,
|
|
PrimaryHeadLSN: 30,
|
|
}
|
|
a := NewVolumeReplicaAdapter(exec)
|
|
a.OnAssignment(AssignmentInfo{
|
|
VolumeID: "v1",
|
|
ReplicaID: "r1",
|
|
Epoch: 1,
|
|
EndpointVersion: 1,
|
|
DataAddr: "data",
|
|
CtrlAddr: "ctrl",
|
|
})
|
|
|
|
sessionID := waitForLatestSessionID(t, exec, "StartCatchUp")
|
|
beforeStarts := countStarts(exec, "StartCatchUp") + countStarts(exec, "StartRebuild")
|
|
|
|
exec.fireClose(SessionCloseResult{
|
|
ReplicaID: "r1",
|
|
SessionID: sessionID,
|
|
Success: false,
|
|
FailureKind: engine.RecoveryFailurePinUnderRetention,
|
|
FailReason: "pin under retention: durable ack below primary S",
|
|
})
|
|
|
|
afterStarts := countStarts(exec, "StartCatchUp") + countStarts(exec, "StartRebuild")
|
|
if afterStarts != beforeStarts {
|
|
t.Fatalf("PinUnderRetention retried same lineage: starts before=%d after=%d log=%v",
|
|
beforeStarts, afterStarts, exec.getCommands())
|
|
}
|
|
if !hasAdapterCommand(a, "PublishDegraded") {
|
|
t.Fatalf("PinUnderRetention must publish degraded; commands=%v", a.CommandLog())
|
|
}
|
|
if got := a.Projection().Mode; got == engine.ModeHealthy {
|
|
t.Fatalf("PinUnderRetention must not leave replica healthy")
|
|
}
|
|
}
|
|
|
|
func TestAdapter_StartCatchUp_UsesFrontierHintOverLegacyTarget(t *testing.T) {
|
|
exec := newMockExecutor()
|
|
exec.autoStart = false
|
|
exec.autoClose = false
|
|
a := NewVolumeReplicaAdapter(exec)
|
|
|
|
cmd := engine.StartCatchUp{
|
|
ReplicaID: "r1",
|
|
Epoch: 1,
|
|
EndpointVersion: 1,
|
|
FromLSN: 5,
|
|
FrontierHint: 123,
|
|
TargetLSN: 999,
|
|
}
|
|
events, queued := a.prepareQueuedCommands([]engine.Command{cmd})
|
|
if len(events) != 1 || len(queued) != 1 {
|
|
t.Fatalf("prepareQueuedCommands produced events=%d queued=%d, want 1/1", len(events), len(queued))
|
|
}
|
|
prepared, ok := events[0].(engine.SessionPrepared)
|
|
if !ok {
|
|
t.Fatalf("event type = %T, want engine.SessionPrepared", events[0])
|
|
}
|
|
if prepared.FrontierHint != 123 || prepared.TargetLSN != 123 {
|
|
t.Fatalf("SessionPrepared hints = frontier:%d legacy:%d, want 123/123",
|
|
prepared.FrontierHint, prepared.TargetLSN)
|
|
}
|
|
|
|
a.executeCommand(queued[0])
|
|
exec.mu.Lock()
|
|
defer exec.mu.Unlock()
|
|
if len(exec.starts) != 1 {
|
|
t.Fatalf("executor starts=%d, want 1", len(exec.starts))
|
|
}
|
|
got := exec.starts[0]
|
|
if got.fromLSN != 5 {
|
|
t.Fatalf("executor fromLSN=%d, want 5", got.fromLSN)
|
|
}
|
|
if got.targetLSN != 123 {
|
|
t.Fatalf("executor legacy target arg=%d, want FrontierHint 123", got.targetLSN)
|
|
}
|
|
}
|
|
|
|
func newHealthyAdapterForDurableAck(t *testing.T, exec *mockExecutor) *VolumeReplicaAdapter {
|
|
t.Helper()
|
|
exec.probeResults["r1"] = ProbeResult{
|
|
ReplicaID: "r1",
|
|
Success: true,
|
|
EndpointVersion: 1,
|
|
TransportEpoch: 1,
|
|
ReplicaFlushedLSN: 20,
|
|
PrimaryTailLSN: 5,
|
|
PrimaryHeadLSN: 20,
|
|
}
|
|
a := NewVolumeReplicaAdapter(exec)
|
|
a.OnAssignment(AssignmentInfo{
|
|
VolumeID: "v1",
|
|
ReplicaID: "r1",
|
|
Epoch: 1,
|
|
EndpointVersion: 1,
|
|
DataAddr: "data",
|
|
CtrlAddr: "ctrl",
|
|
})
|
|
deadline := time.Now().Add(2 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
if a.Projection().Mode == engine.ModeHealthy {
|
|
return a
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
t.Fatalf("adapter did not reach healthy before durable ack test; commands=%v trace=%v",
|
|
a.CommandLog(), a.Trace())
|
|
return nil
|
|
}
|
|
|
|
func durableAckCallback(t *testing.T, exec *mockExecutor) OnDurableAck {
|
|
t.Helper()
|
|
exec.mu.Lock()
|
|
defer exec.mu.Unlock()
|
|
if exec.onDurableAck == nil {
|
|
t.Fatal("durable ack callback was not wired")
|
|
}
|
|
return exec.onDurableAck
|
|
}
|
|
|
|
func latestStartRecord(t *testing.T, exec *mockExecutor, kind string) startRecord {
|
|
t.Helper()
|
|
exec.mu.Lock()
|
|
defer exec.mu.Unlock()
|
|
for i := len(exec.starts) - 1; i >= 0; i-- {
|
|
if exec.starts[i].kind == kind {
|
|
return exec.starts[i]
|
|
}
|
|
}
|
|
t.Fatalf("no %s start record; commands=%v starts=%v", kind, exec.commands, exec.starts)
|
|
return startRecord{}
|
|
}
|
|
|
|
func waitForLatestSessionID(t *testing.T, exec *mockExecutor, kind string) uint64 {
|
|
t.Helper()
|
|
deadline := time.Now().Add(2 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
if sid := exec.latestSessionID(kind); sid != 0 {
|
|
return sid
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
t.Fatalf("no %s session started; commands=%v", kind, exec.getCommands())
|
|
return 0
|
|
}
|
|
|
|
func countStarts(exec *mockExecutor, kind string) int {
|
|
exec.mu.Lock()
|
|
defer exec.mu.Unlock()
|
|
n := 0
|
|
for _, start := range exec.starts {
|
|
if start.kind == kind {
|
|
n++
|
|
}
|
|
}
|
|
return n
|
|
}
|
|
|
|
func hasAdapterCommand(a *VolumeReplicaAdapter, kind string) bool {
|
|
for _, got := range a.CommandLog() {
|
|
if got == kind {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// --- C9: Timer only triggers probe, never direct recovery ---
|
|
|
|
func TestC9_TimerOnlyTriggersProbe(t *testing.T) {
|
|
exec := newMockExecutor()
|
|
a := NewVolumeReplicaAdapter(exec)
|
|
|
|
// Assignment triggers ProbeReplica command, nothing else.
|
|
a.OnAssignment(AssignmentInfo{
|
|
VolumeID: "vol1", ReplicaID: "r1",
|
|
Epoch: 1, EndpointVersion: 1,
|
|
DataAddr: "a", CtrlAddr: "b",
|
|
})
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
cmds := exec.getCommands()
|
|
for _, c := range cmds {
|
|
if c == "StartCatchUp" || c == "StartRebuild" {
|
|
t.Fatalf("C9 violated: assignment directly produced %s", c)
|
|
}
|
|
}
|
|
hasProbe := false
|
|
for _, c := range cmds {
|
|
if c == "Probe" {
|
|
hasProbe = true
|
|
}
|
|
}
|
|
if !hasProbe {
|
|
t.Fatal("C9: expected Probe from assignment")
|
|
}
|
|
}
|
|
|
|
// --- C10: Engine state has no transport fields ---
|
|
|
|
func TestC10_EngineStateNoTransportFields(t *testing.T) {
|
|
// Structural guard: engine state must be plain data.
|
|
// 1. Copy test: proves no hidden pointers/interfaces.
|
|
var st engine.ReplicaState
|
|
st2 := st
|
|
st2.Identity.Epoch = 999
|
|
if st.Identity.Epoch == 999 {
|
|
t.Fatal("C10: state copy leaked — hidden reference")
|
|
}
|
|
|
|
// 2. Import guard: verify the engine package imports ONLY stdlib
|
|
// packages that don't involve transport, networking, or I/O.
|
|
// This is the real C10 closure: if someone adds "net" or "sync"
|
|
// to engine/, this test must be updated — which forces review.
|
|
//
|
|
// The engine package currently imports nothing (zero dependencies).
|
|
// If it ever imports "net", "sync", "io", or any transport package,
|
|
// that's a C10 violation. We verify this by noting: the engine
|
|
// package compiles with no external imports, and ReplicaState is
|
|
// a pure value type with no methods that take or return interfaces
|
|
// from transport packages.
|
|
//
|
|
// Compile-time proof: engine.ReplicaState contains only:
|
|
// string, uint64, bool, and named string/enum types
|
|
// No net.Conn, no *os.File, no sync.Mutex, no func fields.
|
|
|
|
// 3. Reflect-based check: all fields are value types.
|
|
checkPlainData(t, "Identity", st.Identity)
|
|
checkPlainData(t, "Reachability", st.Reachability)
|
|
checkPlainData(t, "Recovery", st.Recovery)
|
|
checkPlainData(t, "Session", st.Session)
|
|
checkPlainData(t, "Publication", st.Publication)
|
|
t.Log("C10: engine state verified as plain data, no transport imports")
|
|
}
|
|
|
|
// checkPlainData recursively verifies a struct has no pointer, interface,
|
|
// func, chan, map, or known-dangerous types (sync.Mutex, net.Conn, etc.).
|
|
func checkPlainData(t *testing.T, path string, v interface{}) {
|
|
t.Helper()
|
|
checkPlainType(t, path, reflect.TypeOf(v))
|
|
}
|
|
|
|
var forbiddenTypeNames = map[string]bool{
|
|
"sync.Mutex": true,
|
|
"sync.RWMutex": true,
|
|
"sync.WaitGroup": true,
|
|
"sync.Cond": true,
|
|
"sync.Once": true,
|
|
"net.Conn": true,
|
|
"os.File": true,
|
|
}
|
|
|
|
func checkPlainType(t *testing.T, path string, rt reflect.Type) {
|
|
t.Helper()
|
|
fullName := rt.PkgPath() + "." + rt.Name()
|
|
if forbiddenTypeNames[fullName] {
|
|
t.Fatalf("C10: %s is %s — forbidden in engine state", path, fullName)
|
|
}
|
|
|
|
switch rt.Kind() {
|
|
case reflect.Ptr, reflect.Interface, reflect.Func, reflect.Chan, reflect.Map:
|
|
t.Fatalf("C10: %s is %s — transport/runtime state may leak", path, rt.Kind())
|
|
case reflect.Struct:
|
|
for i := 0; i < rt.NumField(); i++ {
|
|
f := rt.Field(i)
|
|
checkPlainType(t, path+"."+f.Name, f.Type)
|
|
}
|
|
case reflect.Slice:
|
|
t.Fatalf("C10: %s is slice — mutable shared state may leak", path)
|
|
case reflect.Array:
|
|
checkPlainType(t, path+"[elem]", rt.Elem())
|
|
}
|
|
}
|
|
|
|
// --- C12: Executor cannot choose policy ---
|
|
|
|
func TestC12_ExecutorCannotChoosePolicy(t *testing.T) {
|
|
exec := newMockExecutor()
|
|
|
|
// Pre-set probe result so auto-probe from assignment succeeds.
|
|
exec.probeResults["r1"] = ProbeResult{
|
|
ReplicaID: "r1", Success: true,
|
|
EndpointVersion: 1, TransportEpoch: 1,
|
|
ReplicaFlushedLSN: 5, PrimaryTailLSN: 50, PrimaryHeadLSN: 100,
|
|
}
|
|
|
|
a := NewVolumeReplicaAdapter(exec)
|
|
|
|
a.OnAssignment(AssignmentInfo{
|
|
VolumeID: "vol1", ReplicaID: "r1",
|
|
Epoch: 1, EndpointVersion: 1,
|
|
DataAddr: "a", CtrlAddr: "b",
|
|
})
|
|
|
|
// Wait for auto-probe to fire.
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
// The executor should have received StartRebuild from the engine.
|
|
// The auto-probe with pre-set results handles the full chain.
|
|
|
|
cmds := exec.getCommands()
|
|
hasRebuild := false
|
|
for _, c := range cmds {
|
|
if c == "StartRebuild" {
|
|
hasRebuild = true
|
|
}
|
|
}
|
|
if !hasRebuild {
|
|
t.Fatalf("C12: engine should issue StartRebuild, got: %v", cmds)
|
|
}
|
|
|
|
// The CommandExecutor interface has no method returning RecoveryDecision.
|
|
// Policy flows: engine → adapter → executor. Never executor → engine.
|
|
t.Log("C12: executor receives commands, never returns decisions")
|
|
}
|
|
|
|
// --- Real callback: executor session close wired back to engine ---
|
|
|
|
func TestCallbackWired_SessionCloseReachesEngine(t *testing.T) {
|
|
exec := newMockExecutor()
|
|
|
|
// Pre-set probe result so the auto-probe from assignment succeeds.
|
|
exec.probeResults["r1"] = ProbeResult{
|
|
ReplicaID: "r1", Success: true,
|
|
EndpointVersion: 1, TransportEpoch: 1,
|
|
ReplicaFlushedLSN: 50, PrimaryTailLSN: 10, PrimaryHeadLSN: 100,
|
|
}
|
|
|
|
a := NewVolumeReplicaAdapter(exec)
|
|
|
|
a.OnAssignment(AssignmentInfo{
|
|
VolumeID: "vol1", ReplicaID: "r1",
|
|
Epoch: 1, EndpointVersion: 1,
|
|
DataAddr: "a", CtrlAddr: "b",
|
|
})
|
|
|
|
// Don't need separate OnProbeResult — the auto-probe from assignment
|
|
// will use the pre-set mock result.
|
|
|
|
// The executor's StartCatchUp fires async completion via the
|
|
// registered OnSessionClose callback. Wait for it.
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
p := a.Projection()
|
|
if p.Mode != engine.ModeHealthy {
|
|
t.Logf("Trace:")
|
|
for _, te := range a.Trace() {
|
|
t.Logf(" [%s] %s", te.Step, te.Detail)
|
|
}
|
|
t.Logf("Commands: %v", a.CommandLog())
|
|
t.Fatalf("callback wired: expected healthy after async session close, got %s (decision=%s)",
|
|
p.Mode, p.RecoveryDecision)
|
|
}
|
|
}
|
|
|
|
// --- Full route without manual OnSessionClose ---
|
|
|
|
func TestFullRoute_CatchUpViaCallback(t *testing.T) {
|
|
exec := newMockExecutor()
|
|
|
|
exec.probeResults["r1"] = ProbeResult{
|
|
ReplicaID: "r1", Success: true,
|
|
EndpointVersion: 1, TransportEpoch: 1,
|
|
ReplicaFlushedLSN: 50, PrimaryTailLSN: 10, PrimaryHeadLSN: 100,
|
|
}
|
|
|
|
a := NewVolumeReplicaAdapter(exec)
|
|
|
|
a.OnAssignment(AssignmentInfo{
|
|
VolumeID: "vol1", ReplicaID: "r1",
|
|
Epoch: 1, EndpointVersion: 1,
|
|
DataAddr: "a", CtrlAddr: "b",
|
|
})
|
|
|
|
// Wait for async session completion via callback.
|
|
deadline := time.Now().Add(2 * time.Second)
|
|
for time.Now().Before(deadline) {
|
|
if a.Projection().Mode == engine.ModeHealthy {
|
|
break
|
|
}
|
|
time.Sleep(20 * time.Millisecond)
|
|
}
|
|
|
|
p := a.Projection()
|
|
if p.Mode != engine.ModeHealthy {
|
|
t.Fatalf("full route via callback: expected healthy, got %s", p.Mode)
|
|
}
|
|
|
|
cmds := exec.getCommands()
|
|
hasProbe := false
|
|
hasCatchUp := false
|
|
hasHealthy := false
|
|
for _, c := range cmds {
|
|
switch c {
|
|
case "Probe":
|
|
hasProbe = true
|
|
case "StartCatchUp":
|
|
hasCatchUp = true
|
|
case "PublishHealthy":
|
|
hasHealthy = true
|
|
}
|
|
}
|
|
if !hasProbe || !hasCatchUp || !hasHealthy {
|
|
t.Fatalf("route incomplete: probe=%v catchup=%v healthy=%v cmds=%v",
|
|
hasProbe, hasCatchUp, hasHealthy, cmds)
|
|
}
|
|
}
|
|
|
|
// --- Terminal: only session close can produce healthy ---
|
|
|
|
func TestTerminal_OnlySessionCloseProducesHealthy(t *testing.T) {
|
|
exec := newMockExecutor()
|
|
// Pre-set probe result for auto-probe.
|
|
exec.probeResults["r1"] = ProbeResult{
|
|
ReplicaID: "r1", Success: true,
|
|
EndpointVersion: 1, TransportEpoch: 1,
|
|
ReplicaFlushedLSN: 50, PrimaryTailLSN: 10, PrimaryHeadLSN: 100,
|
|
}
|
|
|
|
a := NewVolumeReplicaAdapter(exec)
|
|
|
|
// Disable callback AFTER construction so the adapter's registration
|
|
// doesn't overwrite our nil. This proves that without the close
|
|
// callback firing, the replica stays non-healthy.
|
|
exec.mu.Lock()
|
|
exec.onSessionClose = nil
|
|
exec.mu.Unlock()
|
|
|
|
a.OnAssignment(AssignmentInfo{
|
|
VolumeID: "vol1", ReplicaID: "r1",
|
|
Epoch: 1, EndpointVersion: 1,
|
|
DataAddr: "a", CtrlAddr: "b",
|
|
})
|
|
|
|
// Wait for auto-probe + StartCatchUp to fire.
|
|
// Session starts but never closes (callback disabled).
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
p := a.Projection()
|
|
if p.Mode == engine.ModeHealthy {
|
|
t.Fatal("should not be healthy without session close callback")
|
|
}
|
|
if p.RecoveryDecision != engine.DecisionCatchUp {
|
|
t.Fatalf("expected catch_up decision (session stuck), got %s", p.RecoveryDecision)
|
|
}
|
|
}
|
|
|
|
// --- Already caught up: no session needed ---
|
|
|
|
func TestFullRoute_AlreadyCaughtUp(t *testing.T) {
|
|
// P14 S1: caught-up under a new identity epoch is NOT
|
|
// immediately healthy. The engine emits FenceAtEpoch and
|
|
// holds publication at not-healthy until FenceCompleted
|
|
// advances the replica's lineage gate. With autoFence=true on
|
|
// the mock, the fence fires inline synchronously, so the
|
|
// sequence is: probe → fence command → fence completes →
|
|
// publication flips to healthy.
|
|
exec := newMockExecutor()
|
|
// Register success for the background probe triggered by
|
|
// OnAssignment so it doesn't race the manual OnProbeResult
|
|
// below with a ProbeFailed that would clobber reachability.
|
|
exec.probeResults["r1"] = ProbeResult{
|
|
ReplicaID: "r1", Success: true,
|
|
EndpointVersion: 1, TransportEpoch: 1,
|
|
ReplicaFlushedLSN: 100, PrimaryTailLSN: 10, PrimaryHeadLSN: 100,
|
|
}
|
|
a := NewVolumeReplicaAdapter(exec)
|
|
|
|
a.OnAssignment(AssignmentInfo{
|
|
VolumeID: "vol1", ReplicaID: "r1",
|
|
Epoch: 1, EndpointVersion: 1,
|
|
DataAddr: "a", CtrlAddr: "b",
|
|
})
|
|
// Let the background probe finish.
|
|
time.Sleep(30 * time.Millisecond)
|
|
|
|
// autoFence completed synchronously during dispatch; projection
|
|
// must now be healthy.
|
|
p := a.Projection()
|
|
if p.Mode != engine.ModeHealthy {
|
|
t.Fatalf("caught up + auto-fence: expected healthy, got %s", p.Mode)
|
|
}
|
|
}
|
|
|
|
// TestFullRoute_CaughtUp_NotHealthyUntilFenceComplete proves the
|
|
// ack-gated contract at the operator-visible surface: if the fence
|
|
// is still in flight (fence callback has not fired), Mode must NOT
|
|
// be ModeHealthy. This is the test the architect called out — P14
|
|
// S1's contract is operator-visible, not just command-list-level.
|
|
func TestFullRoute_CaughtUp_NotHealthyUntilFenceComplete(t *testing.T) {
|
|
exec := newMockExecutor()
|
|
exec.autoFence = false // hold the fence callback
|
|
// Pre-register a caught-up probe result so the background probe
|
|
// triggered by OnAssignment drives the full route on its own.
|
|
// Avoids racing a failing background probe against a manual
|
|
// ProbeSucceeded.
|
|
exec.probeResults["r1"] = ProbeResult{
|
|
ReplicaID: "r1", Success: true,
|
|
EndpointVersion: 1, TransportEpoch: 1,
|
|
ReplicaFlushedLSN: 100, PrimaryTailLSN: 10, PrimaryHeadLSN: 100,
|
|
}
|
|
a := NewVolumeReplicaAdapter(exec)
|
|
|
|
a.OnAssignment(AssignmentInfo{
|
|
VolumeID: "vol1", ReplicaID: "r1",
|
|
Epoch: 1, EndpointVersion: 1,
|
|
DataAddr: "a", CtrlAddr: "b",
|
|
})
|
|
// Wait for the background probe → fence dispatch chain to settle.
|
|
// autoFence=false guarantees the fence stays in flight.
|
|
deadline := time.Now().Add(500 * time.Millisecond)
|
|
for time.Now().Before(deadline) {
|
|
exec.mu.Lock()
|
|
n := len(exec.fences)
|
|
exec.mu.Unlock()
|
|
if n == 1 {
|
|
break
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
|
|
// Fence was dispatched but not completed — projection must
|
|
// NOT be healthy. Must also not be degraded (nothing's wrong).
|
|
p := a.Projection()
|
|
if p.Mode == engine.ModeHealthy {
|
|
t.Fatalf("fence not yet complete: Mode must not be healthy, got %s", p.Mode)
|
|
}
|
|
if p.Mode == engine.ModeDegraded {
|
|
t.Fatalf("fence pending is transitional, not degraded, got %s", p.Mode)
|
|
}
|
|
// Expected: ModeRecovering (transitional, waiting on fence).
|
|
if p.Mode != engine.ModeRecovering {
|
|
t.Fatalf("fence pending should show recovering, got %s", p.Mode)
|
|
}
|
|
|
|
// Find the in-flight fence and fire a success callback.
|
|
exec.mu.Lock()
|
|
if len(exec.fences) != 1 {
|
|
exec.mu.Unlock()
|
|
t.Fatalf("expected 1 fence in flight, got %d", len(exec.fences))
|
|
}
|
|
fr := exec.fences[0]
|
|
exec.mu.Unlock()
|
|
exec.fireFence(FenceResult{
|
|
ReplicaID: fr.replicaID,
|
|
SessionID: fr.sessionID,
|
|
Epoch: fr.epoch,
|
|
EndpointVersion: fr.endpointVersion,
|
|
Success: true,
|
|
})
|
|
|
|
// Now projection must be healthy.
|
|
p = a.Projection()
|
|
if p.Mode != engine.ModeHealthy {
|
|
t.Fatalf("after fence complete: expected healthy, got %s", p.Mode)
|
|
}
|
|
}
|
|
|
|
// TestFence_NewerEpochNotStarvedByOlderInFlightFence — regression
|
|
// test for the adapter's fence dedupe key. If the dedupe is keyed
|
|
// only by replicaID, a newer-epoch fence emitted after an identity
|
|
// advance is dropped while the older fence is still in flight. The
|
|
// old callback then arrives, gets normalized to a FenceCompleted
|
|
// event at the old epoch, which the engine rejects as stale — and
|
|
// nothing re-drives decide() for the new epoch. Result: the replica
|
|
// lingers non-healthy until some unrelated later probe happens.
|
|
//
|
|
// The fix keys dedupe by full fence lineage (replicaID, epoch,
|
|
// endpointVersion). This test proves: after identity advances with
|
|
// an older fence still in flight, the newer-epoch fence is
|
|
// dispatched to the executor (NOT starved), and publishing healthy
|
|
// depends only on the newer fence completing.
|
|
func TestFence_NewerEpochNotStarvedByOlderInFlightFence(t *testing.T) {
|
|
exec := newMockExecutor()
|
|
exec.autoFence = false // hold all fence callbacks
|
|
// Caught-up probe at epoch=1.
|
|
exec.probeResults["r1"] = ProbeResult{
|
|
ReplicaID: "r1", Success: true,
|
|
EndpointVersion: 1, TransportEpoch: 1,
|
|
ReplicaFlushedLSN: 100, PrimaryTailLSN: 10, PrimaryHeadLSN: 100,
|
|
}
|
|
a := NewVolumeReplicaAdapter(exec)
|
|
|
|
a.OnAssignment(AssignmentInfo{
|
|
VolumeID: "vol1", ReplicaID: "r1",
|
|
Epoch: 1, EndpointVersion: 1,
|
|
DataAddr: "a", CtrlAddr: "b",
|
|
})
|
|
// Wait for the first fence to be dispatched but not completed.
|
|
deadline := time.Now().Add(500 * time.Millisecond)
|
|
for time.Now().Before(deadline) {
|
|
exec.mu.Lock()
|
|
n := len(exec.fences)
|
|
exec.mu.Unlock()
|
|
if n == 1 {
|
|
break
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
exec.mu.Lock()
|
|
if len(exec.fences) != 1 {
|
|
exec.mu.Unlock()
|
|
t.Fatalf("expected 1 in-flight fence at epoch 1, got %d", len(exec.fences))
|
|
}
|
|
oldFence := exec.fences[0]
|
|
exec.mu.Unlock()
|
|
if oldFence.epoch != 1 || oldFence.endpointVersion != 1 {
|
|
t.Fatalf("old fence at wrong lineage: epoch=%d ev=%d",
|
|
oldFence.epoch, oldFence.endpointVersion)
|
|
}
|
|
|
|
// Identity advances to epoch=2 before the old fence completes.
|
|
// Update the pre-registered probe result so the background probe
|
|
// triggered by the new assignment returns epoch=2 boundaries.
|
|
exec.mu.Lock()
|
|
exec.probeResults["r1"] = ProbeResult{
|
|
ReplicaID: "r1", Success: true,
|
|
EndpointVersion: 2, TransportEpoch: 2,
|
|
ReplicaFlushedLSN: 200, PrimaryTailLSN: 20, PrimaryHeadLSN: 200,
|
|
}
|
|
exec.mu.Unlock()
|
|
|
|
a.OnAssignment(AssignmentInfo{
|
|
VolumeID: "vol1", ReplicaID: "r1",
|
|
Epoch: 2, EndpointVersion: 2,
|
|
DataAddr: "a2", CtrlAddr: "b2",
|
|
})
|
|
// Wait for the newer-epoch fence to be dispatched.
|
|
deadline = time.Now().Add(500 * time.Millisecond)
|
|
for time.Now().Before(deadline) {
|
|
exec.mu.Lock()
|
|
n := len(exec.fences)
|
|
exec.mu.Unlock()
|
|
if n >= 2 {
|
|
break
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
exec.mu.Lock()
|
|
n := len(exec.fences)
|
|
var newFence fenceRecord
|
|
if n >= 2 {
|
|
newFence = exec.fences[1]
|
|
}
|
|
exec.mu.Unlock()
|
|
if n < 2 {
|
|
t.Fatalf("newer-epoch fence was starved by older in-flight fence — still only %d fence(s) dispatched",
|
|
n)
|
|
}
|
|
if newFence.epoch != 2 || newFence.endpointVersion != 2 {
|
|
t.Fatalf("new fence at wrong lineage: epoch=%d ev=%d",
|
|
newFence.epoch, newFence.endpointVersion)
|
|
}
|
|
|
|
// Projection must not be healthy yet — the newer fence is still
|
|
// in flight and the old one would be stale even if it arrived.
|
|
if got := a.Projection().Mode; got == engine.ModeHealthy {
|
|
t.Fatalf("mode flipped Healthy before newer-epoch fence completed, got %s", got)
|
|
}
|
|
|
|
// Fire the NEW fence success callback. This must drive the
|
|
// replica to healthy under the new lineage.
|
|
exec.fireFence(FenceResult{
|
|
ReplicaID: newFence.replicaID,
|
|
SessionID: newFence.sessionID,
|
|
Epoch: newFence.epoch,
|
|
EndpointVersion: newFence.endpointVersion,
|
|
Success: true,
|
|
})
|
|
if got := a.Projection().Mode; got != engine.ModeHealthy {
|
|
t.Fatalf("after newer-epoch fence complete: expected healthy, got %s", got)
|
|
}
|
|
if got := a.Projection().Epoch; got != 2 {
|
|
t.Fatalf("projection epoch=%d, want 2", got)
|
|
}
|
|
|
|
// Late straggler: the OLD fence callback finally arrives. It
|
|
// must be dropped without disturbing the already-healthy state.
|
|
exec.fireFence(FenceResult{
|
|
ReplicaID: oldFence.replicaID,
|
|
SessionID: oldFence.sessionID,
|
|
Epoch: oldFence.epoch,
|
|
EndpointVersion: oldFence.endpointVersion,
|
|
Success: true,
|
|
})
|
|
if got := a.Projection().Mode; got != engine.ModeHealthy {
|
|
t.Fatalf("late old-epoch callback perturbed healthy state, mode=%s", got)
|
|
}
|
|
}
|
|
|
|
// TestFence_MissingCallback_TimesOutAndReprobeReemits closes the
|
|
// remaining bounded-fate hole on the caught-up fence route. If the
|
|
// executor dispatches FenceAtEpoch successfully but never calls
|
|
// back, the adapter must not wait forever in a silent transitional
|
|
// state. Instead it synthesizes FenceFailed("fence_timeout"),
|
|
// leaves the projection non-healthy, and allows the next probe to
|
|
// re-emit FenceAtEpoch at the same lineage.
|
|
func TestFence_MissingCallback_TimesOutAndReprobeReemits(t *testing.T) {
|
|
exec := newMockExecutor()
|
|
exec.autoFence = false // simulate missing callback forever
|
|
exec.probeResults["r1"] = ProbeResult{
|
|
ReplicaID: "r1", Success: true,
|
|
EndpointVersion: 1, TransportEpoch: 1,
|
|
ReplicaFlushedLSN: 100, PrimaryTailLSN: 10, PrimaryHeadLSN: 100,
|
|
}
|
|
a := NewVolumeReplicaAdapter(exec)
|
|
a.fenceTimeout = 30 * time.Millisecond
|
|
|
|
a.OnAssignment(AssignmentInfo{
|
|
VolumeID: "vol1", ReplicaID: "r1",
|
|
Epoch: 1, EndpointVersion: 1,
|
|
DataAddr: "a", CtrlAddr: "b",
|
|
})
|
|
|
|
deadline := time.Now().Add(500 * time.Millisecond)
|
|
for time.Now().Before(deadline) {
|
|
exec.mu.Lock()
|
|
n := len(exec.fences)
|
|
exec.mu.Unlock()
|
|
if n == 1 {
|
|
break
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
exec.mu.Lock()
|
|
if len(exec.fences) != 1 {
|
|
exec.mu.Unlock()
|
|
t.Fatalf("expected first fence dispatch, got %d", len(exec.fences))
|
|
}
|
|
oldFence := exec.fences[0]
|
|
exec.mu.Unlock()
|
|
|
|
// Let the adapter-local fence timeout synthesize FenceFailed.
|
|
deadline = time.Now().Add(500 * time.Millisecond)
|
|
for time.Now().Before(deadline) {
|
|
trace := a.Trace()
|
|
found := false
|
|
for _, te := range trace {
|
|
if te.Step == "fence_failed" && te.Detail == "epoch=1 reason=fence_timeout" {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if found {
|
|
break
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
assertTraceHasDetail(t, a.Trace(), "fence_failed", "epoch=1 reason=fence_timeout")
|
|
if got := a.Projection().Mode; got == engine.ModeHealthy {
|
|
t.Fatalf("missing fence callback must not leave projection healthy, got %s", got)
|
|
}
|
|
|
|
// The next probe should re-drive the caught-up fence route at the
|
|
// same lineage. Missing callback is now bounded as timeout+retry,
|
|
// not a forever-wait assumption.
|
|
a.OnProbeResult(exec.probeResults["r1"])
|
|
deadline = time.Now().Add(500 * time.Millisecond)
|
|
for time.Now().Before(deadline) {
|
|
exec.mu.Lock()
|
|
n := len(exec.fences)
|
|
exec.mu.Unlock()
|
|
if n >= 2 {
|
|
break
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
exec.mu.Lock()
|
|
if len(exec.fences) < 2 {
|
|
exec.mu.Unlock()
|
|
t.Fatalf("expected probe-driven re-emitted fence, got %d dispatches", len(exec.fences))
|
|
}
|
|
newFence := exec.fences[1]
|
|
exec.mu.Unlock()
|
|
if newFence.epoch != oldFence.epoch || newFence.endpointVersion != oldFence.endpointVersion {
|
|
t.Fatalf("probe-driven retry changed lineage unexpectedly: old=(%d,%d) new=(%d,%d)",
|
|
oldFence.epoch, oldFence.endpointVersion, newFence.epoch, newFence.endpointVersion)
|
|
}
|
|
|
|
exec.fireFence(FenceResult{
|
|
ReplicaID: newFence.replicaID,
|
|
SessionID: newFence.sessionID,
|
|
Epoch: newFence.epoch,
|
|
EndpointVersion: newFence.endpointVersion,
|
|
Success: true,
|
|
})
|
|
if got := a.Projection().Mode; got != engine.ModeHealthy {
|
|
t.Fatalf("after retried fence completion: expected healthy, got %s", got)
|
|
}
|
|
|
|
// A very late callback from the timed-out attempt must still be
|
|
// dropped and must not perturb the healthy state.
|
|
exec.fireFence(FenceResult{
|
|
ReplicaID: oldFence.replicaID,
|
|
SessionID: oldFence.sessionID,
|
|
Epoch: oldFence.epoch,
|
|
EndpointVersion: oldFence.endpointVersion,
|
|
Success: true,
|
|
})
|
|
if got := a.Projection().Mode; got != engine.ModeHealthy {
|
|
t.Fatalf("late callback from timed-out fence perturbed healthy state, got %s", got)
|
|
}
|
|
}
|
|
|
|
// TestFence_DuplicateSameLineageCallback_Dropped proves the adapter
|
|
// drops a duplicate callback for the same fence lineage after the
|
|
// first completion already advanced the route. This pins the current
|
|
// bounded behavior: one fence attempt has one accepted completion
|
|
// path, and a second identical callback must not re-enter the engine
|
|
// or duplicate PublishHealthy.
|
|
func TestFence_DuplicateSameLineageCallback_Dropped(t *testing.T) {
|
|
exec := newMockExecutor()
|
|
exec.autoFence = false
|
|
exec.probeResults["r1"] = ProbeResult{
|
|
ReplicaID: "r1", Success: true,
|
|
EndpointVersion: 1, TransportEpoch: 1,
|
|
ReplicaFlushedLSN: 100, PrimaryTailLSN: 10, PrimaryHeadLSN: 100,
|
|
}
|
|
a := NewVolumeReplicaAdapter(exec)
|
|
|
|
a.OnAssignment(AssignmentInfo{
|
|
VolumeID: "vol1", ReplicaID: "r1",
|
|
Epoch: 1, EndpointVersion: 1,
|
|
DataAddr: "a", CtrlAddr: "b",
|
|
})
|
|
|
|
deadline := time.Now().Add(500 * time.Millisecond)
|
|
for time.Now().Before(deadline) {
|
|
exec.mu.Lock()
|
|
n := len(exec.fences)
|
|
exec.mu.Unlock()
|
|
if n == 1 {
|
|
break
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
exec.mu.Lock()
|
|
if len(exec.fences) != 1 {
|
|
exec.mu.Unlock()
|
|
t.Fatalf("expected one fence dispatch, got %d", len(exec.fences))
|
|
}
|
|
fr := exec.fences[0]
|
|
exec.mu.Unlock()
|
|
|
|
exec.fireFence(FenceResult{
|
|
ReplicaID: fr.replicaID,
|
|
SessionID: fr.sessionID,
|
|
Epoch: fr.epoch,
|
|
EndpointVersion: fr.endpointVersion,
|
|
Success: true,
|
|
})
|
|
if got := a.Projection().Mode; got != engine.ModeHealthy {
|
|
t.Fatalf("after first fence complete: expected healthy, got %s", got)
|
|
}
|
|
|
|
before := 0
|
|
for _, c := range exec.getCommands() {
|
|
if c == "PublishHealthy" {
|
|
before++
|
|
}
|
|
}
|
|
if before != 1 {
|
|
t.Fatalf("expected exactly one PublishHealthy after first completion, got %d", before)
|
|
}
|
|
|
|
// Duplicate callback at the exact same lineage/session must be dropped.
|
|
exec.fireFence(FenceResult{
|
|
ReplicaID: fr.replicaID,
|
|
SessionID: fr.sessionID,
|
|
Epoch: fr.epoch,
|
|
EndpointVersion: fr.endpointVersion,
|
|
Success: true,
|
|
})
|
|
if got := a.Projection().Mode; got != engine.ModeHealthy {
|
|
t.Fatalf("duplicate same-lineage fence callback perturbed healthy state, got %s", got)
|
|
}
|
|
|
|
after := 0
|
|
for _, c := range exec.getCommands() {
|
|
if c == "PublishHealthy" {
|
|
after++
|
|
}
|
|
}
|
|
if after != before {
|
|
t.Fatalf("duplicate same-lineage fence callback duplicated PublishHealthy: before=%d after=%d", before, after)
|
|
}
|
|
}
|
|
|
|
func TestSessionStarted_ComesFromExecutorStartCallback(t *testing.T) {
|
|
exec := newMockExecutor()
|
|
exec.autoStart = false
|
|
exec.autoClose = false
|
|
a := NewVolumeReplicaAdapter(exec)
|
|
|
|
exec.probeResults["r1"] = ProbeResult{
|
|
ReplicaID: "r1", Success: true,
|
|
EndpointVersion: 1, TransportEpoch: 1,
|
|
ReplicaFlushedLSN: 50, PrimaryTailLSN: 10, PrimaryHeadLSN: 100,
|
|
}
|
|
a.OnAssignment(AssignmentInfo{
|
|
VolumeID: "vol1", ReplicaID: "r1",
|
|
Epoch: 1, EndpointVersion: 1,
|
|
DataAddr: "a", CtrlAddr: "b",
|
|
})
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
sessionID := exec.latestSessionID("StartCatchUp")
|
|
if sessionID == 0 {
|
|
t.Fatal("expected catch-up session")
|
|
}
|
|
if got := a.Projection().SessionPhase; got != engine.PhaseStarting {
|
|
t.Fatalf("before runtime start callback, phase=%s want %s", got, engine.PhaseStarting)
|
|
}
|
|
|
|
exec.fireStart(SessionStartResult{ReplicaID: "r1", SessionID: sessionID})
|
|
time.Sleep(20 * time.Millisecond)
|
|
|
|
if got := a.Projection().SessionPhase; got != engine.PhaseRunning {
|
|
t.Fatalf("after runtime start callback, phase=%s want %s", got, engine.PhaseRunning)
|
|
}
|
|
|
|
exec.fireClose(SessionCloseResult{
|
|
ReplicaID: "r1",
|
|
SessionID: sessionID,
|
|
Success: true,
|
|
AchievedLSN: 100,
|
|
})
|
|
time.Sleep(20 * time.Millisecond)
|
|
|
|
if got := a.Projection().Mode; got != engine.ModeHealthy {
|
|
t.Fatalf("after runtime close callback, mode=%s want %s", got, engine.ModeHealthy)
|
|
}
|
|
}
|
|
|
|
func TestPreparedNeverStarted_FailsClosedOnImmediateStartError(t *testing.T) {
|
|
exec := newMockExecutor()
|
|
exec.autoStart = false
|
|
exec.autoClose = false
|
|
exec.catchUpErr = fmt.Errorf("boom_start")
|
|
exec.rebuildErr = fmt.Errorf("boom_start") // round-47: catch-up exhaustion escalates to rebuild; rebuild also fails for terminal degraded
|
|
a := NewVolumeReplicaAdapter(exec)
|
|
|
|
exec.probeResults["r1"] = ProbeResult{
|
|
ReplicaID: "r1", Success: true,
|
|
EndpointVersion: 1, TransportEpoch: 1,
|
|
ReplicaFlushedLSN: 50, PrimaryTailLSN: 10, PrimaryHeadLSN: 100,
|
|
}
|
|
a.OnAssignment(AssignmentInfo{
|
|
VolumeID: "vol1", ReplicaID: "r1",
|
|
Epoch: 1, EndpointVersion: 1,
|
|
DataAddr: "a", CtrlAddr: "b",
|
|
})
|
|
// Round-47 changes the failure path: catch-up #1 fails → retry
|
|
// up to MaxRetries=3 → exhaustion escalates to StartRebuild →
|
|
// rebuild also fails (rebuildErr set) → MaxRetries=0 for rebuild
|
|
// → exhaustion → PublishDegraded. Each attempt is synchronous
|
|
// so 50ms is plenty for the full chain.
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
p := a.Projection()
|
|
// After retry+escalate+terminal: mode must be Degraded; phase
|
|
// can be Failed (post-rebuild-fail) — what matters is "no
|
|
// silent acceptance."
|
|
if p.Mode == engine.ModeHealthy {
|
|
t.Fatal("immediate start error chain must not produce healthy")
|
|
}
|
|
if got := p.Mode; got != engine.ModeDegraded {
|
|
t.Fatalf("immediate start error chain should end at degraded, mode=%s", got)
|
|
}
|
|
// Round-47: the final failure trace should reflect rebuild
|
|
// terminal (or catch-up retry budget exhausted, depending on
|
|
// exact timing). Just verify SOME session_failed trace exists.
|
|
foundFailedTrace := false
|
|
for _, te := range a.Trace() {
|
|
if te.Step == "session_failed" {
|
|
foundFailedTrace = true
|
|
break
|
|
}
|
|
}
|
|
if !foundFailedTrace {
|
|
t.Fatal("expected at least one session_failed trace in the retry+escalate chain")
|
|
}
|
|
}
|
|
|
|
func TestPreparedNeverStarted_TimesOutAndFailsClosed(t *testing.T) {
|
|
exec := newMockExecutor()
|
|
exec.autoStart = false
|
|
exec.autoClose = false
|
|
a := NewVolumeReplicaAdapter(exec)
|
|
a.startTimeout = 30 * time.Millisecond
|
|
|
|
exec.probeResults["r1"] = ProbeResult{
|
|
ReplicaID: "r1", Success: true,
|
|
EndpointVersion: 1, TransportEpoch: 1,
|
|
ReplicaFlushedLSN: 50, PrimaryTailLSN: 10, PrimaryHeadLSN: 100,
|
|
}
|
|
a.OnAssignment(AssignmentInfo{
|
|
VolumeID: "vol1", ReplicaID: "r1",
|
|
Epoch: 1, EndpointVersion: 1,
|
|
DataAddr: "a", CtrlAddr: "b",
|
|
})
|
|
|
|
deadline := time.Now().Add(500 * time.Millisecond)
|
|
for time.Now().Before(deadline) {
|
|
if a.Projection().SessionPhase == engine.PhaseFailed {
|
|
break
|
|
}
|
|
time.Sleep(10 * time.Millisecond)
|
|
}
|
|
|
|
p := a.Projection()
|
|
if got := p.SessionPhase; got != engine.PhaseFailed {
|
|
t.Fatalf("start timeout should fail prepared session, phase=%s", got)
|
|
}
|
|
if got := p.Mode; got != engine.ModeDegraded {
|
|
t.Fatalf("start timeout should degrade projection, mode=%s", got)
|
|
}
|
|
assertTraceHasDetail(t, a.Trace(), "session_failed", "start_timeout")
|
|
}
|
|
|
|
func TestDelayedStartAndCloseAfterTimeout_Ignored(t *testing.T) {
|
|
exec := newMockExecutor()
|
|
exec.autoStart = false
|
|
exec.autoClose = false
|
|
a := NewVolumeReplicaAdapter(exec)
|
|
a.startTimeout = 30 * time.Millisecond
|
|
|
|
exec.probeResults["r1"] = ProbeResult{
|
|
ReplicaID: "r1", Success: true,
|
|
EndpointVersion: 1, TransportEpoch: 1,
|
|
ReplicaFlushedLSN: 50, PrimaryTailLSN: 10, PrimaryHeadLSN: 100,
|
|
}
|
|
a.OnAssignment(AssignmentInfo{
|
|
VolumeID: "vol1", ReplicaID: "r1",
|
|
Epoch: 1, EndpointVersion: 1,
|
|
DataAddr: "a", CtrlAddr: "b",
|
|
})
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
sessionID := exec.latestSessionID("StartCatchUp")
|
|
if sessionID == 0 {
|
|
t.Fatal("expected catch-up session")
|
|
}
|
|
if got := a.Projection().SessionPhase; got != engine.PhaseFailed {
|
|
t.Fatalf("expected timeout to fail session first, phase=%s", got)
|
|
}
|
|
|
|
exec.fireStart(SessionStartResult{ReplicaID: "r1", SessionID: sessionID})
|
|
exec.fireClose(SessionCloseResult{
|
|
ReplicaID: "r1",
|
|
SessionID: sessionID,
|
|
Success: true,
|
|
AchievedLSN: 100,
|
|
})
|
|
time.Sleep(30 * time.Millisecond)
|
|
|
|
p := a.Projection()
|
|
if got := p.SessionPhase; got != engine.PhaseFailed {
|
|
t.Fatalf("delayed callbacks after timeout should not revive session, phase=%s", got)
|
|
}
|
|
if got := p.Mode; got == engine.ModeHealthy {
|
|
t.Fatal("delayed callbacks after timeout must not produce healthy")
|
|
}
|
|
trace := a.Trace()
|
|
assertTraceHasDetail(t, trace, "session_started_ignored", "current phase=failed")
|
|
assertTraceHasDetail(t, trace, "session_completed_ignored", "current phase=failed")
|
|
}
|
|
|
|
func TestStaleCallback_OldSessionIgnoredAfterNewAssignment(t *testing.T) {
|
|
exec := newMockExecutor()
|
|
exec.autoClose = false
|
|
a := NewVolumeReplicaAdapter(exec)
|
|
|
|
exec.probeResults["r1"] = ProbeResult{
|
|
ReplicaID: "r1", Success: true,
|
|
EndpointVersion: 1, TransportEpoch: 1,
|
|
ReplicaFlushedLSN: 50, PrimaryTailLSN: 10, PrimaryHeadLSN: 100,
|
|
}
|
|
a.OnAssignment(AssignmentInfo{
|
|
VolumeID: "vol1", ReplicaID: "r1",
|
|
Epoch: 1, EndpointVersion: 1,
|
|
DataAddr: "a", CtrlAddr: "b",
|
|
})
|
|
time.Sleep(50 * time.Millisecond)
|
|
oldSessionID := exec.latestSessionID("StartCatchUp")
|
|
if oldSessionID == 0 {
|
|
t.Fatal("expected first catch-up session")
|
|
}
|
|
|
|
exec.probeResults["r1"] = ProbeResult{
|
|
ReplicaID: "r1", Success: true,
|
|
EndpointVersion: 2, TransportEpoch: 2,
|
|
ReplicaFlushedLSN: 60, PrimaryTailLSN: 20, PrimaryHeadLSN: 120,
|
|
}
|
|
a.OnAssignment(AssignmentInfo{
|
|
VolumeID: "vol1", ReplicaID: "r1",
|
|
Epoch: 2, EndpointVersion: 2,
|
|
DataAddr: "b", CtrlAddr: "b",
|
|
})
|
|
time.Sleep(50 * time.Millisecond)
|
|
newSessionID := exec.latestSessionID("StartCatchUp")
|
|
if newSessionID == 0 || newSessionID == oldSessionID {
|
|
t.Fatalf("expected second distinct catch-up session, old=%d new=%d", oldSessionID, newSessionID)
|
|
}
|
|
|
|
exec.fireClose(SessionCloseResult{
|
|
ReplicaID: "r1",
|
|
SessionID: oldSessionID,
|
|
Success: true,
|
|
AchievedLSN: 100,
|
|
})
|
|
time.Sleep(20 * time.Millisecond)
|
|
|
|
p := a.Projection()
|
|
if p.Mode == engine.ModeHealthy {
|
|
t.Fatal("stale callback should not make new assignment healthy")
|
|
}
|
|
if p.SessionPhase != engine.PhaseRunning {
|
|
t.Fatalf("stale callback should not stop live session, phase=%s", p.SessionPhase)
|
|
}
|
|
|
|
exec.fireClose(SessionCloseResult{
|
|
ReplicaID: "r1",
|
|
SessionID: newSessionID,
|
|
Success: true,
|
|
AchievedLSN: 120,
|
|
})
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
if got := a.Projection().Mode; got != engine.ModeHealthy {
|
|
t.Fatalf("new session close should converge to healthy, got %s", got)
|
|
}
|
|
}
|
|
|
|
func assertTraceHasDetail(t *testing.T, trace []engine.TraceEntry, step, detail string) {
|
|
t.Helper()
|
|
for _, te := range trace {
|
|
if te.Step == step && te.Detail == detail {
|
|
return
|
|
}
|
|
}
|
|
t.Fatalf("expected trace [%s] %q, got %+v", step, detail, trace)
|
|
}
|
|
|
|
func TestStaleStartCallback_OldSessionIgnoredAfterNewAssignment(t *testing.T) {
|
|
exec := newMockExecutor()
|
|
exec.autoStart = false
|
|
exec.autoClose = false
|
|
a := NewVolumeReplicaAdapter(exec)
|
|
|
|
exec.probeResults["r1"] = ProbeResult{
|
|
ReplicaID: "r1", Success: true,
|
|
EndpointVersion: 1, TransportEpoch: 1,
|
|
ReplicaFlushedLSN: 50, PrimaryTailLSN: 10, PrimaryHeadLSN: 100,
|
|
}
|
|
a.OnAssignment(AssignmentInfo{
|
|
VolumeID: "vol1", ReplicaID: "r1",
|
|
Epoch: 1, EndpointVersion: 1,
|
|
DataAddr: "a", CtrlAddr: "b",
|
|
})
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
oldSessionID := exec.latestSessionID("StartCatchUp")
|
|
if oldSessionID == 0 {
|
|
t.Fatal("expected first catch-up session")
|
|
}
|
|
|
|
exec.probeResults["r1"] = ProbeResult{
|
|
ReplicaID: "r1", Success: true,
|
|
EndpointVersion: 2, TransportEpoch: 2,
|
|
ReplicaFlushedLSN: 60, PrimaryTailLSN: 20, PrimaryHeadLSN: 120,
|
|
}
|
|
a.OnAssignment(AssignmentInfo{
|
|
VolumeID: "vol1", ReplicaID: "r1",
|
|
Epoch: 2, EndpointVersion: 2,
|
|
DataAddr: "b", CtrlAddr: "b",
|
|
})
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
newSessionID := exec.latestSessionID("StartCatchUp")
|
|
if newSessionID == 0 || newSessionID == oldSessionID {
|
|
t.Fatalf("expected second distinct catch-up session, old=%d new=%d", oldSessionID, newSessionID)
|
|
}
|
|
if got := a.Projection().SessionPhase; got != engine.PhaseStarting {
|
|
t.Fatalf("before start callbacks, phase=%s want %s", got, engine.PhaseStarting)
|
|
}
|
|
|
|
exec.fireStart(SessionStartResult{ReplicaID: "r1", SessionID: oldSessionID})
|
|
time.Sleep(20 * time.Millisecond)
|
|
|
|
if got := a.Projection().SessionPhase; got != engine.PhaseStarting {
|
|
t.Fatalf("stale start callback should not advance live session, phase=%s", got)
|
|
}
|
|
|
|
exec.fireStart(SessionStartResult{ReplicaID: "r1", SessionID: newSessionID})
|
|
time.Sleep(20 * time.Millisecond)
|
|
|
|
if got := a.Projection().SessionPhase; got != engine.PhaseRunning {
|
|
t.Fatalf("current start callback should advance to running, phase=%s", got)
|
|
}
|
|
}
|