Skip to main content

confluent_kafka/admin/
producer_state.rs

1// Copyright 2025 Confluent Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! State of an active producer as reported by `DescribeProducers`.
16
17/// The state of an active producer for a partition.
18#[derive(Clone, Debug, PartialEq, Eq, Hash)]
19#[doc(alias = "org.apache.kafka.clients.admin.ProducerState")]
20pub struct ProducerState {
21    producer_id: i64,
22    producer_epoch: i32,
23    last_sequence: i32,
24    last_timestamp: i64,
25    coordinator_epoch: Option<i32>,
26    current_transaction_start_offset: Option<i64>,
27}
28
29impl ProducerState {
30    /// Creates a new `ProducerState`.
31    ///
32    /// # Arguments
33    ///
34    /// - `producer_id` - the producer id.
35    /// - `producer_epoch` - the producer epoch.
36    /// - `last_sequence` - the last sequence number written by this producer.
37    /// - `last_timestamp` - the timestamp of the last record written by this producer.
38    /// - `coordinator_epoch` - the epoch of the transaction coordinator, or `None` if not known.
39    /// - `current_transaction_start_offset` - the start offset of the producer's open
40    ///   transaction, or `None` if there is none.
41    #[doc(alias = "org.apache.kafka.clients.admin.ProducerState#ProducerState")]
42    pub fn new(
43        producer_id: i64,
44        producer_epoch: i32,
45        last_sequence: i32,
46        last_timestamp: i64,
47        coordinator_epoch: Option<i32>,
48        current_transaction_start_offset: Option<i64>,
49    ) -> Self {
50        Self {
51            producer_id,
52            producer_epoch,
53            last_sequence,
54            last_timestamp,
55            coordinator_epoch,
56            current_transaction_start_offset,
57        }
58    }
59
60    /// Returns the producer id.
61    #[doc(alias = "org.apache.kafka.clients.admin.ProducerState#producerId")]
62    pub fn producer_id(&self) -> i64 {
63        self.producer_id
64    }
65
66    /// Returns the producer epoch.
67    #[doc(alias = "org.apache.kafka.clients.admin.ProducerState#producerEpoch")]
68    pub fn producer_epoch(&self) -> i32 {
69        self.producer_epoch
70    }
71
72    /// Returns the last sequence number written by this producer.
73    #[doc(alias = "org.apache.kafka.clients.admin.ProducerState#lastSequence")]
74    pub fn last_sequence(&self) -> i32 {
75        self.last_sequence
76    }
77
78    /// Returns the last timestamp written by this producer.
79    #[doc(alias = "org.apache.kafka.clients.admin.ProducerState#lastTimestamp")]
80    pub fn last_timestamp(&self) -> i64 {
81        self.last_timestamp
82    }
83
84    /// Returns the offset of the first record in the current transaction, if any.
85    #[doc(alias = "org.apache.kafka.clients.admin.ProducerState#currentTransactionStartOffset")]
86    pub fn current_transaction_start_offset(&self) -> Option<i64> {
87        self.current_transaction_start_offset
88    }
89
90    /// Returns the coordinator epoch, if any.
91    #[doc(alias = "org.apache.kafka.clients.admin.ProducerState#coordinatorEpoch")]
92    pub fn coordinator_epoch(&self) -> Option<i32> {
93        self.coordinator_epoch
94    }
95}
96
97impl std::fmt::Display for ProducerState {
98    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
99        write!(
100            f,
101            "ProducerState(producerId={}, producerEpoch={}, lastSequence={}, lastTimestamp={}, \
102             coordinatorEpoch={:?}, currentTransactionStartOffset={:?})",
103            self.producer_id,
104            self.producer_epoch,
105            self.last_sequence,
106            self.last_timestamp,
107            self.coordinator_epoch,
108            self.current_transaction_start_offset,
109        )
110    }
111}
112
113#[cfg(test)]
114mod tests {
115    use super::*;
116
117    #[test]
118    fn accessors_return_constructor_values() {
119        let state = ProducerState::new(12345, 15, 9, 1_600_000_000_000, Some(3), Some(100));
120        assert_eq!(state.producer_id(), 12345);
121        assert_eq!(state.producer_epoch(), 15);
122        assert_eq!(state.last_sequence(), 9);
123        assert_eq!(state.last_timestamp(), 1_600_000_000_000);
124        assert_eq!(state.coordinator_epoch(), Some(3));
125        assert_eq!(state.current_transaction_start_offset(), Some(100));
126    }
127
128    #[test]
129    fn equality_uses_all_fields() {
130        let a = ProducerState::new(1, 2, 3, 4, Some(5), None);
131        let b = ProducerState::new(1, 2, 3, 4, Some(5), None);
132        assert_eq!(a, b);
133        assert_ne!(a, ProducerState::new(1, 2, 3, 4, Some(5), Some(6)));
134    }
135}