1use std::{
17 collections::{BTreeMap, BTreeSet},
18 fmt,
19};
20
21use krabka_protocol::owned::{
22 describe_configs_request::{DescribeConfigsRequest, DescribeConfigsResource},
23 describe_configs_response::{
24 DescribeConfigsResourceResult, DescribeConfigsResponse, DescribeConfigsResult,
25 },
26 incremental_alter_configs_request::{
27 AlterConfigsResource, AlterableConfig, IncrementalAlterConfigsRequest,
28 },
29 incremental_alter_configs_response::IncrementalAlterConfigsResponse,
30};
31
32use crate::{
33 AdminClient, AdminError, KafkaError, NOT_CONTROLLER,
34 config_resources::{ConfigResource, ConfigResourceType},
35 groups::list_groups_kafka_error,
36 kafka_error_name,
37 log_dirs::{NodeTarget, call_node},
38 retry::{
39 ControllerRetry, CoordinatorRetry, REQUEST_TIMED_OUT, call_timeout_error,
40 is_connection_failure,
41 },
42};
43
44const UNKNOWN_SERVER_ERROR: i16 = -1;
47const NOT_LEADER_OR_FOLLOWER: i16 = 6;
49const DESCRIBE_CONFIGS_DOCUMENTATION_VERSION: i16 = 3;
51
52impl ConfigResource {
53 #[must_use]
55 pub fn new(resource_type: ConfigResourceType, name: impl Into<String>) -> Self {
56 Self {
57 resource_type,
58 name: name.into(),
59 }
60 }
61
62 #[must_use]
64 pub fn topic(name: impl Into<String>) -> Self {
65 Self::new(ConfigResourceType::Topic, name)
66 }
67
68 #[must_use]
70 pub fn broker(broker_id: i32) -> Self {
71 Self::new(ConfigResourceType::Broker, broker_id.to_string())
72 }
73
74 #[must_use]
77 pub fn broker_default() -> Self {
78 Self::new(ConfigResourceType::Broker, "")
79 }
80
81 #[must_use]
83 pub fn broker_logger(broker_id: i32) -> Self {
84 Self::new(ConfigResourceType::BrokerLogger, broker_id.to_string())
85 }
86
87 #[must_use]
89 pub fn client_metrics(name: impl Into<String>) -> Self {
90 Self::new(ConfigResourceType::ClientMetrics, name)
91 }
92
93 #[must_use]
95 pub fn group(name: impl Into<String>) -> Self {
96 Self::new(ConfigResourceType::Group, name)
97 }
98
99 #[must_use]
102 pub fn is_default(&self) -> bool {
103 self.name.is_empty()
104 }
105
106 #[must_use]
114 pub fn dynamic_config_source(&self) -> Option<ConfigSource> {
115 match self.resource_type {
116 ConfigResourceType::Topic => Some(ConfigSource::DynamicTopicConfig),
117 ConfigResourceType::Broker if self.is_default() => {
118 Some(ConfigSource::DynamicDefaultBrokerConfig)
119 }
120 ConfigResourceType::Broker => Some(ConfigSource::DynamicBrokerConfig),
121 ConfigResourceType::BrokerLogger => Some(ConfigSource::DynamicBrokerLoggerConfig),
122 ConfigResourceType::ClientMetrics => Some(ConfigSource::DynamicClientMetricsConfig),
123 ConfigResourceType::Group => Some(ConfigSource::DynamicGroupConfig),
124 ConfigResourceType::Unknown => None,
125 }
126 }
127}
128
129#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
132pub enum ConfigSource {
133 DynamicTopicConfig,
135 DynamicBrokerLoggerConfig,
137 DynamicBrokerConfig,
139 DynamicDefaultBrokerConfig,
141 DynamicClientMetricsConfig,
143 DynamicGroupConfig,
145 StaticBrokerConfig,
147 DefaultConfig,
149 Unknown,
151}
152
153impl ConfigSource {
154 #[must_use]
156 pub const fn id(self) -> i8 {
157 match self {
158 Self::Unknown => 0,
159 Self::DynamicTopicConfig => 1,
160 Self::DynamicBrokerConfig => 2,
161 Self::DynamicDefaultBrokerConfig => 3,
162 Self::StaticBrokerConfig => 4,
163 Self::DefaultConfig => 5,
164 Self::DynamicBrokerLoggerConfig => 6,
165 Self::DynamicClientMetricsConfig => 7,
166 Self::DynamicGroupConfig => 8,
167 }
168 }
169
170 #[must_use]
174 pub const fn from_id(id: i8) -> Option<Self> {
175 Some(match id {
176 i8::MIN..=-1 => return None,
177 1 => Self::DynamicTopicConfig,
178 2 => Self::DynamicBrokerConfig,
179 3 => Self::DynamicDefaultBrokerConfig,
180 4 => Self::StaticBrokerConfig,
181 5 => Self::DefaultConfig,
182 6 => Self::DynamicBrokerLoggerConfig,
183 7 => Self::DynamicClientMetricsConfig,
184 8 => Self::DynamicGroupConfig,
185 _ => Self::Unknown,
186 })
187 }
188}
189
190#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
192pub enum ConfigType {
193 Unknown,
196 Boolean,
198 String,
200 Int,
202 Short,
204 Long,
206 Double,
208 List,
210 Class,
212 Password,
214}
215
216impl ConfigType {
217 #[must_use]
219 pub const fn id(self) -> i8 {
220 match self {
221 Self::Unknown => 0,
222 Self::Boolean => 1,
223 Self::String => 2,
224 Self::Int => 3,
225 Self::Short => 4,
226 Self::Long => 5,
227 Self::Double => 6,
228 Self::List => 7,
229 Self::Class => 8,
230 Self::Password => 9,
231 }
232 }
233
234 #[must_use]
238 pub const fn from_id(id: i8) -> Option<Self> {
239 Some(match id {
240 i8::MIN..=-1 => return None,
241 1 => Self::Boolean,
242 2 => Self::String,
243 3 => Self::Int,
244 4 => Self::Short,
245 5 => Self::Long,
246 6 => Self::Double,
247 7 => Self::List,
248 8 => Self::Class,
249 9 => Self::Password,
250 _ => Self::Unknown,
251 })
252 }
253}
254
255#[derive(Debug, Clone, PartialEq, Eq)]
258pub struct ConfigSynonym {
259 pub name: String,
262 pub value: Option<String>,
265 pub source: ConfigSource,
267}
268
269#[derive(Clone, PartialEq, Eq)]
274pub struct ConfigEntry {
275 pub name: String,
277 pub value: Option<String>,
280 pub source: ConfigSource,
282 pub is_sensitive: bool,
284 pub is_read_only: bool,
286 pub synonyms: Vec<ConfigSynonym>,
289 pub config_type: ConfigType,
291 pub documentation: Option<String>,
294}
295
296impl ConfigEntry {
297 #[must_use]
300 pub fn is_default(&self) -> bool {
301 self.source == ConfigSource::DefaultConfig
302 }
303}
304
305impl fmt::Debug for ConfigEntry {
306 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
307 let value: &dyn fmt::Debug = if self.is_sensitive {
308 &"Redacted"
309 } else {
310 &self.value
311 };
312 f.debug_struct("ConfigEntry")
313 .field("name", &self.name)
314 .field("value", value)
315 .field("source", &self.source)
316 .field("is_sensitive", &self.is_sensitive)
317 .field("is_read_only", &self.is_read_only)
318 .field("synonyms", &self.synonyms)
319 .field("config_type", &self.config_type)
320 .field("documentation", &self.documentation)
321 .finish()
322 }
323}
324
325#[derive(Debug, Clone, Default, PartialEq, Eq)]
327pub struct Config {
328 pub entries: BTreeMap<String, ConfigEntry>,
331}
332
333impl Config {
334 #[must_use]
336 pub fn get(&self, name: &str) -> Option<&ConfigEntry> {
337 self.entries.get(name)
338 }
339
340 #[must_use]
346 pub fn dynamic_overrides(&self, resource: &ConfigResource) -> BTreeMap<String, String> {
347 let Some(source) = resource.dynamic_config_source() else {
348 return BTreeMap::new();
349 };
350 self.entries
351 .values()
352 .filter(|entry| entry.source == source)
353 .filter_map(|entry| Some((entry.name.clone(), entry.value.clone()?)))
354 .collect()
355 }
356}
357
358#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, bon::Builder)]
361pub struct DescribeConfigsOptions {
362 #[builder(default)]
365 pub include_synonyms: bool,
366 #[builder(default)]
368 pub include_documentation: bool,
369}
370
371#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
373pub enum AlterConfigOpType {
374 Set,
376 Delete,
378 Append,
380 Subtract,
382}
383
384impl AlterConfigOpType {
385 #[must_use]
387 pub const fn id(self) -> i8 {
388 match self {
389 Self::Set => 0,
390 Self::Delete => 1,
391 Self::Append => 2,
392 Self::Subtract => 3,
393 }
394 }
395}
396
397#[derive(Debug, Clone, PartialEq, Eq)]
399pub struct AlterConfigOp {
400 pub name: String,
402 pub value: Option<String>,
404 pub op_type: AlterConfigOpType,
406}
407
408impl AlterConfigOp {
409 #[must_use]
411 pub fn set(name: impl Into<String>, value: impl Into<String>) -> Self {
412 Self {
413 name: name.into(),
414 value: Some(value.into()),
415 op_type: AlterConfigOpType::Set,
416 }
417 }
418
419 #[must_use]
421 pub fn delete(name: impl Into<String>) -> Self {
422 Self {
423 name: name.into(),
424 value: None,
425 op_type: AlterConfigOpType::Delete,
426 }
427 }
428
429 #[must_use]
431 pub fn append(name: impl Into<String>, values: impl Into<String>) -> Self {
432 Self {
433 name: name.into(),
434 value: Some(values.into()),
435 op_type: AlterConfigOpType::Append,
436 }
437 }
438
439 #[must_use]
441 pub fn subtract(name: impl Into<String>, values: impl Into<String>) -> Self {
442 Self {
443 name: name.into(),
444 value: Some(values.into()),
445 op_type: AlterConfigOpType::Subtract,
446 }
447 }
448}
449
450#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, bon::Builder)]
453pub struct IncrementalAlterConfigsOptions {
454 #[builder(default)]
456 pub validate_only: bool,
457}
458
459pub type DescribeConfigsResults = BTreeMap<ConfigResource, Result<Config, KafkaError>>;
461
462pub type AlterConfigsResults = BTreeMap<ConfigResource, Result<(), KafkaError>>;
465
466fn node_for(resource: &ConfigResource) -> Result<Option<i32>, AdminError> {
470 let by_id = match resource.resource_type {
471 ConfigResourceType::Broker => !resource.is_default(),
472 ConfigResourceType::BrokerLogger => true,
473 _ => false,
474 };
475 if !by_id {
476 return Ok(None);
477 }
478 resource.name.parse().map(Some).map_err(|_| {
479 AdminError::InvalidArgument(format!(
480 "the name of a {:?} config resource must be a broker id, found {:?}",
481 resource.resource_type, resource.name
482 ))
483 })
484}
485
486fn call_error(api: &str, error: &AdminError) -> KafkaError {
490 if is_connection_failure(error) {
491 return KafkaError {
492 code: REQUEST_TIMED_OUT,
493 name: kafka_error_name(REQUEST_TIMED_OUT),
494 message: Some(format!("{api} timed out: {error}")),
495 };
496 }
497 list_groups_kafka_error(error)
498}
499
500fn unknown_server_error(message: String) -> KafkaError {
503 KafkaError {
504 code: UNKNOWN_SERVER_ERROR,
505 name: kafka_error_name(UNKNOWN_SERVER_ERROR),
506 message: Some(message),
507 }
508}
509
510fn describe_request(
511 resources: &BTreeSet<ConfigResource>,
512 options: DescribeConfigsOptions,
513) -> DescribeConfigsRequest {
514 DescribeConfigsRequest {
515 resources: resources
516 .iter()
517 .map(|resource| DescribeConfigsResource {
518 resource_type: resource.resource_type.id(),
519 resource_name: resource.name.clone(),
520 configuration_keys: None,
521 ..Default::default()
522 })
523 .collect(),
524 include_synonyms: options.include_synonyms,
525 include_documentation: options.include_documentation,
526 ..Default::default()
527 }
528}
529
530fn describe_min_version(options: DescribeConfigsOptions) -> Option<i16> {
534 options
535 .include_documentation
536 .then_some(DESCRIBE_CONFIGS_DOCUMENTATION_VERSION)
537}
538
539fn source_of(id: i8) -> Result<ConfigSource, String> {
541 ConfigSource::from_id(id).ok_or_else(|| format!("id should be positive, id: {id}"))
542}
543
544fn config_of(configs: Vec<DescribeConfigsResourceResult>) -> Result<Config, String> {
548 let mut entries = BTreeMap::new();
549 for config in configs {
550 let synonyms = config
551 .synonyms
552 .into_iter()
553 .map(|synonym| {
554 Ok(ConfigSynonym {
555 name: synonym.name,
556 value: synonym.value,
557 source: source_of(synonym.source)?,
558 })
559 })
560 .collect::<Result<_, String>>()?;
561 let entry = ConfigEntry {
562 source: source_of(config.config_source)?,
563 config_type: ConfigType::from_id(config.config_type)
564 .ok_or_else(|| format!("id should be positive, id: {}", config.config_type))?,
565 name: config.name,
566 value: config.value,
567 is_sensitive: config.is_sensitive,
568 is_read_only: config.read_only,
569 synonyms,
570 documentation: config.documentation,
571 };
572 entries.insert(entry.name.clone(), entry);
573 }
574 Ok(Config { entries })
575}
576
577fn describe_results(
588 asked: &BTreeSet<ConfigResource>,
589 answer: Result<DescribeConfigsResponse, KafkaError>,
590) -> DescribeConfigsResults {
591 let fail_all = |error: KafkaError| {
592 asked
593 .iter()
594 .map(|resource| (resource.clone(), Err(error.clone())))
595 .collect()
596 };
597 let response = match answer {
598 Ok(response) => response,
599 Err(error) => return fail_all(error),
600 };
601 let mut answered = BTreeMap::<ConfigResource, DescribeConfigsResult>::new();
602 for result in response.results {
603 let resource = ConfigResource::new(
604 ConfigResourceType::from_id(result.resource_type),
605 result.resource_name.clone(),
606 );
607 if answered.insert(resource.clone(), result).is_some() {
608 return fail_all(unknown_server_error(format!(
609 "Duplicate key for config resource {resource:?}"
610 )));
611 }
612 }
613 let mut out = BTreeMap::new();
614 for resource in asked {
615 let result = match answered.remove(resource) {
616 None => Err(unknown_server_error(format!(
617 "The node response did not contain a result for config resource {resource:?}"
618 ))),
619 Some(result) if result.error_code != 0 => Err(KafkaError {
620 code: result.error_code,
621 name: kafka_error_name(result.error_code),
622 message: result.error_message,
623 }),
624 Some(result) => match config_of(result.configs) {
625 Ok(config) => Ok(config),
626 Err(message) => return fail_all(unknown_server_error(message)),
627 },
628 };
629 out.insert(resource.clone(), result);
630 }
631 for resource in answered.into_keys() {
632 tracing::warn!(
633 ?resource,
634 "the DescribeConfigs response names a resource that was not asked for"
635 );
636 }
637 out
638}
639
640fn alter_request(
641 resources: &BTreeMap<ConfigResource, &[AlterConfigOp]>,
642 options: IncrementalAlterConfigsOptions,
643) -> IncrementalAlterConfigsRequest {
644 IncrementalAlterConfigsRequest {
645 resources: resources
646 .iter()
647 .map(|(resource, ops)| AlterConfigsResource {
648 resource_type: resource.resource_type.id(),
649 resource_name: resource.name.clone(),
650 configs: ops
651 .iter()
652 .map(|op| AlterableConfig {
653 name: op.name.clone(),
654 config_operation: op.op_type.id(),
655 value: op.value.clone(),
656 ..Default::default()
657 })
658 .collect(),
659 ..Default::default()
660 })
661 .collect(),
662 validate_only: options.validate_only,
663 ..Default::default()
664 }
665}
666
667fn names_not_controller(response: &IncrementalAlterConfigsResponse, controllers: bool) -> bool {
672 response.responses.iter().any(|result| {
673 result.error_code == NOT_CONTROLLER
674 || (controllers && result.error_code == NOT_LEADER_OR_FOLLOWER)
675 })
676}
677
678fn alter_results<'a>(
683 asked: impl Iterator<Item = &'a ConfigResource>,
684 answer: Result<IncrementalAlterConfigsResponse, KafkaError>,
685) -> AlterConfigsResults {
686 let response = match answer {
687 Ok(response) => response,
688 Err(error) => {
689 return asked
690 .map(|resource| (resource.clone(), Err(error.clone())))
691 .collect();
692 }
693 };
694 let answered = response
695 .responses
696 .into_iter()
697 .map(|result| {
698 (
699 ConfigResource::new(
700 ConfigResourceType::from_id(result.resource_type),
701 result.resource_name,
702 ),
703 (result.error_code, result.error_message),
704 )
705 })
706 .collect::<BTreeMap<_, _>>();
707 asked
708 .map(|resource| {
709 let result = match answered.get(resource) {
710 None => Err(unknown_server_error(format!(
711 "The node response did not contain a result for config resource {resource:?}"
712 ))),
713 Some((0, _)) => Ok(()),
714 Some((code, message)) => Err(KafkaError {
715 code: *code,
716 name: kafka_error_name(*code),
717 message: message.clone(),
718 }),
719 };
720 (resource.clone(), result)
721 })
722 .collect()
723}
724
725impl AdminClient {
726 pub async fn describe_configs(
751 &self,
752 resources: &[ConfigResource],
753 options: DescribeConfigsOptions,
754 ) -> Result<DescribeConfigsResults, AdminError> {
755 let mut by_node = BTreeMap::<Option<i32>, BTreeSet<ConfigResource>>::new();
756 for resource in resources {
757 by_node
758 .entry(node_for(resource)?)
759 .or_default()
760 .insert(resource.clone());
761 }
762 let start = self.retry.start();
763 let min_version = describe_min_version(options);
764 let answers = futures_util::future::join_all(by_node.iter().map(|(node, asked)| {
765 let request = describe_request(asked, options);
766 async move {
767 let answer = if let Some(node_id) = *node {
768 call_node(
769 &self.conn,
770 NodeTarget {
771 node_id,
772 supports_controllers: true,
773 min_version,
774 },
775 &self.options,
776 request,
777 CoordinatorRetry::from_deadline(start),
778 )
779 .await
780 } else {
781 let retry = ControllerRetry::new("DescribeConfigs", self.retry);
782 let sent = match min_version {
783 Some(min_version) => {
784 retry
785 .bounded(self.conn.send_at_least(request, min_version))
786 .await
787 }
788 None => retry.bounded(self.conn.send(request)).await,
789 };
790 sent.map_err(|error| call_error("DescribeConfigs", &error))
791 };
792 describe_results(asked, answer)
793 }
794 }))
795 .await;
796 Ok(answers.into_iter().flatten().collect())
797 }
798
799 pub async fn incremental_alter_configs(
823 &mut self,
824 configs: &BTreeMap<ConfigResource, Vec<AlterConfigOp>>,
825 options: IncrementalAlterConfigsOptions,
826 ) -> Result<AlterConfigsResults, AdminError> {
827 let controllers = self.conn.uses_controller_bootstrap();
828 let mut unified = BTreeMap::new();
829 let mut by_node = Vec::new();
830 for (resource, ops) in configs {
831 let node = node_for(resource)?.filter(|_| {
832 !controllers || resource.resource_type == ConfigResourceType::BrokerLogger
833 });
834 match node {
835 Some(node_id) => by_node.push((node_id, resource, ops.as_slice())),
836 None => {
837 unified.insert(resource.clone(), ops.as_slice());
838 }
839 }
840 }
841 let start = self.retry.start();
842 let (conn, client_options) = (&self.conn, &self.options);
843 let node_answers =
844 futures_util::future::join_all(by_node.iter().map(|(node_id, resource, ops)| {
845 let request = alter_request(&maplit::btreemap! {
846 (*resource).clone() => *ops,
847 }, options);
848 let target = NodeTarget {
849 node_id: *node_id,
850 supports_controllers: true,
851 min_version: None,
852 };
853 async move {
854 let mut deadline = start;
855 let mut attempts = 1;
856 let answer = loop {
857 let answer = call_node(
858 conn,
859 target,
860 client_options,
861 request.clone(),
862 CoordinatorRetry::from_deadline(deadline),
863 )
864 .await;
865 if !matches!(&answer, Ok(response) if names_not_controller(response, controllers))
866 {
867 break answer;
868 }
869 let timed_out = || {
870 Err(call_timeout_error(
871 "IncrementalAlterConfigs",
872 attempts,
873 "NOT_CONTROLLER",
874 ))
875 };
876 if deadline.exhausted() {
877 break timed_out();
878 }
879 deadline.backoff().await;
880 if deadline.expired() {
881 break timed_out();
882 }
883 attempts += 1;
884 };
885 alter_results(std::iter::once(*resource), answer)
886 }
887 }))
888 .await;
889 let mut out: AlterConfigsResults = node_answers.into_iter().flatten().collect();
890 if !unified.is_empty() {
891 let answer = self.alter_unified(&unified, options, controllers).await;
892 out.extend(alter_results(unified.keys(), answer));
893 }
894 Ok(out)
895 }
896
897 async fn alter_unified(
901 &mut self,
902 resources: &BTreeMap<ConfigResource, &[AlterConfigOp]>,
903 options: IncrementalAlterConfigsOptions,
904 controllers: bool,
905 ) -> Result<IncrementalAlterConfigsResponse, KafkaError> {
906 const API: &str = "IncrementalAlterConfigs";
907 let request = alter_request(resources, options);
908 let mut retry = ControllerRetry::new(API, self.retry);
909 loop {
910 let response = retry
911 .bounded(self.conn.send(request.clone()))
912 .await
913 .map_err(|error| call_error(API, &error))?;
914 if !names_not_controller(&response, controllers) {
915 return Ok(response);
916 }
917 retry
918 .after_not_controller(self)
919 .await
920 .map_err(|error| call_error(API, &error))?;
921 }
922 }
923}
924
925#[cfg(test)]
926mod tests;