From 85f74f7052f6ff686b25f2f2085c466715df32bb Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sat, 2 May 2026 12:26:29 -0700 Subject: [PATCH] adapter: record flow control verdicts without acting --- seaweed-block/core/adapter/adapter.go | 33 ++++++ .../core/adapter/flow_control_test.go | 107 ++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 seaweed-block/core/adapter/flow_control_test.go diff --git a/seaweed-block/core/adapter/adapter.go b/seaweed-block/core/adapter/adapter.go index 53df0afcdc..5d1faf1db3 100644 --- a/seaweed-block/core/adapter/adapter.go +++ b/seaweed-block/core/adapter/adapter.go @@ -37,6 +37,10 @@ type VolumeReplicaAdapter struct { fenceWatchdogs map[fenceKey]*time.Timer watchdogLog []WatchdogEvent + flowControlPolicy engine.FlowControlPolicy + lastFlowControlVerdict engine.FlowControlVerdict + flowControlObserved bool + // fenceInFlight: at most one fence attempt outstanding per // fence LINEAGE — keyed by (replicaID, epoch, endpointVersion). // Value is the sessionID of the in-flight fence; used to match @@ -142,6 +146,35 @@ func (a *VolumeReplicaAdapter) OnDurableAck(result DurableAckResult) ApplyLog { return a.applyBatchAndExecute([]engine.Event{NormalizeDurableAck(result)}, "DurableAck") } +// SetFlowControlPolicy installs the primary write-pressure policy used by +// OnFlowControlObservation. This does not change write behavior by itself; +// it only controls the recorded verdict for tests/diagnostics/future wiring. +func (a *VolumeReplicaAdapter) SetFlowControlPolicy(policy engine.FlowControlPolicy) { + a.mu.Lock() + defer a.mu.Unlock() + a.flowControlPolicy = policy +} + +// OnFlowControlObservation records the current primary write-pressure verdict. +// It is intentionally not part of the engine event stream: flow-control facts +// do not start recovery, do not advance WAL pins, and do not feed replicas. +func (a *VolumeReplicaAdapter) OnFlowControlObservation(obs engine.FlowControlObservation) engine.FlowControlVerdict { + facts := engine.BuildFlowControlFacts(obs) + a.mu.Lock() + defer a.mu.Unlock() + verdict := engine.EvaluateFlowControl(a.flowControlPolicy, facts) + a.lastFlowControlVerdict = verdict + a.flowControlObserved = true + return verdict +} + +// FlowControlVerdict returns the last recorded write-pressure verdict. +func (a *VolumeReplicaAdapter) FlowControlVerdict() (engine.FlowControlVerdict, bool) { + a.mu.Lock() + defer a.mu.Unlock() + return a.lastFlowControlVerdict, a.flowControlObserved +} + // OnFenceComplete processes a fence outcome from the transport. // Releases the in-flight slot (keyed by lineage) and normalizes to // FenceCompleted (success) or FenceFailed (failure) into the engine. diff --git a/seaweed-block/core/adapter/flow_control_test.go b/seaweed-block/core/adapter/flow_control_test.go new file mode 100644 index 0000000000..cd12b3e055 --- /dev/null +++ b/seaweed-block/core/adapter/flow_control_test.go @@ -0,0 +1,107 @@ +package adapter + +import ( + "testing" + + "github.com/seaweedfs/seaweed-block/core/engine" +) + +func TestAdapter_FlowControlObservation_PrimaryFlushLagRecordsShapeWithoutCommands(t *testing.T) { + exec := newMockExecutor() + a := NewVolumeReplicaAdapter(exec) + a.SetFlowControlPolicy(engine.FlowControlPolicy{ + MaxPrimaryFlushLag: 10, + MaxReplicaDurableLag: 10, + MaxRetentionPressure: 20, + }) + + before := len(a.CommandLog()) + got := a.OnFlowControlObservation(engine.FlowControlObservation{ + PrimaryDurableLSN: 10, + PrimaryTailLSN: 5, + PrimaryHeadLSN: 30, + PrimaryBoundsKnown: true, + SlowestReplicaDurableLSN: 1, + SlowestReplicaDurableKnown: true, + }) + + if got.Action != engine.FlowControlShapePrimaryWrites { + t.Fatalf("Action=%s want %s", got.Action, engine.FlowControlShapePrimaryWrites) + } + if got.Reason != engine.FlowControlReasonPrimaryFlushLag { + t.Fatalf("Reason=%s want %s", got.Reason, engine.FlowControlReasonPrimaryFlushLag) + } + if after := len(a.CommandLog()); after != before { + t.Fatalf("flow-control observation emitted commands: before=%d after=%d log=%v", + before, after, a.CommandLog()) + } +} + +func TestAdapter_FlowControlObservation_ExplicitDurabilityFailureRecordsOnly(t *testing.T) { + exec := newMockExecutor() + a := NewVolumeReplicaAdapter(exec) + a.SetFlowControlPolicy(engine.FlowControlPolicy{MaxPrimaryFlushLag: 10}) + + got := a.OnFlowControlObservation(engine.FlowControlObservation{ + PrimaryDurableLSN: 10, + PrimaryHeadLSN: 30, + PrimaryBoundsKnown: true, + ExplicitDurability: true, + }) + + if got.Action != engine.FlowControlFailDurabilityWrite { + t.Fatalf("Action=%s want %s", got.Action, engine.FlowControlFailDurabilityWrite) + } + if len(a.CommandLog()) != 0 { + t.Fatalf("flow-control verdict must not execute commands; log=%v", a.CommandLog()) + } +} + +func TestAdapter_FlowControlObservation_ReplicaRetentionRiskRecordsDegradeWithoutPublishing(t *testing.T) { + exec := newMockExecutor() + a := NewVolumeReplicaAdapter(exec) + a.SetFlowControlPolicy(engine.FlowControlPolicy{ + MaxReplicaDurableLag: 10, + MaxRetentionPressure: 20, + }) + + got := a.OnFlowControlObservation(engine.FlowControlObservation{ + PrimaryDurableLSN: 95, + PrimaryTailLSN: 70, + PrimaryHeadLSN: 100, + PrimaryBoundsKnown: true, + SlowestReplicaDurableLSN: 80, + SlowestReplicaDurableKnown: true, + }) + + if got.Action != engine.FlowControlDegradeSlowReplica { + t.Fatalf("Action=%s want %s", got.Action, engine.FlowControlDegradeSlowReplica) + } + if got.Reason != engine.FlowControlReasonReplicaRetentionRisk { + t.Fatalf("Reason=%s want %s", got.Reason, engine.FlowControlReasonReplicaRetentionRisk) + } + if hasAdapterCommand(a, "PublishDegraded") { + t.Fatalf("flow-control verdict must not publish degraded directly; log=%v", a.CommandLog()) + } +} + +func TestAdapter_FlowControlVerdictReturnsLastRecordedVerdict(t *testing.T) { + exec := newMockExecutor() + a := NewVolumeReplicaAdapter(exec) + if _, ok := a.FlowControlVerdict(); ok { + t.Fatal("FlowControlVerdict observed=true before any observation") + } + + a.SetFlowControlPolicy(engine.FlowControlPolicy{MaxSyncQuorumMisses: 2}) + want := a.OnFlowControlObservation(engine.FlowControlObservation{ + SyncQuorumMisses: 3, + }) + + got, ok := a.FlowControlVerdict() + if !ok { + t.Fatal("FlowControlVerdict observed=false after observation") + } + if got != want { + t.Fatalf("FlowControlVerdict=%+v want %+v", got, want) + } +}