adapter: record flow control verdicts without acting

This commit is contained in:
pingqiu committed 2026-05-02 12:26:29 -07:00
1 parent 41e2cab069
commit 85f74f7052
2 files changed
+140

No files matched your search

+33
View File
@@ -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.
@@ -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)
}
}