RFC: Add ResamplerStack to coordinate stacked resamplers#1348
RFC: Add ResamplerStack to coordinate stacked resamplers#1348Marenz wants to merge 1 commit intofrequenz-floss:v1.x.xfrom
Conversation
Add Resampler.trigger() method and ResamplerStack class to solve the timing issue when stacking resamplers (resampling already-resampled data). When both resamplers fire simultaneously, the higher-level one may process before the lower-level one emits its boundary sample. Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
|
After a meeting, I think we are going for a simpler approach, right? I would try as much as possible to add a solution that introduces more complexity and requires inter-resampler synchronization. For the short term, IIUC we are going to just abuse the resampler max_sample_windows (or whatever is called) to set it to a large value and use a resampling function that only looks at the first few windows, so we make sure all samples arrived and were calculated before we get that sample (fixing the timestamp of the sample too). In the future we think this problem should go away once we can have reporting data directly from the microgrid API. Once we have that, we need a different resampler for sure, the current real-time resampler is attached to the wall clock and won't work for historical data anyways, so then we can take a more batched approach. |

This PR proposes a solution to the stacked resampler timing issue. I'm opening this as an RFC to discuss the approach before finalizing.
Problem
When resampling already-resampled data (e.g., 1s samples → 15min aggregates), there's a race condition at window boundaries. If both resamplers fire at the same moment, the higher-level resampler may process BEFORE the lower-level one has emitted its boundary sample.
Minimal reproduction
Impact
sumormax: error compoundsProposed Solution
This PR adds:
Resampler.trigger(timestamp)- Allows external control of when resampling occurs, without waiting for the internal timer.ResamplerStack- Coordinates multiple resamplers:Usage
Alternatives Considered
Add
asyncio.sleep(0)yields insideResampler.resample()- Tried this but it doesn't reliably solve the problem because both resamplers enter the timer loop before either processes.Emit samples slightly before window boundary - Would require changing timestamp semantics.
Use open intervals (samples at exactly window_end go to next window) - Breaking change to current behavior.
Have higher-level resamplers wait longer - Fragile, doesn't guarantee ordering.
Questions for Discussion
Is
ResamplerStackthe right abstraction? Should this be handled differently?Should the continuous mode use
WallClockTimerinstead ofTimerfor better alignment?Should we also provide a way to automatically detect stacked resamplers (e.g., via channel inspection)?
Is exporting
Resamplerpublicly the right approach, or should users only useResamplerStack?