Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 34 additions & 16 deletions llo/dev/v31/blobpump.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,14 @@ const (
BlobBroadcastAttempts = 3
// BlobBroadcastRetryBackoff is the wait before the second broadcast attempt,
// doubled for each attempt after it. All attempts share the cycle's
// observation timeout, so this cannot extend how long a cycle runs.
// broadcast timeout, so this cannot extend how long a cycle runs.
BlobBroadcastRetryBackoff = 50 * time.Millisecond
// DefaultBlobBroadcastTimeoutMultiplier scales the pump's observation
// timeout into the default broadcast timeout. BroadcastBlob returns only
// once a quorum of peers has fetched, stored and signed the blob, at least
// two network round trips, so it gets its own deadline rather than what
// DataSource.Observe left of the observation timeout.
DefaultBlobBroadcastTimeoutMultiplier = 5
// MissStreakLogThreshold is how many consecutive rounds may find no usable
// snapshot before the pump escalates from debug to error logging. A node
// that never contributes stream values is a silent failure otherwise.
Expand All @@ -61,8 +67,8 @@ const (
BlobReapingMarginRounds = 16
// MinPerOracleUnexpiredBlobCount is the floor for the derived budget.
MinPerOracleUnexpiredBlobCount = 32
// closeTimeoutSlackMultiplier scales the pump's observation timeout into how
// long Close waits for an in-flight cycle.
// closeTimeoutSlackMultiplier scales the longer of the pump's observation
// and broadcast timeouts into how long Close waits for an in-flight cycle.
closeTimeoutSlackMultiplier = 5
// minCloseTimeout fixes the minimum time Close waits waits for an in-flight cycle.
minCloseTimeout = 1 * time.Second
Expand Down Expand Up @@ -178,12 +184,15 @@ type blobPump struct {

// blobPumpParams is the pump's configuration, resolved by the factory.
type blobPumpParams struct {
bbf ocr3_1types.BlobBroadcastFetcher
ds DataSource
configDigest ocrtypes.ConfigDigest
donID uint32
verboseLogging bool
bbf ocr3_1types.BlobBroadcastFetcher
ds DataSource
configDigest ocrtypes.ConfigDigest
donID uint32
verboseLogging bool
// observationTimeout is the deadline given to DataSource.Observe.
observationTimeout time.Duration
// broadcastTimeout bounds every broadcast attempt of a cycle together.
broadcastTimeout time.Duration
// blobLifetimeRounds is the fetchability bound. See DefaultBlobLifetimeRounds.
blobLifetimeRounds uint64
// inFlightWait bounds how long Take waits for an in-flight cycle to park.
Expand All @@ -200,7 +209,7 @@ func newBlobPump(lggr logger.Logger, params blobPumpParams) *blobPump {
ctx: ctx,
cancel: cancel,
metrics: newBlobPumpMetrics(params.donID),
closeTimeout: max(params.observationTimeout*closeTimeoutSlackMultiplier, minCloseTimeout),
closeTimeout: max(max(params.observationTimeout, params.broadcastTimeout)*closeTimeoutSlackMultiplier, minCloseTimeout),
}
}

Expand Down Expand Up @@ -240,7 +249,7 @@ func (p *blobPump) Close() bool {
return true
case <-time.After(p.closeTimeout):
p.lggr.Errorw("Blob pump cycle did not unwind after context cancellation; abandoning goroutine",
"closeTimeout", p.closeTimeout, "observationTimeout", p.observationTimeout)
"closeTimeout", p.closeTimeout, "observationTimeout", p.observationTimeout, "broadcastTimeout", p.broadcastTimeout)
return false
}
}
Expand Down Expand Up @@ -521,12 +530,9 @@ func (p *blobPump) observeStreamValuesAndBroadcastBlob(in pumpInput) (*blobSnaps
sv[sid] = nil
}

ctx, cancel := context.WithTimeout(p.ctx, p.observationTimeout)
defer cancel()

observedAt := time.Now()
opts := datasource.NewDSOpts(p.verboseLogging, in.seqNr, p.configDigest, observedAt, in.lifeCycleStage)
if err := p.ds.Observe(ctx, sv, opts); err != nil {
if err := p.observe(sv, opts); err != nil {
return nil, fmt.Errorf("DataSource.Observe error: %w", err)
}
// Broadcast only the requested streams. Peers reject an observation over
Expand Down Expand Up @@ -556,7 +562,12 @@ func (p *blobPump) observeStreamValuesAndBroadcastBlob(in pumpInput) (*blobSnaps
return nil, fmt.Errorf("no stream values observed for %d streams", len(in.streams))
}

handle, expirationHintSeqNr, err := p.broadcast(ctx, payload, in.seqNr)
// The broadcast gets its own deadline. The data source scales its cache to
// the observation timeout, and may spend all of it, so sharing that budget
// would leave the broadcast too little time to collect a quorum.
broadcastCtx, cancel := context.WithTimeout(p.ctx, p.broadcastTimeout)
defer cancel()
handle, expirationHintSeqNr, err := p.broadcast(broadcastCtx, payload, in.seqNr)
if err != nil {
return nil, err
}
Expand All @@ -574,8 +585,15 @@ func (p *blobPump) observeStreamValuesAndBroadcastBlob(in pumpInput) (*blobSnaps
}, nil
}

// observe runs DataSource.Observe under the observation timeout.
func (p *blobPump) observe(sv protocol.StreamValues, opts datasource.DSOpts) error {
ctx, cancel := context.WithTimeout(p.ctx, p.observationTimeout)
defer cancel()
return p.ds.Observe(ctx, sv, opts)
}

// broadcast hands the payload to the blob transport, retrying a failed
// broadcast within the cycle's own timeout, and returns the handle together
// broadcast within the cycle's broadcast timeout, and returns the handle together
// with the expiration hint it was broadcast under.
//
// cycleStartSeqNr is the round the values were gathered for. The expiration hint is
Expand Down
54 changes: 53 additions & 1 deletion llo/dev/v31/blobpump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func testPump(t *testing.T, ds DataSource, bbf ocr3_1types.BlobBroadcastFetcher)
configDigest: ocrtypes.ConfigDigest{1},
verboseLogging: true,
observationTimeout: tests.WaitTimeout(t),
broadcastTimeout: tests.WaitTimeout(t),
blobLifetimeRounds: DefaultBlobLifetimeRounds,
})
p.Start()
Expand Down Expand Up @@ -616,9 +617,10 @@ func Test_blobPump_CloseDoesNotHangOnStuckDataSource(t *testing.T) {
bbf: newFakeBroadcaster(),
ds: ds,
observationTimeout: time.Minute,
broadcastTimeout: time.Second,
blobLifetimeRounds: DefaultBlobLifetimeRounds,
})
require.Equal(t, closeTimeoutSlackMultiplier*time.Minute, p.closeTimeout, "closeTimeout must derive from the observation timeout")
require.Equal(t, closeTimeoutSlackMultiplier*time.Minute, p.closeTimeout, "closeTimeout must derive from the longer of the cycle timeouts")
p.closeTimeout = 100 * time.Millisecond
p.Start()

