laser-sdk 0.8.0

LaserData SDK over Apache Iggy: typed streaming, operational and lakehouse query, materialization destinations, key-value state, copy-on-write forks, and an optional agent runtime with the Agent Data Exchange Protocol (AGDX).
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use laser_wire::filter::{
    ExecutionMode, FaultReason, FilteredPage, FilteredStart, SourceGeneration, StopReason,
};
use laser_wire::schema::Digest32;
use std::collections::{BTreeSet, VecDeque};
use tokio::time::Instant;

/// One partition's read position and acknowledgment state.
///
/// Pages are read ahead of acknowledgments. A page's safe offset is stored
/// only once it and every earlier page are fully handled, so an acknowledgment
/// never covers a record the caller has not finished.
pub(crate) struct PartitionProgress {
    original: FilteredStart,
    start: FilteredStart,
    pages: VecDeque<PendingPage>,
    unstored: Option<AckTarget>,
    stored_offset: Option<u64>,
    blocked: Option<Blocked>,
    retry_at: Option<Instant>,
    first_sequence: Option<u64>,
    settled_sequence: u64,
}

/// The offset a completed prefix of pages makes safe to store, with the
/// history and the policy it was read under.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) struct AckTarget {
    pub(crate) group_id: Option<u64>,
    pub(crate) offset: u64,
    pub(crate) generation: SourceGeneration,
    pub(crate) digest: Option<Digest32>,
    pub(crate) mode: ExecutionMode,
    pub(crate) policy_generation: u64,
}

/// Why a partition cannot be read past its current position.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Blocked {
    Fault { offset: u64, reason: FaultReason },
    Oversized { offset: u64 },
}

struct PendingPage {
    sequence: u64,
    target: Option<AckTarget>,
    outstanding: BTreeSet<u64>,
}

impl PartitionProgress {
    pub(crate) fn new(start: FilteredStart) -> Self {
        Self {
            original: start.clone(),
            start,
            pages: VecDeque::new(),
            unstored: None,
            stored_offset: None,
            blocked: None,
            retry_at: None,
            first_sequence: None,
            settled_sequence: 0,
        }
    }

    /// Whether the partition waits out a delay after an error or a block, so
    /// one failing partition neither starves the others nor spins the reader.
    pub(crate) fn waiting(&self, now: Instant) -> bool {
        self.retry_at.is_some_and(|retry_at| now < retry_at)
    }

    pub(crate) const fn defer(&mut self, until: Instant) {
        self.retry_at = Some(until);
    }

    /// Where the next read starts.
    pub(crate) const fn start(&self) -> &FilteredStart {
        &self.start
    }

    pub(crate) const fn blocked(&self) -> Option<Blocked> {
        self.blocked
    }

    /// Pages read but not yet fully handled.
    pub(crate) fn in_flight(&self) -> usize {
        self.pages.len()
    }

    pub(crate) fn knows(&self, sequence: u64) -> bool {
        self.pages.iter().any(|page| page.sequence == sequence)
            || self
                .first_sequence
                .is_some_and(|first| sequence >= first && sequence <= self.settled_sequence)
    }

    /// Track a page and its returned `offsets`, and continue after it. A
    /// blocked partition is read again after its delay, so a block ends once
    /// the record ages out of the partition or the policy stops faulting.
    ///
    /// A page without records waits only for the pages before it, so behind
    /// an unfinished page it folds its safe offset into the newest one
    /// instead of queueing. Only pages that returned records hold a slot, and
    /// a caller holding one page on a quiet partition never fills the queue.
    pub(crate) fn record(
        &mut self,
        sequence: u64,
        page: &FilteredPage,
        offsets: impl IntoIterator<Item = u64>,
    ) {
        self.retry_at = None;
        self.first_sequence.get_or_insert(sequence);
        self.start = page.next_start(&self.original);
        self.blocked = match (page.stop, page.fault) {
            (StopReason::Fault, Some(fault)) => Some(Blocked::Fault {
                offset: fault.offset,
                reason: fault.reason,
            }),
            (StopReason::OversizedRecord, Some(fault)) => Some(Blocked::Oversized {
                offset: fault.offset,
            }),
            _ => None,
        };
        let target = page.safe_ack_offset.map(|offset| AckTarget {
            group_id: page.policy.group_id,
            offset,
            generation: page.generation,
            digest: page.policy.digest.clone(),
            mode: page.policy.mode,
            policy_generation: page.policy.policy_generation,
        });
        let outstanding: BTreeSet<u64> = offsets.into_iter().collect();
        match self.pages.back_mut() {
            Some(newest) if outstanding.is_empty() => {
                if target.is_some() {
                    newest.target = target;
                }
            }
            _ => self.pages.push_back(PendingPage {
                sequence,
                target,
                outstanding,
            }),
        }
        self.settle();
    }

