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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//! Cluster description (`DescribeCluster`, KIP-700), as Kafka's
//! `Admin.describeCluster`.
use std::collections::BTreeSet;
use krabka_protocol::owned::{
describe_cluster_request::DescribeClusterRequest,
describe_cluster_response::{DescribeClusterBroker, DescribeClusterResponse},
};
use crate::{
AdminClient, AdminError, kafka_error_name,
retry::ControllerRetry,
users::{AclOperation, wire_to_operation},
};
/// `endpointType` of a `DescribeCluster` request for broker endpoints.
const ENDPOINT_TYPE_BROKERS: i8 = 1;
/// `endpointType` of a `DescribeCluster` request for controller endpoints
/// (KIP-919).
const ENDPOINT_TYPE_CONTROLLERS: i8 = 2;
/// The authorized-operations value of a response that did not compute them
/// (`Integer.MIN_VALUE`).
const AUTHORIZED_OPERATIONS_OMITTED: i32 = i32::MIN;
/// What [`AdminClient::describe_cluster`] asks for, as Kafka's
/// `DescribeClusterOptions`.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, bon::Builder)]
pub struct DescribeClusterOptions {
/// Ask for the operations that the caller may perform on the cluster.
#[builder(default)]
pub include_authorized_operations: bool,
/// List fenced brokers too (KIP-1073). Needs `DescribeCluster` v2.
#[builder(default)]
pub include_fenced_brokers: bool,
}
/// One node of the cluster, as Kafka's `Node`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ClusterNode {
/// The broker or controller ID.
pub id: i32,
/// The advertised host.
pub host: String,
/// The advertised port.
pub port: i32,
/// The rack, when the node has one.
pub rack: Option<String>,
/// Whether the controller has fenced this broker (KIP-1073).
pub is_fenced: bool,
}
/// The cluster that [`AdminClient::describe_cluster`] found, as Kafka's
/// `DescribeClusterResult`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ClusterDescription {
/// The cluster ID.
pub cluster_id: String,
/// The node that the response names as controller, or `None` when it
/// names no listed node. A broker names a random live broker, as `KRaft`
/// clients cannot reach the active controller directly.
pub controller: Option<ClusterNode>,
/// Every node of the response.
pub nodes: Vec<ClusterNode>,
/// The operations that the caller may perform on the cluster, or `None`
/// when the caller did not ask for them.
pub authorized_operations: Option<BTreeSet<AclOperation>>,
}
fn cluster_node(broker: DescribeClusterBroker) -> ClusterNode {
ClusterNode {
id: broker.broker_id,
host: broker.host,
port: broker.port,
rack: broker.rack,
is_fenced: broker.is_fenced,
}
}
pub(crate) fn node_with_host_port(id: i32, host: String, port: i32) -> ClusterNode {
ClusterNode {
id,
host,
port,
rack: None,
is_fenced: false,
}
}
/// The operations of an authorized-operations bit field, as Kafka's
/// `KafkaAdminClient.validAclOperations` reads it: `None` for
/// `Integer.MIN_VALUE`, and without `UNKNOWN`, `ANY` and `ALL`.
pub(crate) fn authorized_operations(bits: i32) -> Option<BTreeSet<AclOperation>> {
if bits == AUTHORIZED_OPERATIONS_OMITTED {
return None;
}
Some(
(0_i8..32)
.filter(|bit| bits & (1_i32 << bit) != 0)
.filter_map(|bit| wire_to_operation(bit).ok())
.filter(|operation| *operation != AclOperation::All)
.collect(),
)
}
/// Maps a `DescribeCluster` answer to the cluster description, as Kafka's
/// `describeCluster` `handleResponse` does.
fn cluster_description(
response: DescribeClusterResponse,
) -> Result<ClusterDescription, AdminError> {
if response.error_code != 0 {
return Err(AdminError::Broker {
api: "DescribeCluster",
code: response.error_code,
name: kafka_error_name(response.error_code),
message: response.error_message,
});
}
let nodes = response
.brokers
.into_iter()
.map(cluster_node)
.collect::<Vec<_>>();
let controller = nodes
.iter()
.find(|node| node.id == response.controller_id)
.cloned();
Ok(ClusterDescription {
cluster_id: response.cluster_id,
controller,
nodes,
authorized_operations: authorized_operations(response.cluster_authorized_operations),
})
}
impl AdminClient {
/// Describes the cluster: its ID, its nodes, the node that serves as
/// controller, and optionally the operations that the caller may perform
/// on it, as Kafka's `describeCluster` operation does.
///
/// The client sends `DescribeCluster` on its connection. Through broker
/// bootstrap it asks for broker endpoints; through controller bootstrap
/// (KIP-919) it asks for controller endpoints with v1 or higher, as
/// Kafka's `KafkaAdminClient` does. `include_fenced_brokers` needs v2,
/// as Kafka's builder refuses a lower version. The call stops at Kafka's
/// default `default.api.timeout.ms` (60 s).
///
/// # Errors
///
/// Returns [`AdminError::Broker`] for a top-level error, such as
/// `CLUSTER_AUTHORIZATION_FAILED` (31), and with `REQUEST_TIMED_OUT` (7)
/// at the deadline. Returns [`AdminError::Transport`] when the
/// connection fails, or when the peer does not support the
/// `DescribeCluster` version that the options need.
pub async fn describe_cluster(
&self,
options: DescribeClusterOptions,
) -> Result<ClusterDescription, AdminError> {
let controllers = self.conn.uses_controller_bootstrap();
let request = DescribeClusterRequest {
include_cluster_authorized_operations: options.include_authorized_operations,
endpoint_type: if controllers {
ENDPOINT_TYPE_CONTROLLERS
} else {
ENDPOINT_TYPE_BROKERS
},
include_fenced_brokers: options.include_fenced_brokers,
..Default::default()
};
let min_version = if options.include_fenced_brokers {
2
} else {
i16::from(controllers)
};
let retry = ControllerRetry::new("DescribeCluster", self.retry);
let response = retry
.bounded(self.conn.send_at_least(request, min_version))
.await?;
cluster_description(response)
}
}
#[cfg(test)]
mod tests {
use std::sync::{Arc, Mutex};
use krabka_client_core::MockBroker;
use krabka_protocol::owned::{api_versions_request, describe_cluster_request};
use super::*;
use crate::partition_leaders::test_support::{
ApiVersionRange, api_versions, decode_request, encode_response, fast_admin,
};
#[test]
fn authorized_operations_skip_all_and_unknown_bits() {
// Bits 0 (UNKNOWN), 1 (ANY), 2 (ALL), 3 (READ), 13 (CREATE_TOKENS)
// and 20 (no such operation).
let bits = 1 | 1 << 1 | 1 << 2 | 1 << 3 | 1 << 13 | 1 << 20;
for (name, bits, expected) in [
("omitted", i32::MIN, None),
("none", 0, Some(BTreeSet::new())),
(
"some",
bits,
Some(maplit::btreeset! {
AclOperation::Read,
AclOperation::CreateTokens,
}),
),
] {
assert2::assert!(authorized_operations(bits) == expected, "case {name}");
}
}
fn broker(broker_id: i32, rack: Option<&str>, is_fenced: bool) -> DescribeClusterBroker {
DescribeClusterBroker {
broker_id,
host: format!("broker-{broker_id}"),
port: 9092,
rack: rack.map(str::to_owned),
is_fenced,
..Default::default()
}
}
fn node(id: i32, rack: Option<&str>, is_fenced: bool) -> ClusterNode {
ClusterNode {
id,
host: format!("broker-{id}"),
port: 9092,
rack: rack.map(str::to_owned),
is_fenced,
}
}
/// Kafka's `describeCluster` asks for broker endpoints, names the
/// listed node with the controller ID as controller, reads the
/// authorized operations, and fails on a top-level code.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn describe_cluster_maps_the_answer_as_kafka_does() {
let answer =
|error_code, controller_id, cluster_authorized_operations| DescribeClusterResponse {
error_code,
error_message: (error_code != 0).then(|| "denied".to_owned()),
cluster_id: "lkc-1".to_owned(),
controller_id,
brokers: vec![broker(1, Some("r1"), false), broker(2, None, true)],
cluster_authorized_operations,
..Default::default()
};
let nodes = vec![node(1, Some("r1"), false), node(2, None, true)];
for (name, options, response, expected) in [
(
"plain",
DescribeClusterOptions::builder().build(),
answer(0, 2, i32::MIN),
Ok(ClusterDescription {
cluster_id: "lkc-1".to_owned(),
controller: Some(node(2, None, true)),
nodes: nodes.clone(),
authorized_operations: None,
}),
),
(
"fenced brokers and authorized operations",
DescribeClusterOptions::builder()
.include_authorized_operations(true)
.include_fenced_brokers(true)
.build(),
answer(0, 7, 1 << 8),
Ok(ClusterDescription {
cluster_id: "lkc-1".to_owned(),
controller: None,
nodes,
authorized_operations: Some(maplit::btreeset! {
AclOperation::Describe,
}),
}),
),
(
"cluster authorization failed is final",
DescribeClusterOptions::builder().build(),
answer(31, 1, i32::MIN),
Err(("DescribeCluster", 31, Some("denied".to_owned()))),
),
] {
let requests = Arc::new(Mutex::new(Vec::<DescribeClusterRequest>::new()));
let handler_requests = Arc::clone(&requests);
let broker = MockBroker::start(move |api_key, version, _, body| match api_key {
api_versions_request::API_KEY => Some(
api_versions()
.apis(vec![
ApiVersionRange::builder()
.api_key(describe_cluster_request::API_KEY)
.max_version(2)
.build(),
])
.call(),
),
describe_cluster_request::API_KEY => {
handler_requests
.lock()
.expect("requests lock")
.push(decode_request(body, version, true));
Some(encode_response(&response, version, true))
}
_ => None,
})
.await;
let admin = fast_admin(broker.addr, krabka_units::secs(5)).await;
let result = admin
.describe_cluster(options)
.await
.map_err(|error| match error {
AdminError::Broker {
api, code, message, ..
} => (api, code, message),
other => panic!("case {name}: unexpected error {other:?}"),
});
broker.stop();
assert2::assert!(
(result, requests.lock().expect("requests lock").clone())
== (
expected,
vec![DescribeClusterRequest {
include_cluster_authorized_operations: options
.include_authorized_operations,
endpoint_type: ENDPOINT_TYPE_BROKERS,
include_fenced_brokers: options.include_fenced_brokers,
..Default::default()
}]
),
"case {name}"
);
}
}
/// Kafka's `DescribeClusterRequest.Builder` refuses
/// `includeFencedBrokers` below v2.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn fenced_brokers_need_describe_cluster_v2() {
let broker = MockBroker::start(move |api_key, _, _, _| match api_key {
api_versions_request::API_KEY => Some(
api_versions()
.apis(vec![
ApiVersionRange::builder()
.api_key(describe_cluster_request::API_KEY)
.max_version(1)
.build(),
])
.call(),
),
_ => None,
})
.await;
let admin = fast_admin(broker.addr, krabka_units::secs(5)).await;
let result = admin
.describe_cluster(
DescribeClusterOptions::builder()
.include_fenced_brokers(true)
.build(),
)
.await;
broker.stop();
assert2::assert!(matches!(
result,
Err(AdminError::Transport(
krabka_client_core::ClientError::IncompatibleVersion { .. }
))
));
}
}