Expand Down Expand Up @@ -713,6 +715,56 @@ func Test_blobPump_BroadcastRetryRefreshesExpiry(t *testing.T) {
require.EqualValues(t, 5+DefaultBlobLifetimeRounds, snap.blobExpirationHintSeqNr)
}

// slowDataSource spends its whole deadline before returning values, as the
// chainlink data source can on its first call.
type slowDataSource struct{ vals protocol.StreamValues }

func (s *slowDataSource) Observe(ctx context.Context, sv protocol.StreamValues, opts DSOpts) error {
<-ctx.Done()
for id := range sv {
sv[id] = s.vals[id]
}
return nil
}

// deadlineBroadcaster fails a broadcast whose context is already done, as the
// libocr blob endpoint does.
type deadlineBroadcaster struct {
*llotest.BlobBroadcastFetcher
}

func (d deadlineBroadcaster) BroadcastBlob(ctx context.Context, payload []byte, hint ocr3_1types.BlobExpirationHint) (ocr3_1types.BlobHandle, error) {
if err := ctx.Err(); err != nil {
return ocr3_1types.BlobHandle{}, err
}
return d.BlobBroadcastFetcher.BroadcastBlob(ctx, payload, hint)
}

// Test_blobPump_BroadcastHasItsOwnDeadline asserts a DataSource that uses its
// whole observation timeout does not leave the broadcast with an expired
// context.
func Test_blobPump_BroadcastHasItsOwnDeadline(t *testing.T) {
ds := &slowDataSource{vals: protocol.StreamValues{100: protocol.ToDecimal(decimal.NewFromInt(7))}}
bc := deadlineBroadcaster{newFakeBroadcaster()}

p := newBlobPump(logger.Test(t), blobPumpParams{
bbf: bc,
ds: ds,
observationTimeout: 20 * time.Millisecond,
broadcastTimeout: tests.WaitTimeout(t),
blobLifetimeRounds: DefaultBlobLifetimeRounds,
})
p.Start()
t.Cleanup(func() { assert.True(t, p.Close(), "pump did not stop cleanly") })

p.SetInput(pumpInputFor(2))
_, _ = p.Take(t.Context(), 2)

require.Eventually(t, func() bool { return p.Cycles() >= 1 }, tests.WaitTimeout(t), 10*time.Millisecond)
snap, reason := p.Take(t.Context(), 3)
require.NotNil(t, snap, reason)
}

