From 0a08b9238c0a4017ec5e5323cb8a3ceb6fce7e95 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sat, 2 May 2026 12:31:22 -0700 Subject: [PATCH] adapter: expose flow control diagnostics --- seaweed-block/core/adapter/adapter.go | 20 +++++++++++++++ .../core/adapter/flow_control_test.go | 25 +++++++++++++++++++ .../flow_control_observation_test.go | 5 ++++ 3 files changed, 50 insertions(+) diff --git a/seaweed-block/core/adapter/adapter.go b/seaweed-block/core/adapter/adapter.go index 5d1faf1db3..6843c831de 100644 --- a/seaweed-block/core/adapter/adapter.go +++ b/seaweed-block/core/adapter/adapter.go @@ -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() diff --git a/seaweed-block/core/adapter/flow_control_test.go b/seaweed-block/core/adapter/flow_control_test.go index 248de1843c..f9ea687fcc 100644 --- a/seaweed-block/core/adapter/flow_control_test.go +++ b/seaweed-block/core/adapter/flow_control_test.go @@ -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", diff --git a/seaweed-block/core/replication/component/flow_control_observation_test.go b/seaweed-block/core/replication/component/flow_control_observation_test.go index 83da13abb9..552b40a809 100644 --- a/seaweed-block/core/replication/component/flow_control_observation_test.go +++ b/seaweed-block/core/replication/component/flow_control_observation_test.go @@ -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) + } }) }