1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//! AdminClient operation group: configs.
use std::collections::HashMap;
use std::time::Duration;
use tracing::info;
use crate::error::{KrafkaError, ProtocolErrorKind, Result};
use crate::protocol::{
AlterConfigOp, AlterableConfig, ApiKey, DescribeClusterRequest, DescribeClusterResponse,
DescribeConfigsResponse, IncrementalAlterConfigsRequest, IncrementalAlterConfigsResponse,
VersionedDecode, VersionedEncode, versions,
};
#[allow(clippy::wildcard_imports)]
use super::*;
impl AdminClient {
/// Describe configuration for one or more resources (topics, brokers, etc.).
///
/// Uses DescribeConfigs (API Key 32). Build a [`DescribeConfigsRequest`]
/// via its convenience constructors (`for_topic`, `for_broker`) or manually
/// populate the `resources` field for multi-resource queries.
pub async fn describe_configs(
&self,
request: DescribeConfigsRequest,
) -> Result<Vec<ConfigEntry>> {
let conn = self.get_any_broker_connection().await?;
let version = conn
.negotiate_api_version(
ApiKey::DescribeConfigs,
versions::DESCRIBE_CONFIGS_MAX,
versions::DESCRIBE_CONFIGS_MIN,
)
.await
.ok_or_else(|| {
KrafkaError::protocol_kind(
ProtocolErrorKind::UnknownApiVersion,
"no mutually supported DescribeConfigs API version",
)
})?;
let response_bytes = conn
.send_request(ApiKey::DescribeConfigs, version, |buf| {
request.encode_versioned(version, buf)
})
.await?;
let mut buf = response_bytes;
let response = DescribeConfigsResponse::decode_versioned(version, &mut buf)?;
let entries = response
.results
.into_iter()
.flat_map(|r| {
if !r.error_code.is_ok() {
return Vec::new();
}
r.configs
.into_iter()
.map(|c| ConfigEntry {
name: c.name,
value: c.value,
read_only: c.read_only,
is_default: c.is_default,
is_sensitive: c.is_sensitive,
config_source: c.config_source,
synonyms: c
.synonyms
.into_iter()
.map(|s| ConfigSynonymEntry {
name: s.name,
value: s.value,
source: s.source,
})
.collect(),
config_type: c.config_type,
documentation: c.documentation,
})
.collect()
})
.collect();
Ok(entries)
}
/// Alter configuration for a topic.
///
/// Uses IncrementalAlterConfigs (API Key 44) to set individual config keys
/// without replacing the entire config. Each key-value pair is applied as a
/// SET operation.
pub async fn alter_topic_config(
&self,
topic: &str,
configs: HashMap<String, String>,
) -> Result<AlterConfigResult> {
let conn = self.get_any_broker_connection().await?;
let request = IncrementalAlterConfigsRequest::for_topic(
topic,
configs
.into_iter()
.map(|(name, value)| AlterableConfig {
name,
config_operation: AlterConfigOp::Set,
value: Some(value),
})
.collect(),
);
let version = conn
.negotiate_api_version(
ApiKey::IncrementalAlterConfigs,
versions::INCREMENTAL_ALTER_CONFIGS_MAX,
versions::INCREMENTAL_ALTER_CONFIGS_MIN,
)
.await
.ok_or_else(|| {
KrafkaError::protocol_kind(
ProtocolErrorKind::UnknownApiVersion,
"no mutually supported IncrementalAlterConfigs API version",
)
})?;
let response_bytes = conn
.send_request(ApiKey::IncrementalAlterConfigs, version, |buf| {
request.encode_versioned(version, buf)
})
.await?;
let mut buf = response_bytes;
let response = IncrementalAlterConfigsResponse::decode_versioned(version, &mut buf)?;
let result = response
.results
.into_iter()
.next()
.map(|r| AlterConfigResult {
resource_name: r.resource_name,
error: if r.error_code.is_ok() {
None
} else {
Some(
r.error_message
.unwrap_or_else(|| format!("{:?}", r.error_code)),
)
},
})
.unwrap_or(AlterConfigResult {
resource_name: topic.to_string(),
error: Some("no response received".to_string()),
});
if result.error.is_none() {
info!("Altered config for topic {}", topic);
}
Ok(result)
}
/// List all topics.
pub async fn list_topics(&self) -> Result<Vec<String>> {
self.check_not_closed()?;
self.metadata.refresh().await?;
Ok(self.metadata.topics().into_iter().map(|t| t.name).collect())
}
/// Fetch specific config keys for a topic.
///
/// A convenience wrapper around [`describe_configs`](Self::describe_configs)
/// for the common case of reading a small set of well-known topic-level keys.
/// Pass an empty `keys` slice to fetch all config entries for the topic.
///
/// Returns a map of config key → [`ConfigValue`].
///
/// # Examples
///
/// ```rust,ignore
/// let cfg = admin.topic_config("my-topic", &["retention.ms", "cleanup.policy"]).await?;
/// if let Some(krafka::admin::ConfigValue::Value(v)) = cfg.get("retention.ms") {
/// println!("retention.ms = {v}");
/// }
/// ```
pub async fn topic_config(
&self,
topic: &str,
keys: &[&str],
) -> Result<HashMap<String, super::ConfigValue>> {
let request = DescribeConfigsRequest {
resources: vec![super::DescribeConfigsResource {
resource_type: super::ConfigResourceType::Topic,
resource_name: topic.to_string(),
config_names: if keys.is_empty() {
None
} else {
Some(keys.iter().map(|k| k.to_string()).collect())
},
}],
include_synonyms: false,
include_documentation: false,
};
let entries = self.describe_configs(request).await?;
Ok(entries
.into_iter()
.map(|e| {
let cv = e.config_value();
(e.name, cv)
})
.collect())
}
/// Describe topics by name, returning a map of topic name → [`TopicInfo`].
///
/// Topics not found in cluster metadata are absent from the returned map;
/// callers can detect missing topics by comparing request and response key sets.
pub async fn describe_topics<S: AsRef<str>>(
&self,
topics: &[S],
) -> Result<HashMap<String, TopicInfo>> {
self.check_not_closed()?;
self.metadata.refresh().await?;
// Use O(1) per-topic metadata look-up instead of a full Vec scan.
let result = topics
.iter()
.filter_map(|name| {
self.metadata
.topic(name.as_ref())
.map(|info| (name.as_ref().to_owned(), info))
})
.collect();
Ok(result)
}
/// Describe a single topic by name.
///
/// Returns `None` if the topic does not exist or is not visible in cluster metadata.
pub async fn describe_topic(&self, topic: &str) -> Result<Option<TopicInfo>> {
self.check_not_closed()?;
self.metadata.refresh().await?;
Ok(self.metadata.topic(topic))
}
/// Describe the cluster using the DescribeCluster API (Key 60).
///
/// Returns cluster metadata including cluster ID, controller, brokers,
/// and authorized operations.
pub async fn describe_cluster(&self) -> Result<DescribeClusterResult> {
self.check_not_closed()?;
let conn = self.get_any_broker_connection().await?;
let request = DescribeClusterRequest::default();
let version = conn
.negotiate_api_version(
ApiKey::DescribeCluster,
versions::DESCRIBE_CLUSTER_MAX,
versions::DESCRIBE_CLUSTER_MIN,
)
.await
.ok_or_else(|| {
KrafkaError::protocol_kind(
ProtocolErrorKind::UnknownApiVersion,
"no mutually supported DescribeCluster API version",
)
})?;
let response_bytes = conn
.send_request(ApiKey::DescribeCluster, version, |buf| {
request.encode_versioned(version, buf)
})
.await?;
let mut buf = response_bytes;
let response = DescribeClusterResponse::decode_versioned(version, &mut buf)?;
if !response.error_code.is_ok() {
let msg = response
.error_message
.unwrap_or_else(|| format!("{:?}", response.error_code));
return Err(KrafkaError::protocol_kind(
ProtocolErrorKind::Malformed,
msg,
));
}
Ok(DescribeClusterResult {
cluster_id: response.cluster_id,
controller_id: response.controller_id,
brokers: response
.brokers
.into_iter()
.map(|b| DescribeClusterBrokerInfo {
broker_id: b.broker_id,
host: b.host,
port: b.port,
rack: b.rack,
})
.collect(),
cluster_authorized_operations: response.cluster_authorized_operations,
})
}
/// Get partition count for a topic.
pub async fn partition_count(&self, topic: &str) -> Result<Option<usize>> {
self.check_not_closed()?;
self.metadata.refresh().await?;
Ok(self.metadata.partition_count(topic))
}
/// Get the client ID.
#[inline]
pub fn client_id(&self) -> &str {
&self.config.client_id
}
/// Get the request timeout.
#[inline]
pub fn request_timeout(&self) -> Duration {
self.config.request_timeout
}
}