// Test_blobPump_BroadcastRetriesAreBounded asserts a transport that stays down
// costs a bounded number of attempts and parks nothing.
func Test_blobPump_BroadcastRetriesAreBounded(t *testing.T) {
Expand Down
18 changes: 14 additions & 4 deletions llo/dev/v31/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,15 @@ type PluginFactoryParams struct {
// bound staleness, the state transition does. Must be within
// [MinBlobLifetimeRounds, MaxBlobLifetimeRounds].
BlobLifetimeRounds uint64
// MaxDurationBlobObservation overrides the pump's per-cycle observation
// budget (default: cfg.MaxDurationObservation, the deadline v30 passes to
// DataSource.Observe). The data source scales its cache TTL and refresh
// cadence with this budget, so a larger value serves older values.
// MaxDurationBlobObservation overrides the deadline the pump gives
// DataSource.Observe (default: cfg.MaxDurationObservation, the deadline v30
// passes to DataSource.Observe). The data source scales its cache TTL and
// refresh cadence with this budget, so a larger value serves older values.
MaxDurationBlobObservation time.Duration
// MaxDurationBlobBroadcast overrides the deadline shared by every broadcast
// attempt of a pump cycle (default: DefaultBlobBroadcastTimeoutMultiplier *
// the resolved MaxDurationBlobObservation).
MaxDurationBlobBroadcast time.Duration
// BlobInFlightWaitFactor overrides defaultBlobInFlightWaitFactor if
// non-zero. Divides cfg.MaxDurationObservation into how long Take waits for
// a cycle already in flight to park, so a larger factor waits less. Must
Expand Down Expand Up @@ -140,6 +144,11 @@ func (f *PluginFactory) NewReportingPlugin(ctx context.Context, cfg ocr3types.Re
blobObservationTimeout = cfg.MaxDurationObservation
}

blobBroadcastTimeout := f.MaxDurationBlobBroadcast
if blobBroadcastTimeout <= 0 {
blobBroadcastTimeout = DefaultBlobBroadcastTimeoutMultiplier * blobObservationTimeout
}

p := &Plugin{
freshnessMetrics: newFreshnessMetrics(f.DonID),
precursorMetrics: newPrecursorMetrics(f.DonID),
Expand Down Expand Up @@ -183,6 +192,7 @@ func (f *PluginFactory) NewReportingPlugin(ctx context.Context, cfg ocr3types.Re
donID: f.DonID,
verboseLogging: f.Config.VerboseLogging,
observationTimeout: blobObservationTimeout,
broadcastTimeout: blobBroadcastTimeout,
inFlightWait: cfg.MaxDurationObservation / time.Duration(blobInFlightWaitFactor),
blobLifetimeRounds: blobLifetimeRounds,
})
Expand Down
33 changes: 33 additions & 0 deletions llo/dev/v31/flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,42 @@ func Test_Factory_NewReportingPlugin_maxDurationObservation(t *testing.T) {
require.NoError(t, err)
pump := p.(*Plugin).pump
require.Equal(t, time.Second, pump.observationTimeout)
require.Equal(t, DefaultBlobBroadcastTimeoutMultiplier*time.Second, pump.broadcastTimeout)
require.Equal(t, time.Second/time.Duration(defaultBlobInFlightWaitFactor), pump.inFlightWait)
require.NoError(t, p.Close())
})

t.Run("derives the broadcast timeout from the blob observation override", func(t *testing.T) {
f := NewPluginFactory(PluginFactoryParams{
OnchainConfigCodec: mockOnchainConfigCodec{},
Logger: logger.Test(t),
MaxDurationBlobObservation: 250 * time.Millisecond,
})
cfg := base
cfg.MaxDurationObservation = time.Second
p, _, err := f.NewReportingPlugin(ctx, cfg, nil)
require.NoError(t, err)
pump := p.(*Plugin).pump
require.Equal(t, 250*time.Millisecond, pump.observationTimeout)
require.Equal(t, DefaultBlobBroadcastTimeoutMultiplier*250*time.Millisecond, pump.broadcastTimeout)
require.NoError(t, p.Close())
})

t.Run("applies the blob broadcast override", func(t *testing.T) {
f := NewPluginFactory(PluginFactoryParams{
OnchainConfigCodec: mockOnchainConfigCodec{},
Logger: logger.Test(t),
MaxDurationBlobBroadcast: 3 * time.Second,
})
cfg := base
cfg.MaxDurationObservation = time.Second
p, _, err := f.NewReportingPlugin(ctx, cfg, nil)
require.NoError(t, err)
pump := p.(*Plugin).pump
require.Equal(t, time.Second, pump.observationTimeout)
require.Equal(t, 3*time.Second, pump.broadcastTimeout)
require.NoError(t, p.Close())
})
}

