Skip to main content

confluent_kafka/admin/
describe_replica_log_dirs_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::describe_replica_log_dirs`.
16
17use crate::common::requests::DescribeLogDirsResponse;
18use std::collections::HashMap;
19
20use crate::common::KafkaFuture;
21use crate::common::TopicPartitionReplica;
22
23/// The result of the `Admin::describe_replica_log_dirs` call.
24#[derive(Debug)]
25#[doc(alias = "org.apache.kafka.clients.admin.DescribeReplicaLogDirsResult")]
26pub struct DescribeReplicaLogDirsResult {
27    futures: HashMap<TopicPartitionReplica, KafkaFuture<ReplicaLogDirInfo>>,
28}
29
30impl DescribeReplicaLogDirsResult {
31    /// Creates a new result from the per-replica futures.
32    #[doc(alias = "org.apache.kafka.clients.admin.DescribeReplicaLogDirsResult#DescribeReplicaLogDirsResult")]
33    pub(crate) fn new(futures: HashMap<TopicPartitionReplica, KafkaFuture<ReplicaLogDirInfo>>) -> Self {
34        Self { futures }
35    }
36
37    /// Returns a map from replica to a future which can be used to check the log
38    /// directory information of individual replicas.
39    #[doc(alias = "org.apache.kafka.clients.admin.DescribeReplicaLogDirsResult#values")]
40    pub fn values(&self) -> &HashMap<TopicPartitionReplica, KafkaFuture<ReplicaLogDirInfo>> {
41        &self.futures
42    }
43
44    /// Returns a future which succeeds if log directory information of all
45    /// replicas is available.
46    #[doc(alias = "org.apache.kafka.clients.admin.DescribeReplicaLogDirsResult#all")]
47    pub fn all(&self) -> KafkaFuture<HashMap<TopicPartitionReplica, ReplicaLogDirInfo>> {
48        let entries: Vec<(TopicPartitionReplica, KafkaFuture<ReplicaLogDirInfo>)> =
49            self.futures.iter().map(|(k, v)| (k.clone(), v.clone())).collect();
50        KafkaFuture::join_map(entries)
51    }
52}
53
54/// Log directory information for a replica of a partition on a given broker.
55#[derive(Clone, Debug)]
56#[doc(alias = "org.apache.kafka.clients.admin.DescribeReplicaLogDirsResult$ReplicaLogDirInfo")]
57pub struct ReplicaLogDirInfo {
58    current_replica_log_dir: Option<String>,
59    current_replica_offset_lag: i64,
60    future_replica_log_dir: Option<String>,
61    future_replica_offset_lag: i64,
62}
63
64impl ReplicaLogDirInfo {
65    /// Creates a fully specified `ReplicaLogDirInfo`.
66    #[doc(alias = "org.apache.kafka.clients.admin.DescribeReplicaLogDirsResult$ReplicaLogDirInfo#ReplicaLogDirInfo")]
67    pub(crate) fn new(
68        current_replica_log_dir: Option<String>,
69        current_replica_offset_lag: i64,
70        future_replica_log_dir: Option<String>,
71        future_replica_offset_lag: i64,
72    ) -> Self {
73        Self {
74            current_replica_log_dir,
75            current_replica_offset_lag,
76            future_replica_log_dir,
77            future_replica_offset_lag,
78        }
79    }
80
81    /// Returns the current log directory of the replica of this partition on the given
82    /// broker. `None` if no replica is found for this partition on the broker.
83    #[doc(
84        alias = "org.apache.kafka.clients.admin.DescribeReplicaLogDirsResult$ReplicaLogDirInfo#getCurrentReplicaLogDir"
85    )]
86    pub fn current_replica_log_dir(&self) -> Option<&str> {
87        self.current_replica_log_dir.as_deref()
88    }
89
90    /// Returns the lag of the current replica, defined as
91    /// `max(HW of partition - LEO of the replica, 0)`.
92    #[doc(
93        alias = "org.apache.kafka.clients.admin.DescribeReplicaLogDirsResult$ReplicaLogDirInfo#getCurrentReplicaOffsetLag"
94    )]
95    pub fn current_replica_offset_lag(&self) -> i64 {
96        self.current_replica_offset_lag
97    }
98
99    /// Returns the future log directory of the replica of this partition on the given
100    /// broker. `None` if the replica of this partition is not being moved to
101    /// another log directory on the given broker.
102    #[doc(
103        alias = "org.apache.kafka.clients.admin.DescribeReplicaLogDirsResult$ReplicaLogDirInfo#getFutureReplicaLogDir"
104    )]
105    pub fn future_replica_log_dir(&self) -> Option<&str> {
106        self.future_replica_log_dir.as_deref()
107    }
108
109    /// Returns the LEO of the replica minus the LEO of the future log of this replica in
110    /// the destination log directory. `-1` if either there is no replica for
111    /// this partition or the replica is not being moved to another log
112    /// directory on the given broker.
113    #[doc(
114        alias = "org.apache.kafka.clients.admin.DescribeReplicaLogDirsResult$ReplicaLogDirInfo#getFutureReplicaOffsetLag"
115    )]
116    pub fn future_replica_offset_lag(&self) -> i64 {
117        self.future_replica_offset_lag
118    }
119}
120
121impl Default for ReplicaLogDirInfo {
122    /// The no-argument default, mirroring Java's `ReplicaLogDirInfo()`:
123    /// `(null, INVALID_OFFSET_LAG, null, INVALID_OFFSET_LAG)`.
124    fn default() -> Self {
125        Self::new(
126            None,
127            DescribeLogDirsResponse::INVALID_OFFSET_LAG,
128            None,
129            DescribeLogDirsResponse::INVALID_OFFSET_LAG,
130        )
131    }
132}
133
134impl std::fmt::Display for ReplicaLogDirInfo {
135    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
136        match &self.future_replica_log_dir {
137            Some(future) => write!(
138                f,
139                "(currentReplicaLogDir={}, futureReplicaLogDir={}, futureReplicaOffsetLag={})",
140                self.current_replica_log_dir.as_deref().unwrap_or("null"),
141                future,
142                self.future_replica_offset_lag
143            ),
144            None => write!(
145                f,
146                "ReplicaLogDirInfo(currentReplicaLogDir={})",
147                self.current_replica_log_dir.as_deref().unwrap_or("null")
148            ),
149        }
150    }
151}
152
153#[cfg(test)]
154mod tests {
155    use super::*;
156
157    #[test]
158    fn default_matches_java() {
159        let info = ReplicaLogDirInfo::default();
160        assert_eq!(info.current_replica_log_dir(), None);
161        assert_eq!(info.current_replica_offset_lag(), -1);
162        assert_eq!(info.future_replica_log_dir(), None);
163        assert_eq!(info.future_replica_offset_lag(), -1);
164    }
165
166    #[test]
167    fn display_with_future() {
168        let info = ReplicaLogDirInfo::new(Some("/cur".to_string()), 0, Some("/fut".to_string()), 5);
169        assert_eq!(
170            info.to_string(),
171            "(currentReplicaLogDir=/cur, futureReplicaLogDir=/fut, futureReplicaOffsetLag=5)"
172        );
173    }
174
175    #[test]
176    fn display_without_future() {
177        let info = ReplicaLogDirInfo::new(Some("/cur".to_string()), 0, None, -1);
178        assert_eq!(info.to_string(), "ReplicaLogDirInfo(currentReplicaLogDir=/cur)");
179    }
180}