Repository navigation
Move the resampling buffer to its own class #1501
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| # License: MIT | ||
| # Copyright © 2026 Frequenz Energy-as-a-Service GmbH | ||
|
|
||
| """The buffer holding the samples of a timeseries until they are resampled.""" | ||
|
|
||
| import itertools | ||
| import logging | ||
| from bisect import bisect, bisect_left | ||
| from collections import deque | ||
| from datetime import datetime | ||
| from typing import assert_never | ||
|
|
||
| from ._base_types import SourceProperties | ||
| from ._config import WindowSide | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class _ResamplingBuffer: | ||
| """The buffer holding the samples of a timeseries until they are resampled. | ||
|
|
||
| The buffer can't hold more than `max_len` samples: when it is full, the oldest | ||
| sample is dropped when a new one is added. Otherwise, old samples are only | ||
| removed when asked to, while advancing to the next resampling window. | ||
|
|
||
| It keeps the number of received and dropped samples, the windows without | ||
| relevant samples, and the time of the first sample, up to date in the | ||
| source properties. It logs when relevant samples may have been dropped or | ||
| when it holds more than `warn_len` samples, and when it has no relevant | ||
| samples, once per gap in the data. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| name: str, | ||
| *, | ||
| max_len: int, | ||
| warn_len: int, | ||
| source_properties: SourceProperties, | ||
| ) -> None: | ||
| """Initialize an instance. | ||
|
|
||
| Args: | ||
| name: The name of the timeseries (for logging purposes). | ||
| max_len: The maximum number of samples the buffer can hold. | ||
| warn_len: The number of samples above which a warning is logged. | ||
| source_properties: The properties of the timeseries to keep up to date. | ||
| """ | ||
| self._name = name | ||
| self._samples: deque[tuple[datetime, float]] = deque(maxlen=max_len) | ||
| self._warn_len = warn_len | ||
| self._source_properties = source_properties | ||
| self._removed_samples = 0 | ||
| """The number of samples removed because they were too old.""" | ||
| self._logged_dropped = False | ||
| """Whether dropping relevant samples was already logged.""" | ||
| self._logged_too_big = False | ||
| """Whether the buffer holding more than `warn_len` was already logged.""" | ||
|
|
||
| def __len__(self) -> int: | ||
| """Return the number of samples in the buffer. | ||
|
|
||
| Returns: | ||
| The number of samples in the buffer. | ||
| """ | ||
| return len(self._samples) | ||
|
|
||
| def add(self, sample: tuple[datetime, float]) -> None: | ||
| """Add a new sample to the buffer. | ||
|
|
||
| Args: | ||
| sample: The sample to add. | ||
| """ | ||
| self._samples.append(sample) | ||
| if self._source_properties.sampling_start is None: | ||
| self._source_properties.sampling_start = sample[0] | ||
| self._source_properties.received_samples += 1 | ||
|
|
||
| def advance_window( | ||
| self, | ||
| start: datetime, | ||
| end: datetime, | ||
| closed: WindowSide, | ||
| *, | ||
| remove_older: bool, | ||
| ) -> list[tuple[datetime, float]]: | ||
| """Advance to the next resampling window and return its relevant samples. | ||
|
|
||
| It must be called once for each resampled window, as it also updates the | ||
| dropped samples and the windows without relevant samples in the source | ||
| properties, logs the problems it finds, and removes the samples before | ||
| the relevant ones when asked to. | ||
|
|
||
| Args: | ||
| start: The start of the period of the relevant samples. | ||
| end: The end of the period of the relevant samples. | ||
| closed: Which side of the period between `start` and `end` includes | ||
| the samples exactly at its edge. | ||
| remove_older: Whether to remove the samples before the relevant ones | ||
| from the buffer. | ||
|
|
||
| Returns: | ||
| The relevant samples, oldest first. | ||
| """ | ||
| match closed: | ||
|
shsms marked this conversation as resolved.
Resolved
|
||
| case WindowSide.LEFT: | ||
| bisect_func = bisect_left | ||
| case WindowSide.RIGHT: | ||
| bisect_func = bisect | ||
| case unexpected: | ||
| assert_never(unexpected) | ||
|
|
||
| min_index = bisect_func(self._samples, start, key=lambda s: s[0]) | ||
| max_index = bisect_func(self._samples, end, key=lambda s: s[0]) | ||
|
|
||
| # Dropped samples are counted here, as counting them when adding samples | ||
| # would slow down adding samples, which must be as fast as possible. | ||
| self._update_dropped_samples(min_index) | ||
|
|
||
| # Using itertools for slicing doesn't look very efficient, but | ||
| # experiments with a custom (ring) buffer that can slice showed that | ||
| # it is not that bad. See: | ||
| # https://github.com/frequenz-floss/frequenz-sdk-python/pull/130 | ||
| # So if we need more performance beyond this point, we probably need to | ||
| # resort to some C (or similar) implementation. | ||
| relevant_samples = list(itertools.islice(self._samples, min_index, max_index)) | ||
| # Missing samples are logged once per gap in the data, not for each window. | ||
| props = self._source_properties | ||
| if not relevant_samples: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: if a device stops sending for good, there's now one warning and then nothing, and the gap's length is only logged if samples come back (or never, if the timeseries is removed first). The commit explains why not to log it only once, which makes sense. Did you consider repeating it with a backoff (e.g. after 10, 100, 1000 windows) with the current count?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, I didn't consider it for this PR. In frequenz-floss/frequenz-client-base-python#301 we only log the recovery after 5 minutes without drops, to avoid logging over and over when a buffer is at the edge. We could do something similar here, but if this becomes a common pattern I'd rather have a shared solution, like the log rate limiting we discussed in #1132 (comment). A generic rate limiter can't add details like the number of windows, but it could simplify things quite a bit. I'd leave this as is for now, and bring up log rate limiting as a general topic in a meeting. |
||
| props.total_windows_without_samples += 1 | ||
| props.current_windows_without_samples += 1 | ||
| if props.current_windows_without_samples == 1: | ||
| self._log_no_relevant_samples(start, end) | ||
| elif props.current_windows_without_samples: | ||
| _logger.info( | ||
| "Relevant samples found for timeseries %s after %s windows " | ||
| "without them", | ||
| self._name, | ||
| props.current_windows_without_samples, | ||
| ) | ||
| props.current_windows_without_samples = 0 | ||
|
|
||
| if remove_older: | ||
| for _ in range(min_index): | ||
| self._samples.popleft() | ||
| self._removed_samples += min_index | ||
| self._log_too_big() | ||
|
|
||
| return relevant_samples | ||
|
|
||
| def _update_dropped_samples(self, min_index: int) -> None: | ||
| """Update the number of dropped samples, logging once if needed ones may be. | ||
|
|
||
| Args: | ||
| min_index: The index of the oldest relevant sample in the buffer. | ||
| """ | ||
| props = self._source_properties | ||
| dropped = props.received_samples - self._removed_samples - len(self._samples) | ||
| new_drops = dropped > props.dropped_samples | ||
| props.dropped_samples = dropped | ||
| # Dropped samples are older than the oldest one in the buffer, so they can | ||
| # only be needed if the oldest one in the buffer is not older than the | ||
| # relevant ones. Their timestamps are not kept, so they may also be older | ||
| # than the relevant ones. | ||
| if self._logged_dropped or not new_drops or min_index > 0: | ||
| return | ||
| self._logged_dropped = True | ||
| _logger.error( | ||
| "The buffer for timeseries %s is full (max_buffer_len=%s), so samples " | ||
| "needed for resampling may have been dropped. Use a bigger " | ||
| "max_buffer_len.", | ||
| self._name, | ||
| self._samples.maxlen, | ||
| ) | ||
|
|
||
| def _log_too_big(self) -> None: | ||
| """Log, once, if the buffer holds more than `warn_len` samples.""" | ||
| if self._logged_too_big or len(self._samples) <= self._warn_len: | ||
| return | ||
| self._logged_too_big = True | ||
| _logger.warning( | ||
| "The buffer for timeseries %s holds %s samples, more than " | ||
| "warn_buffer_len (%s)", | ||
| self._name, | ||
| len(self._samples), | ||
| self._warn_len, | ||
| ) | ||
|
|
||
| def _log_no_relevant_samples(self, start: datetime, end: datetime) -> None: | ||
| """Log that no relevant samples were found. | ||
|
|
||
| Args: | ||
| start: The start of the period of the relevant samples. | ||
| end: The end of the period of the relevant samples. | ||
| """ | ||
| if not _logger.isEnabledFor(logging.WARNING): | ||
|
shsms marked this conversation as resolved.
Resolved
|
||
| return | ||
|
|
||
| if self._samples: | ||
| buffer_info = ( | ||
| f"{self._samples[0][0]} to {self._samples[-1][0]}, " | ||
| f"{len(self._samples)} samples" | ||
| ) | ||
| else: | ||
| buffer_info = "empty" | ||
|
|
||
| _logger.warning( | ||
| "No relevant samples found for timeseries %s between %s and %s " | ||
| "(buffer: %s)", | ||
| self._name, | ||
| start, | ||
| end, | ||
| buffer_info, | ||
| ) | ||
Uh oh!
There was an error while loading. Please reload this page.