laser-sdk 0.8.2

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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
use crate::error::LaserError;
use crate::filters::FilterPreviewBuilder;
use crate::filters::reader::{FilteredReaderBuilder, SourceIncarnation};
use crate::laser::Laser;
use crate::stream::Topic;
use crate::stream::transport::ConsumerBuilder;
use iggy::prelude::{ConsumerGroupClient, ConsumerGroupDetails, Identifier, IggyError};
use laser_wire::authz::scoped_resource;
use laser_wire::filter::{
    CatalogPosition, ConsumerFilter, FilterBinding, FilterConsumer, FilterError, FilterErrorReason,
    FilterGroupIdentity, FilterGroupRef, FilterHeader, FilterMutation, FilterRef,
    FilterRevisionPage, FilterRevisionRef, FilterSource, FilterTestResult, GroupFilterSpec,
};
use laser_wire::validate::Validate;
use std::sync::{Arc, Mutex};

// Filters whose name contains a group's own filter name: the name is unique,
// so the first page holds it when it exists.
const GROUP_FILTER_LOOKUP_PAGE: u32 = 8;

/// A consumer group by name or by its native numeric id.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(crate) enum GroupTarget {
    Name(String),
    Id(u64),
}

/// One consumer group of a topic. The group owns its filter policy: readers
/// built from this handle run whatever the group is configured with, bound
/// or unbound, and never name a filter themselves. Build it with
/// [`Topic::consumer_group`] or [`Topic::consumer_group_id`]. Handles are
/// free to construct and clone.
#[derive(Clone)]
pub struct ConsumerGroup {
    topic: Topic,
    target: GroupTarget,
    // The control-log position of the last configuration this handle, or a
    // clone of it, performed. Every read built from the handle carries it, so
    // the application never observes its own configuration as absent through
    // another node.
    configured_at: Arc<Mutex<Option<CatalogPosition>>>,
}

/// A consumer group as the server knows it after [`CreateConsumerGroup::build`]
/// or [`ConsumerGroup::info`].
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ConsumerGroupInfo {
    /// The native numeric group id.
    pub id: u32,
    pub name: String,
    /// The exact group incarnation inside its stream and topic incarnations.
    /// A recreated group is another identity and inherits no policy.
    pub identity: FilterGroupIdentity,
    /// The active policy, `None` for an unbound group whose readers receive
    /// every record.
    pub filter: Option<FilterBinding>,
}

impl ConsumerGroup {
    pub(crate) fn new(topic: Topic, target: GroupTarget) -> Self {
        Self {
            topic,
            target,
            configured_at: Arc::new(Mutex::new(None)),
        }
    }

    /// The group name, `None` for a handle addressed by numeric id.
    pub fn name(&self) -> Option<&str> {
        match &self.target {
            GroupTarget::Name(name) => Some(name),
            GroupTarget::Id(_) => None,
        }
    }

    /// The native numeric id, `None` for a handle addressed by name.
    pub const fn id(&self) -> Option<u64> {
        match self.target {
            GroupTarget::Id(id) => Some(id),
            GroupTarget::Name(_) => None,
        }
    }

    pub const fn topic(&self) -> &Topic {
        &self.topic
    }

    /// Create the group, with an optional filter policy configured in the
    /// same call. Idempotent: an existing group is kept, and a repeated
    /// policy with the same digest keeps its binding.
    pub fn create(&self) -> CreateConsumerGroup {
        CreateConsumerGroup {
            group: self.clone(),
            policy: None,
            operation_id: None,
        }
    }

    /// The group's filter policy: configure it, inspect it, draft and pause
    /// revisions, release it, preview and sample-test it.
    pub fn filter(&self) -> GroupFilter {
        GroupFilter {
            group: self.clone(),
        }
    }

