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
4 changes: 4 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

- `SourceProperties` has a new `dropped_samples` field, with the number of samples the resampler dropped because its buffer was full.

- `SourceProperties` has new `total_windows_without_samples` and `current_windows_without_samples` fields, with the number of resampling windows without relevant samples in total and in the current gap in the data. They tell how healthy a timeseries is, and whether it is in a gap right now. The windows before the first sample was received are counted too.

## Bug Fixes

<!-- Here goes notable bug fixes that are worth a special mention or explanation -->
Expand All @@ -23,3 +25,5 @@
- `initial_buffer_len` is the number of samples to receive before estimating the input sampling period.
- `warn_buffer_len` logs a warning, once per timeseries, when a buffer holds more samples than this after removing old ones.
- `max_buffer_len` logs an error, once per timeseries, when samples needed to resample may have been dropped because the buffer was full.

- The resampler no longer logs a warning at every resampling while a timeseries has no relevant samples. It logs it once when the samples stop, and logs when they are back, with the number of windows without them.
18 changes: 18 additions & 0 deletions src/frequenz/sdk/timeseries/_resampling/_base_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,21 @@ class SourceProperties:
It is updated when resampling, so it doesn't include the samples dropped
since the last resampling.
"""

total_windows_without_samples: int = 0
Comment thread
shsms marked this conversation as resolved.
Resolved
"""The number of resampling windows that had no relevant samples.

The resampled value of these windows is `None`. The windows before the first
sample was received are counted too. It is updated when resampling.
"""

current_windows_without_samples: int = 0
Comment thread
shsms marked this conversation as resolved.
Resolved
"""The number of resampling windows without relevant samples in the current gap.

It is `0` when the last resampled window had relevant samples, so it tells
whether the timeseries currently has a gap in its data, and for how long. It
is updated when resampling.

Resampling functions are only called for windows with relevant samples, so
they always see `0` here.
"""
214 changes: 214 additions & 0 deletions src/frequenz/sdk/timeseries/_resampling/_buffer.py
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:
Comment thread
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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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):
Comment thread
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,
)
Loading