    /// Mark one returned record handled. A record already settled is a no-op.
    pub(crate) fn complete_record(&mut self, sequence: u64, offset: u64) {
        if let Some(page) = self.pages.iter_mut().find(|page| page.sequence == sequence) {
            page.outstanding.remove(&offset);
        }
        self.settle();
    }

    pub(crate) fn complete_through(&mut self, sequence: u64, offset: u64) -> bool {
        let Some(mut target) = self
            .pages
            .iter()
            .find(|page| page.sequence == sequence)
            .and_then(|page| page.target.clone())
        else {
            return true;
        };
        if offset > target.offset {
            return false;
        }
        for page in &mut self.pages {
            if page.sequence < sequence {
                page.outstanding.clear();
            } else if page.sequence == sequence {
                page.outstanding.retain(|pending| *pending > offset);
            }
        }
        self.settle();
        target.offset = offset;
        if self
            .unstored
            .as_ref()
            .is_none_or(|pending| pending.offset < offset)
        {
            self.unstored = Some(target);
        }
        true
    }

    /// Mark every record of a page handled. A page already settled is a no-op.
    pub(crate) fn complete_page(&mut self, sequence: u64) {
        if let Some(page) = self.pages.iter_mut().find(|page| page.sequence == sequence) {
            page.outstanding.clear();
        }
        self.settle();
    }

    /// The newest offset the completed prefix makes safe and that is not yet
    /// stored.
    pub(crate) const fn unstored(&self) -> Option<&AckTarget> {
        self.unstored.as_ref()
    }

    /// `target` is stored. A newer target completed meanwhile stays pending.
    pub(crate) fn stored(&mut self, target: &AckTarget) {
        self.stored_offset = Some(
            self.stored_offset
                .map_or(target.offset, |stored| stored.max(target.offset)),
        );
        if self.unstored.as_ref() == Some(target) {
            self.unstored = None;
        }
    }

    /// The newest offset this reader stored for the partition.
    pub(crate) const fn stored_offset(&self) -> Option<u64> {
        self.stored_offset
    }

