obzenflow_runtime 0.2.4

Runtime services for ObzenFlow - execution and coordination business logic
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// SPDX-License-Identifier: MIT OR Apache-2.0
// SPDX-FileCopyrightText: 2025-2026 ObzenFlow Contributors
// https://obzenflow.dev

//! One metrics-supervisor-owned sampler, independent of durable accounting.

use super::observations::{ObservationOwner, ObservationRegistry};
use obzenflow_core::event::context::StageType;
use obzenflow_core::event::observability::{CaptureReason, CaptureScope};
use obzenflow_core::metrics::{StageMetadata, ThroughputMeasurement, ThroughputSnapshot};
use obzenflow_core::time::MetricsDuration;
use obzenflow_core::StageId;
use std::collections::HashMap;
use tokio::time::Instant;

#[derive(Clone, Copy, Hash, PartialEq, Eq)]
enum Subject {
    Stage(StageId),
    Input,
    Output,
}

struct Baseline {
    scope: CaptureScope,
    count: u64,
    at: Instant,
}

#[derive(Default)]
pub(crate) struct ThroughputSampler {
    owner: Option<ObservationOwner>,
    previous: HashMap<Subject, Baseline>,
    pub(crate) latest: ThroughputSnapshot,
}

impl ThroughputSampler {
    pub(crate) fn new(owner: Option<ObservationOwner>) -> Self {
        Self {
            owner,
            ..Self::default()
        }
    }

    fn observe(&mut self, subject: Subject, scope: CaptureScope, count: u64, at: Instant) {
        let Some(previous) = self
            .previous
            .get(&subject)
            .filter(|previous| previous.scope == scope && previous.count <= count)
        else {
            self.previous.insert(subject, Baseline { scope, count, at });
            return;
        };
        let delta = count - previous.count;
        let Some(elapsed) = at
            .checked_duration_since(previous.at)
            .filter(|value| !value.is_zero())
        else {
            return;
        };
        let Ok(nanos) = u64::try_from(elapsed.as_nanos()) else {
            return;
        };
        let rate = delta as f64 / elapsed.as_secs_f64();
        if !rate.is_finite() {
            return;
        }
        let Some(packet) = self
            .owner
            .as_ref()
            .and_then(|owner| owner.capture(CaptureReason::Periodic))
        else {
            return;
        };
        self.previous.insert(subject, Baseline { scope, count, at });
        let measurement = ThroughputMeasurement {
            capture: packet.capture,
            event_delta: delta,
            elapsed: MetricsDuration::from_nanos(nanos),
            events_per_second: rate,
        };
        match subject {
            Subject::Stage(stage) => {
                self.latest.stages.insert(stage, measurement);
            }
            Subject::Input => self.latest.flow_input = Some(measurement),
            Subject::Output => self.latest.flow_output = Some(measurement),
        }
    }

    pub(crate) fn sample(
        &mut self,
        observations: &ObservationRegistry,
        metadata: &HashMap<StageId, StageMetadata>,
        at: Instant,
    ) {
        self.sample_counters(observations.live_counters(), metadata, at);
    }

