Repository navigation
Fix the resampler dropping relevant samples - #1498
Merged
llucax merged 2 commits intoNov 30, 2026
Merged
Conversation
llucax
marked this pull request as ready for review
November 27, 2026 13:37
llucax
requested review from
daniel-zullo-frequenz
and removed request for
a team
November 27, 2026 13:37
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
force-pushed
the
fix-resampler-buffer-eviction
branch
from
November 27, 2026 14:48
b827c07 to
b7132ce
Compare
cwasicki
approved these changes
Nov 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_lensamples (#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 withtick_delay.Now old samples are removed by time when resampling, and the buffer only drops samples when it holds
max_buffer_lenof them. The first commit addsSourceProperties.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,
WallClockTimercan tick again for an older timestamp. Old samples are already removed by then, so that repeated tick now givesNoneinstead of repeating the previous value. I added this to #1478, which is about repeated ticks after wall clock jumps.Fixes #1471.