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