    fn sample_counters(
        &mut self,
        counters: HashMap<StageId, (CaptureScope, u64)>,
        metadata: &HashMap<StageId, StageMetadata>,
        at: Instant,
    ) {
        if !self
            .owner
            .as_ref()
            .is_some_and(ObservationOwner::measurements_allowed)
        {
            return;
        }
        let Some(scope) = self.owner.as_ref().map(ObservationOwner::scope) else {
            return;
        };
        for (subject, input) in [(Subject::Input, true), (Subject::Output, false)] {
            let required: Vec<_> = metadata
                .iter()
                .filter(|(_, metadata)| {
                    if input {
                        matches!(
                            metadata.stage_type,
                            StageType::FiniteSource | StageType::InfiniteSource
                        )
                    } else {
                        metadata.stage_type == StageType::Sink
                    }
                })
                .map(|(stage, _)| stage)
                .collect();
            if required.is_empty() {
                continue;
            }
            // A reset of one member must not be hidden by another member's
            // growth, even when the aggregate is temporarily unavailable.
            if required.iter().any(|stage| {
                counters
                    .get(stage)
                    .zip(self.previous.get(&Subject::Stage(**stage)))
                    .is_some_and(|((sample_scope, count), previous)| {
                        *sample_scope != previous.scope || *count < previous.count
                    })
            }) {
                self.previous.remove(&subject);
            }
            let total = required.into_iter().try_fold(0u64, |total, stage| {
                let (sample_scope, count) = counters.get(stage)?;
                (*sample_scope == scope).then_some(())?;
                total.checked_add(*count)
            });
            if let Some(total) = total {
                self.observe(subject, scope, total, at);
            }
        }
        for stage in metadata.keys() {
            if let Some((sample_scope, count)) = counters
                .get(stage)
                .filter(|(sample_scope, _)| *sample_scope == scope)
            {
                self.observe(Subject::Stage(*stage), *sample_scope, *count, at);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::execution::{RuntimeExecution, RuntimeMode};
    use crate::metrics::instrumentation::StageInstrumentation;
    use obzenflow_core::{FlowId, ReaderGeneration, SystemId};
    use std::sync::{atomic::Ordering, Arc};
    use std::time::Duration;

    fn sampler(flow: FlowId) -> (ThroughputSampler, CaptureScope) {
        let execution = RuntimeExecution::new(RuntimeMode::Live, None);
        let scope = CaptureScope {
            flow_id: flow,
            resume_generation: ReaderGeneration(0),
        };
        let owner = execution.observations().capture_owner(
            scope,
            SystemId::new().into(),
            execution.clone(),
        );
        (ThroughputSampler::new(Some(owner)), scope)
    }

    fn metadata(stages: &[(StageId, StageType)], flow: FlowId) -> HashMap<StageId, StageMetadata> {
        stages
            .iter()
            .map(|(stage, stage_type)| {
                (
                    *stage,
                    StageMetadata {
                        name: stage.to_string(),
                        stage_type: *stage_type,
                        reference_mode: None,
                        flow_name: "throughput".into(),
                        flow_id: Some(flow),
                    },
                )
            })
            .collect()
    }

    #[tokio::test(start_paused = true)]
    async fn actual_elapsed_missing_reads_zero_reset_and_scope_keep_whole_bundles() {
        let flow = FlowId::new();
        let stage = StageId::new();
        let (mut sampler, scope) = sampler(flow);
        let metadata = metadata(&[(stage, StageType::Transform)], flow);
        let start = Instant::now();
        sampler.sample_counters(HashMap::from([(stage, (scope, 100))]), &metadata, start);
        assert!(sampler.latest.stages.is_empty());
        sampler.sample_counters(
            HashMap::from([(stage, (scope, 121))]),
            &metadata,
            start + Duration::from_millis(500),
        );
        let measured = sampler.latest.stages[&stage].clone();
        assert_eq!(measured.event_delta, 21);
        assert_eq!(measured.elapsed, MetricsDuration::from_millis(500));
        assert_eq!(measured.events_per_second, 42.0);
        assert!(measured.capture.observer.is_system());
        sampler.sample_counters(HashMap::new(), &metadata, start + Duration::from_secs(10));
        assert_eq!(sampler.latest.stages[&stage], measured);
        sampler.sample_counters(
            HashMap::from([(stage, (scope, 163))]),
            &metadata,
            start + Duration::from_millis(1500),
        );
        assert_eq!(sampler.latest.stages[&stage].events_per_second, 42.0);
        assert_eq!(
            sampler.latest.stages[&stage].elapsed,
            MetricsDuration::from_secs(1)
        );
        // A zero-time read is not a baseline or a fabricated zero.
        sampler.sample_counters(
            HashMap::from([(stage, (scope, 170))]),
            &metadata,
            start + Duration::from_millis(1500),
        );
        sampler.sample_counters(
            HashMap::from([(stage, (scope, 163))]),
            &metadata,
            start + Duration::from_millis(2000),
        );
        let zero = sampler.latest.stages[&stage].clone();
        assert_eq!(zero.event_delta, 0);
        assert_eq!(zero.events_per_second, 0.0);
        sampler.sample_counters(
            HashMap::from([(stage, (scope, 1))]),
            &metadata,
            start + Duration::from_secs(3),
        );
        assert_eq!(sampler.latest.stages[&stage], zero);
        sampler.sample_counters(
            HashMap::from([(stage, (scope, 22))]),
            &metadata,
            start + Duration::from_millis(3500),
        );
        assert_eq!(sampler.latest.stages[&stage].events_per_second, 42.0);
        let before_scope = sampler.latest.stages[&stage].clone();
        let owner = sampler.owner.as_mut().unwrap();
        let execution = RuntimeExecution::new(RuntimeMode::Live, None);
        let new_scope = CaptureScope {
            resume_generation: ReaderGeneration(1),
            ..scope
        };
        *owner = execution.observations().capture_owner(
            new_scope,
            SystemId::new().into(),
            execution.clone(),
        );
        sampler.sample_counters(
            HashMap::from([(stage, (new_scope, 1000))]),
            &metadata,
            start + Duration::from_secs(4),
        );
        assert_eq!(sampler.latest.stages[&stage], before_scope);
        sampler.sample_counters(
            HashMap::from([(stage, (new_scope, 1021))]),
            &metadata,
            start + Duration::from_millis(4500),
        );
        assert_eq!(
            sampler.latest.stages[&stage].capture.capture_scope,
            new_scope
        );
        assert_eq!(sampler.latest.stages[&stage].event_delta, 21);
        let (mut restarted, _) = self::sampler(flow);
        restarted.sample_counters(HashMap::from([(stage, (scope, 1021))]), &metadata, start);
        assert!(restarted.latest.stages.is_empty());
    }

    #[tokio::test(start_paused = true)]
    async fn aggregates_require_all_members_and_cannot_hide_a_member_reset() {
        let flow = FlowId::new();
        let a = StageId::new();
        let b = StageId::new();
        let sink = StageId::new();
        let transform = StageId::new();
        let (mut sampler, scope) = sampler(flow);
        let metadata = metadata(
            &[
                (a, StageType::FiniteSource),
                (b, StageType::InfiniteSource),
                (sink, StageType::Sink),
                (transform, StageType::Transform),
            ],
            flow,
        );
        let counters = |a_count, b_count, sink_count| {
            HashMap::from([
                (a, (scope, a_count)),
                (b, (scope, b_count)),
                (sink, (scope, sink_count)),
                (transform, (scope, 9000)),
            ])
        };
        let start = Instant::now();
        sampler.sample_counters(counters(10, 20, 30), &metadata, start);
        sampler.sample_counters(
            counters(20, 30, 35),
            &metadata,
            start + Duration::from_secs(1),
        );
        let input = sampler.latest.flow_input.clone().unwrap();
        let retained_b = sampler.latest.stages[&b].clone();
        assert_eq!(input.events_per_second, 20.0);
        assert_eq!(
            sampler
                .latest
                .flow_output
                .as_ref()
                .unwrap()
                .events_per_second,
            5.0
        );
        let mut partial = counters(25, 35, 40);
        partial.remove(&b);
        sampler.sample_counters(partial, &metadata, start + Duration::from_secs(2));
        assert_eq!(sampler.latest.flow_input.as_ref(), Some(&input));
        assert_eq!(sampler.latest.stages[&b], retained_b);
        assert_ne!(sampler.latest.stages[&a].capture, retained_b.capture);
        sampler.sample_counters(
            counters(30, 40, 45),
            &metadata,
            start + Duration::from_secs(3),
        );
        assert_eq!(sampler.latest.flow_input.as_ref().unwrap().event_delta, 20);
        assert_eq!(
            sampler.latest.flow_input.as_ref().unwrap().elapsed,
            MetricsDuration::from_secs(2)
        );
        let before_reset = sampler.latest.flow_input.clone();
        sampler.sample_counters(
            counters(1, 100, 50),
            &metadata,
            start + Duration::from_secs(4),
        );
        assert_eq!(sampler.latest.flow_input, before_reset);
        sampler.sample_counters(
            counters(6, 105, 55),
            &metadata,
            start + Duration::from_secs(5),
        );
        assert_eq!(
            sampler
                .latest
                .flow_input
                .as_ref()
                .unwrap()
                .events_per_second,
            10.0
        );
    }

    #[tokio::test(start_paused = true)]
    async fn replay_cannot_seed_live_baseline_and_histogram_contention_cannot_block_counters() {
        let flow = FlowId::new();
        let stage = StageId::new();
        let (mut sampler, _) = sampler(flow);
        let metadata = metadata(&[(stage, StageType::Sink)], flow);
        let start = Instant::now();
        let replay = RuntimeExecution::new(RuntimeMode::Replay, None);
        let replay_metrics = Arc::new(StageInstrumentation::new());
        replay_metrics.bind_observations(flow, stage.into(), &replay);
        replay_metrics
            .events_processed_total
            .store(1_000_000, Ordering::Relaxed);
        sampler.sample(replay.observations(), &metadata, start);
        assert!(sampler.previous.is_empty());
        let live = RuntimeExecution::new(RuntimeMode::Live, None);
        let metrics = Arc::new(StageInstrumentation::new());
        metrics.bind_observations(flow, stage.into(), &live);
        metrics
            .events_processed_total
            .store(1_000_000, Ordering::Relaxed);
        let _histogram = metrics.processing_time_histogram.write().unwrap();
        sampler.sample(
            live.observations(),
            &metadata,
            start + Duration::from_secs(1),
        );
        assert!(sampler.latest.stages.is_empty());
        metrics
            .events_processed_total
            .store(1_000_021, Ordering::Relaxed);
        sampler.sample(
            live.observations(),
            &metadata,
            start + Duration::from_millis(1500),
        );
        assert_eq!(sampler.latest.stages[&stage].events_per_second, 42.0);
    }
}