confluent_kafka/admin/new_partitions.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//! Describes new partitions for a topic in a call to `Admin::create_partitions`.
16
17/// Describes new partitions for a particular topic in a call to
18/// `Admin::create_partitions`.
19#[derive(Clone, Debug)]
20#[doc(alias = "org.apache.kafka.clients.admin.NewPartitions")]
21pub struct NewPartitions {
22 total_count: i32,
23 new_assignments: Option<Vec<Vec<i32>>>,
24}
25
26impl NewPartitions {
27 /// Increase the partition count for a topic to the given `total_count`.
28 /// The assignment of new replicas to brokers will be decided by the broker.
29 ///
30 /// `total_count` is the total number of partitions after the operation
31 /// succeeds.
32 ///
33 /// # Arguments
34 ///
35 /// - `total_count` - The total number of partitions after the operation succeeds.
36 #[doc(alias = "org.apache.kafka.clients.admin.NewPartitions#increaseTo")]
37 pub fn increase_to(total_count: i32) -> Self {
38 Self { total_count, new_assignments: None }
39 }
40
41 /// Increase the partition count for a topic to the given `total_count`
42 /// assigning the new partitions according to the given `new_assignments`.
43 ///
44 /// The length of the given `new_assignments` should equal
45 /// `total_count - old_count`, since the assignment of existing partitions
46 /// is not changed. Each inner list of `new_assignments` should have a
47 /// length equal to the topic's replication factor. The first broker id in
48 /// each inner list is the "preferred replica".
49 ///
50 /// `total_count` is the total number of partitions after the operation
51 /// succeeds; `new_assignments` are the replica assignments for the new
52 /// partitions.
53 ///
54 /// # Arguments
55 ///
56 /// - `total_count` - The total number of partitions after the operation succeeds.
57 /// - `new_assignments` - The replica assignments for the new partitions.
58 #[doc(alias = "org.apache.kafka.clients.admin.NewPartitions#increaseTo")]
59 pub fn increase_to_with_new_assignments(total_count: i32, new_assignments: Vec<Vec<i32>>) -> Self {
60 Self { total_count, new_assignments: Some(new_assignments) }
61 }
62
63 /// Returns the total number of partitions after the operation succeeds.
64 #[doc(alias = "org.apache.kafka.clients.admin.NewPartitions#totalCount")]
65 pub fn total_count(&self) -> i32 {
66 self.total_count
67 }
68
69 /// Returns the replica assignments for the new partitions, or `None` if the
70 /// assignment will be done by the controller.
71 #[doc(alias = "org.apache.kafka.clients.admin.NewPartitions#assignments")]
72 pub fn assignments(&self) -> Option<&Vec<Vec<i32>>> {
73 self.new_assignments.as_ref()
74 }
75}
76
77impl std::fmt::Display for NewPartitions {
78 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
79 write!(
80 f,
81 "(totalCount={}, newAssignments={:?})",
82 self.total_count, self.new_assignments
83 )
84 }
85}
86
87#[cfg(test)]
88mod tests {
89 use super::*;
90
91 #[test]
92 fn increase_to_has_no_assignments() {
93 let np = NewPartitions::increase_to(3);
94 assert_eq!(np.total_count(), 3);
95 assert!(np.assignments().is_none());
96 }
97
98 #[test]
99 fn increase_to_new_assignments() {
100 let np = NewPartitions::increase_to_with_new_assignments(6, vec![vec![1, 2], vec![2, 3], vec![3, 1]]);
101 assert_eq!(np.total_count(), 6);
102 assert_eq!(np.assignments(), Some(&vec![vec![1, 2], vec![2, 3], vec![3, 1]]));
103 }
104}