Skip to main content

confluent_kafka/admin/
alter_partition_reassignments_result.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 result of `Admin::alter_partition_reassignments`.
16
17use std::collections::HashMap;
18
19use crate::common::{KafkaFuture, TopicPartition};
20
21/// The result of `Admin::alter_partition_reassignments`.
22#[derive(Clone, Debug)]
23#[doc(alias = "org.apache.kafka.clients.admin.AlterPartitionReassignmentsResult")]
24pub struct AlterPartitionReassignmentsResult {
25    futures: HashMap<TopicPartition, KafkaFuture<()>>,
26}
27
28impl AlterPartitionReassignmentsResult {
29    /// Creates a result from the per-partition futures.
30    #[doc(alias = "org.apache.kafka.clients.admin.AlterPartitionReassignmentsResult#AlterPartitionReassignmentsResult")]
31    pub(crate) fn new(futures: HashMap<TopicPartition, KafkaFuture<()>>) -> Self {
32        Self { futures }
33    }
34
35    /// Returns a map from partitions to futures which can be used to check the status of the
36    /// reassignment. Possible errors:
37    ///
38    /// - `Error::is_invalid_replica_assignment_error` (`INVALID_REPLICA_ASSIGNMENT`, 39) - if
39    ///   the specified replica assignment was not valid -- for example, if it included negative
40    ///   numbers, repeated numbers, or specified a broker ID that the controller was not aware
41    ///   of.
42    /// - `Error::is_no_reassignment_in_progress_error` (`NO_REASSIGNMENT_IN_PROGRESS`, 85) - if
43    ///   the request wants to cancel reassignments but none exist
44    /// - `Error::is_unknown_server_error` (`UNKNOWN`, -1)
45    #[doc(alias = "org.apache.kafka.clients.admin.AlterPartitionReassignmentsResult#values")]
46    pub fn values(&self) -> &HashMap<TopicPartition, KafkaFuture<()>> {
47        &self.futures
48    }
49
50    /// Returns a future which succeeds only if all the reassignments were
51    /// successfully initiated.
52    #[doc(alias = "org.apache.kafka.clients.admin.AlterPartitionReassignmentsResult#all")]
53    pub fn all(&self) -> KafkaFuture<()> {
54        KafkaFuture::all_of(self.futures.values().cloned().collect())
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61    use crate::common::Error;
62    use crate::common::internals::KafkaFutureImpl;
63    use crate::common::protocol::Errors;
64
65    #[tokio::test]
66    async fn all_succeeds_when_each_completes() {
67        let h: KafkaFutureImpl<()> = KafkaFutureImpl::new();
68        let mut map = HashMap::new();
69        map.insert(TopicPartition::new("t", 0), h.future());
70        let result = AlterPartitionReassignmentsResult::new(map);
71        h.complete(());
72        assert_eq!(result.all().get().await.unwrap(), ());
73    }
74
75    #[tokio::test]
76    async fn all_fails_if_one_fails() {
77        let h: KafkaFutureImpl<()> = KafkaFutureImpl::new();
78        let mut map = HashMap::new();
79        map.insert(TopicPartition::new("t", 0), h.future());
80        let result = AlterPartitionReassignmentsResult::new(map);
81        h.complete_with_error(Error::new(Errors::InvalidReplicaAssignment));
82        assert!(result.all().get().await.is_err());
83    }
84}