    /// Build a live, load-balanced consumer of this group with server-backed
    /// offsets. On a server that resolves group policies the consumer runs
    /// the group's policy, filtered or unfiltered, and acknowledges through
    /// the fenced group contract. On Apache Iggy it is the native group
    /// consumer. The built consumer implements `futures::Stream`.
    pub fn consumer(&self) -> ConsumerBuilder {
        ConsumerBuilder::group(self.clone())
    }

    /// The match-oriented reader of this group's filter policy: pages of
    /// records with their original offsets, an independent scan budget, and
    /// explicit acknowledgments. An unbound group receives every record.
    ///
    /// # Errors
    ///
    /// [`LaserError::NoStream`] when the topic has no stream.
    pub fn reader(&self) -> Result<FilteredReaderBuilder<'_>, LaserError> {
        self.reader_with(FilterRef::Group)
    }

    /// The group as the server knows it: its id, exact identity and active
    /// policy.
    pub async fn info(&self) -> Result<ConsumerGroupInfo, LaserError> {
        let details = self.native().await?;
        let identity = self.identity_of(details.id).await?;
        let filter = if self.laser().capabilities().await.filters.catalog {
            self.filter().get().await?
        } else {
            None
        };
        if filter
            .as_ref()
            .is_some_and(|binding| binding.identity != identity)
        {
            return Err(FilterError::new(
                FilterErrorReason::SourceChanged,
                "the group incarnation changed while its policy was inspected",
            )
            .into());
        }
        Ok(ConsumerGroupInfo {
            id: details.id,
            name: details.name,
            identity,
            filter,
        })
    }

    pub(crate) fn reader_with(
        &self,
        filter: FilterRef,
    ) -> Result<FilteredReaderBuilder<'_>, LaserError> {
        Ok(FilteredReaderBuilder::new(
            self.laser(),
            self.source()?,
            self.selector(),
            filter,
            self.catalog_position(),
        ))
    }

    /// Create the native group by name without touching its policy.
    pub(crate) async fn ensure_native(&self) -> Result<(), LaserError> {
        let GroupTarget::Name(name) = &self.target else {
            return Err(LaserError::Invalid(
                "a consumer group is created by name, address it with consumer_group(name)"
                    .to_owned(),
            ));
        };
        let stream = Identifier::named(self.topic.stream()?)?;
        let topic = Identifier::named(self.topic.name())?;
        match self
            .laser()
            .client()
            .create_consumer_group(&stream, &topic, name)
            .await
        {
            Ok(_) | Err(IggyError::ConsumerGroupNameAlreadyExists(..)) => Ok(()),
            Err(error) => Err(error.into()),
        }
    }

    pub(crate) fn laser(&self) -> &Laser {
        self.topic.laser()
    }

    pub(crate) fn selector(&self) -> FilterConsumer {
        match &self.target {
            GroupTarget::Name(name) => FilterConsumer::Group(name.clone()),
            GroupTarget::Id(id) => FilterConsumer::GroupId(*id),
        }
    }

    pub(crate) fn source(&self) -> Result<FilterSource, LaserError> {
        Ok(FilterSource {
            stream: self.topic.stream()?.to_owned(),
            topic: self.topic.name().to_owned(),
        })
    }

    pub(crate) fn catalog_position(&self) -> Option<CatalogPosition> {
        *self
            .configured_at
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }

    pub(crate) async fn native_name(&self) -> Result<String, LaserError> {
        match &self.target {
            GroupTarget::Name(name) => Ok(name.clone()),
            GroupTarget::Id(_) => Ok(self.native().await?.name),
        }
    }

    fn remember_position(&self, position: Option<CatalogPosition>) {
        let Some(position) = position else {
            return;
        };
        let mut configured_at = self
            .configured_at
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner);
        if configured_at.is_none_or(|known| {
            position.operation_id.is_some() && known.operation_id != position.operation_id
                || known.partition_id != position.partition_id
                || known.offset < position.offset
        }) {
            *configured_at = Some(position);
        }
    }

    // The names the catalog addresses the group by. A numeric handle reads
    // them from the server.
    async fn group_ref(&self) -> Result<FilterGroupRef, LaserError> {
        let source = self.source()?;
        let group = match &self.target {
            GroupTarget::Name(name) => name.clone(),
            GroupTarget::Id(_) => self.native().await?.name,
        };
        Ok(FilterGroupRef {
            stream: source.stream,
            topic: source.topic,
            group,
        })
    }

    async fn native(&self) -> Result<ConsumerGroupDetails, LaserError> {
        let stream = Identifier::named(self.topic.stream()?)?;
        let topic = Identifier::named(self.topic.name())?;
        let group = match &self.target {
            GroupTarget::Name(name) => Identifier::named(name)?,
            GroupTarget::Id(id) => Identifier::numeric(
                u32::try_from(*id)
                    .map_err(|_| LaserError::Config("consumer group id exceeds 32 bits"))?,
            )?,
        };
        self.laser()
            .client()
            .get_consumer_group(&stream, &topic, &group)
            .await?
            .ok_or_else(|| {
                FilterError::new(
                    FilterErrorReason::NotFound,
                    format!("consumer group {} does not exist", self.selector()),
                )
                .into()
            })
    }

    async fn identity_of(&self, group_id: u32) -> Result<FilterGroupIdentity, LaserError> {
        let source = self.source()?;
        Ok(SourceIncarnation::read(&self.laser().client(), &source)
            .await?
            .identity(u64::from(group_id)))
    }
}

