Skip to main content

kafka_client_engine/admin/list_partition_reassignments/
outcome.rs

1//! Stable engine terminal values for partition-reassignment listing.
2
3use core::fmt;
4
5use kafka_client_core::{
6    DeliveryStatus as CoreDeliveryStatus, ListPartitionReassignmentsFailureKind as CoreFailureKind,
7    ListPartitionReassignmentsTerminal as CoreTerminal,
8};
9
10/// Stable delivery certainty independent of core and driver types.
11#[derive(Clone, Copy, Debug, Eq, PartialEq)]
12pub enum ListPartitionReassignmentsDeliveryStatus {
13    /// The request definitely did not reach Kafka.
14    NotSent,
15    /// The request may have reached Kafka.
16    PossiblySent,
17}
18
19/// One ordered active reassignment description.
20#[derive(Clone, Debug, Eq, PartialEq)]
21pub struct PartitionReassignment {
22    replicas: Vec<i32>,
23    adding_replicas: Vec<i32>,
24    removing_replicas: Vec<i32>,
25}
26
27impl PartitionReassignment {
28    /// Consumes the description into ordered stable scalar lists.
29    pub fn into_parts(self) -> (Vec<i32>, Vec<i32>, Vec<i32>) {
30        (self.replicas, self.adding_replicas, self.removing_replicas)
31    }
32}
33
34/// One active reassignment attached to its topic-partition identity.
35#[derive(Clone, Debug, Eq, PartialEq)]
36pub struct PartitionReassignmentResult {
37    topic: String,
38    partition: i32,
39    reassignment: PartitionReassignment,
40}
41
42impl PartitionReassignmentResult {
43    /// Consumes this result into stable scalar parts.
44    pub fn into_parts(self) -> (String, i32, PartitionReassignment) {
45        (self.topic, self.partition, self.reassignment)
46    }
47}
48
49/// Ordered successful result plus Kafka's throttle observation.
50#[derive(Clone, Debug, Eq, PartialEq)]
51pub struct ListPartitionReassignmentsBatch {
52    throttle_time_ms: u32,
53    reassignments: Vec<PartitionReassignmentResult>,
54}
55
56impl ListPartitionReassignmentsBatch {
57    /// Consumes the batch into throttle and ordered active reassignments.
58    pub fn into_parts(self) -> (u32, Vec<PartitionReassignmentResult>) {
59        (self.throttle_time_ms, self.reassignments)
60    }
61}
62
63/// Exact controller failure with bounded nullable diagnostic.
64#[derive(Clone, Debug, Eq, PartialEq)]
65pub struct ListPartitionReassignmentsBrokerError {
66    code: i16,
67    message: Option<String>,
68    message_truncated: bool,
69}
70
71impl ListPartitionReassignmentsBrokerError {
72    /// Consumes the exact broker facts.
73    pub fn into_parts(self) -> (i16, Option<String>, bool) {
74        (self.code, self.message, self.message_truncated)
75    }
76}
77
78/// Stable whole-operation failure category.
79#[derive(Clone, Debug, Eq, PartialEq)]
80pub enum ListPartitionReassignmentsFailureKind {
81    /// The original absolute deadline elapsed.
82    DeadlineElapsed,
83    /// The driver rejected the request before ownership.
84    DriverRejected,
85    /// Driver-owned transport execution failed.
86    Transport,
87    /// Kafka's controller rejected the query with exact broker facts.
88    Broker(ListPartitionReassignmentsBrokerError),
89    /// A valid response exceeded admitted retained capacity.
90    ResponseTooLarge,
91    /// The selected API cannot represent the request.
92    Compatibility,
93    /// The broker response could not be normalized.
94    InvalidResponse,
95}
96
97/// Whole-operation failure with authoritative delivery certainty.
98#[derive(Clone, Debug, Eq, PartialEq)]
99pub struct ListPartitionReassignmentsFailure {
100    kind: ListPartitionReassignmentsFailureKind,
101    delivery: ListPartitionReassignmentsDeliveryStatus,
102}
103
104impl ListPartitionReassignmentsFailure {
105    /// Consumes the failure into its stable parts.
106    pub fn into_parts(
107        self,
108    ) -> (
109        ListPartitionReassignmentsFailureKind,
110        ListPartitionReassignmentsDeliveryStatus,
111    ) {
112        (self.kind, self.delivery)
113    }
114}
115
116/// Exactly one engine-owned terminal decision.
117#[derive(Clone, Debug, Eq, PartialEq)]
118pub enum ListPartitionReassignmentsOutcome {
119    /// Ordered active rows plus Kafka's throttle observation.
120    Reassignments(ListPartitionReassignmentsBatch),
121    /// Whole-operation failure.
122    Failed(ListPartitionReassignmentsFailure),
123}
124
125/// Failure to observe a named completion.
126#[derive(Clone, Copy, Debug, Eq, PartialEq)]
127pub enum ListPartitionReassignmentsObserverError {
128    /// This linear observer already consumed its terminal.
129    AlreadyObserved,
130    /// The observer generation is no longer live.
131    Stale,
132}
133
134impl fmt::Display for ListPartitionReassignmentsObserverError {
135    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
136        formatter.write_str(match self {
137            Self::AlreadyObserved => "ListPartitionReassignments result was already observed",
138            Self::Stale => "ListPartitionReassignments observer is stale",
139        })
140    }
141}
142
143impl std::error::Error for ListPartitionReassignmentsObserverError {}
144
145pub(crate) fn translate_terminal(terminal: CoreTerminal) -> ListPartitionReassignmentsOutcome {
146    match terminal {
147        CoreTerminal::Reassignments(batch) => {
148            let (throttle_time_ms, reassignments) = batch.into_parts();
149            ListPartitionReassignmentsOutcome::Reassignments(ListPartitionReassignmentsBatch {
150                throttle_time_ms,
151                reassignments: reassignments
152                    .into_iter()
153                    .map(|outcome| {
154                        let (topic, partition, reassignment) = outcome.into_parts();
155                        let (replicas, adding_replicas, removing_replicas) =
156                            reassignment.into_parts();
157                        PartitionReassignmentResult {
158                            topic,
159                            partition,
160                            reassignment: PartitionReassignment {
161                                replicas,
162                                adding_replicas,
163                                removing_replicas,
164                            },
165                        }
166                    })
167                    .collect(),
168            })
169        }
170        CoreTerminal::Failed(failure) => {
171            ListPartitionReassignmentsOutcome::Failed(ListPartitionReassignmentsFailure {
172                kind: failure_kind(failure.kind().clone()),
173                delivery: delivery(failure.delivery()),
174            })
175        }
176    }
177}
178
179fn failure_kind(kind: CoreFailureKind) -> ListPartitionReassignmentsFailureKind {
180    match kind {
181        CoreFailureKind::DeadlineElapsed => ListPartitionReassignmentsFailureKind::DeadlineElapsed,
182        CoreFailureKind::DriverRejected => ListPartitionReassignmentsFailureKind::DriverRejected,
183        CoreFailureKind::Transport => ListPartitionReassignmentsFailureKind::Transport,
184        CoreFailureKind::Broker(error) => {
185            ListPartitionReassignmentsFailureKind::Broker(ListPartitionReassignmentsBrokerError {
186                code: error.code(),
187                message: error.message().map(str::to_owned),
188                message_truncated: error.message_truncated(),
189            })
190        }
191        CoreFailureKind::ResponseTooLarge => {
192            ListPartitionReassignmentsFailureKind::ResponseTooLarge
193        }
194        CoreFailureKind::Compatibility => ListPartitionReassignmentsFailureKind::Compatibility,
195        CoreFailureKind::InvalidResponse => ListPartitionReassignmentsFailureKind::InvalidResponse,
196    }
197}
198
199const fn delivery(status: CoreDeliveryStatus) -> ListPartitionReassignmentsDeliveryStatus {
200    match status {
201        CoreDeliveryStatus::NotSent => ListPartitionReassignmentsDeliveryStatus::NotSent,
202        CoreDeliveryStatus::PossiblySent => ListPartitionReassignmentsDeliveryStatus::PossiblySent,
203    }
204}