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
7 changes: 4 additions & 3 deletions llo/dev/v31/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,10 @@
// hot state carries the agreed timestamps of the last few rounds, and each
// contribution is held to a round age and a time age derived from those
// (freshness.go). A stream whose fresh contributions cannot reach the
// contribution floor aggregates every contribution instead, which is the
// behaviour without the rule, so a bound that proves too tight for a DON costs
// freshness, never liveness. The fallback is counted (llo_freshness metrics).
// contribution floor has no aggregate that round. Contributions past either
// bound are never re-admitted. Only the freshest contributions of unknown age,
// from before the timestamp window fills, are, as many as it takes to reach the
// floor. The fallback is counted (llo_freshness metrics).
//
// # Parity status (vs v30)
//
Expand Down
116 changes: 74 additions & 42 deletions llo/dev/v31/freshness.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package llo

import (
"cmp"
"math"
"slices"
"sort"
"strconv"
"time"
Expand Down Expand Up @@ -34,15 +36,27 @@ import (
// cycle's round is a lower bound on when the values were gathered, since
// the cycle started after the previous round committed. It catches a stall:
// seqNr stands still while time passes, so round age alone would call
// values gathered before the stall one round old. It is skipped while the
// round interval is unknown, during the first rounds after a fresh state.
// values gathered before the stall one round old.
//
// A cycle that started before the window has no known timestamp. The oldest
// window entry stands in for it, which can only understate the age because
// agreed timestamps never decrease: agreedObservationTimestamp holds the
// previous one against a regressing median. The time bound is skipped while the
// round interval is unknown. It is also weak while the window is short, since
// a window of two entries can estimate the interval from a stall gap alone.
// That only happens in the first rounds after a fresh state.
//
// Contributions stale by rounds or by time are never aggregated: after a DON
// wide stall every contribution is old, and admitting them would report
// pre-stall values under the post-stall timestamp. The stream has no aggregate
// that round instead, as it had when the pump withheld the snapshot.
//
// A per stream fallback keeps liveness only where the age is unknown: when the
// fresh contributions cannot reach minContributions, the freshest ones whose
// cycle started before the agreed timestamp window are aggregated too, only as
// many as it takes to reach it. That happens during the first rounds after a
// fresh state, while the window fills.
//
// A per stream fallback keeps liveness: when the fresh contributions cannot
// reach minContributions, the ones stale by rounds are aggregated too, as
// before this rule existed. Contributions stale by time never come back: after
// a DON wide stall every contribution is old, and re-admitting them would
// report pre-stall values under the post-stall timestamp. The stream has no
// aggregate that round instead, as it had when the pump withheld the snapshot.
// There is no persisted freshness state and no mode.
//
// The agreed timestamps come from a window the hot state carries, one entry
Expand Down Expand Up @@ -80,14 +94,15 @@ type freshnessStats struct {
// streamsFiltered is how many streams aggregated fresh contributions only,
// leaving at least one stale contribution out.
streamsFiltered int
// streamsFallenBack is how many streams re-admitted contributions stale by
// rounds because the fresh ones missed minContributions.
// streamsFallenBack is how many streams re-admitted contributions of unknown
// age because the fresh ones missed minContributions.
streamsFallenBack int
// contributionsStale is how many contributions were judged stale, whether
// or not the fallback let them in.
contributionsStale int
// staleByRounds, staleByTime and staleBeforeWindow split contributionsStale
// by the bound that rejected first.
// staleByRounds, staleByTime and staleBeforeWindow split contributionsStale.
// The time bound is checked first, so a contribution past both bounds counts
// as stale by time.
staleByRounds int
staleByTime int
staleBeforeWindow int
Expand Down Expand Up @@ -143,10 +158,10 @@ func roundIntervalEstimate(window []uint64, configured time.Duration) time.Durat
// selectFreshContributions decides, per stream, which contributions this round
// aggregates. A contribution is fresh when its round age and its time age are
// both within bound. A roundInterval of zero disables the time bound. See the
// package comment above. The fresh set is used when
// it reaches minContributions, otherwise the contributions stale by rounds are
// re-admitted. Values keep their input order, so the result does not depend on
// how the split went.
// package comment above. The fresh set is used when it reaches
// minContributions, otherwise the freshest contributions of unknown age are
// re-admitted, only as many as it takes to reach it. Values keep their input
// order, so the result does not depend on how the split went.
//
// Every input is replicated: seqNr, the agreed timestamps, the configured
// interval from the shared OCR config, and a cycle start seqNr the sender can
Expand All @@ -165,42 +180,59 @@ func selectFreshContributions(
out := make(map[llotypes.StreamID][]protocol.StreamValue, len(byStream))

for sid, contributions := range byStream {
fresh := make([]protocol.StreamValue, 0, len(contributions))
// fallback is every contribution the fallback may re-admit: the fresh
// ones and those stale by rounds only. Stale by time never comes back.
fallback := make([]protocol.StreamValue, 0, len(contributions))
stale := 0
for _, c := range contributions {
// keep marks the contributions this stream aggregates. spare lists, by
// index, the ones the fallback may add. These are the contributions of
// unknown age. Stale by rounds or time never comes back.
keep := make([]bool, len(contributions))
spare := make([]int, 0, len(contributions))
fresh := 0
for i, c := range contributions {
// A cycle that started before the window has no known timestamp, but
// the oldest entry bounds it from above, so the age is at least
// roundTs minus that entry.
cycleTs, inWindow := agreedTimestampAt(window, seqNr, c.cycleStartSeqNr)
ageKnown := inWindow
if !inWindow && c.cycleStartSeqNr <= seqNr && len(window) > 0 {
cycleTs, ageKnown = window[0], true
}
switch {
case ageKnown && maxAge > 0 && roundTs > cycleTs && roundTs-cycleTs > maxAge:
stats.staleByTime++
case c.cycleStartSeqNr > seqNr || seqNr-c.cycleStartSeqNr > MaxStreamValueAgeRounds:
stats.staleByRounds++
fallback = append(fallback, c.value)
case !inWindow:
stats.staleBeforeWindow++
spare = append(spare, i)
default:
cycleTs, ok := agreedTimestampAt(window, seqNr, c.cycleStartSeqNr)
if !ok {
stats.staleBeforeWindow++
fallback = append(fallback, c.value)
} else if maxAge > 0 && roundTs > cycleTs && roundTs-cycleTs > maxAge {
stats.staleByTime++
} else {
fresh = append(fresh, c.value)
fallback = append(fallback, c.value)
continue
}
keep[i] = true
fresh++
}
stale++
}
stale := len(contributions) - fresh
stats.contributionsStale += stale
missing := minContributions - fresh
switch {
case stale == 0:
out[sid] = fresh
case len(fresh) >= minContributions || len(fallback) == len(fresh):
stats.streamsFiltered++
out[sid] = fresh
default:
case missing > 0 && len(spare) > 0:
// Freshest first. The sort is stable, so equal ages keep their input
// order.
slices.SortStableFunc(spare, func(x, y int) int {
return cmp.Compare(contributions[y].cycleStartSeqNr, contributions[x].cycleStartSeqNr)
})
for _, i := range spare[:min(missing, len(spare))] {
keep[i] = true
}
stats.streamsFallenBack++
out[sid] = fallback
default:
stats.streamsFiltered++
}
values := make([]protocol.StreamValue, 0, len(contributions))
for i, c := range contributions {
if keep[i] {
values = append(values, c.value)
}
}
out[sid] = values
}
return out, stats
}
Expand All @@ -219,7 +251,7 @@ var (
Namespace: "llo",
Subsystem: "freshness",
Name: "fallback_streams_total",
Help: "Streams that re-admitted contributions stale by rounds because the fresh ones could not reach min contributions.",
Help: "Streams that re-admitted contributions of unknown age because the fresh ones could not reach min contributions.",
}, []string{"donID"})
freshnessRoundIntervalMetric = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: "llo",
Expand Down
59 changes: 53 additions & 6 deletions llo/dev/v31/freshness_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,46 @@ func Test_selectFreshContributions(t *testing.T) {
require.Equal(t, 1, stats.staleByRounds)
})

t.Run("falls back to contributions stale by rounds below min contributions", func(t *testing.T) {
t.Run("stale by rounds is never re-admitted", func(t *testing.T) {
in := map[llotypes.StreamID][]streamContribution{1: {contribution(1, 20), contribution(9, 1), contribution(2, 20)}}
out, stats := selectFreshContributions(in, seqNr, roundTs, window, interval, 3)
require.Equal(t, []int64{1, 9, 2}, values(out[1]), "every contribution, in input order")
require.Equal(t, []int64{1, 2}, values(out[1]))
require.Equal(t, 1, stats.streamsFiltered)
require.Equal(t, 0, stats.streamsFallenBack)
require.Equal(t, 1, stats.staleByRounds)
})

t.Run("no aggregate when every contribution is stale by rounds", func(t *testing.T) {
in := map[llotypes.StreamID][]streamContribution{1: {contribution(1, 1), contribution(2, 2), contribution(3, 3)}}
out, stats := selectFreshContributions(in, seqNr, roundTs, window, interval, 3)
require.Empty(t, out[1])
require.Equal(t, 3, stats.staleByRounds)
require.Equal(t, 1, stats.streamsFiltered)
require.Equal(t, 0, stats.streamsFallenBack)
})

t.Run("falls back to contributions of unknown age below min contributions", func(t *testing.T) {
short := window[len(window)-2:] // rounds 19 and 20 only
in := map[llotypes.StreamID][]streamContribution{1: {contribution(1, 20), contribution(9, 18), contribution(2, 20), contribution(8, 1)}}
out, stats := selectFreshContributions(in, seqNr, roundTs, short, interval, 3)
require.Equal(t, []int64{1, 9, 2}, values(out[1]), "fresh and unknown age, in input order")
require.Equal(t, 0, stats.streamsFiltered)
require.Equal(t, 1, stats.streamsFallenBack)
require.Equal(t, 1, stats.contributionsStale)
require.Equal(t, 2, stats.contributionsStale)
require.Equal(t, 1, stats.staleBeforeWindow)
require.Equal(t, 1, stats.staleByRounds)
})

t.Run("fallback takes the freshest of unknown age and no more", func(t *testing.T) {
short := window[len(window)-2:] // rounds 19 and 20 only
in := map[llotypes.StreamID][]streamContribution{1: {
contribution(1, 20), contribution(7, 16), contribution(8, 17), contribution(9, 18), contribution(6, 18),
}}
out, stats := selectFreshContributions(in, seqNr, roundTs, short, interval, 3)
require.Equal(t, []int64{1, 9, 6}, values(out[1]), "two missing: the freshest two, input order kept")
require.Equal(t, 1, stats.streamsFallenBack)
require.Equal(t, 0, stats.streamsFiltered)
require.Equal(t, 4, stats.contributionsStale)
})

t.Run("stale by time is never re-admitted", func(t *testing.T) {
Expand All @@ -186,6 +219,20 @@ func Test_selectFreshContributions(t *testing.T) {
require.Equal(t, 4, stats.staleByTime)
})

t.Run("stale by rounds and by time is never re-admitted", func(t *testing.T) {
// This round's timestamp jumped, as in a stall, and every contribution
// comes from a cycle that started before the window, so its own
// timestamp is unknown. The oldest window entry still proves it is too
// old.
stalled := roundTs + uint64(MaxStreamValueAgeRounds)*sec + 1
stalledWindow := append(append([]uint64(nil), window[:len(window)-1]...), stalled)
in := map[llotypes.StreamID][]streamContribution{1: {contribution(1, 1), contribution(2, 1), contribution(3, 2), contribution(4, 20-MaxStreamValueAgeRounds-1)}}
out, stats := selectFreshContributions(in, seqNr, stalled, stalledWindow, interval, 3)
require.Empty(t, out[1])
require.Equal(t, 4, stats.staleByTime)
require.Equal(t, 0, stats.streamsFallenBack)
})

t.Run("per stream", func(t *testing.T) {
in := map[llotypes.StreamID][]streamContribution{
1: {contribution(1, 20), contribution(2, 20), contribution(3, 20), contribution(9, 1)},
Expand All @@ -194,10 +241,10 @@ func Test_selectFreshContributions(t *testing.T) {
}
out, stats := selectFreshContributions(in, seqNr, roundTs, window, interval, 3)
require.Equal(t, []int64{1, 2, 3}, values(out[1]))
require.Equal(t, []int64{1, 9, 8}, values(out[2]))
require.Equal(t, []int64{1}, values(out[2]), "stale by rounds is never re-admitted")
require.Equal(t, []int64{1, 2}, values(out[3]))
require.Equal(t, 1, stats.streamsFiltered)
require.Equal(t, 1, stats.streamsFallenBack)
require.Equal(t, 2, stats.streamsFiltered)
require.Equal(t, 0, stats.streamsFallenBack)
require.Equal(t, 3, stats.contributionsStale)
})
}
Expand Down
10 changes: 9 additions & 1 deletion llo/dev/v31/history_flow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,17 @@ func historyExprChannel(expression string) llotypes.ChannelDefinition {

// valueRound feeds 4 identical observations carrying a value for stream 100.
func valueRound(t *testing.T, ts uint64, value int64) []ocrtypes.AttributedObservation {
t.Helper()
return valueRoundAt(t, ts, value, 0)
}

// valueRoundAt is valueRound with the round the cycle started in. Zero keeps
// the fixture default, which is only fresh for the first few rounds.
func valueRoundAt(t *testing.T, ts uint64, value int64, cycleStartSeqNr uint64) []ocrtypes.AttributedObservation {
t.Helper()
obs := Observation{
ObservationTimestampNanoseconds: ts,
CycleStartSeqNr: cycleStartSeqNr,
StreamValues: protocol.StreamValues{100: protocol.ToDecimal(decimal.NewFromInt(value))},
}
aos := make([]ocrtypes.AttributedObservation, 0, 4)
Expand Down Expand Up @@ -118,7 +126,7 @@ func Test_History_EvictsAtDepth(t *testing.T) {

seqNr := uint64(3)
for round := 1; round <= 6; round++ {
_, err := p.StateTransition(ctx, seqNr, ocrtypes.AttributedQuery{}, valueRound(t, uint64(round)*10_000, int64(round)), kv, testBlobs)
_, err := p.StateTransition(ctx, seqNr, ocrtypes.AttributedQuery{}, valueRoundAt(t, uint64(round)*10_000, int64(round), seqNr), kv, testBlobs)
require.NoError(t, err)
seqNr++
}
Expand Down
6 changes: 3 additions & 3 deletions llo/dev/v31/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ func mustEncodeObs(t *testing.T, obs Observation) []byte {
require.NoError(t, err)
return b
}
// A cycle start of 1 is valid for every round and stale after
// MaxStreamValueAgeRounds, where the fallback aggregates every
// contribution anyway. Tests of the freshness rule set it explicitly.
// A cycle start of 1 is fresh only until MaxStreamValueAgeRounds, after
// which the contribution is left out. Tests running past it, and tests of
// the freshness rule, set it explicitly.
cycleStartSeqNr := obs.CycleStartSeqNr
if cycleStartSeqNr == 0 {
cycleStartSeqNr = 1
Expand Down
4 changes: 2 additions & 2 deletions llo/transmitter/dataengine/persistence_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ func TestPersistenceManagerPrune(t *testing.T) {
err = pm.Start(ctx)
require.NoError(t, err)

// Wait for next poll.
observedLogs.TakeAll()
// Only the first poll deletes rows, so it is the only one that logs. Do not
// drain the observed logs here: the poll can run before this goroutine does.
tests.AssertLogEventually(t, observedLogs, "Pruned transmit requests table")

result, err := pm.Load(ctx)
Expand Down
Loading