/// Creates a consumer group. Build it with [`ConsumerGroup::create`].
pub struct CreateConsumerGroup {
    group: ConsumerGroup,
    policy: Option<GroupFilterSpec>,
    operation_id: Option<u128>,
}

impl CreateConsumerGroup {
    /// Configure the group with `filter` once it exists. The definition is
    /// saved as the group's own filter.
    #[must_use]
    pub fn filter(mut self, filter: ConsumerFilter) -> Self {
        self.policy = Some(GroupFilterSpec::Definition(filter));
        self
    }

    /// Configure the group with a definition or one of its own revisions.
    #[must_use]
    pub fn policy(mut self, policy: GroupFilterSpec) -> Self {
        self.policy = Some(policy);
        self
    }

    /// The idempotency key of the policy configuration. Record it first to
    /// resume the same configuration after a crash and read its first
    /// outcome.
    #[must_use]
    pub const fn operation_id(mut self, operation_id: u128) -> Self {
        self.operation_id = Some(operation_id);
        self
    }

    /// Create the group, then configure its policy when one was given. A
    /// policy is preflighted against the server's capabilities before the
    /// native group is created. A group that exists with the same policy is
    /// returned unchanged, one that runs another policy is a typed conflict.
    ///
    /// # Errors
    ///
    /// [`LaserError::ConsumerGroupSetup`] when the group exists but its
    /// policy could not be configured. The group is left as it is.
    pub async fn build(self) -> Result<ConsumerGroupInfo, LaserError> {
        let group = self.group;
        if let Some(policy) = &self.policy {
            FilterMutation::ConfigureGroup {
                group: group.group_ref().await?,
                policy: policy.clone(),
                expected_identity: None,
            }
            .validate()?;
            group.laser().filters().catalog_capabilities().await?;
        }
        group.ensure_native().await?;
        let details = group.native().await?;
        let identity = group.identity_of(details.id).await?;
        let filter = match self.policy {
            Some(policy) => match group
                .filter()
                .configure_scoped_as(self.operation_id, policy, identity)
                .await
            {
                Ok(binding) => Some(binding),
                Err(source) => {
                    return Err(LaserError::ConsumerGroupSetup {
                        group_id: details.id,
                        name: details.name,
                        identity,
                        source: Box::new(source),
                    });
                }
            },
            None if group.laser().capabilities().await.filters.catalog => {
                group.filter().get().await?
            }
            None => None,
        };
        Ok(ConsumerGroupInfo {
            id: details.id,
            name: details.name,
            identity,
            filter,
        })
    }
}

