Skip to main content

confluent_kafka/admin/
partition_reassignment.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//! An ongoing partition reassignment listed via
16//! `Admin::list_partition_reassignments`.
17
18/// A partition reassignment, which has been listed via
19/// `Admin::list_partition_reassignments`.
20#[derive(Clone, Debug)]
21#[doc(alias = "org.apache.kafka.clients.admin.PartitionReassignment")]
22pub struct PartitionReassignment {
23    replicas: Vec<i32>,
24    adding_replicas: Vec<i32>,
25    removing_replicas: Vec<i32>,
26}
27
28impl PartitionReassignment {
29    /// Creates a partition reassignment from its replica sets.
30    ///
31    /// # Arguments
32    ///
33    /// - `replicas` - the brokers which this partition currently resides on.
34    /// - `adding_replicas` - the brokers that we are adding this partition to as part of a
35    ///   reassignment.
36    /// - `removing_replicas` - the brokers that we are removing this partition from as part of
37    ///   a reassignment.
38    #[doc(alias = "org.apache.kafka.clients.admin.PartitionReassignment#PartitionReassignment")]
39    pub fn new(replicas: Vec<i32>, adding_replicas: Vec<i32>, removing_replicas: Vec<i32>) -> Self {
40        Self { replicas, adding_replicas, removing_replicas }
41    }
42
43    /// Returns the brokers which this partition currently resides on.
44    #[doc(alias = "org.apache.kafka.clients.admin.PartitionReassignment#replicas")]
45    pub fn replicas(&self) -> &[i32] {
46        &self.replicas
47    }
48
49    /// Returns the brokers that we are adding this partition to as part of a
50    /// reassignment. A subset of replicas.
51    #[doc(alias = "org.apache.kafka.clients.admin.PartitionReassignment#addingReplicas")]
52    pub fn adding_replicas(&self) -> &[i32] {
53        &self.adding_replicas
54    }
55
56    /// Returns the brokers that we are removing this partition from as part of a
57    /// reassignment. A subset of replicas.
58    #[doc(alias = "org.apache.kafka.clients.admin.PartitionReassignment#removingReplicas")]
59    pub fn removing_replicas(&self) -> &[i32] {
60        &self.removing_replicas
61    }
62}
63
64impl std::fmt::Display for PartitionReassignment {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        write!(
67            f,
68            "PartitionReassignment(replicas={:?}, addingReplicas={:?}, removingReplicas={:?})",
69            self.replicas, self.adding_replicas, self.removing_replicas
70        )
71    }
72}
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77
78    #[test]
79    fn accessors_return_stored_values() {
80        let r = PartitionReassignment::new(vec![1, 2, 3], vec![4], vec![1]);
81        assert_eq!(r.replicas(), &[1, 2, 3]);
82        assert_eq!(r.adding_replicas(), &[4]);
83        assert_eq!(r.removing_replicas(), &[1]);
84    }
85}