1use bytes::{Buf, BufMut};
4
5use crate::{
6 Decode, Encode, ProtocolError, UnknownTaggedFields,
7 primitives::{
8 fixed::{
9 get_bool, get_i16, get_i32, get_i64, get_u16, put_bool, put_i16, put_i32, put_i64,
10 put_u16,
11 },
12 string_bytes::{
13 compact_nullable_string_len, compact_string_len, get_compact_nullable_string_owned,
14 get_compact_string_owned, get_nullable_string_owned, get_string_owned,
15 nullable_string_len, put_compact_nullable_string, put_compact_string,
16 put_nullable_string, put_string, string_len,
17 },
18 },
19 tagged_fields::{WriteTaggedFields, read_tagged_fields, tagged_fields_len},
20};
21pub const API_KEY: i16 = 62;
22pub const MIN_VERSION: i16 = 0;
23pub const MAX_VERSION: i16 = 4;
24pub const LATEST_STABLE_VERSION: i16 = 4;
25pub const FLEXIBLE_MIN: i16 = 0;
26#[inline]
27#[must_use]
28pub fn is_flexible(version: i16) -> bool {
29 version >= FLEXIBLE_MIN
30}
31#[derive(Debug, Clone, PartialEq, Eq)]
32pub struct BrokerRegistrationRequest {
33 pub broker_id: i32,
34 pub cluster_id: String,
35 pub incarnation_id: crate::primitives::uuid::Uuid,
36 pub listeners: Vec<Listener>,
37 pub features: Vec<Feature>,
38 pub rack: Option<String>,
39 pub is_migrating_zk_broker: bool,
40 pub log_dirs: Vec<crate::primitives::uuid::Uuid>,
41 pub previous_broker_epoch: i64,
42 pub unknown_tagged_fields: UnknownTaggedFields,
43}
44impl Default for BrokerRegistrationRequest {
45 fn default() -> Self {
46 Self {
47 broker_id: 0i32,
48 cluster_id: String::new(),
49 incarnation_id: crate::primitives::uuid::Uuid::default(),
50 listeners: Vec::new(),
51 features: Vec::new(),
52 rack: Some(String::new()),
53 is_migrating_zk_broker: false,
54 log_dirs: Vec::new(),
55 previous_broker_epoch: -1i64,
56 unknown_tagged_fields: UnknownTaggedFields::default(),
57 }
58 }
59}
60impl BrokerRegistrationRequest {
61 fn encode_field_0<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
62 if version >= 0 {
63 put_i32(buf, self.broker_id);
64 }
65 }
66 fn encode_field_1<B: BufMut>(&self, buf: &mut B, version: i16, flex: bool) {
67 if version >= 0 {
68 if flex {
69 let () = put_compact_string(buf, &self.cluster_id);
70 } else {
71 let () = put_string(buf, &self.cluster_id);
72 }
73 }
74 }
75 fn encode_field_2<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
76 if version >= 0 {
77 crate::primitives::uuid::put_uuid(buf, self.incarnation_id);
78 }
79 }
80 fn encode_field_3<B: BufMut>(
81 &self,
82 buf: &mut B,
83 version: i16,
84 flex: bool,
85 ) -> Result<(), ProtocolError> {
86 if version >= 0 {
87 {
88 crate::primitives::array::put_array_len(buf, (self.listeners).len(), flex);
89 for it in &self.listeners {
90 it.encode(buf, version)?;
91 }
92 };
93 }
94 Ok(())
95 }
96 fn encode_field_4<B: BufMut>(
97 &self,
98 buf: &mut B,
99 version: i16,
100 flex: bool,
101 ) -> Result<(), ProtocolError> {
102 if version >= 0 {
103 {
104 crate::primitives::array::put_array_len(buf, (self.features).len(), flex);
105 for it in &self.features {
106 it.encode(buf, version)?;
107 }
108 };
109 }
110 Ok(())
111 }
112 fn encode_field_5<B: BufMut>(&self, buf: &mut B, version: i16, flex: bool) {
113 if version >= 0 {
114 if flex {
115 let () = put_compact_nullable_string(buf, self.rack.as_deref());
116 } else {
117 let () = put_nullable_string(buf, self.rack.as_deref());
118 }
119 }
120 }
121 fn encode_field_6<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
122 if version >= 1 {
123 put_bool(buf, self.is_migrating_zk_broker);
124 }
125 }
126 fn encode_field_7<B: BufMut>(&self, buf: &mut B, version: i16, flex: bool) {
127 if version >= 2 {
128 {
129 crate::primitives::array::put_array_len(buf, (self.log_dirs).len(), flex);
130 for it in &self.log_dirs {
131 crate::primitives::uuid::put_uuid(buf, *it);
132 }
133 };
134 }
135 }
136 fn encode_field_8<B: BufMut>(&self, buf: &mut B, version: i16, _flex: bool) {
137 if version >= 3 {
138 put_i64(buf, self.previous_broker_epoch);
139 }
140 }
141 fn encode_tagged_fields<B: BufMut>(&self, buf: &mut B, _version: i16, flex: bool) {
142 if flex {
143 let tagged = WriteTaggedFields::new();
144 tagged.write(buf, &self.unknown_tagged_fields);
145 }
146 }
147 fn decode_field_0<B: Buf>(
148 out: &mut Self,
149 buf: &mut B,
150 version: i16,
151 _flex: bool,
152 ) -> Result<(), ProtocolError> {
153 if version >= 0 {
154 out.broker_id = get_i32(buf)?;
155 }
156 Ok(())
157 }
158 fn decode_field_1<B: Buf>(
159 out: &mut Self,
160 buf: &mut B,
161 version: i16,
162 flex: bool,
163 ) -> Result<(), ProtocolError> {
164 if version >= 0 {
165 out.cluster_id = if flex {
166 get_compact_string_owned(buf)?
167 } else {
168 get_string_owned(buf)?
169 };
170 }
171 Ok(())
172 }
173 fn decode_field_2<B: Buf>(
174 out: &mut Self,
175 buf: &mut B,
176 version: i16,
177 _flex: bool,
178 ) -> Result<(), ProtocolError> {
179 if version >= 0 {
180 out.incarnation_id = crate::primitives::uuid::get_uuid(buf)?;
181 }
182 Ok(())
183 }
184 fn decode_field_3<B: Buf>(
185 out: &mut Self,
186 buf: &mut B,
187 version: i16,
188 flex: bool,
189 ) -> Result<(), ProtocolError> {
190 if version >= 0 {
191 out.listeners = {
192 let n = crate::primitives::array::get_array_len(buf, flex)?;
193 let mut v = Vec::with_capacity(n);
194 for _ in 0..n {
195 v.push(Listener::decode(buf, version)?);
196 }
197 v
198 };
199 }
200 Ok(())
201 }
202 fn decode_field_4<B: Buf>(
203 out: &mut Self,
204 buf: &mut B,
205 version: i16,
206 flex: bool,
207 ) -> Result<(), ProtocolError> {
208 if version >= 0 {
209 out.features = {
210 let n = crate::primitives::array::get_array_len(buf, flex)?;
211 let mut v = Vec::with_capacity(n);
212 for _ in 0..n {
213 v.push(Feature::decode(buf, version)?);
214 }
215 v
216 };
217 }
218 Ok(())
219 }
220 fn decode_field_5<B: Buf>(
221 out: &mut Self,
222 buf: &mut B,
223 version: i16,
224 flex: bool,
225 ) -> Result<(), ProtocolError> {
226 if version >= 0 {
227 out.rack = if flex {
228 get_compact_nullable_string_owned(buf)?
229 } else {
230 get_nullable_string_owned(buf)?
231 };
232 }
233 Ok(())
234 }
235 fn decode_field_6<B: Buf>(
236 out: &mut Self,
237 buf: &mut B,
238 version: i16,
239 _flex: bool,
240 ) -> Result<(), ProtocolError> {
241 if version >= 1 {
242 out.is_migrating_zk_broker = get_bool(buf)?;
243 }
244 Ok(())
245 }
246 fn decode_field_7<B: Buf>(
247 out: &mut Self,
248 buf: &mut B,
249 version: i16,
250 flex: bool,
251 ) -> Result<(), ProtocolError> {
252 if version >= 2 {
253 out.log_dirs = {
254 let n = crate::primitives::array::get_array_len(buf, flex)?;
255 let mut v = Vec::with_capacity(n);
256 for _ in 0..n {
257 v.push(crate::primitives::uuid::get_uuid(buf)?);
258 }
259 v
260 };
261 }
262 Ok(())
263 }
264 fn decode_field_8<B: Buf>(
265 out: &mut Self,
266 buf: &mut B,
267 version: i16,
268 _flex: bool,
269 ) -> Result<(), ProtocolError> {
270 if version >= 3 {
271 out.previous_broker_epoch = get_i64(buf)?;
272 }
273 Ok(())
274 }
275 fn decode_tagged_fields<B: Buf>(
276 out: &mut Self,
277 buf: &mut B,
278 _version: i16,
279 flex: bool,
280 ) -> Result<(), ProtocolError> {
281 if flex {
282 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
283 }
284 Ok(())
285 }
286}
287impl Encode for BrokerRegistrationRequest {
288 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
289 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
290 return Err(ProtocolError::UnsupportedVersion {
291 api_key: API_KEY,
292 version,
293 });
294 }
295 let flex = is_flexible(version);
296 self.encode_field_0(buf, version, flex);
297 self.encode_field_1(buf, version, flex);
298 self.encode_field_2(buf, version, flex);
299 self.encode_field_3(buf, version, flex)?;
300 self.encode_field_4(buf, version, flex)?;
301 self.encode_field_5(buf, version, flex);
302 self.encode_field_6(buf, version, flex);
303 self.encode_field_7(buf, version, flex);
304 self.encode_field_8(buf, version, flex);
305 self.encode_tagged_fields(buf, version, flex);
306 Ok(())
307 }
308 fn encoded_len(&self, version: i16) -> usize {
309 let flex = is_flexible(version);
310 let mut n: usize = 0;
311 if version >= 0 {
312 n += 4;
313 }
314 if version >= 0 {
315 n += if flex {
316 compact_string_len(&self.cluster_id)
317 } else {
318 string_len(&self.cluster_id)
319 };
320 }
321 if version >= 0 {
322 n += 16;
323 }
324 if version >= 0 {
325 n += {
326 let prefix =
327 crate::primitives::array::array_len_prefix_len((self.listeners).len(), flex);
328 let body: usize = (self.listeners)
329 .iter()
330 .map(|it| it.encoded_len(version))
331 .sum();
332 prefix + body
333 };
334 }
335 if version >= 0 {
336 n += {
337 let prefix =
338 crate::primitives::array::array_len_prefix_len((self.features).len(), flex);
339 let body: usize = (self.features)
340 .iter()
341 .map(|it| it.encoded_len(version))
342 .sum();
343 prefix + body
344 };
345 }
346 if version >= 0 {
347 n += if flex {
348 compact_nullable_string_len(self.rack.as_deref())
349 } else {
350 nullable_string_len(self.rack.as_deref())
351 };
352 }
353 if version >= 1 {
354 n += 1;
355 }
356 if version >= 2 {
357 n += {
358 let prefix =
359 crate::primitives::array::array_len_prefix_len((self.log_dirs).len(), flex);
360 let body: usize = (self.log_dirs).iter().map(|_| 16).sum();
361 prefix + body
362 };
363 }
364 if version >= 3 {
365 n += 8;
366 }
367 if flex {
368 let known_pairs: Vec<(u32, usize)> = Vec::new();
369 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
370 }
371 n
372 }
373}
374impl Decode<'_> for BrokerRegistrationRequest {
375 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
376 if !(MIN_VERSION..=MAX_VERSION).contains(&version) {
377 return Err(ProtocolError::UnsupportedVersion {
378 api_key: API_KEY,
379 version,
380 });
381 }
382 let flex = is_flexible(version);
383 let mut out = Self::default();
384 Self::decode_field_0(&mut out, buf, version, flex)?;
385 Self::decode_field_1(&mut out, buf, version, flex)?;
386 Self::decode_field_2(&mut out, buf, version, flex)?;
387 Self::decode_field_3(&mut out, buf, version, flex)?;
388 Self::decode_field_4(&mut out, buf, version, flex)?;
389 Self::decode_field_5(&mut out, buf, version, flex)?;
390 Self::decode_field_6(&mut out, buf, version, flex)?;
391 Self::decode_field_7(&mut out, buf, version, flex)?;
392 Self::decode_field_8(&mut out, buf, version, flex)?;
393 Self::decode_tagged_fields(&mut out, buf, version, flex)?;
394 Ok(out)
395 }
396}
397#[cfg(test)]
398impl BrokerRegistrationRequest {
399 #[must_use]
400 pub fn populated(version: i16) -> Self {
401 let mut m = Self::default();
402 if version >= 0 {
403 m.broker_id = 1i32;
404 }
405 if version >= 0 {
406 m.cluster_id = "x".to_string();
407 }
408 if version >= 0 {
409 m.incarnation_id = crate::primitives::uuid::Uuid([1u8; 16]);
410 }
411 if version >= 0 {
412 m.listeners = vec![Listener::populated(version)];
413 }
414 if version >= 0 {
415 m.features = vec![Feature::populated(version)];
416 }
417 if version >= 0 {
418 m.rack = Some("x".to_string());
419 }
420 if version >= 1 {
421 m.is_migrating_zk_broker = true;
422 }
423 if version >= 2 {
424 m.log_dirs = vec![crate::primitives::uuid::Uuid([1u8; 16])];
425 }
426 if version >= 3 {
427 m.previous_broker_epoch = 1i64;
428 }
429 m
430 }
431}
432#[derive(Debug, Clone, PartialEq, Eq, Default)]
433pub struct Listener {
434 pub name: String,
435 pub host: String,
436 pub port: u16,
437 pub security_protocol: i16,
438 pub unknown_tagged_fields: UnknownTaggedFields,
439}
440impl Encode for Listener {
441 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
442 let flex = version >= 0;
443 if version >= 0 {
444 if flex {
445 let () = put_compact_string(buf, &self.name);
446 } else {
447 let () = put_string(buf, &self.name);
448 }
449 }
450 if version >= 0 {
451 if flex {
452 let () = put_compact_string(buf, &self.host);
453 } else {
454 let () = put_string(buf, &self.host);
455 }
456 }
457 if version >= 0 {
458 put_u16(buf, self.port);
459 }
460 if version >= 0 {
461 put_i16(buf, self.security_protocol);
462 }
463 if flex {
464 let tagged = WriteTaggedFields::new();
465 tagged.write(buf, &self.unknown_tagged_fields);
466 }
467 Ok(())
468 }
469 fn encoded_len(&self, version: i16) -> usize {
470 let flex = version >= 0;
471 let mut n: usize = 0;
472 if version >= 0 {
473 n += if flex {
474 compact_string_len(&self.name)
475 } else {
476 string_len(&self.name)
477 };
478 }
479 if version >= 0 {
480 n += if flex {
481 compact_string_len(&self.host)
482 } else {
483 string_len(&self.host)
484 };
485 }
486 if version >= 0 {
487 n += 2;
488 }
489 if version >= 0 {
490 n += 2;
491 }
492 if flex {
493 let known_pairs: Vec<(u32, usize)> = Vec::new();
494 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
495 }
496 n
497 }
498}
499impl Decode<'_> for Listener {
500 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
501 let flex = version >= 0;
502 let mut out = Self::default();
503 if version >= 0 {
504 out.name = if flex {
505 get_compact_string_owned(buf)?
506 } else {
507 get_string_owned(buf)?
508 };
509 }
510 if version >= 0 {
511 out.host = if flex {
512 get_compact_string_owned(buf)?
513 } else {
514 get_string_owned(buf)?
515 };
516 }
517 if version >= 0 {
518 out.port = get_u16(buf)?;
519 }
520 if version >= 0 {
521 out.security_protocol = get_i16(buf)?;
522 }
523 if flex {
524 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
525 }
526 Ok(out)
527 }
528}
529#[cfg(test)]
530impl Listener {
531 #[must_use]
532 pub fn populated(version: i16) -> Self {
533 let mut m = Self::default();
534 if version >= 0 {
535 m.name = "x".to_string();
536 }
537 if version >= 0 {
538 m.host = "x".to_string();
539 }
540 if version >= 0 {
541 m.port = 1u16;
542 }
543 if version >= 0 {
544 m.security_protocol = 1i16;
545 }
546 m
547 }
548}
549#[derive(Debug, Clone, PartialEq, Eq, Default)]
550pub struct Feature {
551 pub name: String,
552 pub min_supported_version: i16,
553 pub max_supported_version: i16,
554 pub unknown_tagged_fields: UnknownTaggedFields,
555}
556impl Encode for Feature {
557 fn encode<B: BufMut>(&self, buf: &mut B, version: i16) -> Result<(), ProtocolError> {
558 let flex = version >= 0;
559 if version >= 0 {
560 if flex {
561 let () = put_compact_string(buf, &self.name);
562 } else {
563 let () = put_string(buf, &self.name);
564 }
565 }
566 if version >= 0 {
567 put_i16(buf, self.min_supported_version);
568 }
569 if version >= 0 {
570 put_i16(buf, self.max_supported_version);
571 }
572 if flex {
573 let tagged = WriteTaggedFields::new();
574 tagged.write(buf, &self.unknown_tagged_fields);
575 }
576 Ok(())
577 }
578 fn encoded_len(&self, version: i16) -> usize {
579 let flex = version >= 0;
580 let mut n: usize = 0;
581 if version >= 0 {
582 n += if flex {
583 compact_string_len(&self.name)
584 } else {
585 string_len(&self.name)
586 };
587 }
588 if version >= 0 {
589 n += 2;
590 }
591 if version >= 0 {
592 n += 2;
593 }
594 if flex {
595 let known_pairs: Vec<(u32, usize)> = Vec::new();
596 n += tagged_fields_len(&known_pairs, &self.unknown_tagged_fields);
597 }
598 n
599 }
600}
601impl Decode<'_> for Feature {
602 fn decode<B: Buf>(buf: &mut B, version: i16) -> Result<Self, ProtocolError> {
603 let flex = version >= 0;
604 let mut out = Self::default();
605 if version >= 0 {
606 out.name = if flex {
607 get_compact_string_owned(buf)?
608 } else {
609 get_string_owned(buf)?
610 };
611 }
612 if version >= 0 {
613 out.min_supported_version = get_i16(buf)?;
614 }
615 if version >= 0 {
616 out.max_supported_version = get_i16(buf)?;
617 }
618 if flex {
619 out.unknown_tagged_fields = read_tagged_fields(buf, |_tag, _payload| Ok(false))?;
620 }
621 Ok(out)
622 }
623}
624#[cfg(test)]
625impl Feature {
626 #[must_use]
627 pub fn populated(version: i16) -> Self {
628 let mut m = Self::default();
629 if version >= 0 {
630 m.name = "x".to_string();
631 }
632 if version >= 0 {
633 m.min_supported_version = 1i16;
634 }
635 if version >= 0 {
636 m.max_supported_version = 1i16;
637 }
638 m
639 }
640}
641#[must_use]
644#[allow(unused_comparisons)]
645pub fn default_json(version: i16) -> ::serde_json::Value {
646 let mut obj = ::serde_json::Map::new();
647 obj.insert("brokerId".to_string(), ::serde_json::json!(0));
648 obj.insert(
649 "clusterId".to_string(),
650 ::serde_json::Value::String(String::new()),
651 );
652 obj.insert(
653 "incarnationId".to_string(),
654 ::serde_json::Value::String("AAAAAAAAAAAAAAAAAAAAAA".to_string()),
655 );
656 obj.insert("listeners".to_string(), ::serde_json::Value::Array(vec![]));
657 obj.insert("features".to_string(), ::serde_json::Value::Array(vec![]));
658 obj.insert(
659 "rack".to_string(),
660 ::serde_json::Value::String(String::new()),
661 );
662 if version >= 1 {
663 obj.insert(
664 "isMigratingZkBroker".to_string(),
665 ::serde_json::Value::Bool(false),
666 );
667 }
668 if version >= 2 {
669 obj.insert("logDirs".to_string(), ::serde_json::Value::Array(vec![]));
670 }
671 if version >= 3 {
672 obj.insert("previousBrokerEpoch".to_string(), ::serde_json::json!(-1));
673 }
674 ::serde_json::Value::Object(obj)
675}
676impl crate::ProtocolRequest for BrokerRegistrationRequest {
677 const API_KEY: i16 = API_KEY;
678 const MIN_VERSION: i16 = MIN_VERSION;
679 const MAX_VERSION: i16 = MAX_VERSION;
680 const LATEST_STABLE_VERSION: i16 = LATEST_STABLE_VERSION;
681 const FLEXIBLE_MIN: i16 = FLEXIBLE_MIN;
682 type Response = super::broker_registration_response::BrokerRegistrationResponse;
683}