    // Retire the completed prefix. Its newest safe offset supersedes any older
    // unstored one, because a stored offset covers everything before it.
    fn settle(&mut self) {
        while self
            .pages
            .front()
            .is_some_and(|page| page.outstanding.is_empty())
        {
            if let Some(page) = self.pages.pop_front() {
                self.settled_sequence = page.sequence;
                if let Some(target) = page.target {
                    self.unstored = Some(target);
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use laser_wire::codes::FILTER_OP_VERSION;
    use laser_wire::filter::{AppliedPolicy, Continuation, ReadMode, RecordFault};

    fn generation() -> SourceGeneration {
        SourceGeneration {
            stream_id: 1,
            stream_created_at_micros: 10,
            topic_id: 2,
            topic_created_at_micros: 20,
            partition_id: 0,
            partition_created_revision: 30,
            purge_generation: 0,
        }
    }

    fn page(next_scan_offset: Option<u64>, stop: StopReason) -> FilteredPage {
        FilteredPage {
            v: FILTER_OP_VERSION,
            partition_id: 0,
            policy: AppliedPolicy {
                group_id: None,
                digest: Some(Digest32::new([7; 32])),
                filter_id: None,
                revision: None,
                mode: ExecutionMode::Filtered,
                policy_generation: 0,
            },
            generation: generation(),
            read_mode: ReadMode::Primary,
            next_scan_offset,
            safe_ack_offset: next_scan_offset.and_then(|next| next.checked_sub(1)),
            frontier: 100,
            examined: 10,
            matched: 0,
            stop,
            fault: None,
            unevaluated: Vec::new(),
            evaluation_limits: None,
            records: Vec::new(),
        }
    }

    #[test]
    fn given_out_of_order_acks_when_settled_then_should_store_only_the_completed_prefix() {
        let mut progress = PartitionProgress::new(FilteredStart::Next);
        progress.record(1, &page(Some(10), StopReason::Filled), [3, 7]);
        progress.record(2, &page(Some(20), StopReason::Filled), [15]);

        progress.complete_page(2);
        assert_eq!(progress.unstored(), None, "the first page is still open");
        progress.complete_record(1, 3);
        assert_eq!(progress.unstored(), None, "offset 7 is still open");
        progress.complete_record(1, 7);

        assert_eq!(progress.unstored().map(|target| target.offset), Some(19));
        assert_eq!(progress.in_flight(), 0);
    }

    #[test]
    fn given_a_checkpoint_inside_a_page_when_acknowledging_through_it_then_should_leave_later_records_pending()
     {
        let mut progress = PartitionProgress::new(FilteredStart::First);
        progress.record(1, &page(Some(10), StopReason::Filled), [3, 7]);
        progress.record(2, &page(Some(20), StopReason::Filled), [11, 15]);
        assert!(progress.complete_through(2, 11));
        assert_eq!(progress.unstored().map(|target| target.offset), Some(11));
        assert_eq!(progress.in_flight(), 1);
        let stored = progress
            .unstored()
            .cloned()
            .expect("the checkpoint can be stored");
        progress.stored(&stored);
        assert!(progress.complete_through(2, 11));
        assert_eq!(progress.unstored().map(|target| target.offset), Some(11));
        progress.complete_record(2, 15);
        assert_eq!(progress.unstored().map(|target| target.offset), Some(19));
    }

    #[test]
    fn given_an_offset_past_the_page_when_acknowledging_through_it_then_should_keep_progress_unchanged()
     {
        let mut progress = PartitionProgress::new(FilteredStart::First);
        progress.record(1, &page(Some(10), StopReason::Filled), [3, 7]);
        assert!(!progress.complete_through(1, 10));
        assert_eq!(progress.unstored(), None);
        assert_eq!(progress.in_flight(), 1);
    }

    #[test]
    fn given_an_empty_page_when_recorded_then_should_make_its_scan_safe_at_once() {
        let mut progress = PartitionProgress::new(FilteredStart::Next);
        progress.record(1, &page(Some(500), StopReason::Budget), []);

        let target = progress
            .unstored()
            .cloned()
            .expect("an examined range is safe");
        assert_eq!(target.offset, 499);
        progress.stored(&target);
        assert_eq!(progress.unstored(), None);
    }

    #[test]
    fn given_a_page_that_examined_nothing_when_recorded_then_should_repeat_the_original_start() {
        let mut progress = PartitionProgress::new(FilteredStart::Next);
        let mut empty = page(None, StopReason::EndOfVisible);
        empty.safe_ack_offset = None;
        progress.record(1, &empty, []);

        assert_eq!(progress.start(), &FilteredStart::Next);
        assert_eq!(
            progress.unstored(),
            None,
            "nothing examined, nothing to store"
        );
    }

    #[test]
    fn given_a_scanned_page_when_recorded_then_should_continue_after_it() {
        let mut progress = PartitionProgress::new(FilteredStart::First);
        progress.record(1, &page(Some(42), StopReason::Budget), []);

        assert_eq!(
            progress.start(),
            &FilteredStart::Continue(Continuation {
                group_id: None,
                next_scan_offset: 42,
                generation: generation(),
                digest: Some(Digest32::new([7; 32])),
                read_mode: ReadMode::Primary,
                mode: ExecutionMode::Filtered,
                policy_generation: 0,
            })
        );
    }

    #[test]
    fn given_a_fault_stop_when_recorded_then_should_block_the_partition_at_the_fault() {
        let mut progress = PartitionProgress::new(FilteredStart::First);
        let mut faulted = page(Some(8), StopReason::Fault);
        faulted.fault = Some(RecordFault {
            offset: 8,
            reason: FaultReason::Malformed,
        });
        progress.record(1, &faulted, [2]);

        assert_eq!(
            progress.blocked(),
            Some(Blocked::Fault {
                offset: 8,
                reason: FaultReason::Malformed
            })
        );
        progress.complete_record(1, 2);
        assert_eq!(
            progress.unstored().map(|target| target.offset),
            Some(7),
            "the fault itself stays unacknowledged"
        );
    }

    #[test]
    fn given_empty_pages_behind_a_held_page_when_recorded_then_should_not_grow_the_queue() {
        let mut progress = PartitionProgress::new(FilteredStart::Next);
        progress.record(1, &page(Some(10), StopReason::Filled), [4]);
        for sequence in 2..200 {
            progress.record(sequence, &page(Some(10 + sequence), StopReason::Budget), []);
        }
        assert_eq!(progress.in_flight(), 1, "idle rounds hold no slot");
        assert_eq!(
            progress.unstored(),
            None,
            "the held record keeps the offset"
        );
        progress.complete_record(1, 4);
        assert_eq!(
            progress.unstored().map(|target| target.offset),
            Some(208),
            "the newest scan becomes safe with the held page"
        );
    }

    #[test]
    fn given_a_newer_completion_while_storing_when_stored_then_should_keep_the_newer_target() {
        let mut progress = PartitionProgress::new(FilteredStart::Next);
        progress.record(1, &page(Some(10), StopReason::Budget), []);
        let first = progress.unstored().cloned().expect("safe");
        progress.record(2, &page(Some(20), StopReason::Budget), []);

        progress.stored(&first);

        assert_eq!(progress.unstored().map(|target| target.offset), Some(19));
    }
    #[test]
    fn given_a_retired_assignment_when_new_pages_arrive_then_should_refuse_the_old_sequence() {
        let mut original = PartitionProgress::new(FilteredStart::Next);
        original.record(1, &page(Some(10), StopReason::Filled), [3]);
        original.complete_page(1);
        assert!(
            original.knows(1),
            "an already completed delivery is retryable in its assignment"
        );
        let mut reassigned = PartitionProgress::new(FilteredStart::Next);
        assert!(!reassigned.knows(1));
        reassigned.record(2, &page(Some(20), StopReason::Filled), [15]);
        assert!(!reassigned.knows(1));
        assert!(reassigned.knows(2));
    }
}