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