func Test_Factory_NewReportingPlugin(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions llo/dev/v31/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func attachPump(t *testing.T, p *Plugin, ds DataSource, bbf ocr3_1types.BlobBroa
configDigest: p.ConfigDigest,
verboseLogging: true,
observationTimeout: tests.WaitTimeout(t),
broadcastTimeout: tests.WaitTimeout(t),
blobLifetimeRounds: DefaultBlobLifetimeRounds,
})
p.pump.Start()
Expand Down
11 changes: 9 additions & 2 deletions llo/pluginconfig/v31config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,14 @@ type V31Config struct {
// bound staleness, the state transition does.
BlobLifetimeRounds uint64 `json:"blobLifetimeRounds" toml:"blobLifetimeRounds"`

// MaxDurationBlobObservation is the blob pump's per-cycle observation
// budget. Zero uses the OCR MaxDurationObservation, as v30 does.
// MaxDurationBlobObservation is the deadline the blob pump gives
// DataSource.Observe. Zero uses the OCR MaxDurationObservation, as v30 does.
MaxDurationBlobObservation Duration `json:"maxDurationBlobObservation" toml:"maxDurationBlobObservation"`

// MaxDurationBlobBroadcast is the deadline shared by every broadcast
// attempt of a blob pump cycle. Zero uses 5 * MaxDurationBlobObservation.
MaxDurationBlobBroadcast Duration `json:"maxDurationBlobBroadcast" toml:"maxDurationBlobBroadcast"`

// BlobInFlightWaitFactor divides MaxDurationObservation into how long Take
// waits for a cycle already in flight to park, so a larger factor waits
// less.
Expand Down Expand Up @@ -99,5 +103,8 @@ func (o V31Config) Validate() (merr error) {
if o.MaxDurationBlobObservation < 0 {
merr = errors.Join(merr, fmt.Errorf("llo: v31: MaxDurationBlobObservation must not be negative, got: %s", o.MaxDurationBlobObservation))
}
if o.MaxDurationBlobBroadcast < 0 {
merr = errors.Join(merr, fmt.Errorf("llo: v31: MaxDurationBlobBroadcast must not be negative, got: %s", o.MaxDurationBlobBroadcast))
}
return merr
}
4 changes: 4 additions & 0 deletions llo/pluginconfig/v31config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func Test_V31Config_Unmarshal(t *testing.T) {
verboseLogging = true
blobLifetimeRounds = 8
maxDurationBlobObservation = "1s"
maxDurationBlobBroadcast = "2s"
blobInFlightWaitFactor = 4
captureOutcomeTelemetry = true
captureReportTelemetry = true
Expand All @@ -43,6 +44,7 @@ func Test_V31Config_Unmarshal(t *testing.T) {
assert.True(t, pc.V31.VerboseLogging)
assert.Equal(t, uint64(8), pc.V31.BlobLifetimeRounds)
assert.Equal(t, time.Second, pc.V31.MaxDurationBlobObservation.Duration())
assert.Equal(t, 2*time.Second, pc.V31.MaxDurationBlobBroadcast.Duration())
assert.Equal(t, uint64(4), pc.V31.BlobInFlightWaitFactor)
assert.True(t, pc.V31.CaptureOutcomeTelemetry)
assert.True(t, pc.V31.CaptureReportTelemetry)
Expand Down Expand Up @@ -100,9 +102,11 @@ func Test_V31Config_Validate(t *testing.T) {
t.Run("rejects a negative duration", func(t *testing.T) {
pc := v31Base()
pc.V31.MaxDurationBlobObservation = Duration(-time.Second)
pc.V31.MaxDurationBlobBroadcast = Duration(-time.Second)
err := pc.Validate()
require.Error(t, err)
assert.Contains(t, err.Error(), "MaxDurationBlobObservation must not be negative")
assert.Contains(t, err.Error(), "MaxDurationBlobBroadcast must not be negative")
})

t.Run("rejects the block on OCR3.0", func(t *testing.T) {
Expand Down
Loading