/// A consumer group's filter policy. Build it with [`ConsumerGroup::filter`].
/// Every verb needs a managed backend that serves the filter catalog.
pub struct GroupFilter {
    group: ConsumerGroup,
}

impl GroupFilter {
    /// Give the group `filter` as its policy. The definition is saved as the
    /// group's own filter and the group is bound to it in one catalog
    /// transaction. The same digest again keeps the binding. Another digest
    /// on a group that runs a policy is a typed conflict: create a new group
    /// for another policy.
    pub async fn configure(&self, filter: ConsumerFilter) -> Result<FilterBinding, LaserError> {
        self.configure_as(None, GroupFilterSpec::Definition(filter))
            .await
    }

    /// [`configure`](Self::configure) with a definition or one of the group's
    /// own revisions.
    pub async fn configure_with(
        &self,
        policy: GroupFilterSpec,
    ) -> Result<FilterBinding, LaserError> {
        self.configure_as(None, policy).await
    }

    /// [`configure_with`](Self::configure_with) under a caller-chosen
    /// operation id, so a caller that records the id first resumes the same
    /// configuration after a crash and reads its first outcome.
    pub async fn configure_as(
        &self,
        operation_id: Option<u128>,
        policy: GroupFilterSpec,
    ) -> Result<FilterBinding, LaserError> {
        let details = self.group.native().await?;
        let identity = self.group.identity_of(details.id).await?;
        self.configure_scoped_as(operation_id, policy, identity)
            .await
    }

    async fn configure_scoped_as(
        &self,
        operation_id: Option<u128>,
        policy: GroupFilterSpec,
        identity: FilterGroupIdentity,
    ) -> Result<FilterBinding, LaserError> {
        let group = self.group.group_ref().await?;
        let (binding, position) = self
            .group
            .laser()
            .filters()
            .configure_group(group, policy, operation_id, identity)
            .await?;
        if binding.identity != identity {
            return Err(FilterError::new(
                FilterErrorReason::SourceChanged,
                "configured binding identifies another group incarnation",
            )
            .into());
        }
        self.group.remember_position(position);
        Ok(binding)
    }

    /// The active policy, `None` for an unbound group.
    pub async fn get(&self) -> Result<Option<FilterBinding>, LaserError> {
        self.group.laser().filters().catalog_capabilities().await?;
        let details = self.group.native().await?;
        let identity = self.group.identity_of(details.id).await?;
        let group = self.group.group_ref().await?;
        match self.group.laser().filters().binding(group).await {
            Ok(binding) if binding.identity == identity => Ok(Some(binding)),
            Ok(_) => Err(FilterError::new(
                FilterErrorReason::SourceChanged,
                "the group incarnation changed while its policy was inspected",
            )
            .into()),
            Err(error) if error.is_not_found() => Ok(None),
            Err(error) => Err(error),
        }
    }

    /// One page of the group's own filter revisions, newest first.
    pub async fn revisions(
        &self,
        page: u32,
        page_size: u32,
    ) -> Result<FilterRevisionPage, LaserError> {
        let active = self.active().await?;
        self.group
            .laser()
            .filters()
            .revisions(active.filter_id, page, page_size)
            .await
    }

    /// Draft a revision of the group's own filter. Readers keep running the
    /// active revision. A different digest requires a separate group.
    /// `expected_revision` must still be the latest.
    pub async fn revise(
        &self,
        expected_revision: u32,
        filter: ConsumerFilter,
    ) -> Result<FilterRevisionRef, LaserError> {
        let active = self.active().await?;
        self.group
            .laser()
            .filters()
            .revise(active.filter_id, expected_revision, filter)
            .await
    }

