Skip to main content

kafka_client_engine/admin/list_offsets/
model.rs

1//! Engine-owned scalar intent for one Admin `ListOffsets` query.
2
3use kafka_client_core::{
4    AdminListOffsetSpec, AdminListOffsetTarget, AdminListOffsetsPlan, AdminListOffsetsPlanError,
5};
6
7use crate::config::ConsumerReadIsolation;
8
9/// Stable engine request specification.
10#[derive(Clone, Copy, Debug, Eq, PartialEq)]
11pub enum AdminListOffsetsRequestSpec {
12    /// Select the earliest available offset.
13    Earliest,
14    /// Select the latest available offset.
15    Latest,
16    /// Select the record offset carrying the greatest timestamp.
17    MaxTimestamp,
18    /// Select the local-log start offset.
19    EarliestLocal,
20    /// Select the greatest offset already retained in tiered storage.
21    LatestTiered,
22    /// Select the earliest offset not yet uploaded to tiered storage.
23    EarliestPendingUpload,
24    /// Select the earliest offset at or after this nonnegative timestamp.
25    Timestamp(i64),
26}
27
28/// One engine-owned topic-partition request.
29#[derive(Clone, Debug, Eq, PartialEq)]
30pub struct AdminListOffsetsRequestTarget {
31    topic: String,
32    partition: i32,
33    spec: AdminListOffsetsRequestSpec,
34    current_leader_epoch: Option<i32>,
35}
36
37impl AdminListOffsetsRequestTarget {
38    /// Creates one inert target for validation at admission.
39    pub const fn new(topic: String, partition: i32, spec: AdminListOffsetsRequestSpec) -> Self {
40        Self {
41            topic,
42            partition,
43            spec,
44            current_leader_epoch: None,
45        }
46    }
47
48    /// Replaces the optional leader epoch used to fence stale partition leaders.
49    pub const fn current_leader_epoch(mut self, current_leader_epoch: i32) -> Self {
50        self.current_leader_epoch = Some(current_leader_epoch);
51        self
52    }
53
54    fn canonicalize(mut self) -> Self {
55        self.topic = self.topic.into_boxed_str().into_string();
56        self
57    }
58
59    fn into_core(self) -> AdminListOffsetTarget {
60        let spec = match self.spec {
61            AdminListOffsetsRequestSpec::Earliest => AdminListOffsetSpec::Earliest,
62            AdminListOffsetsRequestSpec::Latest => AdminListOffsetSpec::Latest,
63            AdminListOffsetsRequestSpec::MaxTimestamp => AdminListOffsetSpec::MaxTimestamp,
64            AdminListOffsetsRequestSpec::EarliestLocal => AdminListOffsetSpec::EarliestLocal,
65            AdminListOffsetsRequestSpec::LatestTiered => AdminListOffsetSpec::LatestTiered,
66            AdminListOffsetsRequestSpec::EarliestPendingUpload => {
67                AdminListOffsetSpec::EarliestPendingUpload
68            }
69            AdminListOffsetsRequestSpec::Timestamp(timestamp) => {
70                AdminListOffsetSpec::Timestamp(timestamp)
71            }
72        };
73        AdminListOffsetTarget::new(self.topic, self.partition, spec)
74            .with_current_leader_epoch(self.current_leader_epoch)
75    }
76}
77
78/// One caller-ordered bounded request.
79#[derive(Clone, Debug, Eq, PartialEq)]
80pub struct AdminListOffsetsRequest {
81    targets: Vec<AdminListOffsetsRequestTarget>,
82    read_isolation: ConsumerReadIsolation,
83}
84
85impl AdminListOffsetsRequest {
86    /// Creates inert request intent for validation at the operation boundary.
87    pub const fn new(targets: Vec<AdminListOffsetsRequestTarget>) -> Self {
88        Self {
89            targets,
90            read_isolation: ConsumerReadIsolation::ReadUncommitted,
91        }
92    }
93
94    /// Replaces the immutable visibility policy applied to every target.
95    pub const fn with_read_isolation(mut self, read_isolation: ConsumerReadIsolation) -> Self {
96        self.read_isolation = read_isolation;
97        self
98    }
99
100    pub(crate) fn canonicalize(mut self) -> Self {
101        self.targets = self
102            .targets
103            .into_iter()
104            .map(AdminListOffsetsRequestTarget::canonicalize)
105            .collect();
106        self.targets.shrink_to_fit();
107        self
108    }
109
110    pub(crate) fn into_plan(self) -> Result<AdminListOffsetsPlan, AdminListOffsetsPlanError> {
111        AdminListOffsetsPlan::with_read_isolation(
112            self.targets
113                .into_iter()
114                .map(AdminListOffsetsRequestTarget::into_core)
115                .collect(),
116            self.read_isolation.core(),
117        )
118    }
119}