engine: build flow control facts from progress gauges

This commit is contained in:
pingqiu committed 2026-05-02 12:24:28 -07:00
1 parent db595eb843
commit 41e2cab069
2 files changed
+150

No files matched your search

@@ -0,0 +1,53 @@
package engine
// FlowControlObservation is the raw fact bundle used to build
// FlowControlFacts. It mirrors the architecture split:
// - primary storage reports local R/S/H;
// - replica/session feedback reports durable frontiers;
// - sync-quorum misses come from the higher-level durability contract.
//
// This type is intentionally not an Event. It does not change engine state,
// start recovery, advance recycle pins, or affect feeder ownership by itself.
type FlowControlObservation struct {
PrimaryDurableLSN uint64 // primary local R: synced/durable frontier
PrimaryTailLSN uint64 // primary local S: retained WAL tail
PrimaryHeadLSN uint64 // primary local H: accepted WAL head
PrimaryBoundsKnown bool
SlowestReplicaDurableLSN uint64
SlowestReplicaDurableKnown bool
SessionDurableLSN uint64
SessionDurableKnown bool
SyncQuorumMisses uint64
ExplicitDurability bool
}
// BuildFlowControlFacts converts raw observations into pressure facts consumed
// by EvaluateFlowControl. Unknown primary bounds deliberately leave LSN-derived
// gauges at zero; callers must not invent pressure from incomplete facts.
func BuildFlowControlFacts(obs FlowControlObservation) FlowControlFacts {
facts := FlowControlFacts{
SyncQuorumMisses: obs.SyncQuorumMisses,
ExplicitDurability: obs.ExplicitDurability,
}
if !obs.PrimaryBoundsKnown {
return facts
}
facts.PrimaryFlushLag = saturatingLag(obs.PrimaryDurableLSN, obs.PrimaryHeadLSN)
facts.RetentionPressure = saturatingLag(obs.PrimaryTailLSN, obs.PrimaryHeadLSN)
if obs.SlowestReplicaDurableKnown {
facts.ReplicaDurableLag = saturatingLag(obs.SlowestReplicaDurableLSN, obs.PrimaryHeadLSN)
}
if obs.SessionDurableKnown {
facts.RecoveryBacklog = saturatingLag(obs.SessionDurableLSN, obs.PrimaryHeadLSN)
}
return facts
}
func saturatingLag(frontier, head uint64) uint64 {
if frontier >= head {
return 0
}
return head - frontier
}
@@ -0,0 +1,97 @@
package engine
import "testing"
func TestBuildFlowControlFacts_PrimaryBoundariesDriveFlushAndRetentionPressure(t *testing.T) {
got := BuildFlowControlFacts(FlowControlObservation{
PrimaryDurableLSN: 40,
PrimaryTailLSN: 30,
PrimaryHeadLSN: 100,
PrimaryBoundsKnown: true,
})
if got.PrimaryFlushLag != 60 {
t.Fatalf("PrimaryFlushLag=%d want 60", got.PrimaryFlushLag)
}
if got.RetentionPressure != 70 {
t.Fatalf("RetentionPressure=%d want 70", got.RetentionPressure)
}
}
func TestBuildFlowControlFacts_SlowestDurableAckDrivesReplicaLag(t *testing.T) {
got := BuildFlowControlFacts(FlowControlObservation{
PrimaryDurableLSN: 90,
PrimaryTailLSN: 20,
PrimaryHeadLSN: 100,
PrimaryBoundsKnown: true,
SlowestReplicaDurableLSN: 70,
SlowestReplicaDurableKnown: true,
})
if got.ReplicaDurableLag != 30 {
t.Fatalf("ReplicaDurableLag=%d want 30", got.ReplicaDurableLag)
}
if got.PrimaryFlushLag != 10 {
t.Fatalf("PrimaryFlushLag=%d want 10", got.PrimaryFlushLag)
}
}
func TestBuildFlowControlFacts_SessionDurableAckDrivesRecoveryBacklog(t *testing.T) {
got := BuildFlowControlFacts(FlowControlObservation{
PrimaryDurableLSN: 100,
PrimaryTailLSN: 50,
PrimaryHeadLSN: 120,
PrimaryBoundsKnown: true,
SessionDurableLSN: 80,
SessionDurableKnown: true,
})
if got.RecoveryBacklog != 40 {
t.Fatalf("RecoveryBacklog=%d want 40", got.RecoveryBacklog)
}
}
func TestBuildFlowControlFacts_UnknownPrimaryBoundsDoNotInventLSNPressure(t *testing.T) {
got := BuildFlowControlFacts(FlowControlObservation{
PrimaryHeadLSN: 100,
SlowestReplicaDurableLSN: 10,
SlowestReplicaDurableKnown: true,
SessionDurableLSN: 20,
SessionDurableKnown: true,
SyncQuorumMisses: 3,
ExplicitDurability: true,
})
if got.PrimaryFlushLag != 0 ||
got.RetentionPressure != 0 ||
got.ReplicaDurableLag != 0 ||
got.RecoveryBacklog != 0 {
t.Fatalf("LSN pressure with unknown primary bounds: %+v", got)
}
if got.SyncQuorumMisses != 3 {
t.Fatalf("SyncQuorumMisses=%d want 3", got.SyncQuorumMisses)
}
if !got.ExplicitDurability {
t.Fatal("ExplicitDurability=false want true")
}
}
func TestBuildFlowControlFacts_FrontiersAheadOfHeadSaturateAtZero(t *testing.T) {
got := BuildFlowControlFacts(FlowControlObservation{
PrimaryDurableLSN: 101,
PrimaryTailLSN: 102,
PrimaryHeadLSN: 100,
PrimaryBoundsKnown: true,
SlowestReplicaDurableLSN: 103,
SlowestReplicaDurableKnown: true,
SessionDurableLSN: 104,
SessionDurableKnown: true,
})
if got.PrimaryFlushLag != 0 ||
got.RetentionPressure != 0 ||
got.ReplicaDurableLag != 0 ||
got.RecoveryBacklog != 0 {
t.Fatalf("saturated facts=%+v want all LSN pressures zero", got)
}
}