Skip to content

Fix the resampler dropping relevant samples - #1498

Merged
llucax merged 2 commits into
frequenz-floss:v1.x.xfrom
llucax:fix-resampler-buffer-eviction
Nov 30, 2026
Merged

llucax merged 2 commits into
frequenz-floss:v1.x.xfrom
llucax:fix-resampler-buffer-eviction

Conversation

@llucax

@llucax llucax commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

The resampling buffer was a deque sized from the input period, so every new sample pushed out the oldest one, even when the window being resampled still needed it. Because of this, the first windows after a start used only the last initial_buffer_len samples (#1471). A sample of the current window could also be dropped when a sample of the next one arrived before the window was resampled, for example with tick_delay.

Now old samples are removed by time when resampling, and the buffer only drops samples when it holds max_buffer_len of them. The first commit adds SourceProperties.dropped_samples, so dropped samples are visible, and the fix uses it to log an error when needed samples may have been dropped. That error can't be exact, because the timestamps of dropped samples aren't kept, and checking each added sample made adding samples about 50% slower.

After a backwards wall clock jump, WallClockTimer can tick again for an older timestamp. Old samples are already removed by then, so that repeated tick now gives None instead of repeating the previous value. I added this to #1478, which is about repeated ticks after wall clock jumps.

Fixes #1471.

@github-actions github-actions Bot added part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests part:data-pipeline Affects the data pipeline labels Nov 27, 2026
@llucax
llucax marked this pull request as ready for review November 27, 2026 13:37
@llucax
llucax requested a review from a team as a code owner November 27, 2026 13:37
@llucax
llucax requested review from daniel-zullo-frequenz and removed request for a team November 27, 2026 13:37
@llucax llucax self-assigned this Nov 27, 2026
@llucax llucax added this to the v1.0.0-rc2217 milestone Nov 27, 2026
@llucax
llucax enabled auto-merge November 27, 2026 14:47
The resampling buffer drops its oldest sample when a new one arrives
and it is full, and nothing tells users that samples were lost.

SourceProperties now has a dropped_samples field with the number of
samples dropped this way. It is calculated when resampling, as the
received samples minus the ones in the buffer, because counting in
add_sample() would slow down adding samples, which must be as fast as
possible.

For now, the buffer is sized to hold only the relevant samples, so
dropping is also how old samples are removed and this counts them too.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
The resampling buffer was a deque with a maximum length calculated
from the resampling period, the input period and
max_data_age_in_periods, so every new sample pushed out the oldest one,
even if it was still needed. This dropped samples in two cases:

- Before the input period was estimated, the buffer had
  initial_buffer_len slots, so the first windows after a start were
  resampled from only the last initial_buffer_len samples (frequenz-floss#1471).
- A sample of the next window arriving after a tick, but before the
  window of that tick was resampled (because of tick_delay or a late
  wakeup), pushed out a sample of that window.

Now the buffer can hold max_buffer_len samples, and samples older than
the relevant ones are removed when resampling, as they won't be
relevant for later windows either. Until the input period is estimated,
the look-back can still grow, so samples are only removed once it is
known. Making the buffer bigger to cover the second case was not an
option: a whole extra period would double the buffer of a 15-minute
window, and anything smaller would be a guess.

This changes what the buffer settings mean: initial_buffer_len is now
the number of samples to receive before estimating the input period,
warn_buffer_len warns when a buffer holds more samples than that after
removing old ones, and max_buffer_len logs an error when samples needed
to resample may have been dropped. Both messages are logged once per
timeseries.

SourceProperties.dropped_samples now leaves out the samples removed
because they were too old, so it only counts the samples dropped
because the buffer held max_buffer_len samples. That counter is also
how the resampler finds out that samples were dropped, as checking
when adding samples made adding them about 50% slower.

The timestamps of dropped samples are not kept, so the resampler
can't know whether they were needed. It logs the error when new
samples were dropped and the oldest sample in the buffer is not older
than the relevant ones, as then the dropped samples can be relevant
too. Guessing their timestamps from the input period would be wrong
with irregular samples, both missing real losses and reporting
harmless ones as certain.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
@llucax
llucax force-pushed the fix-resampler-buffer-eviction branch from b827c07 to b7132ce Compare November 27, 2026 14:48
@llucax llucax changed the title Fix the resampler dropping samples a window needs Fix the resampler dropping relevant samples Nov 27, 2026
@llucax
llucax added this pull request to the merge queue Nov 30, 2026
@github-project-automation github-project-automation Bot moved this from To do to Review approved in Python SDK Roadmap Nov 30, 2026
Merged via the queue into frequenz-floss:v1.x.x with commit e418a12 Nov 30, 2026
9 checks passed
@llucax
llucax deleted the fix-resampler-buffer-eviction branch November 30, 2026 11:40
@github-project-automation github-project-automation Bot moved this from Review approved to Done in Python SDK Roadmap Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

part:data-pipeline Affects the data pipeline part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests

Projects

Development

Successfully merging this pull request may close these issues.

Resampler drops samples before its buffer is sized, so the first window after a start is wrong

2 participants