1use std::collections::{HashMap, HashSet};
49use std::hash::{Hash, Hasher};
50use std::sync::LazyLock;
51use std::sync::atomic::{self, AtomicI32};
52
53use log::warn;
54
55use crate::common::Error;
56use crate::common::config::abstract_config::{AbstractConfig, ConfigMap, ValuesReader, string_originals};
57use crate::common::config::config_def::{
58 CaseInsensitiveValidString, ConfigDef, DefaultValue, Importance, LambdaValidator, NonEmptyString, ParsedValue,
59 Range, Type, ValidList, ValidString, as_string, config_error,
60};
61use crate::common::config::{SaslConfigs, SslConfigs};
62use crate::common::errors::InvalidConfigurationError;
63use crate::common::requests::JoinGroupRequest;
64use crate::common::security::auth::SecurityProtocol;
65use crate::common::utils::Utils;
66use crate::consumer::internals::{AutoOffsetResetStrategy, auto_offset_reset_strategy};
67use crate::{ClientDnsLookup, CommonClientConfigs};
68
69static CONSUMER_CLIENT_ID_SEQUENCE: AtomicI32 = AtomicI32::new(1);
76
77const CLASSIC_PROTOCOL_UNSUPPORTED_CONFIGS: [&str; 3] = [
80 ConsumerConfig::GROUP_REMOTE_ASSIGNOR_CONFIG,
81 ConsumerConfig::SHARE_ACKNOWLEDGEMENT_MODE_CONFIG,
82 ConsumerConfig::SHARE_ACQUIRE_MODE_CONFIG,
83];
84
85const CONSUMER_PROTOCOL_UNSUPPORTED_CONFIGS: [&str; 5] = [
88 ConsumerConfig::PARTITION_ASSIGNMENT_STRATEGY_CONFIG,
89 ConsumerConfig::HEARTBEAT_INTERVAL_MS_CONFIG,
90 ConsumerConfig::SESSION_TIMEOUT_MS_CONFIG,
91 ConsumerConfig::SHARE_ACKNOWLEDGEMENT_MODE_CONFIG,
92 ConsumerConfig::SHARE_ACQUIRE_MODE_CONFIG,
93];
94
95static CONFIG: LazyLock<ConfigDef> = LazyLock::new(|| {
97 ConfigDef::new()
98 .define_with_validator(
99 ConsumerConfig::BOOTSTRAP_SERVERS_CONFIG,
100 Type::List,
101 ConfigDef::NO_DEFAULT_VALUE,
102 ValidList::any_non_duplicate_values(false, false),
103 Importance::High,
104 )
105 .define_with_validator(
106 ConsumerConfig::CLIENT_DNS_LOOKUP_CONFIG,
107 Type::String,
108 ClientDnsLookup::UseAllDnsIps.config_value(),
109 ValidString::r#in(&[
110 ClientDnsLookup::UseAllDnsIps.config_value(),
111 ClientDnsLookup::ResolveCanonicalBootstrapServersOnly.config_value(),
112 ]),
113 Importance::Medium,
114 )
115 .define_with_validator(
116 ConsumerConfig::BOOTSTRAP_RESOLVE_TIMEOUT_MS_CONFIG,
117 Type::Long,
118 CommonClientConfigs::DEFAULT_BOOTSTRAP_RESOLVE_TIMEOUT_MS,
119 Range::at_least(0_i64),
120 Importance::High,
121 )
122 .define(
123 ConsumerConfig::GROUP_ID_CONFIG,
124 Type::String,
125 DefaultValue::Null,
126 Importance::High,
127 )
128 .define_with_validator(
129 ConsumerConfig::GROUP_INSTANCE_ID_CONFIG,
130 Type::String,
131 DefaultValue::Null,
132 NonEmptyString,
133 Importance::Medium,
134 )
135 .define(ConsumerConfig::SESSION_TIMEOUT_MS_CONFIG, Type::Int, 45000, Importance::High)
136 .define(ConsumerConfig::HEARTBEAT_INTERVAL_MS_CONFIG, Type::Int, 3000, Importance::High)
137 .define_with_validator(
140 ConsumerConfig::METADATA_MAX_AGE_CONFIG,
141 Type::Long,
142 5 * 60 * 1000_i64,
143 Range::at_least(0),
144 Importance::Low,
145 )
146 .define(
147 ConsumerConfig::ENABLE_AUTO_COMMIT_CONFIG,
148 Type::Boolean,
149 true,
150 Importance::Medium,
151 )
152 .define_with_validator(
153 ConsumerConfig::AUTO_COMMIT_INTERVAL_MS_CONFIG,
154 Type::Int,
155 5000,
156 Range::at_least(0),
157 Importance::Low,
158 )
159 .define(ConsumerConfig::CLIENT_ID_CONFIG, Type::String, "", Importance::Low)
160 .define(
161 ConsumerConfig::CLIENT_RACK_CONFIG,
162 Type::String,
163 ConsumerConfig::DEFAULT_CLIENT_RACK,
164 Importance::Low,
165 )
166 .define_with_validator(
167 ConsumerConfig::MAX_PARTITION_FETCH_BYTES_CONFIG,
168 Type::Int,
169 ConsumerConfig::DEFAULT_MAX_PARTITION_FETCH_BYTES,
170 Range::at_least(0),
171 Importance::High,
172 )
173 .define_with_validator(
174 ConsumerConfig::SEND_BUFFER_CONFIG,
175 Type::Int,
176 128 * 1024,
177 Range::at_least(CommonClientConfigs::SEND_BUFFER_LOWER_BOUND),
178 Importance::Medium,
179 )
180 .define_with_validator(
181 ConsumerConfig::RECEIVE_BUFFER_CONFIG,
182 Type::Int,
183 64 * 1024,
184 Range::at_least(CommonClientConfigs::RECEIVE_BUFFER_LOWER_BOUND),
185 Importance::Medium,
186 )
187 .define_with_validator(
188 ConsumerConfig::FETCH_MIN_BYTES_CONFIG,
189 Type::Int,
190 ConsumerConfig::DEFAULT_FETCH_MIN_BYTES,
191 Range::at_least(0),
192 Importance::High,
193 )
194 .define_with_validator(
195 ConsumerConfig::FETCH_MAX_BYTES_CONFIG,
196 Type::Int,
197 ConsumerConfig::DEFAULT_FETCH_MAX_BYTES,
198 Range::at_least(0),
199 Importance::Medium,
200 )
201 .define_with_validator(
202 ConsumerConfig::FETCH_MAX_WAIT_MS_CONFIG,
203 Type::Int,
204 ConsumerConfig::DEFAULT_FETCH_MAX_WAIT_MS,
205 Range::at_least(0),
206 Importance::Low,
207 )
208 .define_with_validator(
209 ConsumerConfig::RECONNECT_BACKOFF_MS_CONFIG,
210 Type::Long,
211 50_i64,
212 Range::at_least(0_i64),
213 Importance::Low,
214 )
215 .define_with_validator(
216 ConsumerConfig::RECONNECT_BACKOFF_MAX_MS_CONFIG,
217 Type::Long,
218 1000_i64,
219 Range::at_least(0_i64),
220 Importance::Low,
221 )
222 .define_with_validator(
223 ConsumerConfig::RETRY_BACKOFF_MS_CONFIG,
224 Type::Long,
225 CommonClientConfigs::DEFAULT_RETRY_BACKOFF_MS,
226 Range::at_least(0_i64),
227 Importance::Low,
228 )
229 .define_with_validator(
230 ConsumerConfig::RETRY_BACKOFF_MAX_MS_CONFIG,
231 Type::Long,
232 CommonClientConfigs::DEFAULT_RETRY_BACKOFF_MAX_MS,
233 Range::at_least(0_i64),
234 Importance::Low,
235 )
236 .define_with_validator(
239 ConsumerConfig::AUTO_OFFSET_RESET_CONFIG,
240 Type::String,
241 AutoOffsetResetStrategy::LATEST.name(),
242 auto_offset_reset_strategy::Validator,
243 Importance::Medium,
244 )
245 .define(ConsumerConfig::CHECK_CRCS_CONFIG, Type::Boolean, true, Importance::Low)
246 .define_with_validator(
247 ConsumerConfig::METRICS_SAMPLE_WINDOW_MS_CONFIG,
248 Type::Long,
249 30000_i64,
250 Range::at_least(0),
251 Importance::Low,
252 )
253 .define_with_validator(
254 ConsumerConfig::METRICS_NUM_SAMPLES_CONFIG,
255 Type::Int,
256 2,
257 Range::at_least(1),
258 Importance::Low,
259 )
260 .define_with_validator(
261 ConsumerConfig::METRICS_RECORDING_LEVEL_CONFIG,
262 Type::String,
263 "INFO",
264 ValidString::r#in(&["INFO", "DEBUG", "TRACE"]),
265 Importance::Low,
266 )
267 .define_with_validator(
270 ConsumerConfig::REQUEST_TIMEOUT_MS_CONFIG,
271 Type::Int,
272 30000,
273 Range::at_least(0),
274 Importance::Medium,
275 )
276 .define_with_validator(
277 ConsumerConfig::DEFAULT_API_TIMEOUT_MS_CONFIG,
278 Type::Int,
279 60000,
280 Range::at_least(0),
281 Importance::Medium,
282 )
283 .define(
284 ConsumerConfig::SOCKET_CONNECTION_SETUP_TIMEOUT_MS_CONFIG,
285 Type::Long,
286 CommonClientConfigs::DEFAULT_SOCKET_CONNECTION_SETUP_TIMEOUT_MS,
287 Importance::Medium,
288 )
289 .define(
290 ConsumerConfig::SOCKET_CONNECTION_SETUP_TIMEOUT_MAX_MS_CONFIG,
291 Type::Long,
292 CommonClientConfigs::DEFAULT_SOCKET_CONNECTION_SETUP_TIMEOUT_MAX_MS,
293 Importance::Medium,
294 )
295 .define(
296 ConsumerConfig::CONNECTIONS_MAX_IDLE_MS_CONFIG,
297 Type::Long,
298 9 * 60 * 1000_i64,
299 Importance::Medium,
300 )
301 .define_with_validator(
303 ConsumerConfig::MAX_POLL_RECORDS_CONFIG,
304 Type::Int,
305 ConsumerConfig::DEFAULT_MAX_POLL_RECORDS,
306 Range::at_least(1),
307 Importance::Medium,
308 )
309 .define_with_validator(
310 ConsumerConfig::MAX_POLL_INTERVAL_MS_CONFIG,
311 Type::Int,
312 300000,
313 Range::at_least(1),
314 Importance::Medium,
315 )
316 .define(
317 ConsumerConfig::EXCLUDE_INTERNAL_TOPICS_CONFIG,
318 Type::Boolean,
319 ConsumerConfig::DEFAULT_EXCLUDE_INTERNAL_TOPICS,
320 Importance::Medium,
321 )
322 .define_internal(
323 ConsumerConfig::THROW_ON_FETCH_STABLE_OFFSET_UNSUPPORTED,
324 Type::Boolean,
325 false,
326 Importance::Low,
327 )
328 .define_with_validator(
329 ConsumerConfig::ISOLATION_LEVEL_CONFIG,
330 Type::String,
331 ConsumerConfig::DEFAULT_ISOLATION_LEVEL,
332 ValidString::r#in(&["read_committed", "read_uncommitted"]),
333 Importance::Medium,
334 )
335 .define(
336 ConsumerConfig::ALLOW_AUTO_CREATE_TOPICS_CONFIG,
337 Type::Boolean,
338 ConsumerConfig::DEFAULT_ALLOW_AUTO_CREATE_TOPICS,
339 Importance::Medium,
340 )
341 .define_with_validator(
342 ConsumerConfig::GROUP_PROTOCOL_CONFIG,
343 Type::String,
344 ConsumerConfig::DEFAULT_GROUP_PROTOCOL,
345 CaseInsensitiveValidString::r#in(&["CLASSIC", "CONSUMER"]),
346 Importance::High,
347 )
348 .define(
349 ConsumerConfig::GROUP_REMOTE_ASSIGNOR_CONFIG,
350 Type::String,
351 DefaultValue::Null,
352 Importance::Medium,
353 )
354 .define_with_validator(
356 CommonClientConfigs::SECURITY_PROTOCOL_CONFIG,
357 Type::String,
358 CommonClientConfigs::DEFAULT_SECURITY_PROTOCOL,
359 CaseInsensitiveValidString::r#in(&SecurityProtocol::names()),
360 Importance::Medium,
361 )
362 .with_client_ssl_support()
363 .with_client_sasl_support()
364 .define_with_validator(
365 CommonClientConfigs::METADATA_RECOVERY_STRATEGY_CONFIG,
366 Type::String,
367 CommonClientConfigs::DEFAULT_METADATA_RECOVERY_STRATEGY,
368 CaseInsensitiveValidString::r#in(&["NONE", "REBOOTSTRAP"]),
369 Importance::Low,
370 )
371 .define_with_validator(
372 CommonClientConfigs::METADATA_RECOVERY_REBOOTSTRAP_TRIGGER_MS_CONFIG,
373 Type::Long,
374 CommonClientConfigs::DEFAULT_METADATA_RECOVERY_REBOOTSTRAP_TRIGGER_MS,
375 Range::at_least(0),
376 Importance::Low,
377 )
378 .define(
379 CommonClientConfigs::METADATA_CLUSTER_CHECK_ENABLE_CONFIG,
380 Type::Boolean,
381 true,
382 Importance::Low,
383 )
384 .define_with_validator(
391 ConsumerConfig::SHARE_ACKNOWLEDGEMENT_MODE_CONFIG,
392 Type::String,
393 "implicit",
394 share_acknowledgement_mode_validator(),
395 Importance::Medium,
396 )
397 .define_with_validator(
400 ConsumerConfig::SHARE_ACQUIRE_MODE_CONFIG,
401 Type::String,
402 "BATCH_OPTIMIZED",
403 share_acquire_mode_validator(),
404 Importance::Medium,
405 )
406 });
408
409fn share_acknowledgement_mode_validator() -> LambdaValidator {
414 LambdaValidator::with(
415 |name, value| {
416 let acknowledgement_mode = as_string(value)?;
417 if acknowledgement_mode.is_some_and(|mode| ["implicit", "explicit"].contains(&mode)) {
418 return Ok(());
419 }
420 Err(config_error(
421 name,
422 value,
423 format!(
424 "Invalid value `{}` for configuration {name}. The value must either be 'implicit' or 'explicit'.",
425 acknowledgement_mode.unwrap_or("null")
426 ),
427 ))
428 },
429 || "[implicit, explicit]".to_owned(),
430 )
431}
432
433fn share_acquire_mode_validator() -> LambdaValidator {
437 LambdaValidator::with(
438 |name, value| {
439 let acquire_mode = as_string(value)?;
440 if acquire_mode
441 .is_some_and(|mode| ["BATCH_OPTIMIZED", "RECORD_LIMIT"].contains(&mode.to_uppercase().as_str()))
442 {
443 return Ok(());
444 }
445 Err(config_error(
446 name,
447 value,
448 format!(
449 "Invalid value `{}` for configuration {name}. The value must either be 'batch_optimized' or 'record_limit'.",
450 acquire_mode.unwrap_or("null")
451 ),
452 ))
453 },
454 || "[batch_optimized, record_limit]".to_owned(),
455 )
456}
457
458#[derive(Debug)]
479#[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfig")]
480pub struct ConsumerConfig {
481 pub(crate) config: AbstractConfig,
483 pub(crate) read_keys: Vec<&'static str>,
486
487 pub(crate) group_id: Option<String>,
490 pub(crate) group_instance_id: Option<String>,
492 pub(crate) group_protocol: String,
494 pub(crate) group_remote_assignor: Option<String>,
496
497 pub(crate) max_poll_records: i32,
501 pub(crate) max_poll_interval_ms: i32,
503
504 pub(crate) bootstrap_servers: Vec<String>,
507 pub(crate) client_dns_lookup: ClientDnsLookup,
509 pub(crate) client_id: String,
511 pub(crate) client_rack: String,
513
514 pub(crate) enable_auto_commit: bool,
517 pub(crate) auto_commit_interval_ms: i32,
519
520 pub(crate) auto_offset_reset: String,
523
524 pub(crate) fetch_min_bytes: i32,
527 pub(crate) fetch_max_bytes: i32,
529 pub(crate) fetch_max_wait_ms: i32,
531 pub(crate) max_partition_fetch_bytes: i32,
533 pub(crate) check_crcs: bool,
535
536 pub(crate) send_buffer_bytes: i32,
539 pub(crate) receive_buffer_bytes: i32,
541 pub(crate) socket_connection_setup_timeout_ms: i64,
543 pub(crate) socket_connection_setup_timeout_max_ms: i64,
545 pub(crate) connections_max_idle_ms: i64,
547
548 pub(crate) reconnect_backoff_ms: i64,
551 pub(crate) reconnect_backoff_max_ms: i64,
553 pub(crate) retry_backoff_ms: i64,
555 pub(crate) retry_backoff_max_ms: i64,
557 pub(crate) bootstrap_resolve_timeout_ms: i64,
559
560 pub(crate) request_timeout_ms: i32,
563 pub(crate) default_api_timeout_ms: i32,
565
566 pub(crate) metadata_max_age_ms: i64,
569 pub(crate) metadata_recovery_strategy: String,
571 pub(crate) metadata_recovery_rebootstrap_trigger_ms: i64,
573 pub(crate) metadata_cluster_check_enable: bool,
575
576 pub(crate) exclude_internal_topics: bool,
579 pub(crate) throw_on_fetch_stable_offset_unsupported: bool,
581 pub(crate) isolation_level: String,
583 pub(crate) allow_auto_create_topics: bool,
585
586 pub(crate) metrics_sample_window_ms: i64,
589 pub(crate) metrics_num_samples: i32,
591 pub(crate) metrics_recording_level: String,
593
594 pub(crate) security_protocol: SecurityProtocol,
598
599 pub(crate) sasl_config: SaslConfigs,
601
602 pub(crate) ssl_config: SslConfigs,
604}
605
606impl PartialEq for ConsumerConfig {
607 fn eq(&self, other: &Self) -> bool {
609 self.config == other.config
610 }
611}
612
613impl Eq for ConsumerConfig {}
614
615impl Hash for ConsumerConfig {
616 fn hash<H: Hasher>(&self, state: &mut H) {
618 self.config.hash(state);
619 }
620}
621
622impl ConsumerConfig {
623 #[cfg(test)]
635 pub(crate) fn defaults() -> Self {
636 let props = HashMap::from([(Self::BOOTSTRAP_SERVERS_CONFIG.to_string(), "localhost:9092".to_string())]);
637 let config =
638 AbstractConfig::new(&CONFIG, string_originals(&props), false).expect("the ConfigDef defaults are valid");
639 let mut defaults = Self::from_abstract(config).expect("the ConfigDef defaults are valid");
640 defaults.bootstrap_servers = Vec::new();
641 defaults
642 }
643
644 fn from_abstract(config: AbstractConfig) -> Result<Self, Error> {
647 let mut values = ValuesReader::new(&config);
648 let bootstrap_servers = values.list(Self::BOOTSTRAP_SERVERS_CONFIG)?.to_vec();
649 let client_dns_lookup = ClientDnsLookup::parse_config_value(values.string(Self::CLIENT_DNS_LOOKUP_CONFIG)?)?;
650 let client_id = values.string(Self::CLIENT_ID_CONFIG)?.to_string();
651 let client_rack = values.string(Self::CLIENT_RACK_CONFIG)?.to_string();
652 let group_id = values.string_or_null(Self::GROUP_ID_CONFIG)?.map(str::to_string);
659 let group_instance_id = values.string_or_null(Self::GROUP_INSTANCE_ID_CONFIG)?.map(str::to_string);
660 let group_protocol = values.string(Self::GROUP_PROTOCOL_CONFIG)?.to_string();
661 let group_remote_assignor = values
662 .string_or_null(Self::GROUP_REMOTE_ASSIGNOR_CONFIG)?
663 .filter(|assignor| !assignor.is_empty())
664 .map(str::to_string);
665 let max_poll_records = values.int(Self::MAX_POLL_RECORDS_CONFIG)?;
666 let max_poll_interval_ms = values.int(Self::MAX_POLL_INTERVAL_MS_CONFIG)?;
667 let enable_auto_commit = values.boolean(Self::ENABLE_AUTO_COMMIT_CONFIG)?;
668 let auto_commit_interval_ms = values.int(Self::AUTO_COMMIT_INTERVAL_MS_CONFIG)?;
669 let auto_offset_reset = values.string(Self::AUTO_OFFSET_RESET_CONFIG)?.to_string();
670 let fetch_min_bytes = values.int(Self::FETCH_MIN_BYTES_CONFIG)?;
671 let fetch_max_bytes = values.int(Self::FETCH_MAX_BYTES_CONFIG)?;
672 let fetch_max_wait_ms = values.int(Self::FETCH_MAX_WAIT_MS_CONFIG)?;
673 let max_partition_fetch_bytes = values.int(Self::MAX_PARTITION_FETCH_BYTES_CONFIG)?;
674 let check_crcs = values.boolean(Self::CHECK_CRCS_CONFIG)?;
675 let send_buffer_bytes = values.int(Self::SEND_BUFFER_CONFIG)?;
676 let receive_buffer_bytes = values.int(Self::RECEIVE_BUFFER_CONFIG)?;
677 let socket_connection_setup_timeout_ms = values.long(Self::SOCKET_CONNECTION_SETUP_TIMEOUT_MS_CONFIG)?;
678 let socket_connection_setup_timeout_max_ms =
679 values.long(Self::SOCKET_CONNECTION_SETUP_TIMEOUT_MAX_MS_CONFIG)?;
680 let connections_max_idle_ms = values.long(Self::CONNECTIONS_MAX_IDLE_MS_CONFIG)?;
681 let reconnect_backoff_ms = values.long(Self::RECONNECT_BACKOFF_MS_CONFIG)?;
682 let reconnect_backoff_max_ms = values.long(Self::RECONNECT_BACKOFF_MAX_MS_CONFIG)?;
683 let retry_backoff_ms = values.long(Self::RETRY_BACKOFF_MS_CONFIG)?;
684 let retry_backoff_max_ms = values.long(Self::RETRY_BACKOFF_MAX_MS_CONFIG)?;
685 let bootstrap_resolve_timeout_ms = values.long(Self::BOOTSTRAP_RESOLVE_TIMEOUT_MS_CONFIG)?;
686 let request_timeout_ms = values.int(Self::REQUEST_TIMEOUT_MS_CONFIG)?;
687 let default_api_timeout_ms = values.int(Self::DEFAULT_API_TIMEOUT_MS_CONFIG)?;
688 let metadata_max_age_ms = values.long(Self::METADATA_MAX_AGE_CONFIG)?;
689 let metadata_recovery_strategy = values
690 .string(CommonClientConfigs::METADATA_RECOVERY_STRATEGY_CONFIG)?
691 .to_string();
692 let metadata_recovery_rebootstrap_trigger_ms =
693 values.long(CommonClientConfigs::METADATA_RECOVERY_REBOOTSTRAP_TRIGGER_MS_CONFIG)?;
694 let metadata_cluster_check_enable =
695 values.boolean(CommonClientConfigs::METADATA_CLUSTER_CHECK_ENABLE_CONFIG)?;
696 let exclude_internal_topics = values.boolean(Self::EXCLUDE_INTERNAL_TOPICS_CONFIG)?;
697 let throw_on_fetch_stable_offset_unsupported =
698 values.boolean(Self::THROW_ON_FETCH_STABLE_OFFSET_UNSUPPORTED)?;
699 let isolation_level = values.string(Self::ISOLATION_LEVEL_CONFIG)?.to_string();
700 let allow_auto_create_topics = values.boolean(Self::ALLOW_AUTO_CREATE_TOPICS_CONFIG)?;
701 let metrics_sample_window_ms = values.long(Self::METRICS_SAMPLE_WINDOW_MS_CONFIG)?;
702 let metrics_num_samples = values.int(Self::METRICS_NUM_SAMPLES_CONFIG)?;
703 let metrics_recording_level = values.string(Self::METRICS_RECORDING_LEVEL_CONFIG)?.to_string();
704 let protocol = values.string(CommonClientConfigs::SECURITY_PROTOCOL_CONFIG)?;
705 let security_protocol = SecurityProtocol::for_name(protocol)
707 .ok_or_else(|| Error::local_illegal_state(format!("Unknown security protocol {protocol}")))?;
708
709 let mut ssl_values = ValuesReader::new(&config);
713 let ssl_config = SslConfigs::from_values(&mut ssl_values)?;
714 if matches!(security_protocol, SecurityProtocol::Ssl | SecurityProtocol::SaslSsl) {
715 values.append(ssl_values);
716 }
717 let mut sasl_values = ValuesReader::new(&config);
718 let sasl_config = SaslConfigs::from_values(&mut sasl_values)?;
719 if matches!(security_protocol, SecurityProtocol::SaslPlaintext | SecurityProtocol::SaslSsl) {
720 values.append(sasl_values);
721 }
722 let read_keys = values.into_read_keys();
723
724 Ok(Self {
725 config,
726 read_keys,
727 group_id,
728 group_instance_id,
729 group_protocol,
730 group_remote_assignor,
731 max_poll_records,
732 max_poll_interval_ms,
733 bootstrap_servers,
734 client_dns_lookup,
735 client_id,
736 client_rack,
737 enable_auto_commit,
738 auto_commit_interval_ms,
739 auto_offset_reset,
740 fetch_min_bytes,
741 fetch_max_bytes,
742 fetch_max_wait_ms,
743 max_partition_fetch_bytes,
744 check_crcs,
745 send_buffer_bytes,
746 receive_buffer_bytes,
747 socket_connection_setup_timeout_ms,
748 socket_connection_setup_timeout_max_ms,
749 connections_max_idle_ms,
750 reconnect_backoff_ms,
751 reconnect_backoff_max_ms,
752 retry_backoff_ms,
753 retry_backoff_max_ms,
754 bootstrap_resolve_timeout_ms,
755 request_timeout_ms,
756 default_api_timeout_ms,
757 metadata_max_age_ms,
758 metadata_recovery_strategy,
759 metadata_recovery_rebootstrap_trigger_ms,
760 metadata_cluster_check_enable,
761 exclude_internal_topics,
762 throw_on_fetch_stable_offset_unsupported,
763 isolation_level,
764 allow_auto_create_topics,
765 metrics_sample_window_ms,
766 metrics_num_samples,
767 metrics_recording_level,
768 security_protocol,
769 sasl_config,
770 ssl_config,
771 })
772 }
773}
774
775impl ConsumerConfig {
776 pub const GROUP_ID_CONFIG: &'static str = "group.id";
780 pub const GROUP_INSTANCE_ID_CONFIG: &'static str = "group.instance.id";
782 pub const GROUP_PROTOCOL_CONFIG: &'static str = "group.protocol";
784 pub const DEFAULT_GROUP_PROTOCOL: &'static str = "classic";
786 pub const GROUP_REMOTE_ASSIGNOR_CONFIG: &'static str = "group.remote.assignor";
788
789 pub const MAX_POLL_RECORDS_CONFIG: &'static str = "max.poll.records";
791 pub const DEFAULT_MAX_POLL_RECORDS: i32 = 500;
793 pub const MAX_POLL_INTERVAL_MS_CONFIG: &'static str = "max.poll.interval.ms";
795 pub const SESSION_TIMEOUT_MS_CONFIG: &'static str = "session.timeout.ms";
797 pub const HEARTBEAT_INTERVAL_MS_CONFIG: &'static str = "heartbeat.interval.ms";
799
800 pub const BOOTSTRAP_SERVERS_CONFIG: &'static str = "bootstrap.servers";
802 pub const CLIENT_DNS_LOOKUP_CONFIG: &'static str = CommonClientConfigs::CLIENT_DNS_LOOKUP_CONFIG;
805 pub const CLIENT_ID_CONFIG: &'static str = "client.id";
807 pub const CLIENT_RACK_CONFIG: &'static str = crate::CommonClientConfigs::CLIENT_RACK_CONFIG;
809 pub const DEFAULT_CLIENT_RACK: &'static str = crate::CommonClientConfigs::DEFAULT_CLIENT_RACK;
811
812 pub const ENABLE_AUTO_COMMIT_CONFIG: &'static str = "enable.auto.commit";
814 pub const AUTO_COMMIT_INTERVAL_MS_CONFIG: &'static str = "auto.commit.interval.ms";
816
817 pub const PARTITION_ASSIGNMENT_STRATEGY_CONFIG: &'static str = "partition.assignment.strategy";
819 pub const AUTO_OFFSET_RESET_CONFIG: &'static str = "auto.offset.reset";
869
870 pub const FETCH_MIN_BYTES_CONFIG: &'static str = "fetch.min.bytes";
872 pub const DEFAULT_FETCH_MIN_BYTES: i32 = 1;
874 pub const FETCH_MAX_BYTES_CONFIG: &'static str = "fetch.max.bytes";
876 pub const DEFAULT_FETCH_MAX_BYTES: i32 = 50 * 1024 * 1024;
878 pub const FETCH_MAX_WAIT_MS_CONFIG: &'static str = "fetch.max.wait.ms";
880 pub const DEFAULT_FETCH_MAX_WAIT_MS: i32 = 500;
882 pub const MAX_PARTITION_FETCH_BYTES_CONFIG: &'static str = "max.partition.fetch.bytes";
884 pub const DEFAULT_MAX_PARTITION_FETCH_BYTES: i32 = 1024 * 1024;
886 pub const CHECK_CRCS_CONFIG: &'static str = "check.crcs";
888
889 pub const SEND_BUFFER_CONFIG: &'static str = "send.buffer.bytes";
891 pub const RECEIVE_BUFFER_CONFIG: &'static str = "receive.buffer.bytes";
893 pub const SOCKET_CONNECTION_SETUP_TIMEOUT_MS_CONFIG: &'static str = "socket.connection.setup.timeout.ms";
895 pub const SOCKET_CONNECTION_SETUP_TIMEOUT_MAX_MS_CONFIG: &'static str = "socket.connection.setup.timeout.max.ms";
897 pub const CONNECTIONS_MAX_IDLE_MS_CONFIG: &'static str = "connections.max.idle.ms";
899
900 pub const RECONNECT_BACKOFF_MS_CONFIG: &'static str = "reconnect.backoff.ms";
902 pub const RECONNECT_BACKOFF_MAX_MS_CONFIG: &'static str = "reconnect.backoff.max.ms";
904 pub const RETRY_BACKOFF_MS_CONFIG: &'static str = "retry.backoff.ms";
906 pub const RETRY_BACKOFF_MAX_MS_CONFIG: &'static str = "retry.backoff.max.ms";
908 pub const BOOTSTRAP_RESOLVE_TIMEOUT_MS_CONFIG: &'static str =
919 CommonClientConfigs::BOOTSTRAP_RESOLVE_TIMEOUT_MS_CONFIG;
920
921 pub const REQUEST_TIMEOUT_MS_CONFIG: &'static str = "request.timeout.ms";
923 pub const DEFAULT_API_TIMEOUT_MS_CONFIG: &'static str = "default.api.timeout.ms";
925
926 pub const METADATA_MAX_AGE_CONFIG: &'static str = "metadata.max.age.ms";
928
929 pub const EXCLUDE_INTERNAL_TOPICS_CONFIG: &'static str = "exclude.internal.topics";
931 pub const DEFAULT_EXCLUDE_INTERNAL_TOPICS: bool = true;
933 pub(crate) const THROW_ON_FETCH_STABLE_OFFSET_UNSUPPORTED: &'static str =
938 "internal.throw.on.fetch.stable.offset.unsupported";
939 pub const ISOLATION_LEVEL_CONFIG: &'static str = "isolation.level";
941 pub const DEFAULT_ISOLATION_LEVEL: &'static str = "read_committed";
948 pub const ALLOW_AUTO_CREATE_TOPICS_CONFIG: &'static str = "allow.auto.create.topics";
950 pub const DEFAULT_ALLOW_AUTO_CREATE_TOPICS: bool = true;
952
953 pub const METRICS_SAMPLE_WINDOW_MS_CONFIG: &'static str = "metrics.sample.window.ms";
955 pub const METRICS_NUM_SAMPLES_CONFIG: &'static str = "metrics.num.samples";
957 pub const METRICS_RECORDING_LEVEL_CONFIG: &'static str = "metrics.recording.level";
959
960 pub const SHARE_ACKNOWLEDGEMENT_MODE_CONFIG: &'static str = "share.acknowledgement.mode";
962 pub const SHARE_ACQUIRE_MODE_CONFIG: &'static str = "share.acquire.mode";
964
965 #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfig#ConsumerConfig(Map)")]
1007 pub fn new(props: &HashMap<String, String>) -> Result<Self, Error> {
1008 Self::build(string_originals(props), None)
1009 }
1010
1011 #[cfg_attr(
1038 not(any(feature = "ffi", test)),
1039 expect(dead_code, reason = "only the C config handle copies a configuration")
1040 )]
1041 pub(crate) fn copy_for_client(&self) -> Result<Self, Error> {
1042 Self::build(self.config.originals_map().clone(), Some(&self.client_id))
1043 }
1044
1045 fn build(originals: ConfigMap, resolved_client_id: Option<&str>) -> Result<Self, Error> {
1051 let log_warnings = resolved_client_id.is_none();
1052 let config = AbstractConfig::with_hooks_and_log_warnings(
1053 &CONFIG,
1054 originals,
1055 false,
1056 log_warnings,
1057 "ConsumerConfig",
1058 |originals| originals,
1059 |config| Self::post_process_parsed_config(config, resolved_client_id),
1060 )?;
1061 Self::from_abstract(config)
1062 }
1063
1064 fn post_process_parsed_config(
1070 config: &AbstractConfig,
1071 resolved_client_id: Option<&str>,
1072 ) -> Result<ConfigMap, Error> {
1073 let log_warnings = resolved_client_id.is_none();
1074 CommonClientConfigs::post_validate_sasl_mechanism_config(config)?;
1075 CommonClientConfigs::post_validate_supported_sasl_mechanism(config)?;
1076 CommonClientConfigs::warn_disabling_exponential_backoff(config, log_warnings)?;
1077 let mut refined_configs =
1078 CommonClientConfigs::post_process_reconnect_backoff_configs(config, config.parsed_values(), log_warnings);
1079 Self::maybe_override_client_id_with_configs(config, &mut refined_configs, resolved_client_id)?;
1080 Self::maybe_override_enable_auto_commit(config, &mut refined_configs)?;
1081 Self::check_unsupported_configs_post_process(config)?;
1082 Self::warn_if_connections_max_idle_ms_lower_than_max_poll_interval_ms(config, log_warnings)?;
1083 Ok(refined_configs)
1084 }
1085
1086 fn warn_if_connections_max_idle_ms_lower_than_max_poll_interval_ms(
1089 config: &AbstractConfig,
1090 log_warnings: bool,
1091 ) -> Result<(), Error> {
1092 let group_protocol = config.get_string(Self::GROUP_PROTOCOL_CONFIG)?;
1093 if !group_protocol.is_some_and(|protocol| protocol.eq_ignore_ascii_case("CLASSIC")) {
1094 return Ok(());
1095 }
1096 let connections_max_idle_ms = config.get_long(Self::CONNECTIONS_MAX_IDLE_MS_CONFIG)?.unwrap_or_default();
1097 let max_poll_interval_ms = config.get_int(Self::MAX_POLL_INTERVAL_MS_CONFIG)?.unwrap_or_default();
1098 if log_warnings && connections_max_idle_ms >= 0 && connections_max_idle_ms < i64::from(max_poll_interval_ms) {
1099 warn!(
1100 "Configuration '{}' with value '{}' is lower than configuration '{}' with value '{}'. This may cause \
1101 the connection to the group coordinator to be closed during an ongoing rebalance, which can \
1102 prolong or disrupt group rejoin.",
1103 Self::CONNECTIONS_MAX_IDLE_MS_CONFIG,
1104 connections_max_idle_ms,
1105 Self::MAX_POLL_INTERVAL_MS_CONFIG,
1106 max_poll_interval_ms
1107 );
1108 }
1109 Ok(())
1110 }
1111
1112 fn maybe_override_client_id_with_configs(
1126 config: &AbstractConfig,
1127 configs: &mut ConfigMap,
1128 resolved_client_id: Option<&str>,
1129 ) -> Result<(), Error> {
1130 let client_id = config.get_string(Self::CLIENT_ID_CONFIG)?;
1131 if client_id.is_none_or(str::is_empty) {
1132 let group_id = config.get_string(Self::GROUP_ID_CONFIG)?;
1133 let group_instance_id = config.get_string(Self::GROUP_INSTANCE_ID_CONFIG)?;
1134 if let Some(group_instance_id) = group_instance_id {
1135 JoinGroupRequest::validate_group_instance_id(group_instance_id)?;
1136 }
1137 let generated_client_id = match resolved_client_id {
1138 Some(resolved_client_id) => resolved_client_id.to_string(),
1139 None => {
1140 let group_instance_id_part = match group_instance_id {
1141 Some(group_instance_id) => group_instance_id.to_string(),
1142 None => CONSUMER_CLIENT_ID_SEQUENCE.fetch_add(1, atomic::Ordering::Relaxed).to_string(),
1143 };
1144 format!("consumer-{}-{group_instance_id_part}", group_id.unwrap_or("null"))
1145 },
1146 };
1147 configs.insert(
1148 Self::CLIENT_ID_CONFIG.to_string(),
1149 Some(ParsedValue::String(generated_client_id)),
1150 );
1151 }
1152 Ok(())
1153 }
1154
1155 fn maybe_override_enable_auto_commit(config: &AbstractConfig, configs: &mut ConfigMap) -> Result<(), Error> {
1159 let group_id = config.get_string(Self::GROUP_ID_CONFIG)?;
1160 let auto_commit_set = config.originals_map().contains_key(Self::ENABLE_AUTO_COMMIT_CONFIG);
1161 let enable_auto_commit = if auto_commit_set {
1162 config.get_boolean(Self::ENABLE_AUTO_COMMIT_CONFIG)?.unwrap_or_default()
1163 } else {
1164 false
1165 };
1166 if group_id.is_none() {
1167 if !auto_commit_set {
1169 configs.insert(Self::ENABLE_AUTO_COMMIT_CONFIG.to_string(), Some(ParsedValue::Boolean(false)));
1170 } else if enable_auto_commit {
1171 return Err(Error::InvalidConfiguration(InvalidConfigurationError::new(format!(
1172 "{} cannot be set to true when default group id (null) is used.",
1173 Self::ENABLE_AUTO_COMMIT_CONFIG
1174 ))));
1175 }
1176 }
1177 Ok(())
1178 }
1179
1180 fn check_unsupported_configs_post_process(config: &AbstractConfig) -> Result<(), Error> {
1183 let group_protocol = config.get_string(Self::GROUP_PROTOCOL_CONFIG)?.unwrap_or_default();
1184 if group_protocol.eq_ignore_ascii_case("CLASSIC") {
1185 Self::check_unsupported_configs(config, "CLASSIC", &CLASSIC_PROTOCOL_UNSUPPORTED_CONFIGS)
1186 } else if group_protocol.eq_ignore_ascii_case("CONSUMER") {
1187 Self::check_unsupported_configs(config, "CONSUMER", &CONSUMER_PROTOCOL_UNSUPPORTED_CONFIGS)
1188 } else {
1189 Ok(())
1190 }
1191 }
1192
1193 fn check_unsupported_configs(
1195 config: &AbstractConfig,
1196 group_protocol: &str,
1197 unsupported_configs: &[&str],
1198 ) -> Result<(), Error> {
1199 let mut invalid_configs = Vec::new();
1200 for &config_name in unsupported_configs {
1201 config.ignore(config_name);
1204 if let Some(Some(value)) = config.originals_map().get(config_name)
1205 && !Utils::is_blank(Some(&value.to_string()))
1206 {
1207 invalid_configs.push(config_name);
1208 }
1209 }
1210 if invalid_configs.is_empty() {
1211 Ok(())
1212 } else {
1213 Err(Error::config_message(format!(
1214 "{} cannot be set when {}={group_protocol}",
1215 invalid_configs.join(", "),
1216 Self::GROUP_PROTOCOL_CONFIG
1217 )))
1218 }
1219 }
1220
1221 #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#getShort")]
1239 pub fn get_short(&self, key: &str) -> Result<Option<i16>, Error> {
1240 self.config.get_short(key)
1241 }
1242
1243 #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#getInt")]
1261 pub fn get_int(&self, key: &str) -> Result<Option<i32>, Error> {
1262 self.config.get_int(key)
1263 }
1264
1265 #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#getLong")]
1283 pub fn get_long(&self, key: &str) -> Result<Option<i64>, Error> {
1284 self.config.get_long(key)
1285 }
1286
1287 #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#getDouble")]
1305 pub fn get_double(&self, key: &str) -> Result<Option<f64>, Error> {
1306 self.config.get_double(key)
1307 }
1308
1309 #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#getList")]
1327 pub fn get_list(&self, key: &str) -> Result<Option<&[String]>, Error> {
1328 self.config.get_list(key)
1329 }
1330
1331 #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#getBoolean")]
1352 pub fn get_boolean(&self, key: &str) -> Result<Option<bool>, Error> {
1353 self.config.get_boolean(key)
1354 }
1355
1356 #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#getString")]
1383 pub fn get_string(&self, key: &str) -> Result<Option<&str>, Error> {
1384 self.config.get_string(key)
1385 }
1386
1387 #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#unused")]
1391 pub fn unused(&self) -> HashSet<String> {
1392 self.config.unused()
1393 }
1394
1395 #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#ignore")]
1401 pub fn ignore(&self, key: &str) {
1402 self.config.ignore(key);
1403 }
1404
1405 #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#logUnused")]
1409 pub fn log_unused(&self) {
1410 self.config.log_unused();
1411 }
1412
1413 #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfig#configNames")]
1419 pub fn config_names() -> HashSet<String> {
1420 CONFIG.names()
1421 }
1422
1423 pub(crate) fn mark_read_keys_used(&self) {
1426 for key in &self.read_keys {
1427 self.config.ignore(key);
1428 }
1429 }
1430
1431 pub(crate) fn log_values(&self) {
1434 self.config.log_all("ConsumerConfig");
1435 }
1436
1437 pub(crate) fn bootstrap_servers(&self) -> &[String] {
1439 &self.bootstrap_servers
1440 }
1441 pub(crate) fn client_dns_lookup(&self) -> ClientDnsLookup {
1443 self.client_dns_lookup
1444 }
1445 pub(crate) fn client_id(&self) -> &str {
1447 &self.client_id
1448 }
1449 pub(crate) fn client_rack(&self) -> &str {
1451 &self.client_rack
1452 }
1453 pub(crate) fn group_id(&self) -> Option<&str> {
1455 self.group_id.as_deref()
1456 }
1457 pub(crate) fn group_instance_id(&self) -> Option<&str> {
1459 self.group_instance_id.as_deref()
1460 }
1461 pub(crate) fn group_protocol(&self) -> &str {
1463 &self.group_protocol
1464 }
1465 pub(crate) fn group_remote_assignor(&self) -> Option<&str> {
1467 self.group_remote_assignor.as_deref()
1468 }
1469 pub(crate) fn max_poll_interval_ms(&self) -> i32 {
1471 self.max_poll_interval_ms
1472 }
1473 pub(crate) fn enable_auto_commit(&self) -> bool {
1475 self.enable_auto_commit
1476 }
1477 pub(crate) fn auto_commit_interval_ms(&self) -> i32 {
1479 self.auto_commit_interval_ms
1480 }
1481 pub(crate) fn auto_offset_reset(&self) -> &str {
1483 &self.auto_offset_reset
1484 }
1485 pub(crate) fn metadata_recovery_strategy(&self) -> &str {
1487 &self.metadata_recovery_strategy
1488 }
1489 pub(crate) fn throw_on_fetch_stable_offset_unsupported(&self) -> bool {
1491 self.throw_on_fetch_stable_offset_unsupported
1492 }
1493 pub(crate) fn request_timeout_ms(&self) -> i32 {
1495 self.request_timeout_ms
1496 }
1497 pub(crate) fn retry_backoff_ms(&self) -> i64 {
1499 self.retry_backoff_ms
1500 }
1501 pub(crate) fn retry_backoff_max_ms(&self) -> i64 {
1503 self.retry_backoff_max_ms
1504 }
1505 pub(crate) fn bootstrap_resolve_timeout_ms(&self) -> i64 {
1508 self.bootstrap_resolve_timeout_ms
1509 }
1510
1511 #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfig#maybeOverrideClientId")]
1517 pub(crate) fn maybe_override_client_id(&mut self) -> Result<(), Error> {
1518 if !self.client_id.is_empty() {
1519 return Ok(());
1520 }
1521 if let Some(group_instance_id) = &self.group_instance_id {
1522 JoinGroupRequest::validate_group_instance_id(group_instance_id)?;
1523 }
1524 let group_id = self.group_id.as_deref().unwrap_or("null");
1525 let group_instance_id_part = match &self.group_instance_id {
1526 Some(group_instance_id) => group_instance_id.clone(),
1527 None => CONSUMER_CLIENT_ID_SEQUENCE.fetch_add(1, atomic::Ordering::Relaxed).to_string(),
1528 };
1529 self.client_id = format!("consumer-{group_id}-{group_instance_id_part}");
1530 Ok(())
1531 }
1532}
1533
1534#[cfg(test)]
1535mod tests {
1536 #[test]
1539 fn test_sasl_mechanism_is_validated_when_the_config_is_built() {
1540 let build = |protocol: &str, mechanism: &str| {
1541 let props = std::collections::HashMap::from([
1542 ("bootstrap.servers".to_string(), "localhost:9092".to_string()),
1543 ("security.protocol".to_string(), protocol.to_string()),
1544 ("sasl.mechanism".to_string(), mechanism.to_string()),
1545 ]);
1546 ConsumerConfig::new(&props)
1547 };
1548
1549 let err = build("SASL_PLAINTEXT", "SCRAM-SHA-256").expect_err("SCRAM is not implemented");
1550 assert!(err.is_config_error(), "must be a ConfigError: {err:?}");
1551 assert_eq!(
1552 err.message(),
1553 "Invalid value SCRAM-SHA-256 for configuration sasl.mechanism: only PLAIN is supported by this client"
1554 );
1555
1556 let err = build("SASL_SSL", "").expect_err("an empty mechanism is invalid with SASL");
1557 assert!(err.is_config_error(), "must be a ConfigError: {err:?}");
1558 assert!(
1559 err.message().contains("mechanism must be non-null and non-empty string"),
1560 "got: {}",
1561 err.message()
1562 );
1563
1564 build("SASL_PLAINTEXT", "PLAIN").expect("PLAIN is implemented");
1565 build("PLAINTEXT", "SCRAM-SHA-256").expect("the mechanism only matters with SASL");
1566 }
1567
1568 use super::*;
1569 use crate::common::config::types::Password;
1570 use crate::test_log_capture::capture_logs;
1571 use log::Level;
1572
1573 fn base_props() -> HashMap<String, String> {
1576 HashMap::from([("bootstrap.servers".to_string(), "localhost:9092".to_string())])
1577 }
1578
1579 #[test]
1580 fn test_default_values() {
1581 let c = ConsumerConfig::defaults();
1582 assert_eq!(c.max_poll_records, 500);
1583 assert_eq!(c.max_poll_interval_ms(), 300_000);
1584 assert_eq!(c.get_int(ConsumerConfig::SESSION_TIMEOUT_MS_CONFIG).unwrap(), Some(45_000));
1585 assert_eq!(c.get_int(ConsumerConfig::HEARTBEAT_INTERVAL_MS_CONFIG).unwrap(), Some(3_000));
1586 assert!(c.enable_auto_commit());
1587 assert_eq!(c.auto_commit_interval_ms(), 5_000);
1588 assert_eq!(c.auto_offset_reset(), "latest");
1589 assert_eq!(c.group_protocol(), "classic");
1590 assert_eq!(c.group_id(), None);
1591 assert_eq!(c.security_protocol, SecurityProtocol::Plaintext);
1592 assert_eq!(c.metadata_recovery_strategy(), "rebootstrap");
1593 }
1594
1595 #[test]
1596 fn test_new_basic() {
1597 let mut props = base_props();
1598 props.insert("bootstrap.servers".to_string(), "host1:9092,host2:9093".to_string());
1599 props.insert("group.id".to_string(), "g".to_string());
1600 props.insert("client.id".to_string(), "my-consumer".to_string());
1601 props.insert("max.poll.records".to_string(), "100".to_string());
1602 let c = ConsumerConfig::new(&props).unwrap();
1603 assert_eq!(c.bootstrap_servers(), &["host1:9092".to_string(), "host2:9093".to_string()]);
1604 assert_eq!(c.group_id(), Some("g"));
1605 assert_eq!(c.client_id(), "my-consumer");
1606 assert_eq!(c.max_poll_records, 100);
1607 }
1608
1609 #[test]
1610 fn test_new_unknown_key_ignored() {
1611 let mut props = base_props();
1612 props.insert("unknown.key".to_string(), "value".to_string());
1613 props.insert("bootstrap.servers".to_string(), "localhost:9092".to_string());
1614 let c = ConsumerConfig::new(&props).unwrap();
1615 assert_eq!(c.bootstrap_servers(), &["localhost:9092".to_string()]);
1616 }
1617
1618 #[test]
1624 fn test_parse_bool_matches_java_boolean() {
1625 let props_with = |raw: &str| {
1626 HashMap::from([
1628 ("bootstrap.servers".to_string(), "localhost:9092".to_string()),
1629 ("group.id".to_string(), "g".to_string()),
1630 ("enable.auto.commit".to_string(), raw.to_string()),
1631 ])
1632 };
1633
1634 for raw in ["true", "True", "TRUE"] {
1636 let c = ConsumerConfig::new(&props_with(raw))
1637 .unwrap_or_else(|e| panic!("enable.auto.commit={raw} should be valid: {e:?}"));
1638 assert!(c.enable_auto_commit(), "for enable.auto.commit={raw}");
1639 }
1640 for raw in ["false", "False", " false "] {
1641 let c = ConsumerConfig::new(&props_with(raw))
1642 .unwrap_or_else(|e| panic!("enable.auto.commit={raw} should be valid: {e:?}"));
1643 assert!(!c.enable_auto_commit(), "for enable.auto.commit={raw}");
1644 }
1645
1646 let error =
1648 ConsumerConfig::new(&props_with("yes")).expect_err("a non-boolean enable.auto.commit must be rejected");
1649 assert_eq!(
1650 error.message(),
1651 "Invalid value yes for configuration enable.auto.commit: Expected value to be either true or false",
1652 );
1653 assert!(matches!(error, Error::Config(_)), "expected Error::Config, got {error:?}");
1654 assert!(error.is_kafka_error());
1655 }
1656
1657 #[test]
1660 fn test_metrics_num_samples_validator() {
1661 let mut props = base_props();
1663 props.insert("metrics.num.samples".to_string(), "3".to_string());
1664 let c = ConsumerConfig::new(&props).unwrap();
1665 assert_eq!(c.metrics_num_samples, 3);
1666
1667 let mut props = base_props();
1669 props.insert("metrics.num.samples".to_string(), "0".to_string());
1670 let err = ConsumerConfig::new(&props).unwrap_err();
1671 let msg = err.to_string();
1672 assert!(
1673 msg.contains("metrics.num.samples") && msg.contains("at least 1"),
1674 "unexpected message: {msg}"
1675 );
1676
1677 let mut props = base_props();
1679 props.insert("metrics.num.samples".to_string(), "-1".to_string());
1680 assert!(ConsumerConfig::new(&props).is_err());
1681 }
1682
1683 #[test]
1685 fn test_metadata_cluster_check_enable() {
1686 assert!(ConsumerConfig::new(&base_props()).unwrap().metadata_cluster_check_enable);
1687
1688 let mut props = base_props();
1689 props.insert(
1690 CommonClientConfigs::METADATA_CLUSTER_CHECK_ENABLE_CONFIG.to_string(),
1691 "false".to_string(),
1692 );
1693 assert!(!ConsumerConfig::new(&props).unwrap().metadata_cluster_check_enable);
1694
1695 props.insert(
1696 CommonClientConfigs::METADATA_CLUSTER_CHECK_ENABLE_CONFIG.to_string(),
1697 "maybe".to_string(),
1698 );
1699 assert_eq!(
1700 ConsumerConfig::new(&props).unwrap_err().message(),
1701 "Invalid value maybe for configuration metadata.cluster.check.enable: Expected value to be either true or false"
1702 );
1703 }
1704
1705 #[test]
1709 fn test_metadata_recovery_rebootstrap_trigger_ms_validator() {
1710 let mut props = base_props();
1711 props.insert("metadata.recovery.rebootstrap.trigger.ms".to_string(), "0".to_string());
1712 let c = ConsumerConfig::new(&props).unwrap();
1713 assert_eq!(c.metadata_recovery_rebootstrap_trigger_ms, 0);
1714
1715 let mut props = base_props();
1716 props.insert("metadata.recovery.rebootstrap.trigger.ms".to_string(), "-1".to_string());
1717 let err = ConsumerConfig::new(&props).unwrap_err();
1718 let Error::Config(config_error) = err else {
1719 panic!("expected a config error, got {err:?}");
1720 };
1721 assert_eq!(
1722 config_error.message(),
1723 "Invalid value -1 for configuration metadata.recovery.rebootstrap.trigger.ms: \
1724 Value must be at least 0"
1725 );
1726 }
1727
1728 #[test]
1735 fn test_request_and_default_api_timeout_ms_validators() {
1736 for key in [
1737 ConsumerConfig::REQUEST_TIMEOUT_MS_CONFIG,
1738 ConsumerConfig::DEFAULT_API_TIMEOUT_MS_CONFIG,
1739 ] {
1740 let mut props = base_props();
1741 props.insert(key.to_string(), "0".to_string());
1742 let c = ConsumerConfig::new(&props).unwrap();
1743 if key == ConsumerConfig::REQUEST_TIMEOUT_MS_CONFIG {
1744 assert_eq!(c.request_timeout_ms, 0);
1745 } else {
1746 assert_eq!(c.default_api_timeout_ms, 0);
1747 }
1748
1749 let mut props = base_props();
1750 props.insert(key.to_string(), "-1".to_string());
1751 let err = ConsumerConfig::new(&props).unwrap_err();
1752 let Error::Config(config_error) = err else {
1753 panic!("expected a config error for {key}, got {err:?}");
1754 };
1755 assert_eq!(
1756 config_error.message(),
1757 format!("Invalid value -1 for configuration {key}: Value must be at least 0")
1758 );
1759 }
1760 }
1761
1762 #[test]
1765 fn test_bootstrap_resolve_timeout_ms() {
1766 assert_eq!(
1767 ConsumerConfig::BOOTSTRAP_RESOLVE_TIMEOUT_MS_CONFIG,
1768 "bootstrap.resolve.timeout.ms"
1769 );
1770 let mut props = HashMap::from([("bootstrap.servers".to_string(), "localhost:9092".to_string())]);
1771 assert_eq!(ConsumerConfig::new(&props).unwrap().bootstrap_resolve_timeout_ms(), 0);
1772
1773 props.insert("bootstrap.resolve.timeout.ms".to_string(), "3000".to_string());
1774 assert_eq!(ConsumerConfig::new(&props).unwrap().bootstrap_resolve_timeout_ms(), 3000);
1775
1776 props.insert("bootstrap.resolve.timeout.ms".to_string(), "-1".to_string());
1777 match ConsumerConfig::new(&props) {
1778 Err(Error::Config(e)) => assert_eq!(
1779 e.message(),
1780 "Invalid value -1 for configuration bootstrap.resolve.timeout.ms: Value must be at least 0"
1781 ),
1782 other => panic!("expected a config error, got {other:?}"),
1783 }
1784 }
1785
1786 #[test]
1789 fn test_metrics_sample_window_ms_validator() {
1790 let mut props = base_props();
1792 props.insert("metrics.sample.window.ms".to_string(), "0".to_string());
1793 let c = ConsumerConfig::new(&props).unwrap();
1794 assert_eq!(c.metrics_sample_window_ms, 0);
1795
1796 let mut props = base_props();
1797 props.insert("metrics.sample.window.ms".to_string(), "60000".to_string());
1798 let c = ConsumerConfig::new(&props).unwrap();
1799 assert_eq!(c.metrics_sample_window_ms, 60_000);
1800
1801 let mut props = base_props();
1803 props.insert("metrics.sample.window.ms".to_string(), "-1".to_string());
1804 let err = ConsumerConfig::new(&props).unwrap_err();
1805 let msg = err.to_string();
1806 assert!(
1807 msg.contains("metrics.sample.window.ms") && msg.contains("at least 0"),
1808 "unexpected message: {msg}"
1809 );
1810 }
1811
1812 #[test]
1817 fn test_metrics_recording_level_validator() {
1818 for level in ["INFO", "DEBUG", "TRACE"] {
1819 let mut props = base_props();
1820 props.insert("metrics.recording.level".to_string(), level.to_string());
1821 let c = ConsumerConfig::new(&props).unwrap();
1822 assert_eq!(c.metrics_recording_level, level);
1823 }
1824
1825 let mut props = base_props();
1827 props.insert("metrics.recording.level".to_string(), "debug".to_string());
1828 let err = ConsumerConfig::new(&props).unwrap_err();
1829 assert!(
1830 err.to_string().ends_with(
1831 "Invalid value debug for configuration metrics.recording.level: \
1832 String must be one of: INFO, DEBUG, TRACE"
1833 ),
1834 "unexpected message: {err}"
1835 );
1836
1837 let mut props = base_props();
1839 props.insert("metrics.recording.level".to_string(), "bogus".to_string());
1840 assert!(ConsumerConfig::new(&props).is_err());
1841 }
1842
1843 #[test]
1845 fn test_security_protocol_all_values() {
1846 for (input, expected) in [
1847 ("PLAINTEXT", SecurityProtocol::Plaintext),
1848 ("SSL", SecurityProtocol::Ssl),
1849 ("SASL_PLAINTEXT", SecurityProtocol::SaslPlaintext),
1850 ("SASL_SSL", SecurityProtocol::SaslSsl),
1851 ] {
1852 let mut props = base_props();
1853 props.insert("security.protocol".to_string(), input.to_string());
1854 props.insert("sasl.mechanism".to_string(), "PLAIN".to_string());
1855 let c = ConsumerConfig::new(&props).unwrap();
1856 assert_eq!(c.security_protocol, expected, "for input {input}");
1857 }
1858 }
1859
1860 #[test]
1862 #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#testCaseInsensitiveSecurityProtocol")]
1863 fn test_case_insensitive_security_protocol() {
1864 let mut props = base_props();
1865 props.insert("security.protocol".to_string(), "sasl_ssl".to_string());
1866 props.insert("sasl.mechanism".to_string(), "PLAIN".to_string());
1868 let c = ConsumerConfig::new(&props).unwrap();
1869 assert_eq!(c.security_protocol, SecurityProtocol::SaslSsl);
1870 }
1871
1872 #[test]
1875 #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#testInvalidSecurityProtocol")]
1876 fn test_invalid_security_protocol() {
1877 let mut props = base_props();
1878 props.insert("security.protocol".to_string(), "abc".to_string());
1879 let err = ConsumerConfig::new(&props).unwrap_err();
1880 let msg = format!("{}", err);
1881 assert!(msg.contains("security.protocol"), "should contain config key, got: {msg}");
1882 assert!(msg.contains("abc"), "should contain the invalid value, got: {msg}");
1883 assert!(msg.contains("SASL_SSL"), "should list the valid protocol names, got: {msg}");
1884 }
1885
1886 #[test]
1889 fn test_missing_bootstrap_servers_rejected_with_exact_message() {
1890 let err = ConsumerConfig::new(&HashMap::new()).unwrap_err();
1891 let Error::Config(config_err) = &err else {
1892 panic!("expected a config error, got: {err:?}");
1893 };
1894 assert_eq!(
1895 config_err.message(),
1896 "Missing required configuration \"bootstrap.servers\" which has no default value."
1897 );
1898 }
1899
1900 #[test]
1903 fn test_client_dns_lookup() {
1904 assert_eq!(ConsumerConfig::CLIENT_DNS_LOOKUP_CONFIG, "client.dns.lookup");
1905 assert_eq!(
1906 ConsumerConfig::new(&base_props()).unwrap().client_dns_lookup(),
1907 ClientDnsLookup::UseAllDnsIps
1908 );
1909
1910 let mut props = base_props();
1911 props.insert(
1912 CommonClientConfigs::CLIENT_DNS_LOOKUP_CONFIG.to_string(),
1913 "resolve_canonical_bootstrap_servers_only".to_string(),
1914 );
1915 assert_eq!(
1916 ConsumerConfig::new(&props).unwrap().client_dns_lookup(),
1917 ClientDnsLookup::ResolveCanonicalBootstrapServersOnly
1918 );
1919
1920 props.insert(CommonClientConfigs::CLIENT_DNS_LOOKUP_CONFIG.to_string(), "default".to_string());
1921 assert_eq!(
1922 ConsumerConfig::new(&props).unwrap_err().message(),
1923 "Invalid value default for configuration client.dns.lookup: String must be one of: \
1924 use_all_dns_ips, resolve_canonical_bootstrap_servers_only"
1925 );
1926 }
1927
1928 #[test]
1930 fn test_sasl_config_from_properties() {
1931 let mut props = base_props();
1932 props.insert("sasl.mechanism".to_string(), "PLAIN".to_string());
1933 props.insert(
1934 "sasl.jaas.config".to_string(),
1935 "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"alice\" password=\"secret\";"
1936 .to_string(),
1937 );
1938 let c = ConsumerConfig::new(&props).unwrap();
1939 assert_eq!(c.sasl_config.mechanism, "PLAIN");
1940 assert_eq!(c.sasl_config.resolve_username().as_deref(), Some("alice"));
1941 assert_eq!(c.sasl_config.resolve_password().as_deref(), Some("secret"));
1942 }
1943
1944 #[test]
1950 fn test_unsupported_sasl_mechanism_rejected() {
1951 let jaas = "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"u\" password=\"p\";";
1952 let build = |extra: &[(&str, &str)]| {
1953 let mut props = base_props();
1954 for (k, v) in extra {
1955 props.insert((*k).to_string(), (*v).to_string());
1956 }
1957 ConsumerConfig::new(&props)
1958 };
1959
1960 let err = build(&[
1961 ("security.protocol", "SASL_PLAINTEXT"),
1962 ("sasl.mechanism", "SCRAM-SHA-256"),
1963 ("sasl.jaas.config", jaas),
1964 ])
1965 .unwrap_err();
1966 assert!(err.is_config_error(), "{err:?}");
1967 assert_eq!(
1968 err.to_string(),
1969 "ConfigError: Invalid value SCRAM-SHA-256 for configuration sasl.mechanism: only PLAIN is supported by \
1970 this client"
1971 );
1972
1973 let err = build(&[("security.protocol", "SASL_SSL")]).unwrap_err();
1975 assert!(err.is_config_error(), "{err:?}");
1976 assert_eq!(
1977 err.to_string(),
1978 "ConfigError: Invalid value GSSAPI for configuration sasl.mechanism: only PLAIN is supported by this client"
1979 );
1980
1981 let err = build(&[("security.protocol", "SASL_PLAINTEXT"), ("sasl.mechanism", "plain")]).unwrap_err();
1983 assert!(err.is_config_error(), "{err:?}");
1984
1985 build(&[("security.protocol", "PLAINTEXT")]).unwrap();
1986 build(&[
1987 ("security.protocol", "SASL_PLAINTEXT"),
1988 ("sasl.mechanism", "PLAIN"),
1989 ("sasl.jaas.config", jaas),
1990 ])
1991 .unwrap();
1992
1993 let err = build(&[("security.protocol", "SASL_PLAINTEXT"), ("sasl.mechanism", "")]).unwrap_err();
1995 assert!(err.is_config_error(), "{err:?}");
1996 assert_eq!(
1997 err.to_string(),
1998 "ConfigError: Invalid value null for configuration sasl.mechanism: When the security.protocol \
1999 configuration enables SASL, mechanism must be non-null and non-empty string."
2000 );
2001 }
2002
2003 #[test]
2009 fn test_debug_redacts_password_configs() {
2010 let secrets = [
2011 (
2012 "sasl.jaas.config",
2013 "org.apache.kafka.common.security.plain.PlainLoginModule required \
2014 username=\"jaas-user\" password=\"jaas-S3cr3t\";",
2015 ),
2016 ("ssl.truststore.certificates", "TRUSTSTORE-CERTIFICATES-PEM"),
2017 ("ssl.keystore.key", "KEYSTORE-KEY-PEM"),
2018 ("ssl.keystore.certificate.chain", "KEYSTORE-CERTIFICATE-CHAIN-PEM"),
2019 ];
2020 let undefined = [
2022 ("ssl.truststore.password", "truststore-S3cr3t"),
2023 ("ssl.keystore.password", "keystore-S3cr3t"),
2024 ("ssl.key.password", "key-S3cr3t"),
2025 ];
2026 let mut props: HashMap<String, String> = secrets
2027 .iter()
2028 .chain(&undefined)
2029 .map(|(key, value)| (key.to_string(), value.to_string()))
2030 .collect();
2031 props.insert("bootstrap.servers".to_string(), "visible-host:9092".to_string());
2032 props.insert("group.id".to_string(), "visible-group".to_string());
2033 props.insert("sasl.mechanism".to_string(), "PLAIN".to_string());
2034 props.insert("ssl.truststore.location".to_string(), "/visible/truststore.pem".to_string());
2035 let c = ConsumerConfig::new(&props).unwrap();
2036
2037 assert_eq!(c.sasl_config.resolve_password().as_deref(), Some("jaas-S3cr3t"));
2039 assert_eq!(
2040 c.ssl_config.keystore_key.as_ref().map(Password::value),
2041 Some("KEYSTORE-KEY-PEM")
2042 );
2043
2044 for rendered in [format!("{c:?}"), format!("{c:#?}")] {
2045 for (key, value) in secrets.iter().chain(&undefined) {
2046 assert!(!rendered.contains(value), "{key} leaked: {rendered}");
2047 }
2048 for fragment in ["jaas-S3cr3t", "jaas-user", "PlainLoginModule", "S3cr3t", "-PEM"] {
2049 assert!(!rendered.contains(fragment), "{fragment:?} leaked: {rendered}");
2050 }
2051 assert_eq!(rendered.matches(Password::HIDDEN).count(), secrets.len(), "{rendered}");
2052 for visible in [
2053 "visible-host:9092",
2054 "visible-group",
2055 "\"PLAIN\"",
2056 "/visible/truststore.pem",
2057 ] {
2058 assert!(rendered.contains(visible), "{visible} missing: {rendered}");
2059 }
2060 }
2061 }
2062
2063 #[test]
2065 fn test_sasl_jaas_config_empty_is_none() {
2066 let mut props = base_props();
2067 props.insert("sasl.jaas.config".to_string(), String::new());
2068 let c = ConsumerConfig::new(&props).unwrap();
2069 assert_eq!(c.sasl_config.jaas_config, None);
2070 }
2071
2072 #[test]
2074 fn test_ssl_config_from_properties() {
2075 let mut props = base_props();
2076 props.insert("ssl.truststore.location".to_string(), "/path/to/truststore.pem".to_string());
2077 props.insert("ssl.keystore.location".to_string(), "/path/to/keystore.pem".to_string());
2078 props.insert("ssl.endpoint.identification.algorithm".to_string(), String::new());
2079 let c = ConsumerConfig::new(&props).unwrap();
2080 assert_eq!(c.ssl_config.truststore_location.as_deref(), Some("/path/to/truststore.pem"));
2081 assert_eq!(c.ssl_config.keystore_location.as_deref(), Some("/path/to/keystore.pem"));
2082 assert_eq!(c.ssl_config.endpoint_identification_algorithm, "");
2083 }
2084
2085 #[test]
2095 fn test_builder_selection() {
2096 use crate::common::network::ChannelBuilders;
2097 use crate::common::utils::internals::LogContext;
2098
2099 let mut props = base_props();
2101 props.insert("security.protocol".to_string(), "SASL_PLAINTEXT".to_string());
2102 props.insert("sasl.mechanism".to_string(), "PLAIN".to_string());
2103 props.insert(
2104 "sasl.jaas.config".to_string(),
2105 "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"admin\" password=\"admin-secret\";"
2106 .to_string(),
2107 );
2108 let c = ConsumerConfig::new(&props).unwrap();
2109 assert_eq!(c.security_protocol, SecurityProtocol::SaslPlaintext);
2110 let builder = ChannelBuilders::client_channel_builder(
2111 c.security_protocol,
2112 Some(&c.ssl_config),
2113 Some(&c.sasl_config),
2114 None,
2115 c.client_id(),
2116 LogContext::new("[test] ".to_string()),
2117 );
2118 assert!(builder.is_ok(), "SASL_PLAINTEXT with valid config should build");
2119
2120 let mut props = base_props();
2123 props.insert("security.protocol".to_string(), "SASL_SSL".to_string());
2124 props.insert("sasl.mechanism".to_string(), "PLAIN".to_string());
2125 props.insert(
2126 "sasl.jaas.config".to_string(),
2127 "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"admin\" password=\"admin-secret\";"
2128 .to_string(),
2129 );
2130 let c = ConsumerConfig::new(&props).unwrap();
2131 let builder = ChannelBuilders::client_channel_builder(
2132 c.security_protocol,
2133 None, Some(&c.sasl_config),
2135 None,
2136 c.client_id(),
2137 LogContext::new("[test] ".to_string()),
2138 );
2139 let err = match builder {
2140 Ok(_) => panic!("SASL_SSL with no ssl_config should fail to build"),
2141 Err(e) => e,
2142 };
2143 let msg = format!("{}", err);
2144 let expected = if cfg!(feature = "rustls") {
2146 "ssl_config"
2147 } else {
2148 "the client was built without the `rustls` feature"
2149 };
2150 assert!(msg.contains(expected), "error should mention {expected}, got: {msg}");
2151 }
2152
2153 #[test]
2160 fn test_new_keeps_an_empty_group_id() {
2161 let mut props = base_props();
2162 props.insert("bootstrap.servers".to_string(), "host1:9092".to_string());
2163 props.insert("group.id".to_string(), String::new());
2164 let c = ConsumerConfig::new(&props).unwrap();
2165 assert_eq!(c.group_id(), Some(""), "the empty string must survive to the constructor");
2166 }
2167
2168 #[test]
2170 fn test_new_absent_group_id_is_none() {
2171 let mut props = base_props();
2172 props.insert("bootstrap.servers".to_string(), "host1:9092".to_string());
2173 let c = ConsumerConfig::new(&props).unwrap();
2174 assert_eq!(c.group_id(), None);
2175 }
2176
2177 #[test]
2182 fn test_bootstrap_servers_list_parsing() {
2183 for value in [
2184 "localhost:1,localhost:2",
2185 "localhost:1, localhost:2",
2186 " localhost:1 ,localhost:2 ",
2187 ] {
2188 let props = HashMap::from([("bootstrap.servers".to_string(), value.to_string())]);
2189 let config = ConsumerConfig::new(&props).unwrap();
2190 assert_eq!(
2191 config.bootstrap_servers(),
2192 ["localhost:1".to_string(), "localhost:2".to_string()],
2193 "{value:?}"
2194 );
2195 let addresses = crate::ClientUtils::parse_and_validate_addresses(
2196 config.bootstrap_servers(),
2197 config.client_dns_lookup(),
2198 )
2199 .unwrap();
2200 assert_eq!(addresses.len(), 2, "{value:?}");
2201 }
2202 }
2203
2204 #[test]
2209 fn test_bootstrap_servers_valid_list() {
2210 let error_message = |value: &str| {
2211 let props = HashMap::from([("bootstrap.servers".to_string(), value.to_string())]);
2212 match ConsumerConfig::new(&props) {
2213 Err(Error::Config(e)) => e.message().to_string(),
2214 other => panic!("expected a ConfigError for {value:?}, got {other:?}"),
2215 }
2216 };
2217 for value in ["localhost:9092,,localhost:9093", "a:1, ,b:1", "a:1,"] {
2218 assert_eq!(
2219 error_message(value),
2220 "Configuration 'bootstrap.servers' values must not be empty.",
2221 "{value:?}"
2222 );
2223 }
2224 let props = HashMap::from([("bootstrap.servers".to_string(), "a:1,a:1".to_string())]);
2226 assert_eq!(ConsumerConfig::new(&props).unwrap().bootstrap_servers(), ["a:1".to_string()]);
2227 assert_eq!(
2228 error_message(",,"),
2229 "Configuration 'bootstrap.servers' values must not be empty."
2230 );
2231 for value in ["", " "] {
2232 assert_eq!(
2233 error_message(value),
2234 "Configuration 'bootstrap.servers' must not be empty. Valid values include: any non-empty value",
2235 "{value:?}"
2236 );
2237 }
2238 let props = HashMap::from([("bootstrap.servers".to_string(), "a:1,b:1".to_string())]);
2239 assert_eq!(
2240 ConsumerConfig::new(&props).unwrap().bootstrap_servers(),
2241 ["a:1".to_string(), "b:1".to_string()]
2242 );
2243 }
2244
2245 fn props_with(extra: &[(&str, &str)]) -> HashMap<String, String> {
2247 let mut props = base_props();
2248 for (k, v) in extra {
2249 props.insert((*k).to_string(), (*v).to_string());
2250 }
2251 props
2252 }
2253
2254 fn sequence_suffix<'a>(client_id: &'a str, prefix: &str) -> &'a str {
2256 client_id
2257 .strip_prefix(prefix)
2258 .unwrap_or_else(|| panic!("{client_id:?} does not start with {prefix:?}"))
2259 }
2260
2261 #[test]
2264 fn test_generated_client_id_uses_group_id_and_sequence() {
2265 let props = props_with(&[(ConsumerConfig::GROUP_ID_CONFIG, "test-group")]);
2266 let first = ConsumerConfig::new(&props).unwrap();
2267 let second = ConsumerConfig::new(&props).unwrap();
2268 let n1: i32 = sequence_suffix(first.client_id(), "consumer-test-group-").parse().unwrap();
2269 let n2: i32 = sequence_suffix(second.client_id(), "consumer-test-group-").parse().unwrap();
2270 assert!(n1 >= 1, "{}", first.client_id());
2271 assert_ne!(first.client_id(), second.client_id());
2272 assert!(n2 > n1, "{} then {}", first.client_id(), second.client_id());
2275 }
2276
2277 #[test]
2282 fn test_copy_for_client_keeps_the_resolved_client_id() {
2283 let props = HashMap::from([
2284 (
2285 ConsumerConfig::BOOTSTRAP_SERVERS_CONFIG.to_string(),
2286 "localhost:9092".to_string(),
2287 ),
2288 (ConsumerConfig::GROUP_ID_CONFIG.to_string(), "copy-group".to_string()),
2289 ]);
2290 let first = ConsumerConfig::new(&props).unwrap();
2291 let copy = first.copy_for_client().unwrap();
2292 let second_copy = first.copy_for_client().unwrap();
2293 let next = ConsumerConfig::new(&props).unwrap();
2294 assert_eq!(copy.client_id(), first.client_id());
2295 assert_eq!(second_copy.client_id(), first.client_id());
2296 assert_eq!(
2297 copy.config.get_string(ConsumerConfig::CLIENT_ID_CONFIG).unwrap(),
2298 Some(first.client_id())
2299 );
2300 let n1: i32 = sequence_suffix(first.client_id(), "consumer-copy-group-").parse().unwrap();
2301 let n2: i32 = sequence_suffix(next.client_id(), "consumer-copy-group-").parse().unwrap();
2302 assert!(n2 > n1, "{} then {}", first.client_id(), next.client_id());
2304 assert_eq!(copy.unused(), first.unused());
2305 first.config.ignore(ConsumerConfig::BOOTSTRAP_SERVERS_CONFIG);
2306 assert!(copy.unused().contains(ConsumerConfig::BOOTSTRAP_SERVERS_CONFIG));
2307
2308 let explicit = HashMap::from([
2309 (
2310 ConsumerConfig::BOOTSTRAP_SERVERS_CONFIG.to_string(),
2311 "localhost:9092".to_string(),
2312 ),
2313 (ConsumerConfig::CLIENT_ID_CONFIG.to_string(), "mine".to_string()),
2314 ]);
2315 let config = ConsumerConfig::new(&explicit).unwrap();
2316 assert_eq!(config.copy_for_client().unwrap().client_id(), "mine");
2317 }
2318
2319 #[test]
2325 fn test_copy_for_client_logs_no_configuration_warning() {
2326 let props = props_with(&[
2327 (ConsumerConfig::BOOTSTRAP_SERVERS_CONFIG, "localhost:9092,localhost:9092"),
2328 (ConsumerConfig::GROUP_ID_CONFIG, "copy-log-group"),
2329 (ConsumerConfig::GROUP_PROTOCOL_CONFIG, "classic"),
2330 (ConsumerConfig::RETRY_BACKOFF_MS_CONFIG, "2000"),
2331 (ConsumerConfig::RETRY_BACKOFF_MAX_MS_CONFIG, "1000"),
2332 (ConsumerConfig::RECONNECT_BACKOFF_MS_CONFIG, "100"),
2333 (CommonClientConfigs::SOCKET_CONNECTION_SETUP_TIMEOUT_MS_CONFIG, "40000"),
2334 (CommonClientConfigs::SOCKET_CONNECTION_SETUP_TIMEOUT_MAX_MS_CONFIG, "30000"),
2335 (ConsumerConfig::CONNECTIONS_MAX_IDLE_MS_CONFIG, "1000"),
2336 (ConsumerConfig::MAX_POLL_INTERVAL_MS_CONFIG, "300000"),
2337 ]);
2338 let (config, new_lines) = capture_logs(|| ConsumerConfig::new(&props).unwrap());
2339 let warnings = |lines: &[(Level, String)]| {
2340 lines
2341 .iter()
2342 .filter(|(level, _)| *level == Level::Warn)
2343 .map(|(_, line)| line.split(' ').take(3).collect::<Vec<_>>().join(" "))
2344 .collect::<Vec<_>>()
2345 };
2346 assert_eq!(
2347 warnings(&new_lines),
2348 [
2349 "Configuration key \"bootstrap.servers\"",
2350 "Configuration 'retry.backoff.ms' with",
2351 "Configuration 'socket.connection.setup.timeout.ms' with",
2352 "Disabling exponential reconnect",
2353 "Configuration 'connections.max.idle.ms' with",
2354 ],
2355 "{new_lines:?}"
2356 );
2357 let (copy, copy_lines) = capture_logs(|| config.copy_for_client().unwrap());
2358 assert!(copy_lines.is_empty(), "{copy_lines:?}");
2359 assert_eq!(copy.unused(), ConsumerConfig::new(&props).unwrap().unused());
2360 }
2361
2362 #[test]
2364 fn test_generated_client_id_uses_group_instance_id() {
2365 let props = props_with(&[
2366 (ConsumerConfig::GROUP_ID_CONFIG, "test-group"),
2367 (ConsumerConfig::GROUP_INSTANCE_ID_CONFIG, "inst"),
2368 ]);
2369 assert_eq!(ConsumerConfig::new(&props).unwrap().client_id(), "consumer-test-group-inst");
2370 }
2371
2372 #[test]
2374 fn test_explicit_client_id_preserved() {
2375 let props = props_with(&[
2376 (ConsumerConfig::GROUP_ID_CONFIG, "test-group"),
2377 (ConsumerConfig::CLIENT_ID_CONFIG, "my-consumer"),
2378 ]);
2379 assert_eq!(ConsumerConfig::new(&props).unwrap().client_id(), "my-consumer");
2380 }
2381
2382 #[test]
2385 fn test_generated_client_id_without_group_id() {
2386 let config = ConsumerConfig::new(&base_props()).unwrap();
2387 let n: i32 = sequence_suffix(config.client_id(), "consumer-null-").parse().unwrap();
2388 assert!(n >= 1, "{}", config.client_id());
2389 }
2390
2391 #[test]
2394 fn test_generated_client_id_rejects_invalid_group_instance_id() {
2395 let props = props_with(&[
2396 (ConsumerConfig::GROUP_ID_CONFIG, "test-group"),
2397 (ConsumerConfig::GROUP_INSTANCE_ID_CONFIG, "bad/id"),
2398 ]);
2399 let err = ConsumerConfig::new(&props).unwrap_err();
2400 assert!(matches!(err, Error::InvalidConfiguration(_)), "got {err:?}");
2401 assert!(err.is_kafka_error(), "got {err:?}");
2402 assert_eq!(
2403 err.message(),
2404 "Group instance id is invalid: 'bad/id' contains one or more characters other than ASCII \
2405 alphanumerics, '.', '_' and '-'"
2406 );
2407 }
2408
2409 #[test]
2413 fn test_explicit_client_id_skips_group_instance_id_validation() {
2414 let props = props_with(&[
2415 (ConsumerConfig::CLIENT_ID_CONFIG, "my-consumer"),
2416 (ConsumerConfig::GROUP_INSTANCE_ID_CONFIG, "bad/id"),
2417 ]);
2418 assert_eq!(ConsumerConfig::new(&props).unwrap().client_id(), "my-consumer");
2419 }
2420
2421 #[test]
2424 fn test_client_id_is_trimmed() {
2425 let blank = props_with(&[
2426 (ConsumerConfig::GROUP_ID_CONFIG, "test-group"),
2427 (ConsumerConfig::CLIENT_ID_CONFIG, " "),
2428 ]);
2429 let config = ConsumerConfig::new(&blank).unwrap();
2430 sequence_suffix(config.client_id(), "consumer-test-group-");
2431 let padded = props_with(&[(ConsumerConfig::CLIENT_ID_CONFIG, " my-consumer ")]);
2432 assert_eq!(ConsumerConfig::new(&padded).unwrap().client_id(), "my-consumer");
2433 }
2434
2435 #[test]
2440 fn test_get_string() {
2441 let config = ConsumerConfig::new(&props_with(&[(ConsumerConfig::GROUP_ID_CONFIG, "test-group")])).unwrap();
2442 assert_eq!(Some("test-group"), config.get_string(ConsumerConfig::GROUP_ID_CONFIG).unwrap());
2443 assert_eq!(None, config.get_string(ConsumerConfig::GROUP_INSTANCE_ID_CONFIG).unwrap());
2444 let client_id = config.get_string(ConsumerConfig::CLIENT_ID_CONFIG).unwrap().unwrap();
2445 assert_eq!(config.client_id(), client_id);
2446 sequence_suffix(client_id, "consumer-test-group-");
2447
2448 let defaults: &[(&str, Option<&str>)] = &[
2450 (ConsumerConfig::GROUP_PROTOCOL_CONFIG, Some("classic")),
2451 (ConsumerConfig::GROUP_REMOTE_ASSIGNOR_CONFIG, None),
2452 (ConsumerConfig::CLIENT_DNS_LOOKUP_CONFIG, Some("use_all_dns_ips")),
2453 (ConsumerConfig::CLIENT_RACK_CONFIG, Some("")),
2454 (ConsumerConfig::AUTO_OFFSET_RESET_CONFIG, Some("latest")),
2455 (CommonClientConfigs::METADATA_RECOVERY_STRATEGY_CONFIG, Some("rebootstrap")),
2456 (
2457 ConsumerConfig::ISOLATION_LEVEL_CONFIG,
2458 Some(ConsumerConfig::DEFAULT_ISOLATION_LEVEL),
2459 ),
2460 (ConsumerConfig::METRICS_RECORDING_LEVEL_CONFIG, Some("INFO")),
2461 (ConsumerConfig::SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, Some("implicit")),
2462 (ConsumerConfig::SHARE_ACQUIRE_MODE_CONFIG, Some("BATCH_OPTIMIZED")),
2463 (CommonClientConfigs::SECURITY_PROTOCOL_CONFIG, Some("PLAINTEXT")),
2464 (SaslConfigs::SASL_MECHANISM, Some("GSSAPI")),
2465 (SslConfigs::SSL_TRUSTSTORE_LOCATION_CONFIG, None),
2466 (SslConfigs::SSL_TRUSTSTORE_TYPE_CONFIG, Some("PEM")),
2467 (SslConfigs::SSL_KEYSTORE_LOCATION_CONFIG, None),
2468 (SslConfigs::SSL_KEYSTORE_TYPE_CONFIG, Some("PEM")),
2469 (SslConfigs::SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG, Some("https")),
2470 ];
2471 for (key, expected) in defaults {
2472 assert_eq!(*expected, config.get_string(key).unwrap(), "for {key}");
2473 }
2474
2475 for key in [
2478 SaslConfigs::SASL_JAAS_CONFIG,
2479 SslConfigs::SSL_TRUSTSTORE_CERTIFICATES_CONFIG,
2480 SslConfigs::SSL_KEYSTORE_KEY_CONFIG,
2481 SslConfigs::SSL_KEYSTORE_CERTIFICATE_CHAIN_CONFIG,
2482 ] {
2483 assert_eq!(None, config.get_string(key).unwrap(), "for {key}");
2484 }
2485
2486 let set: &[(&str, &str, &str)] = &[
2488 (ConsumerConfig::GROUP_PROTOCOL_CONFIG, "consumer", "consumer"),
2489 (ConsumerConfig::GROUP_REMOTE_ASSIGNOR_CONFIG, " uniform ", "uniform"),
2490 (
2491 ConsumerConfig::CLIENT_DNS_LOOKUP_CONFIG,
2492 "resolve_canonical_bootstrap_servers_only",
2493 "resolve_canonical_bootstrap_servers_only",
2494 ),
2495 (ConsumerConfig::CLIENT_RACK_CONFIG, " rack-1 ", "rack-1"),
2496 (ConsumerConfig::AUTO_OFFSET_RESET_CONFIG, "earliest", "earliest"),
2497 (CommonClientConfigs::METADATA_RECOVERY_STRATEGY_CONFIG, "none", "none"),
2498 (ConsumerConfig::ISOLATION_LEVEL_CONFIG, "read_uncommitted", "read_uncommitted"),
2499 (ConsumerConfig::METRICS_RECORDING_LEVEL_CONFIG, "DEBUG", "DEBUG"),
2500 (CommonClientConfigs::SECURITY_PROTOCOL_CONFIG, " sasl_ssl ", "sasl_ssl"),
2501 (SaslConfigs::SASL_MECHANISM, " PLAIN ", "PLAIN"),
2502 (SslConfigs::SSL_TRUSTSTORE_LOCATION_CONFIG, " /trust.pem ", "/trust.pem"),
2503 (SslConfigs::SSL_TRUSTSTORE_TYPE_CONFIG, "JKS", "JKS"),
2504 (SslConfigs::SSL_KEYSTORE_LOCATION_CONFIG, "/key.pem", "/key.pem"),
2505 (SslConfigs::SSL_KEYSTORE_TYPE_CONFIG, "PKCS12", "PKCS12"),
2506 (SslConfigs::SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG, "", ""),
2507 ];
2508 for (key, value, expected) in set {
2509 let mut extra = vec![(*key, *value)];
2510 if *key == ConsumerConfig::GROUP_REMOTE_ASSIGNOR_CONFIG {
2512 extra.push((ConsumerConfig::GROUP_PROTOCOL_CONFIG, "consumer"));
2513 }
2514 if *key == CommonClientConfigs::SECURITY_PROTOCOL_CONFIG {
2516 extra.push((SaslConfigs::SASL_MECHANISM, "PLAIN"));
2517 }
2518 let config = ConsumerConfig::new(&props_with(&extra)).unwrap();
2519 assert_eq!(Some(*expected), config.get_string(key).unwrap(), "for {key}={value:?}");
2520 }
2521
2522 let other_types: &[(&str, &str)] = &[
2525 (ConsumerConfig::MAX_POLL_RECORDS_CONFIG, "INT"),
2526 (ConsumerConfig::METADATA_MAX_AGE_CONFIG, "LONG"),
2527 (ConsumerConfig::ENABLE_AUTO_COMMIT_CONFIG, "BOOLEAN"),
2528 (ConsumerConfig::BOOTSTRAP_SERVERS_CONFIG, "LIST"),
2529 (SslConfigs::SSL_KEYSTORE_KEY_CONFIG, "PASSWORD"),
2530 (SaslConfigs::SASL_JAAS_CONFIG, "PASSWORD"),
2531 ];
2532 let passwords = ConsumerConfig::new(&props_with(&[
2533 (SslConfigs::SSL_KEYSTORE_KEY_CONFIG, "secret"),
2534 (SaslConfigs::SASL_JAAS_CONFIG, "x required;"),
2535 ]))
2536 .unwrap();
2537 for (key, config_type) in other_types {
2538 let config = if *config_type == "PASSWORD" {
2539 &passwords
2540 } else {
2541 &config
2542 };
2543 match config.get_string(key) {
2544 Err(e @ Error::LocalIllegalArgument(_)) => {
2545 assert_eq!(
2546 format!("Configuration '{key}' is of type {config_type}, not STRING"),
2547 e.message()
2548 );
2549 assert!(!e.is_kafka_error());
2550 },
2551 other => panic!("expected an IllegalArgumentError for {key}, got {other:?}"),
2552 }
2553 }
2554
2555 match config.get_string("no.such.key") {
2556 Err(Error::Config(e)) => assert_eq!("Unknown configuration 'no.such.key'", e.message()),
2557 other => panic!("expected a ConfigError, got {other:?}"),
2558 }
2559 }
2560
2561 #[test]
2564 fn test_client_rack_is_trimmed_and_defaults_to_empty() {
2565 assert_eq!("client.rack", ConsumerConfig::CLIENT_RACK_CONFIG);
2566 assert_eq!("", ConsumerConfig::DEFAULT_CLIENT_RACK);
2567 assert_eq!(
2568 ConsumerConfig::DEFAULT_CLIENT_RACK,
2569 ConsumerConfig::new(&props_with(&[])).unwrap().client_rack()
2570 );
2571 let padded = props_with(&[(ConsumerConfig::CLIENT_RACK_CONFIG, " rack-1 ")]);
2572 assert_eq!("rack-1", ConsumerConfig::new(&padded).unwrap().client_rack());
2573 let blank = props_with(&[(ConsumerConfig::CLIENT_RACK_CONFIG, " ")]);
2574 assert_eq!("", ConsumerConfig::new(&blank).unwrap().client_rack());
2575 }
2576
2577 #[test]
2581 fn test_blank_group_instance_id_is_config_error() {
2582 let props = props_with(&[
2583 (ConsumerConfig::GROUP_ID_CONFIG, "test-group"),
2584 (ConsumerConfig::GROUP_INSTANCE_ID_CONFIG, " "),
2585 ]);
2586 let err = ConsumerConfig::new(&props).unwrap_err();
2587 assert!(matches!(err, Error::Config(_)), "got {err:?}");
2588 assert!(
2589 err.message().contains(ConsumerConfig::GROUP_INSTANCE_ID_CONFIG),
2590 "{}",
2591 err.message()
2592 );
2593 assert!(err.message().contains("String must be non-empty"), "{}", err.message());
2594
2595 let padded = props_with(&[(ConsumerConfig::GROUP_INSTANCE_ID_CONFIG, " inst ")]);
2596 assert_eq!(ConsumerConfig::new(&padded).unwrap().group_instance_id(), Some("inst"));
2597 }
2598
2599 #[test]
2602 fn test_group_id_is_trimmed() {
2603 let config = ConsumerConfig::new(&props_with(&[(ConsumerConfig::GROUP_ID_CONFIG, " g ")])).unwrap();
2604 assert_eq!(config.group_id(), Some("g"));
2605 sequence_suffix(config.client_id(), "consumer-g-");
2606 let config = ConsumerConfig::new(&props_with(&[(ConsumerConfig::GROUP_ID_CONFIG, " ")])).unwrap();
2609 assert_eq!(config.group_id(), Some(""));
2610 }
2611
2612 #[test]
2637 #[doc(
2638 alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#ensureDefaultThrowOnUnsupportedStableFlagToFalse"
2639 )]
2640 fn ensure_default_return_on_unsupported_stable_flag_to_false() {
2641 let config = ConsumerConfig::new(&base_props()).unwrap();
2642 assert!(!config.throw_on_fetch_stable_offset_unsupported());
2643 }
2644
2645 #[test]
2651 fn test_partition_assignment_strategy_is_unknown() {
2652 let mut props = base_props();
2653 props.insert(
2654 ConsumerConfig::PARTITION_ASSIGNMENT_STRATEGY_CONFIG.to_string(),
2655 "org.apache.kafka.clients.consumer.RangeAssignor".to_string(),
2656 );
2657 let config = ConsumerConfig::new(&props).unwrap();
2658 match config.get_string(ConsumerConfig::PARTITION_ASSIGNMENT_STRATEGY_CONFIG) {
2659 Err(Error::Config(e)) => {
2660 assert_eq!("Unknown configuration 'partition.assignment.strategy'", e.message())
2661 },
2662 other => panic!("expected a ConfigError, got {other:?}"),
2663 }
2664 }
2665
2666 #[test]
2669 fn test_security_providers_is_unknown() {
2670 let mut props = base_props();
2671 props.insert("security.providers".to_string(), "my.Provider".to_string());
2672 let config = ConsumerConfig::new(&props).unwrap();
2673 match config.get_string("security.providers") {
2674 Err(Error::Config(e)) => assert_eq!("Unknown configuration 'security.providers'", e.message()),
2675 other => panic!("expected a ConfigError, got {other:?}"),
2676 }
2677 }
2678
2679 #[test]
2681 #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#testOverrideClientId")]
2682 fn test_override_client_id() {
2683 let mut props = base_props();
2684 props.insert(ConsumerConfig::GROUP_ID_CONFIG.to_string(), "test-group".to_string());
2685 let config = ConsumerConfig::new(&props).unwrap();
2686 assert!(!config.client_id().is_empty());
2687 }
2688
2689 #[test]
2692 #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#testInvalidGroupInstanceId")]
2693 fn test_invalid_group_instance_id() {
2694 let mut props = base_props();
2695 props.insert(ConsumerConfig::GROUP_INSTANCE_ID_CONFIG.to_string(), String::new());
2696 let err = ConsumerConfig::new(&props).unwrap_err();
2697 assert!(
2698 err.message().contains(ConsumerConfig::GROUP_INSTANCE_ID_CONFIG),
2699 "error message should mention the failing config key, got: {}",
2700 err.message()
2701 );
2702 }
2703
2704 #[test]
2709 #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#testDefaultConsumerGroupConfig")]
2710 fn test_default_consumer_group_config() {
2711 let config = ConsumerConfig::new(&base_props()).unwrap();
2712 assert_eq!(config.group_protocol(), "classic");
2713 assert_eq!(config.group_remote_assignor(), None);
2714 }
2715
2716 #[test]
2718 #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#testRemoteAssignorConfig")]
2719 fn test_remote_assignor_config() {
2720 let mut props = base_props();
2721 let remote_assignor_name = "SomeAssignor";
2722 let protocol = "consumer";
2723 props.insert(
2724 ConsumerConfig::GROUP_REMOTE_ASSIGNOR_CONFIG.to_string(),
2725 remote_assignor_name.to_string(),
2726 );
2727 props.insert(ConsumerConfig::GROUP_PROTOCOL_CONFIG.to_string(), protocol.to_string());
2728 let config = ConsumerConfig::new(&props).unwrap();
2729 assert_eq!(config.group_protocol(), protocol);
2730 assert_eq!(config.group_remote_assignor(), Some(remote_assignor_name));
2731 }
2732
2733 #[test]
2735 #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#testDefaultMetadataRecoveryStrategy")]
2736 fn test_default_metadata_recovery_strategy() {
2737 let config = ConsumerConfig::new(&base_props()).unwrap();
2738 assert_eq!(config.metadata_recovery_strategy(), "rebootstrap");
2739 }
2740
2741 #[test]
2743 #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#testInvalidMetadataRecoveryStrategy")]
2744 fn test_invalid_metadata_recovery_strategy() {
2745 let mut props = base_props();
2746 props.insert(
2747 CommonClientConfigs::METADATA_RECOVERY_STRATEGY_CONFIG.to_string(),
2748 "abc".to_string(),
2749 );
2750 let err = ConsumerConfig::new(&props).unwrap_err();
2751 assert!(
2752 err.message().contains(CommonClientConfigs::METADATA_RECOVERY_STRATEGY_CONFIG),
2753 "error message should mention the failing config key, got: {}",
2754 err.message()
2755 );
2756 }
2757
2758 #[test]
2762 #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#testProtocolConfigValidation")]
2763 fn test_protocol_config_validation() {
2764 let cases: &[(&str, bool)] = &[
2765 ("consumer", true),
2766 ("classic", true),
2767 ("Consumer", true),
2768 ("Classic", true),
2769 ("invalid", false),
2770 ];
2771 for (protocol, is_valid) in cases {
2772 let mut props = base_props();
2773 props.insert(ConsumerConfig::GROUP_PROTOCOL_CONFIG.to_string(), protocol.to_string());
2774 if *is_valid {
2775 let config = ConsumerConfig::new(&props).unwrap();
2776 assert_eq!(config.group_protocol(), *protocol);
2777 } else {
2778 let err = ConsumerConfig::new(&props).unwrap_err();
2779 assert!(
2780 err.message().contains(ConsumerConfig::GROUP_PROTOCOL_CONFIG),
2781 "case {protocol}: {}",
2782 err.message()
2783 );
2784 }
2785 }
2786 }
2787 #[test]
2789 #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#testOverrideEnableAutoCommit")]
2790 fn test_override_enable_auto_commit() {
2791 let enable_auto_commit =
2792 |config: &ConsumerConfig| config.get_boolean(ConsumerConfig::ENABLE_AUTO_COMMIT_CONFIG).unwrap();
2793 let mut props = base_props();
2795 let config = ConsumerConfig::new(&props).unwrap();
2796 assert_eq!(Some(false), enable_auto_commit(&config));
2797 assert!(!config.enable_auto_commit());
2798
2799 props.insert(ConsumerConfig::ENABLE_AUTO_COMMIT_CONFIG.to_string(), "false".to_string());
2801 assert_eq!(Some(false), enable_auto_commit(&ConsumerConfig::new(&props).unwrap()));
2802
2803 props.insert(ConsumerConfig::ENABLE_AUTO_COMMIT_CONFIG.to_string(), "true".to_string());
2805 let error = ConsumerConfig::new(&props).unwrap_err();
2806 assert!(error.is_invalid_configuration_error(), "{error:?}");
2807 assert_eq!(
2808 "enable.auto.commit cannot be set to true when default group id (null) is used.",
2809 error.message()
2810 );
2811
2812 props.insert(ConsumerConfig::GROUP_ID_CONFIG.to_string(), "test-group".to_string());
2814 let config = ConsumerConfig::new(&props).unwrap();
2815 assert_eq!(Some(true), enable_auto_commit(&config));
2816 assert!(config.enable_auto_commit());
2817
2818 props.remove(ConsumerConfig::ENABLE_AUTO_COMMIT_CONFIG);
2820 assert_eq!(Some(true), enable_auto_commit(&ConsumerConfig::new(&props).unwrap()));
2821 }
2822
2823 #[test]
2825 #[doc(alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#testRemoteAssignorWithClassicGroupProtocol")]
2826 fn test_remote_assignor_with_classic_group_protocol() {
2827 let props = props_with(&[(ConsumerConfig::GROUP_REMOTE_ASSIGNOR_CONFIG, "SomeAssignor")]);
2828 let error = ConsumerConfig::new(&props).unwrap_err();
2829 assert!(matches!(error, Error::Config(_)), "{error:?}");
2830 assert!(
2831 error
2832 .message()
2833 .contains("group.remote.assignor cannot be set when group.protocol=CLASSIC"),
2834 "{}",
2835 error.message()
2836 );
2837 }
2838
2839 #[test]
2841 #[doc(
2842 alias = "org.apache.kafka.clients.consumer.ConsumerConfigTest#testUnsupportedConfigsWithConsumerGroupProtocol"
2843 )]
2844 fn test_unsupported_configs_with_consumer_group_protocol() {
2845 for (config_name, value) in [
2846 (ConsumerConfig::PARTITION_ASSIGNMENT_STRATEGY_CONFIG, "RoundRobinAssignor"),
2847 (ConsumerConfig::HEARTBEAT_INTERVAL_MS_CONFIG, "1000"),
2848 (ConsumerConfig::SESSION_TIMEOUT_MS_CONFIG, "30000"),
2849 ] {
2850 let props = props_with(&[
2851 (ConsumerConfig::GROUP_PROTOCOL_CONFIG, "CONSUMER"),
2852 (config_name, value),
2853 ]);
2854 let error = ConsumerConfig::new(&props).unwrap_err();
2855 assert!(matches!(error, Error::Config(_)), "{error:?}");
2856 assert_eq!(
2857 format!("{config_name} cannot be set when group.protocol=CONSUMER"),
2858 error.message()
2859 );
2860 }
2861 }
2862
2863 #[test]
2868 fn test_unsupported_configs_are_listed_in_order() {
2869 let props = props_with(&[
2870 (ConsumerConfig::SHARE_ACQUIRE_MODE_CONFIG, "record_limit"),
2871 (ConsumerConfig::SHARE_ACKNOWLEDGEMENT_MODE_CONFIG, "explicit"),
2872 (ConsumerConfig::GROUP_REMOTE_ASSIGNOR_CONFIG, " "),
2873 ]);
2874 assert_eq!(
2875 "share.acknowledgement.mode, share.acquire.mode cannot be set when group.protocol=CLASSIC",
2876 ConsumerConfig::new(&props).unwrap_err().message()
2877 );
2878 let mut props = props;
2879 props.insert(ConsumerConfig::GROUP_PROTOCOL_CONFIG.to_string(), "consumer".to_string());
2880 props.insert(ConsumerConfig::SESSION_TIMEOUT_MS_CONFIG.to_string(), "30000".to_string());
2881 assert_eq!(
2882 "session.timeout.ms, share.acknowledgement.mode, share.acquire.mode cannot be set when \
2883 group.protocol=CONSUMER",
2884 ConsumerConfig::new(&props).unwrap_err().message()
2885 );
2886 let config = ConsumerConfig::new(&props_with(&[(ConsumerConfig::GROUP_REMOTE_ASSIGNOR_CONFIG, " ")])).unwrap();
2888 assert!(!config.unused().contains(ConsumerConfig::GROUP_REMOTE_ASSIGNOR_CONFIG));
2889 }
2890
2891 #[test]
2896 fn test_share_mode_validators() {
2897 for group_protocol in ["classic", "consumer"] {
2898 for (config_name, value, expected) in [
2899 (
2900 ConsumerConfig::SHARE_ACQUIRE_MODE_CONFIG,
2901 "",
2902 "Invalid value for configuration share.acquire.mode: Invalid value `` for configuration \
2903 share.acquire.mode. The value must either be 'batch_optimized' or 'record_limit'.",
2904 ),
2905 (
2906 ConsumerConfig::SHARE_ACQUIRE_MODE_CONFIG,
2907 " ",
2908 "Invalid value for configuration share.acquire.mode: Invalid value `` for configuration \
2909 share.acquire.mode. The value must either be 'batch_optimized' or 'record_limit'.",
2910 ),
2911 (
2912 ConsumerConfig::SHARE_ACQUIRE_MODE_CONFIG,
2913 "invalid",
2914 "Invalid value invalid for configuration share.acquire.mode: Invalid value `invalid` for \
2915 configuration share.acquire.mode. The value must either be 'batch_optimized' or 'record_limit'.",
2916 ),
2917 (
2918 ConsumerConfig::SHARE_ACKNOWLEDGEMENT_MODE_CONFIG,
2919 "",
2920 "Invalid value for configuration share.acknowledgement.mode: Invalid value `` for \
2921 configuration share.acknowledgement.mode. The value must either be 'implicit' or 'explicit'.",
2922 ),
2923 (
2924 ConsumerConfig::SHARE_ACKNOWLEDGEMENT_MODE_CONFIG,
2925 "IMPLICIT",
2926 "Invalid value IMPLICIT for configuration share.acknowledgement.mode: Invalid value `IMPLICIT` \
2927 for configuration share.acknowledgement.mode. The value must either be 'implicit' or \
2928 'explicit'.",
2929 ),
2930 ] {
2931 let error = ConsumerConfig::new(&props_with(&[
2932 (ConsumerConfig::GROUP_PROTOCOL_CONFIG, group_protocol),
2933 (config_name, value),
2934 ]))
2935 .unwrap_err();
2936 assert!(matches!(error, Error::Config(_)), "{error:?}");
2937 assert_eq!(expected, error.message(), "{group_protocol} {config_name}={value:?}");
2938 }
2939 }
2940 for value in ["record_limit", "Batch_Optimized", "BATCH_OPTIMIZED"] {
2942 assert_eq!(
2943 "share.acquire.mode cannot be set when group.protocol=CLASSIC",
2944 ConsumerConfig::new(&props_with(&[(ConsumerConfig::SHARE_ACQUIRE_MODE_CONFIG, value)]))
2945 .unwrap_err()
2946 .message()
2947 );
2948 }
2949 }
2950
2951 #[test]
2955 fn test_group_protocol_validator_message() {
2956 let error =
2957 ConsumerConfig::new(&props_with(&[(ConsumerConfig::GROUP_PROTOCOL_CONFIG, "invalid")])).unwrap_err();
2958 assert_eq!(
2959 "Invalid value invalid for configuration group.protocol: String must be one of (case insensitive): \
2960 CONSUMER, CLASSIC",
2961 error.message()
2962 );
2963 }
2964
2965 #[test]
2968 fn test_at_least_validators() {
2969 let cases: &[(&str, &str, &str)] = &[
2970 (ConsumerConfig::BOOTSTRAP_RESOLVE_TIMEOUT_MS_CONFIG, "-1", "0"),
2971 (ConsumerConfig::METADATA_MAX_AGE_CONFIG, "-1", "0"),
2972 (ConsumerConfig::AUTO_COMMIT_INTERVAL_MS_CONFIG, "-1", "0"),
2973 (ConsumerConfig::MAX_PARTITION_FETCH_BYTES_CONFIG, "-1", "0"),
2974 (ConsumerConfig::SEND_BUFFER_CONFIG, "-2", "-1"),
2975 (ConsumerConfig::RECEIVE_BUFFER_CONFIG, "-2", "-1"),
2976 (ConsumerConfig::FETCH_MIN_BYTES_CONFIG, "-1", "0"),
2977 (ConsumerConfig::FETCH_MAX_BYTES_CONFIG, "-1", "0"),
2978 (ConsumerConfig::FETCH_MAX_WAIT_MS_CONFIG, "-1", "0"),
2979 (ConsumerConfig::RECONNECT_BACKOFF_MS_CONFIG, "-1", "0"),
2980 (ConsumerConfig::RECONNECT_BACKOFF_MAX_MS_CONFIG, "-1", "0"),
2981 (ConsumerConfig::RETRY_BACKOFF_MS_CONFIG, "-1", "0"),
2982 (ConsumerConfig::RETRY_BACKOFF_MAX_MS_CONFIG, "-1", "0"),
2983 (ConsumerConfig::METRICS_SAMPLE_WINDOW_MS_CONFIG, "-1", "0"),
2984 (ConsumerConfig::METRICS_NUM_SAMPLES_CONFIG, "0", "1"),
2985 (ConsumerConfig::REQUEST_TIMEOUT_MS_CONFIG, "-1", "0"),
2986 (ConsumerConfig::DEFAULT_API_TIMEOUT_MS_CONFIG, "-1", "0"),
2987 (ConsumerConfig::MAX_POLL_RECORDS_CONFIG, "0", "1"),
2988 (ConsumerConfig::MAX_POLL_INTERVAL_MS_CONFIG, "0", "1"),
2989 (CommonClientConfigs::METADATA_RECOVERY_REBOOTSTRAP_TRIGGER_MS_CONFIG, "-1", "0"),
2990 ];
2991 for (key, value, bound) in cases {
2992 let error = ConsumerConfig::new(&props_with(&[(key, value)])).unwrap_err();
2993 assert!(matches!(error, Error::Config(_)), "{key}: {error:?}");
2994 assert_eq!(
2995 format!("Invalid value {value} for configuration {key}: Value must be at least {bound}"),
2996 error.message()
2997 );
2998 ConsumerConfig::new(&props_with(&[(key, bound)])).unwrap_or_else(|e| panic!("{key}={bound}: {e:?}"));
3000 }
3001 }
3002
3003 #[test]
3005 fn test_isolation_level_is_case_sensitive() {
3006 let error = ConsumerConfig::new(&props_with(&[(ConsumerConfig::ISOLATION_LEVEL_CONFIG, "READ_COMMITTED")]))
3007 .unwrap_err();
3008 assert_eq!(
3009 "Invalid value READ_COMMITTED for configuration isolation.level: String must be one of: read_committed, \
3010 read_uncommitted",
3011 error.message()
3012 );
3013 }
3014
3015 #[test]
3017 fn test_connections_max_idle_ms_lower_than_max_poll_interval_ms_only_warns() {
3018 let config = ConsumerConfig::new(&props_with(&[
3019 (ConsumerConfig::CONNECTIONS_MAX_IDLE_MS_CONFIG, "1000"),
3020 (ConsumerConfig::MAX_POLL_INTERVAL_MS_CONFIG, "2000"),
3021 ]))
3022 .unwrap();
3023 assert_eq!(1000, config.connections_max_idle_ms);
3024 }
3025
3026 #[test]
3028 fn test_typed_getters() {
3029 let config = ConsumerConfig::new(&props_with(&[
3030 (ConsumerConfig::GROUP_ID_CONFIG, "g"),
3031 (ConsumerConfig::MAX_POLL_RECORDS_CONFIG, "7"),
3032 ]))
3033 .unwrap();
3034 assert_eq!(Some(7), config.get_int(ConsumerConfig::MAX_POLL_RECORDS_CONFIG).unwrap());
3035 assert_eq!(
3036 Some(540_000),
3037 config.get_long(ConsumerConfig::CONNECTIONS_MAX_IDLE_MS_CONFIG).unwrap()
3038 );
3039 assert_eq!(Some(true), config.get_boolean(ConsumerConfig::CHECK_CRCS_CONFIG).unwrap());
3040 assert_eq!(
3041 Some(&["localhost:9092".to_string()][..]),
3042 config.get_list(ConsumerConfig::BOOTSTRAP_SERVERS_CONFIG).unwrap()
3043 );
3044 assert_eq!(Some("g"), config.get_string(ConsumerConfig::GROUP_ID_CONFIG).unwrap());
3045 assert_eq!(
3046 "Configuration 'max.poll.records' is of type INT, not LONG",
3047 config.get_long(ConsumerConfig::MAX_POLL_RECORDS_CONFIG).unwrap_err().message()
3048 );
3049 assert_eq!(
3050 "Configuration 'check.crcs' is of type BOOLEAN, not SHORT",
3051 config.get_short(ConsumerConfig::CHECK_CRCS_CONFIG).unwrap_err().message()
3052 );
3053 assert_eq!(
3054 "Configuration 'group.id' is of type STRING, not DOUBLE",
3055 config.get_double(ConsumerConfig::GROUP_ID_CONFIG).unwrap_err().message()
3056 );
3057 assert_eq!(
3058 "Unknown configuration 'no.such.key'",
3059 config.get_int("no.such.key").unwrap_err().message()
3060 );
3061 }
3062
3063 #[test]
3065 fn test_config_names() {
3066 let names = ConsumerConfig::config_names();
3067 for key in [
3068 ConsumerConfig::BOOTSTRAP_SERVERS_CONFIG,
3069 ConsumerConfig::GROUP_PROTOCOL_CONFIG,
3070 ConsumerConfig::THROW_ON_FETCH_STABLE_OFFSET_UNSUPPORTED,
3071 ConsumerConfig::SHARE_ACQUIRE_MODE_CONFIG,
3072 SaslConfigs::SASL_MECHANISM,
3073 SslConfigs::SSL_KEYSTORE_TYPE_CONFIG,
3074 "ssl.cipher.suites",
3075 ] {
3076 assert!(names.contains(key), "{key}");
3077 }
3078 for key in [
3079 ConsumerConfig::PARTITION_ASSIGNMENT_STRATEGY_CONFIG,
3080 "security.providers",
3081 "config.providers",
3082 "enable.metrics.push",
3083 "ssl.keystore.password",
3084 "ssl.key.password",
3085 "ssl.truststore.password",
3086 "key.deserializer",
3087 "metric.reporters",
3088 "interceptor.classes",
3089 ] {
3090 assert!(!names.contains(key), "{key}");
3091 }
3092 }
3093
3094 #[test]
3097 fn test_unused_and_ignore() {
3098 let config = ConsumerConfig::new(&props_with(&[
3099 (ConsumerConfig::GROUP_ID_CONFIG, "g"),
3100 (ConsumerConfig::FETCH_MIN_BYTES_CONFIG, "2"),
3101 (SslConfigs::SSL_KEYSTORE_TYPE_CONFIG, "PKCS12"),
3102 ("unknown.key", "x"),
3103 ]))
3104 .unwrap();
3105 assert_eq!(
3107 HashSet::from([
3108 ConsumerConfig::BOOTSTRAP_SERVERS_CONFIG.to_string(),
3109 ConsumerConfig::FETCH_MIN_BYTES_CONFIG.to_string(),
3110 SslConfigs::SSL_KEYSTORE_TYPE_CONFIG.to_string(),
3111 "unknown.key".to_string(),
3112 ]),
3113 config.unused()
3114 );
3115 config.get_int(ConsumerConfig::FETCH_MIN_BYTES_CONFIG).unwrap();
3116 config.ignore("unknown.key");
3117 config.mark_read_keys_used();
3119 assert_eq!(
3120 HashSet::from([SslConfigs::SSL_KEYSTORE_TYPE_CONFIG.to_string()]),
3121 config.unused()
3122 );
3123 config.log_unused();
3124 }
3125
3126 #[test]
3129 fn test_enable_metrics_push_is_unused() {
3130 let config = ConsumerConfig::new(&props_with(&[("enable.metrics.push", "true")])).unwrap();
3131 config.mark_read_keys_used();
3132 assert_eq!(HashSet::from(["enable.metrics.push".to_string()]), config.unused());
3133 assert_eq!(
3134 "Unknown configuration 'enable.metrics.push'",
3135 config.get_boolean("enable.metrics.push").unwrap_err().message()
3136 );
3137 }
3138
3139 #[test]
3141 fn test_equals_and_hash() {
3142 use std::collections::hash_map::DefaultHasher;
3143 let hash = |config: &ConsumerConfig| {
3144 let mut hasher = DefaultHasher::new();
3145 config.hash(&mut hasher);
3146 hasher.finish()
3147 };
3148 let a = ConsumerConfig::new(&props_with(&[(ConsumerConfig::CLIENT_ID_CONFIG, "c")])).unwrap();
3149 let b = ConsumerConfig::new(&props_with(&[(ConsumerConfig::CLIENT_ID_CONFIG, "c")])).unwrap();
3150 let c = ConsumerConfig::new(&props_with(&[(ConsumerConfig::CLIENT_ID_CONFIG, "d")])).unwrap();
3151 assert_eq!(a, b);
3152 assert_eq!(hash(&a), hash(&b));
3153 assert_ne!(a, c);
3154 }
3155}