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