Skip to main content

confluent_kafka/admin/
abort_transaction_spec.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//! The specification of a transaction to abort.
16
17use crate::common::TopicPartition;
18
19/// The identifiers required to abort a hanging transaction on a partition.
20#[derive(Clone, Debug, PartialEq, Eq, Hash)]
21#[doc(alias = "org.apache.kafka.clients.admin.AbortTransactionSpec")]
22pub struct AbortTransactionSpec {
23    topic_partition: TopicPartition,
24    producer_id: i64,
25    producer_epoch: i16,
26    coordinator_epoch: i32,
27}
28
29impl AbortTransactionSpec {
30    /// Creates a new `AbortTransactionSpec`.
31    ///
32    /// # Arguments
33    ///
34    /// - `topic_partition` - the partition of the hanging transaction
35    /// - `producer_id` - the producer id of the hanging transaction
36    /// - `producer_epoch` - the producer epoch of the hanging transaction
37    /// - `coordinator_epoch` - the coordinator epoch of the hanging transaction
38    #[doc(alias = "org.apache.kafka.clients.admin.AbortTransactionSpec#AbortTransactionSpec")]
39    pub fn new(topic_partition: TopicPartition, producer_id: i64, producer_epoch: i16, coordinator_epoch: i32) -> Self {
40        Self { topic_partition, producer_id, producer_epoch, coordinator_epoch }
41    }
42
43    /// Returns the topic partition of the hanging transaction.
44    #[doc(alias = "org.apache.kafka.clients.admin.AbortTransactionSpec#topicPartition")]
45    pub fn topic_partition(&self) -> &TopicPartition {
46        &self.topic_partition
47    }
48
49    /// Returns the producer id of the hanging transaction.
50    #[doc(alias = "org.apache.kafka.clients.admin.AbortTransactionSpec#producerId")]
51    pub fn producer_id(&self) -> i64 {
52        self.producer_id
53    }
54
55    /// Returns the producer epoch of the hanging transaction.
56    #[doc(alias = "org.apache.kafka.clients.admin.AbortTransactionSpec#producerEpoch")]
57    pub fn producer_epoch(&self) -> i16 {
58        self.producer_epoch
59    }
60
61    /// Returns the coordinator epoch of the hanging transaction.
62    #[doc(alias = "org.apache.kafka.clients.admin.AbortTransactionSpec#coordinatorEpoch")]
63    pub fn coordinator_epoch(&self) -> i32 {
64        self.coordinator_epoch
65    }
66}
67
68impl std::fmt::Display for AbortTransactionSpec {
69    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70        write!(
71            f,
72            "AbortTransactionSpec(topicPartition={}, producerId={}, producerEpoch={}, coordinatorEpoch={})",
73            self.topic_partition, self.producer_id, self.producer_epoch, self.coordinator_epoch,
74        )
75    }
76}
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81
82    #[test]
83    fn accessors_return_constructor_values() {
84        let spec = AbortTransactionSpec::new(TopicPartition::new("t", 3), 12345, 7, 4);
85        assert_eq!(spec.topic_partition(), &TopicPartition::new("t", 3));
86        assert_eq!(spec.producer_id(), 12345);
87        assert_eq!(spec.producer_epoch(), 7);
88        assert_eq!(spec.coordinator_epoch(), 4);
89    }
90
91    #[test]
92    fn display_matches_java() {
93        let spec = AbortTransactionSpec::new(TopicPartition::new("t", 3), 12345, 7, 4);
94        assert_eq!(
95            spec.to_string(),
96            "AbortTransactionSpec(topicPartition=t-3, producerId=12345, producerEpoch=7, coordinatorEpoch=4)"
97        );
98    }
99}