#![allow(unused)]
use std::borrow::Borrow;
use std::collections::BTreeMap;
use anyhow::{bail, Result};
use bytes::Bytes;
use uuid::Uuid;
use crate::protocol::{
buf::{ByteBuf, ByteBufMut},
compute_unknown_tagged_fields_size, types, write_unknown_tagged_fields, Decodable, Decoder,
Encodable, Encoder, HeaderVersion, Message, StrBytes, VersionRange,
};
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct BrokerRegistrationRequest {
pub broker_id: super::BrokerId,
pub cluster_id: StrBytes,
pub incarnation_id: Uuid,
pub listeners: Vec<Listener>,
pub features: Vec<Feature>,
pub rack: Option<StrBytes>,
pub is_migrating_zk_broker: bool,
pub log_dirs: Vec<Uuid>,
pub previous_broker_epoch: i64,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl BrokerRegistrationRequest {
pub fn with_broker_id(mut self, value: super::BrokerId) -> Self {
self.broker_id = value;
self
}
pub fn with_cluster_id(mut self, value: StrBytes) -> Self {
self.cluster_id = value;
self
}
pub fn with_incarnation_id(mut self, value: Uuid) -> Self {
self.incarnation_id = value;
self
}
pub fn with_listeners(mut self, value: Vec<Listener>) -> Self {
self.listeners = value;
self
}
pub fn with_features(mut self, value: Vec<Feature>) -> Self {
self.features = value;
self
}
pub fn with_rack(mut self, value: Option<StrBytes>) -> Self {
self.rack = value;
self
}
pub fn with_is_migrating_zk_broker(mut self, value: bool) -> Self {
self.is_migrating_zk_broker = value;
self
}
pub fn with_log_dirs(mut self, value: Vec<Uuid>) -> Self {
self.log_dirs = value;
self
}
pub fn with_previous_broker_epoch(mut self, value: i64) -> Self {
self.previous_broker_epoch = value;
self
}
pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
self.unknown_tagged_fields = value;
self
}
pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
self.unknown_tagged_fields.insert(key, value);
self
}
}
#[cfg(feature = "client")]
impl Encodable for BrokerRegistrationRequest {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
types::Int32.encode(buf, &self.broker_id)?;
types::CompactString.encode(buf, &self.cluster_id)?;
types::Uuid.encode(buf, &self.incarnation_id)?;
types::CompactArray(types::Struct { version }).encode(buf, &self.listeners)?;
types::CompactArray(types::Struct { version }).encode(buf, &self.features)?;
types::CompactString.encode(buf, &self.rack)?;
if version >= 1 {
types::Boolean.encode(buf, &self.is_migrating_zk_broker)?;
} else {
if self.is_migrating_zk_broker {
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 2 {
types::CompactArray(types::Uuid).encode(buf, &self.log_dirs)?;
}
if version >= 3 {
types::Int64.encode(buf, &self.previous_broker_epoch)?;
}
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
bail!(
"Too many tagged fields to encode ({} fields)",
num_tagged_fields
);
}
types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
Ok(())
}
fn compute_size(&self, version: i16) -> Result<usize> {
let mut total_size = 0;
total_size += types::Int32.compute_size(&self.broker_id)?;
total_size += types::CompactString.compute_size(&self.cluster_id)?;
total_size += types::Uuid.compute_size(&self.incarnation_id)?;
total_size +=
types::CompactArray(types::Struct { version }).compute_size(&self.listeners)?;
total_size +=
types::CompactArray(types::Struct { version }).compute_size(&self.features)?;
total_size += types::CompactString.compute_size(&self.rack)?;
if version >= 1 {
total_size += types::Boolean.compute_size(&self.is_migrating_zk_broker)?;
} else {
if self.is_migrating_zk_broker {
bail!("A field is set that is not available on the selected protocol version");
}
}
if version >= 2 {
total_size += types::CompactArray(types::Uuid).compute_size(&self.log_dirs)?;
}
if version >= 3 {
total_size += types::Int64.compute_size(&self.previous_broker_epoch)?;
}
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
bail!(
"Too many tagged fields to encode ({} fields)",
num_tagged_fields
);
}
total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
Ok(total_size)
}
}
#[cfg(feature = "broker")]
impl Decodable for BrokerRegistrationRequest {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
let broker_id = types::Int32.decode(buf)?;
let cluster_id = types::CompactString.decode(buf)?;
let incarnation_id = types::Uuid.decode(buf)?;
let listeners = types::CompactArray(types::Struct { version }).decode(buf)?;
let features = types::CompactArray(types::Struct { version }).decode(buf)?;
let rack = types::CompactString.decode(buf)?;
let is_migrating_zk_broker = if version >= 1 {
types::Boolean.decode(buf)?
} else {
false
};
let log_dirs = if version >= 2 {
types::CompactArray(types::Uuid).decode(buf)?
} else {
Default::default()
};
let previous_broker_epoch = if version >= 3 {
types::Int64.decode(buf)?
} else {
-1
};
let mut unknown_tagged_fields = BTreeMap::new();
let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
for _ in 0..num_tagged_fields {
let tag: u32 = types::UnsignedVarInt.decode(buf)?;
let size: u32 = types::UnsignedVarInt.decode(buf)?;
let unknown_value = buf.try_get_bytes(size as usize)?;
unknown_tagged_fields.insert(tag as i32, unknown_value);
}
Ok(Self {
broker_id,
cluster_id,
incarnation_id,
listeners,
features,
rack,
is_migrating_zk_broker,
log_dirs,
previous_broker_epoch,
unknown_tagged_fields,
})
}
}
impl Default for BrokerRegistrationRequest {
fn default() -> Self {
Self {
broker_id: (0).into(),
cluster_id: Default::default(),
incarnation_id: Uuid::nil(),
listeners: Default::default(),
features: Default::default(),
rack: Some(Default::default()),
is_migrating_zk_broker: false,
log_dirs: Default::default(),
previous_broker_epoch: -1,
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for BrokerRegistrationRequest {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct Feature {
pub name: StrBytes,
pub min_supported_version: i16,
pub max_supported_version: i16,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl Feature {
pub fn with_name(mut self, value: StrBytes) -> Self {
self.name = value;
self
}
pub fn with_min_supported_version(mut self, value: i16) -> Self {
self.min_supported_version = value;
self
}
pub fn with_max_supported_version(mut self, value: i16) -> Self {
self.max_supported_version = value;
self
}
pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
self.unknown_tagged_fields = value;
self
}
pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
self.unknown_tagged_fields.insert(key, value);
self
}
}
#[cfg(feature = "client")]
impl Encodable for Feature {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
types::CompactString.encode(buf, &self.name)?;
types::Int16.encode(buf, &self.min_supported_version)?;
types::Int16.encode(buf, &self.max_supported_version)?;
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
bail!(
"Too many tagged fields to encode ({} fields)",
num_tagged_fields
);
}
types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
Ok(())
}
fn compute_size(&self, version: i16) -> Result<usize> {
let mut total_size = 0;
total_size += types::CompactString.compute_size(&self.name)?;
total_size += types::Int16.compute_size(&self.min_supported_version)?;
total_size += types::Int16.compute_size(&self.max_supported_version)?;
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
bail!(
"Too many tagged fields to encode ({} fields)",
num_tagged_fields
);
}
total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
Ok(total_size)
}
}
#[cfg(feature = "broker")]
impl Decodable for Feature {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
let name = types::CompactString.decode(buf)?;
let min_supported_version = types::Int16.decode(buf)?;
let max_supported_version = types::Int16.decode(buf)?;
let mut unknown_tagged_fields = BTreeMap::new();
let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
for _ in 0..num_tagged_fields {
let tag: u32 = types::UnsignedVarInt.decode(buf)?;
let size: u32 = types::UnsignedVarInt.decode(buf)?;
let unknown_value = buf.try_get_bytes(size as usize)?;
unknown_tagged_fields.insert(tag as i32, unknown_value);
}
Ok(Self {
name,
min_supported_version,
max_supported_version,
unknown_tagged_fields,
})
}
}
impl Default for Feature {
fn default() -> Self {
Self {
name: Default::default(),
min_supported_version: 0,
max_supported_version: 0,
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for Feature {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq)]
pub struct Listener {
pub name: StrBytes,
pub host: StrBytes,
pub port: u16,
pub security_protocol: i16,
pub unknown_tagged_fields: BTreeMap<i32, Bytes>,
}
impl Listener {
pub fn with_name(mut self, value: StrBytes) -> Self {
self.name = value;
self
}
pub fn with_host(mut self, value: StrBytes) -> Self {
self.host = value;
self
}
pub fn with_port(mut self, value: u16) -> Self {
self.port = value;
self
}
pub fn with_security_protocol(mut self, value: i16) -> Self {
self.security_protocol = value;
self
}
pub fn with_unknown_tagged_fields(mut self, value: BTreeMap<i32, Bytes>) -> Self {
self.unknown_tagged_fields = value;
self
}
pub fn with_unknown_tagged_field(mut self, key: i32, value: Bytes) -> Self {
self.unknown_tagged_fields.insert(key, value);
self
}
}
#[cfg(feature = "client")]
impl Encodable for Listener {
fn encode<B: ByteBufMut>(&self, buf: &mut B, version: i16) -> Result<()> {
types::CompactString.encode(buf, &self.name)?;
types::CompactString.encode(buf, &self.host)?;
types::UInt16.encode(buf, &self.port)?;
types::Int16.encode(buf, &self.security_protocol)?;
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
bail!(
"Too many tagged fields to encode ({} fields)",
num_tagged_fields
);
}
types::UnsignedVarInt.encode(buf, num_tagged_fields as u32)?;
write_unknown_tagged_fields(buf, 0.., &self.unknown_tagged_fields)?;
Ok(())
}
fn compute_size(&self, version: i16) -> Result<usize> {
let mut total_size = 0;
total_size += types::CompactString.compute_size(&self.name)?;
total_size += types::CompactString.compute_size(&self.host)?;
total_size += types::UInt16.compute_size(&self.port)?;
total_size += types::Int16.compute_size(&self.security_protocol)?;
let num_tagged_fields = self.unknown_tagged_fields.len();
if num_tagged_fields > std::u32::MAX as usize {
bail!(
"Too many tagged fields to encode ({} fields)",
num_tagged_fields
);
}
total_size += types::UnsignedVarInt.compute_size(num_tagged_fields as u32)?;
total_size += compute_unknown_tagged_fields_size(&self.unknown_tagged_fields)?;
Ok(total_size)
}
}
#[cfg(feature = "broker")]
impl Decodable for Listener {
fn decode<B: ByteBuf>(buf: &mut B, version: i16) -> Result<Self> {
let name = types::CompactString.decode(buf)?;
let host = types::CompactString.decode(buf)?;
let port = types::UInt16.decode(buf)?;
let security_protocol = types::Int16.decode(buf)?;
let mut unknown_tagged_fields = BTreeMap::new();
let num_tagged_fields = types::UnsignedVarInt.decode(buf)?;
for _ in 0..num_tagged_fields {
let tag: u32 = types::UnsignedVarInt.decode(buf)?;
let size: u32 = types::UnsignedVarInt.decode(buf)?;
let unknown_value = buf.try_get_bytes(size as usize)?;
unknown_tagged_fields.insert(tag as i32, unknown_value);
}
Ok(Self {
name,
host,
port,
security_protocol,
unknown_tagged_fields,
})
}
}
impl Default for Listener {
fn default() -> Self {
Self {
name: Default::default(),
host: Default::default(),
port: 0,
security_protocol: 0,
unknown_tagged_fields: BTreeMap::new(),
}
}
}
impl Message for Listener {
const VERSIONS: VersionRange = VersionRange { min: 0, max: 3 };
const DEPRECATED_VERSIONS: Option<VersionRange> = None;
}
impl HeaderVersion for BrokerRegistrationRequest {
fn header_version(version: i16) -> i16 {
2
}
}