    /// Pause or resume a revision of the group's own filter. A paused active
    /// revision refuses new reads, while records already delivered can still
    /// be acknowledged.
    pub async fn set_revision_enabled(
        &self,
        revision: u32,
        enabled: bool,
    ) -> Result<(), LaserError> {
        let active = self.active().await?;
        self.group
            .laser()
            .filters()
            .set_revision_enabled(active.filter_id, revision, enabled)
            .await
    }

    /// Delete the group's own filter with every revision. A bound group is
    /// released first, so its consumers receive every record from their next
    /// poll. Nothing of the filter stays in the catalog. `Ok(false)` when the
    /// group has no filter of its own.
    pub async fn delete(&self) -> Result<bool, LaserError> {
        self.group.laser().filters().catalog_capabilities().await?;
        let details = self.group.native().await?;
        let identity = self.group.identity_of(details.id).await?;
        let name = group_filter_name(&identity);
        let scoped_name = scoped_resource(&self.group.source()?.stream, &name);
        let filters = self.group.laser().filters();
        let mut before_id = None;
        loop {
            let page = filters
                .list_before(&name, before_id, GROUP_FILTER_LOOKUP_PAGE)
                .await?;
            let next = page.items.last().map(|summary| summary.id);
            let count = page.items.len();
            if let Some(own) = page
                .items
                .into_iter()
                .find(|summary| summary.name == name || summary.name == scoped_name)
            {
                let position = filters.drop_filter(own.id).await?;
                self.group.remember_position(position);
                return Ok(true);
            }
            let Some(next) = next else {
                return Ok(false);
            };
            if before_id.is_some_and(|before| next >= before) {
                return Err(FilterError::new(
                    FilterErrorReason::CatalogUnavailable,
                    "the catalog lookup cursor did not advance",
                )
                .into());
            }
            if count as u64 >= u64::from(page.total) {
                return Ok(false);
            }
            before_id = Some(next);
        }
    }

    /// Release the group's policy. Its readers then receive every record. A
    /// released group may only be configured with the digest it ran.
    pub async fn release(&self) -> Result<FilterBinding, LaserError> {
        let active = self.active().await?;
        let (released, position) = self.group.laser().filters().release_group(active).await?;
        self.group.remember_position(position);
        Ok(released)
    }

    /// Preview the active policy over the stored records of one partition. A
    /// preview joins no group, stores no offset, and changes no consumer
    /// state.
    pub async fn preview(&self, partition_id: u32) -> Result<FilterPreviewBuilder<'_>, LaserError> {
        let active = self.active().await?;
        let source = self.group.source()?;
        Ok(self.group.laser().filters().preview(
            source.stream,
            source.topic,
            partition_id,
            FilterRef::Revision {
                filter_id: active.filter_id,
                revision: active.revision,
            },
        ))
    }

    /// Evaluate the active policy against one supplied record and explain the
    /// verdict. Nothing is read from a stream and nothing is stored.
    pub async fn test(
        &self,
        payload: impl Into<Vec<u8>>,
        headers: Vec<FilterHeader>,
    ) -> Result<FilterTestResult, LaserError> {
        let active = self.active().await?;
        self.group
            .laser()
            .filters()
            .test(
                FilterRef::Revision {
                    filter_id: active.filter_id,
                    revision: active.revision,
                },
                payload,
                headers,
            )
            .await
    }

    async fn active(&self) -> Result<FilterBinding, LaserError> {
        self.get().await?.ok_or_else(|| {
            FilterError::new(
                FilterErrorReason::NotFound,
                "the consumer group has no filter policy",
            )
            .into()
        })
    }
}

// The catalog names a group's own filter by the ids of the group incarnation
// it was configured for. A catalog that scopes names to streams prefixes it
// with the group's stream.
fn group_filter_name(identity: &FilterGroupIdentity) -> String {
    format!(
        "group:{}:{}:{}:{}:{}",
        identity.stream_id,
        identity.stream_created_at_micros,
        identity.topic_id,
        identity.topic_created_at_micros,
        identity.group_id
    )
}