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