adapter: expose flow control diagnostics

This commit is contained in:
pingqiu committed 2026-05-02 12:31:22 -07:00
1 parent 75f748b079
commit 0a08b9238c
3 files changed
+50

No files matched your search

+20
View File
@@ -307,6 +307,26 @@ func (a *VolumeReplicaAdapter) Projection() engine.ReplicaProjection {
return engine.DeriveProjection(&a.state)
}
// Diagnostics is an adapter-local inspection snapshot. It may include runtime
// observations that are NOT engine truth (for example flow-control verdicts).
// Callers must not feed this back into the engine as control input.
type Diagnostics struct {
Projection engine.ReplicaProjection
FlowControlObserved bool
FlowControlVerdict engine.FlowControlVerdict
}
// Diagnostics returns a point-in-time adapter inspection snapshot.
func (a *VolumeReplicaAdapter) Diagnostics() Diagnostics {
a.mu.Lock()
defer a.mu.Unlock()
return Diagnostics{
Projection: engine.DeriveProjection(&a.state),
FlowControlObserved: a.flowControlObserved,
FlowControlVerdict: a.lastFlowControlVerdict,
}
}
// CommandLog returns all commands executed so far (for testing).
func (a *VolumeReplicaAdapter) CommandLog() []string {
a.mu.Lock()
@@ -106,6 +106,31 @@ func TestAdapter_FlowControlVerdictReturnsLastRecordedVerdict(t *testing.T) {
}
}
func TestAdapter_DiagnosticsIncludesFlowControlWithoutChangingProjection(t *testing.T) {
exec := newMockExecutor()
a := NewVolumeReplicaAdapter(exec)
a.SetFlowControlPolicy(engine.FlowControlPolicy{MaxPrimaryFlushLag: 10})
before := a.Projection()
want := a.OnFlowControlObservation(engine.FlowControlObservation{
PrimaryDurableLSN: 1,
PrimaryHeadLSN: 20,
PrimaryBoundsKnown: true,
})
diag := a.Diagnostics()
if !diag.FlowControlObserved {
t.Fatal("Diagnostics().FlowControlObserved=false want true")
}
if diag.FlowControlVerdict != want {
t.Fatalf("Diagnostics().FlowControlVerdict=%+v want %+v", diag.FlowControlVerdict, want)
}
if diag.Projection != before {
t.Fatalf("flow-control diagnostics changed projection: before=%+v after=%+v",
before, diag.Projection)
}
}
func TestFlowControlObservationFromDurableAck_UsesPrimaryBoundariesAndAck(t *testing.T) {
obs := FlowControlObservationFromDurableAck(90, 30, 100, DurableAckResult{
ReplicaID: "r1",
@@ -35,6 +35,7 @@ func TestComponent_FlowControlObservation_FromStorageBoundariesAndDurableAck(t *
DurableLSN: primaryH - 1,
})
got := a.OnFlowControlObservation(obs)
diag := a.Diagnostics()
if got.Action != engine.FlowControlShapePrimaryWrites {
t.Fatalf("Action=%s want %s (primary local flush lag should dominate)",
@@ -47,6 +48,10 @@ func TestComponent_FlowControlObservation_FromStorageBoundariesAndDurableAck(t *
t.Fatalf("flow-control observation changed command log: before=%d after=%d log=%v",
beforeCommands, afterCommands, a.CommandLog())
}
if !diag.FlowControlObserved || diag.FlowControlVerdict != got {
t.Fatalf("Diagnostics flow-control=(observed=%v verdict=%+v), want %+v",
diag.FlowControlObserved, diag.FlowControlVerdict, got)
}
})
}