1use std::any::{Any, type_name};
46use std::collections::{HashMap, HashSet};
47use std::hash::{Hash, Hasher};
48use std::sync::LazyLock;
49use std::sync::atomic::{self, AtomicI32};
50
51use log::info;
52
53use crate::MetadataRecoveryStrategy;
54use crate::common::Error;
55use crate::common::compress::Compression;
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, Validator, config_error,
60};
61use crate::common::config::{SaslConfigs, SslConfigs};
62use crate::common::record::internal::CompressionType;
63use crate::common::security::auth::SecurityProtocol;
64use crate::producer::internals::KeyHasher;
65use crate::producer::{Partitioner, RoundRobinPartitioner};
66use crate::{ClientDnsLookup, CommonClientConfigs};
67
68static PRODUCER_CLIENT_ID_SEQUENCE: AtomicI32 = AtomicI32::new(1);
75
76const MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION_FOR_IDEMPOTENCE: i32 = 5;
79
80fn partitioner_type_validator() -> LambdaValidator {
83 LambdaValidator::with(
84 |name, value| {
85 let Some(value) = value else {
86 return Ok(());
87 };
88 let partitioner_type = value.to_string();
89 if [
90 ProducerConfig::CONSISTENT_RANDOM_PARTITIONER,
91 ProducerConfig::MURMUR2_RANDOM_PARTITIONER,
92 ProducerConfig::ROUND_ROBIN_PARTITIONER,
93 ]
94 .contains(&partitioner_type.as_str())
95 {
96 return Ok(());
97 }
98 Err(config_error(
99 name,
100 Some(value),
101 format!(
102 "Partitioner type {partitioner_type} is not supported; the supported types are {}, {} and {}.",
103 ProducerConfig::CONSISTENT_RANDOM_PARTITIONER,
104 ProducerConfig::MURMUR2_RANDOM_PARTITIONER,
105 ProducerConfig::ROUND_ROBIN_PARTITIONER
106 ),
107 ))
108 },
109 || {
110 format!(
111 "[{}, {}, {}]",
112 ProducerConfig::CONSISTENT_RANDOM_PARTITIONER,
113 ProducerConfig::MURMUR2_RANDOM_PARTITIONER,
114 ProducerConfig::ROUND_ROBIN_PARTITIONER
115 )
116 },
117 )
118}
119
120fn level_validator(compression_type: CompressionType) -> Box<dyn Validator> {
122 compression_type
123 .level_validator()
124 .expect("gzip, lz4 and zstd have compression levels")
125}
126
127fn default_level(compression_type: CompressionType) -> i32 {
129 compression_type
130 .default_level()
131 .expect("gzip, lz4 and zstd have compression levels")
132}
133
134static CONFIG: LazyLock<ConfigDef> = LazyLock::new(|| {
135 ConfigDef::new()
136 .define_with_validator(
137 ProducerConfig::BOOTSTRAP_SERVERS_CONFIG,
138 Type::List,
139 ConfigDef::NO_DEFAULT_VALUE,
140 ValidList::any_non_duplicate_values(false, false),
141 Importance::High,
142 )
143 .define_with_validator(
144 ProducerConfig::CLIENT_DNS_LOOKUP_CONFIG,
145 Type::String,
146 ClientDnsLookup::UseAllDnsIps.config_value(),
147 ValidString::r#in(&[
148 ClientDnsLookup::UseAllDnsIps.config_value(),
149 ClientDnsLookup::ResolveCanonicalBootstrapServersOnly.config_value(),
150 ]),
151 Importance::Medium,
152 )
153 .define_with_validator(
154 ProducerConfig::BUFFER_MEMORY_CONFIG,
155 Type::Long,
156 32 * 1024 * 1024_i64,
157 Range::at_least(0_i64),
158 Importance::High,
159 )
160 .define_internal_with_validator(
162 ProducerConfig::BUFFER_MEMORY_ALLOCATION_STRATEGY_CONFIG,
163 Type::String,
164 ProducerConfig::BUFFER_MEMORY_ALLOCATION_STRATEGY_FULL,
165 CaseInsensitiveValidString::r#in(&[
166 ProducerConfig::BUFFER_MEMORY_ALLOCATION_STRATEGY_FULL,
167 ProducerConfig::BUFFER_MEMORY_ALLOCATION_STRATEGY_INCREMENTAL,
168 ]),
169 Importance::Medium,
170 )
171 .define_with_validator(
172 ProducerConfig::RETRIES_CONFIG,
173 Type::Int,
174 i32::MAX,
175 Range::between(0, i32::MAX),
176 Importance::High,
177 )
178 .define_with_validator(
179 ProducerConfig::ACKS_CONFIG,
180 Type::String,
181 "all",
182 ValidString::r#in(&["all", "-1", "0", "1"]),
183 Importance::Low,
184 )
185 .define_with_validator(
186 ProducerConfig::COMPRESSION_TYPE_CONFIG,
187 Type::String,
188 CompressionType::None.name(),
189 ValidString::r#in(&CompressionType::names()),
190 Importance::High,
191 )
192 .define_with_validator(
193 ProducerConfig::COMPRESSION_GZIP_LEVEL_CONFIG,
194 Type::Int,
195 default_level(CompressionType::Gzip),
196 level_validator(CompressionType::Gzip),
197 Importance::Medium,
198 )
199 .define_with_validator(
200 ProducerConfig::COMPRESSION_LZ4_LEVEL_CONFIG,
201 Type::Int,
202 default_level(CompressionType::Lz4),
203 level_validator(CompressionType::Lz4),
204 Importance::Medium,
205 )
206 .define_with_validator(
207 ProducerConfig::COMPRESSION_ZSTD_LEVEL_CONFIG,
208 Type::Int,
209 default_level(CompressionType::Zstd),
210 level_validator(CompressionType::Zstd),
211 Importance::Medium,
212 )
213 .define_with_validator(
214 ProducerConfig::BATCH_SIZE_CONFIG,
215 Type::Int,
216 16384,
217 Range::at_least(0),
218 Importance::Medium,
219 )
220 .define(
221 ProducerConfig::PARTITIONER_ADAPTIVE_PARTITIONING_ENABLE_CONFIG,
222 Type::Boolean,
223 true,
224 Importance::Low,
225 )
226 .define_with_validator(
227 ProducerConfig::PARTITIONER_AVAILABILITY_TIMEOUT_MS_CONFIG,
228 Type::Long,
229 0_i64,
230 Range::at_least(0),
231 Importance::Low,
232 )
233 .define(
234 ProducerConfig::PARTITIONER_IGNORE_KEYS_CONFIG,
235 Type::Boolean,
236 false,
237 Importance::Medium,
238 )
239 .define(
240 ProducerConfig::PARTITIONER_RACK_AWARE_CONFIG,
241 Type::Boolean,
242 false,
243 Importance::Low,
244 )
245 .define_with_validator(
246 ProducerConfig::LINGER_MS_CONFIG,
247 Type::Long,
248 5_i64,
249 Range::at_least(0),
250 Importance::Medium,
251 )
252 .define_with_validator(
253 ProducerConfig::DELIVERY_TIMEOUT_MS_CONFIG,
254 Type::Int,
255 120 * 1000,
256 Range::at_least(0),
257 Importance::Medium,
258 )
259 .define(ProducerConfig::CLIENT_ID_CONFIG, Type::String, "", Importance::Medium)
260 .define(
261 ProducerConfig::CLIENT_RACK_CONFIG,
262 Type::String,
263 ProducerConfig::DEFAULT_CLIENT_RACK,
264 Importance::Low,
265 )
266 .define_with_validator(
267 ProducerConfig::SEND_BUFFER_CONFIG,
268 Type::Int,
269 128 * 1024,
270 Range::at_least(CommonClientConfigs::SEND_BUFFER_LOWER_BOUND),
271 Importance::Medium,
272 )
273 .define_with_validator(
274 ProducerConfig::RECEIVE_BUFFER_CONFIG,
275 Type::Int,
276 32 * 1024,
277 Range::at_least(CommonClientConfigs::RECEIVE_BUFFER_LOWER_BOUND),
278 Importance::Medium,
279 )
280 .define_with_validator(
281 ProducerConfig::MAX_REQUEST_SIZE_CONFIG,
282 Type::Int,
283 1024 * 1024,
284 Range::at_least(0),
285 Importance::Medium,
286 )
287 .define_with_validator(
288 ProducerConfig::RECONNECT_BACKOFF_MS_CONFIG,
289 Type::Long,
290 50_i64,
291 Range::at_least(0_i64),
292 Importance::Low,
293 )
294 .define_with_validator(
295 ProducerConfig::RECONNECT_BACKOFF_MAX_MS_CONFIG,
296 Type::Long,
297 1000_i64,
298 Range::at_least(0_i64),
299 Importance::Low,
300 )
301 .define_with_validator(
302 ProducerConfig::RETRY_BACKOFF_MS_CONFIG,
303 Type::Long,
304 CommonClientConfigs::DEFAULT_RETRY_BACKOFF_MS,
305 Range::at_least(0_i64),
306 Importance::Low,
307 )
308 .define_with_validator(
309 ProducerConfig::RETRY_BACKOFF_MAX_MS_CONFIG,
310 Type::Long,
311 CommonClientConfigs::DEFAULT_RETRY_BACKOFF_MAX_MS,
312 Range::at_least(0_i64),
313 Importance::Low,
314 )
315 .define_with_validator(
316 ProducerConfig::BOOTSTRAP_RESOLVE_TIMEOUT_MS_CONFIG,
317 Type::Long,
318 CommonClientConfigs::DEFAULT_BOOTSTRAP_RESOLVE_TIMEOUT_MS,
319 Range::at_least(0_i64),
320 Importance::High,
321 )
322 .define_with_validator(
325 ProducerConfig::MAX_BLOCK_MS_CONFIG,
326 Type::Long,
327 60 * 1000_i64,
328 Range::at_least(0),
329 Importance::Medium,
330 )
331 .define_with_validator(
332 ProducerConfig::REQUEST_TIMEOUT_MS_CONFIG,
333 Type::Int,
334 30 * 1000,
335 Range::at_least(0),
336 Importance::Medium,
337 )
338 .define_with_validator(
339 ProducerConfig::METADATA_MAX_AGE_CONFIG,
340 Type::Long,
341 5 * 60 * 1000_i64,
342 Range::at_least(0),
343 Importance::Low,
344 )
345 .define_with_validator(
346 ProducerConfig::METADATA_MAX_IDLE_CONFIG,
347 Type::Long,
348 5 * 60 * 1000_i64,
349 Range::at_least(5000),
350 Importance::Low,
351 )
352 .define_with_validator(
353 ProducerConfig::METRICS_SAMPLE_WINDOW_MS_CONFIG,
354 Type::Long,
355 30000_i64,
356 Range::at_least(0),
357 Importance::Low,
358 )
359 .define_with_validator(
360 ProducerConfig::METRICS_NUM_SAMPLES_CONFIG,
361 Type::Int,
362 2,
363 Range::at_least(1),
364 Importance::Low,
365 )
366 .define_with_validator(
367 ProducerConfig::METRICS_RECORDING_LEVEL_CONFIG,
368 Type::String,
369 "INFO",
370 ValidString::r#in(&["INFO", "DEBUG", "TRACE"]),
371 Importance::Low,
372 )
373 .define_with_validator(
375 ProducerConfig::MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION,
376 Type::Int,
377 5,
378 Range::at_least(1),
379 Importance::Low,
380 )
381 .define(
384 ProducerConfig::SOCKET_CONNECTION_SETUP_TIMEOUT_MS_CONFIG,
385 Type::Long,
386 CommonClientConfigs::DEFAULT_SOCKET_CONNECTION_SETUP_TIMEOUT_MS,
387 Importance::Medium,
388 )
389 .define(
390 ProducerConfig::SOCKET_CONNECTION_SETUP_TIMEOUT_MAX_MS_CONFIG,
391 Type::Long,
392 CommonClientConfigs::DEFAULT_SOCKET_CONNECTION_SETUP_TIMEOUT_MAX_MS,
393 Importance::Medium,
394 )
395 .define(
398 ProducerConfig::CONNECTIONS_MAX_IDLE_MS_CONFIG,
399 Type::Long,
400 9 * 60 * 1000_i64,
401 Importance::Medium,
402 )
403 .define_with_validator(
406 ProducerConfig::PARTITIONER_TYPE_CONFIG,
407 Type::String,
408 DefaultValue::Null,
409 partitioner_type_validator(),
410 Importance::Medium,
411 )
412 .define_with_validator(
414 CommonClientConfigs::SECURITY_PROTOCOL_CONFIG,
415 Type::String,
416 CommonClientConfigs::DEFAULT_SECURITY_PROTOCOL,
417 CaseInsensitiveValidString::r#in(&SecurityProtocol::names()),
418 Importance::Medium,
419 )
420 .with_client_ssl_support()
423 .with_client_sasl_support()
424 .define(ProducerConfig::ENABLE_IDEMPOTENCE_CONFIG, Type::Boolean, true, Importance::Low)
425 .define(ProducerConfig::TRANSACTION_TIMEOUT_CONFIG, Type::Int, 60000, Importance::Low)
426 .define_with_validator(
427 ProducerConfig::TRANSACTIONAL_ID_CONFIG,
428 Type::String,
429 DefaultValue::Null,
430 NonEmptyString,
431 Importance::Low,
432 )
433 .define(
434 ProducerConfig::TRANSACTION_TWO_PHASE_COMMIT_ENABLE_CONFIG,
435 Type::Boolean,
436 false,
437 Importance::Low,
438 )
439 .define_with_validator(
440 CommonClientConfigs::METADATA_RECOVERY_STRATEGY_CONFIG,
441 Type::String,
442 CommonClientConfigs::DEFAULT_METADATA_RECOVERY_STRATEGY,
443 CaseInsensitiveValidString::r#in(&["NONE", "REBOOTSTRAP"]),
444 Importance::Low,
445 )
446 .define_with_validator(
447 CommonClientConfigs::METADATA_RECOVERY_REBOOTSTRAP_TRIGGER_MS_CONFIG,
448 Type::Long,
449 CommonClientConfigs::DEFAULT_METADATA_RECOVERY_REBOOTSTRAP_TRIGGER_MS,
450 Range::at_least(0),
451 Importance::Low,
452 )
453 .define(
454 CommonClientConfigs::METADATA_CLUSTER_CHECK_ENABLE_CONFIG,
455 Type::Boolean,
456 true,
457 Importance::Low,
458 )
459 });
462
463#[derive(Debug)]
484#[doc(alias = "org.apache.kafka.clients.producer.ProducerConfig")]
485pub struct ProducerConfig {
486 pub(crate) config: AbstractConfig,
488 pub(crate) read_keys: Vec<&'static str>,
491
492 pub(crate) bootstrap_servers: Vec<String>,
496
497 pub(crate) client_dns_lookup: ClientDnsLookup,
500
501 pub(crate) client_id: String,
503
504 pub(crate) client_rack: String,
510
511 pub(crate) security_protocol: SecurityProtocol,
515
516 pub(crate) sasl_config: SaslConfigs,
518
519 pub(crate) ssl_config: SslConfigs,
521
522 pub(crate) batch_size: i32,
527
528 pub(crate) linger_ms: i64,
531
532 pub(crate) buffer_memory: i64,
536
537 pub(crate) buffer_memory_allocation_strategy: String,
551
552 pub(crate) max_block_ms: i64,
555
556 pub(crate) acks: i16,
560
561 pub(crate) retries: i32,
564
565 pub(crate) delivery_timeout_ms: i32,
568
569 pub(crate) request_timeout_ms: i32,
572
573 pub(crate) enable_idempotence: bool,
576
577 pub(crate) max_request_size: i32,
581
582 pub(crate) max_in_flight_requests_per_connection: i32,
585
586 pub(crate) compression_type: CompressionType,
590
591 pub(crate) compression_gzip_level: i32,
594
595 pub(crate) compression_lz4_level: i32,
598
599 pub(crate) compression_zstd_level: i32,
602
603 pub(crate) connections_max_idle_ms: i64,
607
608 pub(crate) reconnect_backoff_ms: i64,
611
612 pub(crate) reconnect_backoff_max_ms: i64,
615
616 pub(crate) retry_backoff_ms: i64,
619
620 pub(crate) retry_backoff_max_ms: i64,
623
624 pub(crate) bootstrap_resolve_timeout_ms: i64,
630
631 pub(crate) send_buffer_bytes: i32,
634
635 pub(crate) receive_buffer_bytes: i32,
637
638 pub(crate) socket_connection_setup_timeout_ms: i64,
641
642 pub(crate) socket_connection_setup_timeout_max_ms: i64,
645
646 pub(crate) metadata_max_age_ms: i64,
650
651 pub(crate) metadata_max_idle_ms: i64,
654
655 pub(crate) metadata_recovery_strategy: MetadataRecoveryStrategy,
659
660 pub(crate) metadata_recovery_rebootstrap_trigger_ms: i64,
664
665 pub(crate) metadata_cluster_check_enable: bool,
670
671 pub(crate) partitioner_adaptive_partitioning_enable: bool,
675
676 pub(crate) partitioner_availability_timeout_ms: i64,
679
680 pub(crate) partitioner_ignore_keys: bool,
683
684 pub(crate) partitioner_rack_aware: bool,
695
696 pub(crate) partitioner_type: Option<String>,
721
722 pub(crate) partitioner: Option<ConfiguredPartitioner>,
728
729 pub(crate) transactional_id: Option<String>,
733
734 pub(crate) transaction_timeout_ms: i32,
737
738 pub(crate) metrics_sample_window_ms: i64,
742
743 pub(crate) metrics_num_samples: i32,
746
747 pub(crate) metrics_recording_level: String,
750
751 pub(crate) two_phase_commit_enable: bool,
755}
756
757#[derive(Debug)]
764pub(crate) struct ConfiguredPartitioner {
765 partitioner: Box<dyn Any + Send + Sync>,
767 type_name: &'static str,
769}
770
771impl PartialEq for ProducerConfig {
772 fn eq(&self, other: &Self) -> bool {
774 self.config == other.config
775 }
776}
777
778impl Eq for ProducerConfig {}
779
780impl Hash for ProducerConfig {
781 fn hash<H: Hasher>(&self, state: &mut H) {
783 self.config.hash(state);
784 }
785}
786
787impl ProducerConfig {
788 #[cfg(test)]
799 pub(crate) fn defaults() -> Self {
800 let props = HashMap::from([(Self::BOOTSTRAP_SERVERS_CONFIG.to_string(), "localhost:9092".to_string())]);
801 let config =
802 AbstractConfig::new(&CONFIG, string_originals(&props), false).expect("the ConfigDef defaults are valid");
803 let mut defaults = Self::from_abstract(config).expect("the ConfigDef defaults are valid");
804 defaults.bootstrap_servers = Vec::new();
805 defaults
806 }
807
808 fn from_abstract(config: AbstractConfig) -> Result<Self, Error> {
811 let mut values = ValuesReader::new(&config);
812 let bootstrap_servers = values.list(Self::BOOTSTRAP_SERVERS_CONFIG)?.to_vec();
813 let client_dns_lookup = ClientDnsLookup::parse_config_value(values.string(Self::CLIENT_DNS_LOOKUP_CONFIG)?)?;
814 let client_id = values.string(Self::CLIENT_ID_CONFIG)?.to_string();
815 let client_rack = values.string(Self::CLIENT_RACK_CONFIG)?.to_string();
816 let batch_size = values.int(Self::BATCH_SIZE_CONFIG)?;
817 let linger_ms = values.long(Self::LINGER_MS_CONFIG)?;
818 let buffer_memory = values.long(Self::BUFFER_MEMORY_CONFIG)?;
819 let buffer_memory_allocation_strategy =
820 values.string(Self::BUFFER_MEMORY_ALLOCATION_STRATEGY_CONFIG)?.to_string();
821 let max_block_ms = values.long(Self::MAX_BLOCK_MS_CONFIG)?;
822 let acks_string = Self::parse_acks(values.string(Self::ACKS_CONFIG)?)?;
827 let acks = acks_string
828 .parse::<i16>()
829 .map_err(|_| Error::config_message(format!("Invalid configuration value for 'acks': {acks_string}")))?;
830 let retries = values.int(Self::RETRIES_CONFIG)?;
831 let delivery_timeout_ms = values.int(Self::DELIVERY_TIMEOUT_MS_CONFIG)?;
832 let request_timeout_ms = values.int(Self::REQUEST_TIMEOUT_MS_CONFIG)?;
833 let enable_idempotence = values.boolean(Self::ENABLE_IDEMPOTENCE_CONFIG)?;
834 let max_request_size = values.int(Self::MAX_REQUEST_SIZE_CONFIG)?;
835 let max_in_flight_requests_per_connection = values.int(Self::MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION)?;
836 let compression_type = CompressionType::for_name(values.string(Self::COMPRESSION_TYPE_CONFIG)?)?;
838 if !Compression::is_enabled(compression_type) {
841 return Err(Error::config_name_value_message(
842 Self::COMPRESSION_TYPE_CONFIG,
843 compression_type.name(),
844 Compression::disabled_message(compression_type),
845 ));
846 }
847 let compression_gzip_level = values.int(Self::COMPRESSION_GZIP_LEVEL_CONFIG)?;
848 let compression_lz4_level = values.int(Self::COMPRESSION_LZ4_LEVEL_CONFIG)?;
849 let compression_zstd_level = values.int(Self::COMPRESSION_ZSTD_LEVEL_CONFIG)?;
850 let connections_max_idle_ms = values.long(Self::CONNECTIONS_MAX_IDLE_MS_CONFIG)?;
851 let reconnect_backoff_ms = values.long(Self::RECONNECT_BACKOFF_MS_CONFIG)?;
852 let reconnect_backoff_max_ms = values.long(Self::RECONNECT_BACKOFF_MAX_MS_CONFIG)?;
853 let retry_backoff_ms = values.long(Self::RETRY_BACKOFF_MS_CONFIG)?;
854 let retry_backoff_max_ms = values.long(Self::RETRY_BACKOFF_MAX_MS_CONFIG)?;
855 let bootstrap_resolve_timeout_ms = values.long(Self::BOOTSTRAP_RESOLVE_TIMEOUT_MS_CONFIG)?;
856 let send_buffer_bytes = values.int(Self::SEND_BUFFER_CONFIG)?;
857 let receive_buffer_bytes = values.int(Self::RECEIVE_BUFFER_CONFIG)?;
858 let socket_connection_setup_timeout_ms = values.long(Self::SOCKET_CONNECTION_SETUP_TIMEOUT_MS_CONFIG)?;
859 let socket_connection_setup_timeout_max_ms =
860 values.long(Self::SOCKET_CONNECTION_SETUP_TIMEOUT_MAX_MS_CONFIG)?;
861 let metadata_max_age_ms = values.long(Self::METADATA_MAX_AGE_CONFIG)?;
862 let metadata_max_idle_ms = values.long(Self::METADATA_MAX_IDLE_CONFIG)?;
863 let strategy = values.string(CommonClientConfigs::METADATA_RECOVERY_STRATEGY_CONFIG)?;
866 let metadata_recovery_strategy =
867 MetadataRecoveryStrategy::for_name(strategy).map_err(Error::local_illegal_argument)?;
868 let metadata_recovery_rebootstrap_trigger_ms =
869 values.long(CommonClientConfigs::METADATA_RECOVERY_REBOOTSTRAP_TRIGGER_MS_CONFIG)?;
870 let metadata_cluster_check_enable =
871 values.boolean(CommonClientConfigs::METADATA_CLUSTER_CHECK_ENABLE_CONFIG)?;
872 let partitioner_adaptive_partitioning_enable =
873 values.boolean(Self::PARTITIONER_ADAPTIVE_PARTITIONING_ENABLE_CONFIG)?;
874 let partitioner_availability_timeout_ms = values.long(Self::PARTITIONER_AVAILABILITY_TIMEOUT_MS_CONFIG)?;
875 let partitioner_ignore_keys = values.boolean(Self::PARTITIONER_IGNORE_KEYS_CONFIG)?;
876 let partitioner_rack_aware = values.boolean(Self::PARTITIONER_RACK_AWARE_CONFIG)?;
877 let partitioner_type = values.string_or_null(Self::PARTITIONER_TYPE_CONFIG)?.map(str::to_string);
878 let transactional_id = values.string_or_null(Self::TRANSACTIONAL_ID_CONFIG)?.map(str::to_string);
879 let transaction_timeout_ms = values.int(Self::TRANSACTION_TIMEOUT_CONFIG)?;
880 let metrics_sample_window_ms = values.long(Self::METRICS_SAMPLE_WINDOW_MS_CONFIG)?;
881 let metrics_num_samples = values.int(Self::METRICS_NUM_SAMPLES_CONFIG)?;
882 let metrics_recording_level = values.string(Self::METRICS_RECORDING_LEVEL_CONFIG)?.to_string();
883 let two_phase_commit_enable = values.boolean(Self::TRANSACTION_TWO_PHASE_COMMIT_ENABLE_CONFIG)?;
884 let protocol = values.string(CommonClientConfigs::SECURITY_PROTOCOL_CONFIG)?;
885 let security_protocol = SecurityProtocol::for_name(protocol)
887 .ok_or_else(|| Error::local_illegal_state(format!("Unknown security protocol {protocol}")))?;
888
889 let mut ssl_values = ValuesReader::new(&config);
893 let ssl_config = SslConfigs::from_values(&mut ssl_values)?;
894 if matches!(security_protocol, SecurityProtocol::Ssl | SecurityProtocol::SaslSsl) {
895 values.append(ssl_values);
896 }
897 let mut sasl_values = ValuesReader::new(&config);
898 let sasl_config = SaslConfigs::from_values(&mut sasl_values)?;
899 if matches!(security_protocol, SecurityProtocol::SaslPlaintext | SecurityProtocol::SaslSsl) {
900 values.append(sasl_values);
901 }
902 let read_keys = values.into_read_keys();
903
904 Ok(Self {
905 config,
906 read_keys,
907 bootstrap_servers,
908 client_dns_lookup,
909 client_id,
910 client_rack,
911 security_protocol,
912 sasl_config,
913 ssl_config,
914 batch_size,
915 linger_ms,
916 buffer_memory,
917 buffer_memory_allocation_strategy,
918 max_block_ms,
919 acks,
920 retries,
921 delivery_timeout_ms,
922 request_timeout_ms,
923 enable_idempotence,
924 max_request_size,
925 max_in_flight_requests_per_connection,
926 compression_type,
927 compression_gzip_level,
928 compression_lz4_level,
929 compression_zstd_level,
930 connections_max_idle_ms,
931 reconnect_backoff_ms,
932 reconnect_backoff_max_ms,
933 retry_backoff_ms,
934 retry_backoff_max_ms,
935 bootstrap_resolve_timeout_ms,
936 send_buffer_bytes,
937 receive_buffer_bytes,
938 socket_connection_setup_timeout_ms,
939 socket_connection_setup_timeout_max_ms,
940 metadata_max_age_ms,
941 metadata_max_idle_ms,
942 metadata_recovery_strategy,
943 metadata_recovery_rebootstrap_trigger_ms,
944 metadata_cluster_check_enable,
945 partitioner_adaptive_partitioning_enable,
946 partitioner_availability_timeout_ms,
947 partitioner_ignore_keys,
948 partitioner_rack_aware,
949 partitioner_type,
950 partitioner: None,
951 transactional_id,
952 transaction_timeout_ms,
953 metrics_sample_window_ms,
954 metrics_num_samples,
955 metrics_recording_level,
956 two_phase_commit_enable,
957 })
958 }
959}
960
961impl ProducerConfig {
962 pub const BOOTSTRAP_SERVERS_CONFIG: &'static str = "bootstrap.servers";
966 pub const CLIENT_DNS_LOOKUP_CONFIG: &'static str = CommonClientConfigs::CLIENT_DNS_LOOKUP_CONFIG;
969 pub const CLIENT_ID_CONFIG: &'static str = "client.id";
971 pub const CLIENT_RACK_CONFIG: &'static str = CommonClientConfigs::CLIENT_RACK_CONFIG;
973 pub const DEFAULT_CLIENT_RACK: &'static str = CommonClientConfigs::DEFAULT_CLIENT_RACK;
975 pub const BATCH_SIZE_CONFIG: &'static str = "batch.size";
977 pub const LINGER_MS_CONFIG: &'static str = "linger.ms";
979 pub const BUFFER_MEMORY_CONFIG: &'static str = "buffer.memory";
981 pub const BUFFER_MEMORY_ALLOCATION_STRATEGY_CONFIG: &'static str = "buffer.memory.allocation.strategy";
984 pub const BUFFER_MEMORY_ALLOCATION_STRATEGY_FULL: &'static str = "full";
987 pub const BUFFER_MEMORY_ALLOCATION_STRATEGY_INCREMENTAL: &'static str = "incremental";
990 pub const MAX_BLOCK_MS_CONFIG: &'static str = "max.block.ms";
992 pub const ACKS_CONFIG: &'static str = "acks";
994 pub const RETRIES_CONFIG: &'static str = "retries";
996 pub const DELIVERY_TIMEOUT_MS_CONFIG: &'static str = "delivery.timeout.ms";
998 pub const REQUEST_TIMEOUT_MS_CONFIG: &'static str = "request.timeout.ms";
1000 pub const ENABLE_IDEMPOTENCE_CONFIG: &'static str = "enable.idempotence";
1002 pub const MAX_REQUEST_SIZE_CONFIG: &'static str = "max.request.size";
1004 pub const MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION: &'static str = "max.in.flight.requests.per.connection";
1006 pub const COMPRESSION_TYPE_CONFIG: &'static str = "compression.type";
1008 pub const CONNECTIONS_MAX_IDLE_MS_CONFIG: &'static str = "connections.max.idle.ms";
1010 pub const RECONNECT_BACKOFF_MS_CONFIG: &'static str = "reconnect.backoff.ms";
1012 pub const RECONNECT_BACKOFF_MAX_MS_CONFIG: &'static str = "reconnect.backoff.max.ms";
1014 pub const RETRY_BACKOFF_MS_CONFIG: &'static str = "retry.backoff.ms";
1016 pub const RETRY_BACKOFF_MAX_MS_CONFIG: &'static str = "retry.backoff.max.ms";
1018 pub const BOOTSTRAP_RESOLVE_TIMEOUT_MS_CONFIG: &'static str =
1032 CommonClientConfigs::BOOTSTRAP_RESOLVE_TIMEOUT_MS_CONFIG;
1033 pub const SEND_BUFFER_CONFIG: &'static str = "send.buffer.bytes";
1035 pub const RECEIVE_BUFFER_CONFIG: &'static str = "receive.buffer.bytes";
1037 pub const METADATA_MAX_AGE_CONFIG: &'static str = "metadata.max.age.ms";
1039 pub const METADATA_MAX_IDLE_CONFIG: &'static str = "metadata.max.idle.ms";
1041 pub const PARTITIONER_ADAPTIVE_PARTITIONING_ENABLE_CONFIG: &'static str =
1043 "partitioner.adaptive.partitioning.enable";
1044 pub const PARTITIONER_AVAILABILITY_TIMEOUT_MS_CONFIG: &'static str = "partitioner.availability.timeout.ms";
1046 pub const PARTITIONER_IGNORE_KEYS_CONFIG: &'static str = "partitioner.ignore.keys";
1048 pub const PARTITIONER_RACK_AWARE_CONFIG: &'static str = "partitioner.rack.aware";
1050 #[doc(alias = "rust-only")]
1053 pub const PARTITIONER_TYPE_CONFIG: &'static str = "partitioner.type";
1054 #[doc(alias = "rust-only")]
1058 pub const CONSISTENT_RANDOM_PARTITIONER: &'static str = "ConsistentRandomPartitioner";
1059 #[doc(alias = "rust-only")]
1063 pub const MURMUR2_RANDOM_PARTITIONER: &'static str = "Murmur2RandomPartitioner";
1064 #[doc(alias = "rust-only")]
1068 pub const ROUND_ROBIN_PARTITIONER: &'static str = "RoundRobinPartitioner";
1069 pub const TRANSACTIONAL_ID_CONFIG: &'static str = "transactional.id";
1071 pub const TRANSACTION_TIMEOUT_CONFIG: &'static str = "transaction.timeout.ms";
1073 pub const METRICS_SAMPLE_WINDOW_MS_CONFIG: &'static str = "metrics.sample.window.ms";
1075 pub const METRICS_NUM_SAMPLES_CONFIG: &'static str = "metrics.num.samples";
1077 pub const METRICS_RECORDING_LEVEL_CONFIG: &'static str = "metrics.recording.level";
1079 pub const TRANSACTION_TWO_PHASE_COMMIT_ENABLE_CONFIG: &'static str = "transaction.two.phase.commit.enable";
1081 pub const COMPRESSION_GZIP_LEVEL_CONFIG: &'static str = "compression.gzip.level";
1083 pub const COMPRESSION_LZ4_LEVEL_CONFIG: &'static str = "compression.lz4.level";
1085 pub const COMPRESSION_ZSTD_LEVEL_CONFIG: &'static str = "compression.zstd.level";
1087 pub const SOCKET_CONNECTION_SETUP_TIMEOUT_MS_CONFIG: &'static str = "socket.connection.setup.timeout.ms";
1089 pub const SOCKET_CONNECTION_SETUP_TIMEOUT_MAX_MS_CONFIG: &'static str = "socket.connection.setup.timeout.max.ms";
1091
1092 #[doc(alias = "org.apache.kafka.clients.producer.ProducerConfig#ProducerConfig(Map)")]
1131 pub fn new(props: &HashMap<String, String>) -> Result<Self, Error> {
1132 Self::build(string_originals(props), None)
1133 }
1134
1135 #[cfg_attr(
1162 not(any(feature = "ffi", test)),
1163 expect(dead_code, reason = "only the C config handle copies a configuration")
1164 )]
1165 pub(crate) fn copy_for_client(&self) -> Result<Self, Error> {
1166 Self::build(self.config.originals_map().clone(), Some(&self.client_id))
1167 }
1168
1169 fn build(originals: ConfigMap, resolved_client_id: Option<&str>) -> Result<Self, Error> {
1175 let log_warnings = resolved_client_id.is_none();
1176 let config = AbstractConfig::with_hooks_and_log_warnings(
1177 &CONFIG,
1178 originals,
1179 false,
1180 log_warnings,
1181 "ProducerConfig",
1182 |originals| originals,
1183 |config| Self::post_process_parsed_config(config, resolved_client_id),
1184 )?;
1185 Self::from_abstract(config)
1186 }
1187
1188 fn post_process_parsed_config(
1194 config: &AbstractConfig,
1195 resolved_client_id: Option<&str>,
1196 ) -> Result<ConfigMap, Error> {
1197 let log_lines = resolved_client_id.is_none();
1198 CommonClientConfigs::post_validate_sasl_mechanism_config(config)?;
1199 CommonClientConfigs::post_validate_supported_sasl_mechanism(config)?;
1200 CommonClientConfigs::warn_disabling_exponential_backoff(config, log_lines)?;
1201 let mut refined_configs =
1202 CommonClientConfigs::post_process_reconnect_backoff_configs(config, config.parsed_values(), log_lines);
1203 Self::post_process_and_validate_idempotence_configs(config, &mut refined_configs, log_lines)?;
1204 Self::maybe_override_client_id(config, &mut refined_configs, resolved_client_id)?;
1205 Ok(refined_configs)
1206 }
1207
1208 #[doc(alias = "org.apache.kafka.clients.producer.ProducerConfig#maybeOverrideClientId")]
1215 fn maybe_override_client_id(
1216 config: &AbstractConfig,
1217 configs: &mut ConfigMap,
1218 resolved_client_id: Option<&str>,
1219 ) -> Result<(), Error> {
1220 let user_configured_client_id = config.originals_map().contains_key(Self::CLIENT_ID_CONFIG);
1221 let refined_client_id = if user_configured_client_id {
1222 config.get_string(Self::CLIENT_ID_CONFIG)?.unwrap_or_default().to_string()
1223 } else {
1224 let transactional_id = config.get_string(Self::TRANSACTIONAL_ID_CONFIG)?;
1225 match (resolved_client_id, transactional_id) {
1226 (Some(resolved_client_id), _) => resolved_client_id.to_string(),
1227 (None, Some(transactional_id)) => format!("producer-{transactional_id}"),
1228 (None, None) => format!(
1229 "producer-{}",
1230 PRODUCER_CLIENT_ID_SEQUENCE.fetch_add(1, atomic::Ordering::Relaxed)
1231 ),
1232 }
1233 };
1234 configs.insert(Self::CLIENT_ID_CONFIG.to_string(), Some(ParsedValue::String(refined_client_id)));
1235 Ok(())
1236 }
1237
1238 #[doc(alias = "org.apache.kafka.clients.producer.ProducerConfig#postProcessAndValidateIdempotenceConfigs")]
1251 fn post_process_and_validate_idempotence_configs(
1252 config: &AbstractConfig,
1253 configs: &mut ConfigMap,
1254 log_lines: bool,
1255 ) -> Result<(), Error> {
1256 let original_configs = config.originals_map();
1257 let acks_str = Self::parse_acks(config.get_string(Self::ACKS_CONFIG)?.unwrap_or_default())?;
1258 configs.insert(Self::ACKS_CONFIG.to_string(), Some(ParsedValue::String(acks_str.clone())));
1259 let user_configured_idempotence = original_configs.contains_key(Self::ENABLE_IDEMPOTENCE_CONFIG);
1260 let mut idempotence_enabled = config.get_boolean(Self::ENABLE_IDEMPOTENCE_CONFIG)?.unwrap_or_default();
1261 let mut should_disable_idempotence = false;
1262
1263 if idempotence_enabled {
1265 let retries = config.get_int(Self::RETRIES_CONFIG)?.unwrap_or_default();
1266 if retries == 0 {
1267 if user_configured_idempotence {
1268 return Err(Error::config_message(format!(
1269 "Must set {} to non-zero when using the idempotent producer.",
1270 Self::RETRIES_CONFIG
1271 )));
1272 }
1273 if log_lines {
1274 info!("Idempotence will be disabled because {} is set to 0.", Self::RETRIES_CONFIG);
1275 }
1276 should_disable_idempotence = true;
1277 }
1278
1279 let acks = acks_str
1282 .parse::<i16>()
1283 .map_err(|_| Error::config_message(format!("Invalid configuration value for 'acks': {acks_str}")))?;
1284 if acks != -1 {
1285 if user_configured_idempotence {
1286 return Err(Error::config_message(format!(
1287 "Must set {} to all in order to use the idempotent producer. Otherwise we cannot guarantee \
1288 idempotence.",
1289 Self::ACKS_CONFIG
1290 )));
1291 }
1292 if log_lines {
1293 info!(
1294 "Idempotence will be disabled because {} is set to {}, not set to 'all'.",
1295 Self::ACKS_CONFIG,
1296 acks
1297 );
1298 }
1299 should_disable_idempotence = true;
1300 }
1301
1302 let in_flight_connection = config.get_int(Self::MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION)?.unwrap_or_default();
1303 if MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION_FOR_IDEMPOTENCE < in_flight_connection {
1304 return Err(Error::config_message(format!(
1305 "To use the idempotent producer, {} must be set to at most 5. Current value is {}.",
1306 Self::MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION,
1307 in_flight_connection
1308 )));
1309 }
1310 }
1311
1312 if should_disable_idempotence {
1313 configs.insert(Self::ENABLE_IDEMPOTENCE_CONFIG.to_string(), Some(ParsedValue::Boolean(false)));
1314 idempotence_enabled = false;
1315 }
1316
1317 let user_configured_transactions = original_configs.contains_key(Self::TRANSACTIONAL_ID_CONFIG);
1319 if !idempotence_enabled && user_configured_transactions {
1320 return Err(Error::config_message(format!(
1321 "Cannot set a {} without also enabling idempotence.",
1322 Self::TRANSACTIONAL_ID_CONFIG
1323 )));
1324 }
1325
1326 let enable_2pc = config
1331 .get_boolean(Self::TRANSACTION_TWO_PHASE_COMMIT_ENABLE_CONFIG)?
1332 .unwrap_or_default();
1333 let user_configured_transaction_timeout = original_configs.contains_key(Self::TRANSACTION_TIMEOUT_CONFIG);
1334 if enable_2pc && user_configured_transaction_timeout {
1335 return Err(Error::config_message(format!(
1336 "Cannot set {} when {} is set to true. Transactions will not expire with two-phase commit enabled.",
1337 Self::TRANSACTION_TIMEOUT_CONFIG,
1338 Self::TRANSACTION_TWO_PHASE_COMMIT_ENABLE_CONFIG
1339 )));
1340 }
1341 Ok(())
1342 }
1343
1344 #[doc(alias = "org.apache.kafka.clients.producer.ProducerConfig#parseAcks")]
1356 pub(crate) fn parse_acks(acks_string: &str) -> Result<String, Error> {
1357 let trimmed = acks_string.trim();
1358 if trimmed.eq_ignore_ascii_case("all") {
1359 Ok("-1".to_string())
1360 } else {
1361 trimmed
1362 .parse::<i16>()
1363 .map(|acks| acks.to_string())
1364 .map_err(|_| Error::config_message(format!("Invalid configuration value for 'acks': {acks_string}")))
1365 }
1366 }
1367
1368 #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#getShort")]
1386 pub fn get_short(&self, key: &str) -> Result<Option<i16>, Error> {
1387 self.config.get_short(key)
1388 }
1389
1390 #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#getInt")]
1408 pub fn get_int(&self, key: &str) -> Result<Option<i32>, Error> {
1409 self.config.get_int(key)
1410 }
1411
1412 #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#getLong")]
1430 pub fn get_long(&self, key: &str) -> Result<Option<i64>, Error> {
1431 self.config.get_long(key)
1432 }
1433
1434 #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#getDouble")]
1452 pub fn get_double(&self, key: &str) -> Result<Option<f64>, Error> {
1453 self.config.get_double(key)
1454 }
1455
1456 #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#getList")]
1474 pub fn get_list(&self, key: &str) -> Result<Option<&[String]>, Error> {
1475 self.config.get_list(key)
1476 }
1477
1478 #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#getBoolean")]
1500 pub fn get_boolean(&self, key: &str) -> Result<Option<bool>, Error> {
1501 self.config.get_boolean(key)
1502 }
1503
1504 #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#getString")]
1532 pub fn get_string(&self, key: &str) -> Result<Option<&str>, Error> {
1533 self.config.get_string(key)
1534 }
1535
1536 #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#unused")]
1540 pub fn unused(&self) -> HashSet<String> {
1541 self.config.unused()
1542 }
1543
1544 #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#ignore")]
1550 pub fn ignore(&self, key: &str) {
1551 self.config.ignore(key);
1552 }
1553
1554 #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#logUnused")]
1558 pub fn log_unused(&self) {
1559 self.config.log_unused();
1560 }
1561
1562 #[doc(alias = "org.apache.kafka.clients.producer.ProducerConfig#configNames")]
1568 pub fn config_names() -> HashSet<String> {
1569 CONFIG.names()
1570 }
1571
1572 pub(crate) fn mark_read_keys_used(&self) {
1575 for key in &self.read_keys {
1576 self.config.ignore(key);
1577 }
1578 }
1579
1580 pub(crate) fn log_values(&self) {
1583 self.config.log_all("ProducerConfig");
1584 }
1585
1586 pub(crate) fn originals(&self) -> HashMap<String, String> {
1589 self.config
1590 .originals_map()
1591 .iter()
1592 .filter_map(|(key, value)| value.as_ref().map(|value| (key.clone(), value.to_string())))
1593 .collect()
1594 }
1595 pub(crate) fn key_hasher(&self) -> KeyHasher {
1612 match self.partitioner_type.as_deref() {
1613 Some(Self::MURMUR2_RANDOM_PARTITIONER) => KeyHasher::Murmur2,
1614 _ => KeyHasher::Crc32,
1618 }
1619 }
1620
1621 pub(crate) fn resolve_partitioner<K: 'static, V: 'static>(
1654 &mut self,
1655 ) -> Result<Option<Box<dyn Partitioner<K, V>>>, Error> {
1656 if let Some(configured) = self.partitioner.take() {
1657 let Ok(partitioner) = configured.partitioner.downcast::<Box<dyn Partitioner<K, V>>>() else {
1658 return Err(Error::kafka_message(format!(
1659 "{} is not an instance of {}",
1660 configured.type_name,
1661 type_name::<dyn Partitioner<K, V>>()
1662 )));
1663 };
1664 return Ok(Some(*partitioner));
1665 }
1666 Ok(match self.partitioner_type.as_deref() {
1667 Some(Self::ROUND_ROBIN_PARTITIONER) => Some(Box::new(RoundRobinPartitioner::new())),
1668 _ => None,
1669 })
1670 }
1671
1672 #[doc(alias = "rust-only")]
1698 pub fn set_partitioner<K: 'static, V: 'static>(mut self, partitioner: Box<dyn Partitioner<K, V>>) -> Self {
1699 self.partitioner_type = None;
1700 self.partitioner = Some(ConfiguredPartitioner {
1701 partitioner: Box::new(partitioner),
1702 type_name: type_name::<dyn Partitioner<K, V>>(),
1703 });
1704 self
1705 }
1706
1707 pub(crate) fn user_configured(&self, key: &str) -> bool {
1713 self.config.originals_map().contains_key(key)
1714 }
1715}
1716
1717#[cfg(test)]
1718mod tests {
1719 #[test]
1722 fn test_sasl_mechanism_is_validated_when_the_config_is_built() {
1723 let build = |protocol: &str, mechanism: &str| {
1724 let props = std::collections::HashMap::from([
1725 ("bootstrap.servers".to_string(), "localhost:9092".to_string()),
1726 ("security.protocol".to_string(), protocol.to_string()),
1727 ("sasl.mechanism".to_string(), mechanism.to_string()),
1728 ]);
1729 ProducerConfig::new(&props)
1730 };
1731
1732 let err = build("SASL_PLAINTEXT", "SCRAM-SHA-256").expect_err("SCRAM is not implemented");
1733 assert!(err.is_config_error(), "must be a ConfigError: {err:?}");
1734 assert_eq!(
1735 err.message(),
1736 "Invalid value SCRAM-SHA-256 for configuration sasl.mechanism: only PLAIN is supported by this client"
1737 );
1738
1739 let err = build("SASL_SSL", "").expect_err("an empty mechanism is invalid with SASL");
1740 assert!(err.is_config_error(), "must be a ConfigError: {err:?}");
1741 assert!(
1742 err.message().contains("mechanism must be non-null and non-empty string"),
1743 "got: {}",
1744 err.message()
1745 );
1746
1747 build("SASL_PLAINTEXT", "PLAIN").expect("PLAIN is implemented");
1748 build("PLAINTEXT", "SCRAM-SHA-256").expect("the mechanism only matters with SASL");
1749 }
1750
1751 use super::*;
1752 use crate::common::config::types::Password;
1753 use crate::test_log_capture::capture_logs;
1754 use log::Level;
1755
1756 fn base_props() -> HashMap<String, String> {
1759 HashMap::from([("bootstrap.servers".to_string(), "localhost:9092".to_string())])
1760 }
1761
1762 #[test]
1763 fn test_default_values() {
1764 let config = ProducerConfig::defaults();
1765 assert_eq!(config.batch_size, 16384);
1766 assert_eq!(config.linger_ms, 5);
1767 assert_eq!(config.buffer_memory, 32 * 1024 * 1024);
1768 assert_eq!(config.max_block_ms, 60_000);
1769 assert_eq!(config.acks, -1);
1770 assert_eq!(config.retries, i32::MAX);
1771 assert_eq!(config.delivery_timeout_ms, 120_000);
1772 assert_eq!(config.request_timeout_ms, 30_000);
1773 assert!(config.enable_idempotence);
1774 assert_eq!(config.max_request_size, 1_048_576);
1775 assert_eq!(config.max_in_flight_requests_per_connection, 5);
1776 assert_eq!(config.compression_type, CompressionType::None);
1777 assert_eq!(config.connections_max_idle_ms, 540_000);
1778 assert_eq!(config.reconnect_backoff_ms, 50);
1779 assert_eq!(config.reconnect_backoff_max_ms, 1000);
1780 assert_eq!(config.retry_backoff_ms, 100);
1781 assert_eq!(config.retry_backoff_max_ms, 1000);
1782 assert_eq!(config.send_buffer_bytes, 131_072);
1783 assert_eq!(config.receive_buffer_bytes, 32_768);
1784 assert_eq!(config.metadata_max_age_ms, 300_000);
1785 assert_eq!(config.metadata_max_idle_ms, 300_000);
1786 assert_eq!(config.metadata_recovery_strategy, MetadataRecoveryStrategy::Rebootstrap);
1787 assert_eq!(config.metadata_recovery_rebootstrap_trigger_ms, 300_000);
1788 assert!(config.partitioner_adaptive_partitioning_enable);
1789 assert_eq!(config.partitioner_availability_timeout_ms, 0);
1790 assert!(!config.partitioner_ignore_keys);
1791 assert!(!config.partitioner_rack_aware);
1792 assert_eq!(config.client_rack, "");
1793 assert!(config.partitioner_type.is_none());
1794 assert_eq!(config.key_hasher(), KeyHasher::Crc32);
1797 assert!(config.transactional_id.is_none());
1798 assert_eq!(config.transaction_timeout_ms, 60_000);
1799 assert_eq!(config.metrics_sample_window_ms, 30_000);
1800 assert_eq!(config.metrics_num_samples, 2);
1801 assert_eq!(config.metrics_recording_level, "INFO");
1802 assert_eq!(config.originals(), base_props());
1804 }
1805
1806 #[test]
1812 fn test_metadata_recovery_configs() {
1813 let mut props = base_props();
1814 props.insert("metadata.recovery.strategy".to_string(), "NONE".to_string());
1815 props.insert("metadata.recovery.rebootstrap.trigger.ms".to_string(), "1234".to_string());
1816 let c = ProducerConfig::new(&props).unwrap();
1817 assert_eq!(c.metadata_recovery_strategy, MetadataRecoveryStrategy::None);
1818 assert_eq!(c.metadata_recovery_rebootstrap_trigger_ms, 1234);
1819
1820 let mut props = base_props();
1821 props.insert("metadata.recovery.strategy".to_string(), "rebootstrap".to_string());
1822 let c = ProducerConfig::new(&props).unwrap();
1823 assert_eq!(c.metadata_recovery_strategy, MetadataRecoveryStrategy::Rebootstrap);
1824
1825 let mut props = base_props();
1826 props.insert("metadata.recovery.strategy".to_string(), "bogus".to_string());
1827 let err = ProducerConfig::new(&props).unwrap_err();
1828 assert_eq!(
1831 err.message(),
1832 "Invalid value bogus for configuration metadata.recovery.strategy: \
1833 String must be one of (case insensitive): REBOOTSTRAP, NONE"
1834 );
1835 }
1836
1837 #[test]
1840 fn test_metrics_num_samples_validator() {
1841 let mut props = base_props();
1842 props.insert("metrics.num.samples".to_string(), "3".to_string());
1843 let c = ProducerConfig::new(&props).unwrap();
1844 assert_eq!(c.metrics_num_samples, 3);
1845
1846 let mut props = base_props();
1847 props.insert("metrics.num.samples".to_string(), "0".to_string());
1848 let err = ProducerConfig::new(&props).unwrap_err();
1849 let msg = err.to_string();
1850 assert!(
1851 msg.contains("metrics.num.samples") && msg.contains("at least 1"),
1852 "unexpected message: {msg}"
1853 );
1854
1855 let mut props = base_props();
1856 props.insert("metrics.num.samples".to_string(), "-1".to_string());
1857 assert!(ProducerConfig::new(&props).is_err());
1858 }
1859
1860 #[test]
1864 fn test_metadata_recovery_rebootstrap_trigger_ms_validator() {
1865 let mut props = base_props();
1866 props.insert("metadata.recovery.rebootstrap.trigger.ms".to_string(), "0".to_string());
1867 let c = ProducerConfig::new(&props).unwrap();
1868 assert_eq!(c.metadata_recovery_rebootstrap_trigger_ms, 0);
1869
1870 let mut props = base_props();
1871 props.insert("metadata.recovery.rebootstrap.trigger.ms".to_string(), "-1".to_string());
1872 let err = ProducerConfig::new(&props).unwrap_err();
1873 let Error::Config(config_error) = err else {
1874 panic!("expected a config error, got {err:?}");
1875 };
1876 assert_eq!(
1877 config_error.message(),
1878 "Invalid value -1 for configuration metadata.recovery.rebootstrap.trigger.ms: \
1879 Value must be at least 0"
1880 );
1881 }
1882
1883 #[test]
1886 fn test_metadata_cluster_check_enable() {
1887 let c = ProducerConfig::new(&base_props()).unwrap();
1888 assert!(c.metadata_cluster_check_enable);
1889
1890 let mut props = base_props();
1891 props.insert(
1892 CommonClientConfigs::METADATA_CLUSTER_CHECK_ENABLE_CONFIG.to_string(),
1893 "false".to_string(),
1894 );
1895 assert!(!ProducerConfig::new(&props).unwrap().metadata_cluster_check_enable);
1896
1897 props.insert(
1898 CommonClientConfigs::METADATA_CLUSTER_CHECK_ENABLE_CONFIG.to_string(),
1899 "maybe".to_string(),
1900 );
1901 assert_eq!(
1902 ProducerConfig::new(&props).unwrap_err().message(),
1903 "Invalid value maybe for configuration metadata.cluster.check.enable: Expected value to be either true or false"
1904 );
1905 }
1906
1907 #[test]
1910 fn test_bootstrap_resolve_timeout_ms() {
1911 assert_eq!(
1912 ProducerConfig::BOOTSTRAP_RESOLVE_TIMEOUT_MS_CONFIG,
1913 "bootstrap.resolve.timeout.ms"
1914 );
1915 let c = ProducerConfig::new(&base_props()).unwrap();
1916 assert_eq!(c.bootstrap_resolve_timeout_ms, 0);
1917
1918 let mut props = base_props();
1919 props.insert("bootstrap.resolve.timeout.ms".to_string(), "3000".to_string());
1920 assert_eq!(ProducerConfig::new(&props).unwrap().bootstrap_resolve_timeout_ms, 3000);
1921
1922 props.insert("bootstrap.resolve.timeout.ms".to_string(), "-1".to_string());
1923 let err = ProducerConfig::new(&props).unwrap_err();
1924 let Error::Config(config_error) = err else {
1925 panic!("expected a config error, got {err:?}");
1926 };
1927 assert_eq!(
1928 config_error.message(),
1929 "Invalid value -1 for configuration bootstrap.resolve.timeout.ms: Value must be at least 0"
1930 );
1931 }
1932
1933 #[test]
1936 fn test_metrics_sample_window_ms_validator() {
1937 let mut props = base_props();
1938 props.insert("metrics.sample.window.ms".to_string(), "0".to_string());
1939 let c = ProducerConfig::new(&props).unwrap();
1940 assert_eq!(c.metrics_sample_window_ms, 0);
1941
1942 let mut props = base_props();
1943 props.insert("metrics.sample.window.ms".to_string(), "60000".to_string());
1944 let c = ProducerConfig::new(&props).unwrap();
1945 assert_eq!(c.metrics_sample_window_ms, 60_000);
1946
1947 let mut props = base_props();
1948 props.insert("metrics.sample.window.ms".to_string(), "-1".to_string());
1949 let err = ProducerConfig::new(&props).unwrap_err();
1950 let msg = err.to_string();
1951 assert!(
1952 msg.contains("metrics.sample.window.ms") && msg.contains("at least 0"),
1953 "unexpected message: {msg}"
1954 );
1955 }
1956
1957 #[test]
1963 fn test_metrics_recording_level_validator() {
1964 for level in ["INFO", "DEBUG", "TRACE"] {
1966 let mut props = base_props();
1967 props.insert("metrics.recording.level".to_string(), level.to_string());
1968 let c = ProducerConfig::new(&props).unwrap();
1969 assert_eq!(c.metrics_recording_level, level);
1970 }
1971
1972 let mut props = base_props();
1975 props.insert("metrics.recording.level".to_string(), "debug".to_string());
1976 let err = ProducerConfig::new(&props).unwrap_err();
1977 assert!(
1978 err.to_string().ends_with(
1979 "Invalid value debug for configuration metrics.recording.level: \
1980 String must be one of: INFO, DEBUG, TRACE"
1981 ),
1982 "unexpected message: {err}"
1983 );
1984
1985 let mut props = base_props();
1987 props.insert("metrics.recording.level".to_string(), "bogus".to_string());
1988 let err = ProducerConfig::new(&props).unwrap_err();
1989 assert!(
1990 err.to_string().ends_with(
1991 "Invalid value bogus for configuration metrics.recording.level: \
1992 String must be one of: INFO, DEBUG, TRACE"
1993 ),
1994 "unexpected message: {err}"
1995 );
1996 }
1997
1998 #[cfg(feature = "gzip")]
1999 #[test]
2000 fn test_new_basic() {
2001 let mut props = base_props();
2002 props.insert("bootstrap.servers".to_string(), "host1:9092,host2:9093".to_string());
2003 props.insert("client.id".to_string(), "my-producer".to_string());
2004 props.insert("batch.size".to_string(), "32768".to_string());
2005 props.insert("linger.ms".to_string(), "10".to_string());
2006 props.insert("acks".to_string(), "all".to_string());
2007 props.insert("compression.type".to_string(), "gzip".to_string());
2008 props.insert("enable.idempotence".to_string(), "false".to_string());
2009
2010 let config = ProducerConfig::new(&props).unwrap();
2011 assert_eq!(config.bootstrap_servers, vec!["host1:9092", "host2:9093"]);
2012 assert_eq!(config.client_id, "my-producer");
2013 assert_eq!(config.batch_size, 32768);
2014 assert_eq!(config.linger_ms, 10);
2015 assert_eq!(config.acks, -1);
2016 assert_eq!(config.compression_type, CompressionType::Gzip);
2017 assert!(!config.enable_idempotence);
2018 }
2019
2020 #[test]
2023 fn test_missing_bootstrap_servers_rejected_with_exact_message() {
2024 let err = ProducerConfig::new(&HashMap::new()).unwrap_err();
2025 let Error::Config(config_err) = &err else {
2026 panic!("expected a config error, got: {err:?}");
2027 };
2028 assert_eq!(
2029 config_err.message(),
2030 "Missing required configuration \"bootstrap.servers\" which has no default value."
2031 );
2032 }
2033
2034 #[test]
2035 fn test_new_defaults_for_missing() {
2036 let props = base_props();
2037 let config = ProducerConfig::new(&props).unwrap();
2038 assert_eq!(config.batch_size, 16384);
2040 assert_eq!(config.linger_ms, 5);
2041 assert_eq!(config.acks, -1);
2042 }
2043
2044 #[test]
2045 fn test_new_invalid_value() {
2046 let mut props = base_props();
2047 props.insert("batch.size".to_string(), "not-a-number".to_string());
2048 let result = ProducerConfig::new(&props);
2049 assert!(result.is_err());
2050 }
2051
2052 #[test]
2053 fn test_new_unknown_key_ignored() {
2054 let mut props = base_props();
2055 props.insert("unknown.key".to_string(), "value".to_string());
2056 props.insert("bootstrap.servers".to_string(), "localhost:9092".to_string());
2057 let config = ProducerConfig::new(&props).unwrap();
2058 assert_eq!(config.bootstrap_servers, vec!["localhost:9092"]);
2059 }
2060
2061 #[test]
2064 fn test_partitioner_type_consistent_random() {
2065 let mut props = base_props();
2066 props.insert("partitioner.type".to_string(), "ConsistentRandomPartitioner".to_string());
2067 let config = ProducerConfig::new(&props).unwrap();
2068 assert_eq!(config.partitioner_type.as_deref(), Some("ConsistentRandomPartitioner"));
2069 assert_eq!(config.key_hasher(), KeyHasher::Crc32);
2070 }
2071
2072 #[test]
2075 fn test_partitioner_type_murmur2_random() {
2076 let mut props = base_props();
2077 props.insert("partitioner.type".to_string(), "Murmur2RandomPartitioner".to_string());
2078 let config = ProducerConfig::new(&props).unwrap();
2079 assert_eq!(config.partitioner_type.as_deref(), Some("Murmur2RandomPartitioner"));
2080 assert_eq!(config.key_hasher(), KeyHasher::Murmur2);
2081 }
2082
2083 #[test]
2087 fn test_partitioner_type_unknown_rejected_with_exact_message() {
2088 let mut props = base_props();
2089 props.insert("partitioner.type".to_string(), "com.example.MyPartitioner".to_string());
2090 let err = ProducerConfig::new(&props).unwrap_err();
2091 assert!(
2095 err.to_string().ends_with(
2096 "Invalid value com.example.MyPartitioner for configuration partitioner.type: \
2097 Partitioner type com.example.MyPartitioner is not supported; the supported types are \
2098 ConsistentRandomPartitioner, Murmur2RandomPartitioner and RoundRobinPartitioner."
2099 ),
2100 "unexpected message: {err}"
2101 );
2102
2103 let mut props = base_props();
2104 props.insert(
2105 "partitioner.type".to_string(),
2106 "org.apache.kafka.clients.producer.RoundRobinPartitioner".to_string(),
2107 );
2108 let err = ProducerConfig::new(&props).unwrap_err();
2109 assert!(
2110 err.to_string().ends_with(
2111 "Invalid value org.apache.kafka.clients.producer.RoundRobinPartitioner for configuration \
2112 partitioner.type: Partitioner type org.apache.kafka.clients.producer.RoundRobinPartitioner \
2113 is not supported; the supported types are ConsistentRandomPartitioner, \
2114 Murmur2RandomPartitioner and RoundRobinPartitioner."
2115 ),
2116 "unexpected message: {err}"
2117 );
2118 }
2119
2120 #[test]
2124 fn test_partitioner_type_round_robin_simple_name() {
2125 let mut props = base_props();
2126 props.insert("partitioner.type".to_string(), "RoundRobinPartitioner".to_string());
2127 let config = ProducerConfig::new(&props).unwrap();
2128 assert_eq!(config.partitioner_type.as_deref(), Some("RoundRobinPartitioner"));
2129 assert_eq!(
2130 config.partitioner_type.as_deref(),
2131 Some(ProducerConfig::ROUND_ROBIN_PARTITIONER)
2132 );
2133 assert_eq!(config.key_hasher(), KeyHasher::Crc32);
2134 }
2135
2136 #[test]
2141 fn test_resolve_partitioner_round_robin_simple_name() {
2142 let mut props = base_props();
2143 props.insert("partitioner.type".to_string(), "RoundRobinPartitioner".to_string());
2144 let mut config = ProducerConfig::new(&props).unwrap();
2145 assert!(config.resolve_partitioner::<String, String>().expect("resolves").is_some());
2146 }
2147
2148 #[test]
2152 fn test_resolve_partitioner_default_none() {
2153 let mut config = ProducerConfig::new(&base_props()).unwrap();
2154 assert!(config.partitioner_type.is_none());
2155 assert!(config.resolve_partitioner::<String, String>().expect("resolves").is_none());
2156 }
2157
2158 #[test]
2164 fn test_resolve_partitioner_random_names_none() {
2165 for name in ["ConsistentRandomPartitioner", "Murmur2RandomPartitioner"] {
2166 let mut props = base_props();
2167 props.insert("partitioner.type".to_string(), name.to_string());
2168 let mut config = ProducerConfig::new(&props).unwrap();
2169 assert!(
2170 config.resolve_partitioner::<String, String>().expect("resolves").is_none(),
2171 "{name} must not resolve to a Partitioner instance"
2172 );
2173 }
2174 }
2175
2176 #[test]
2180 fn test_originals_kept_verbatim_without_generated_client_id() {
2181 let props = props_with(&[("partitioner.type", "RoundRobinPartitioner"), ("acks", "all")]);
2182 let config = ProducerConfig::new(&props).expect("valid");
2183
2184 assert_eq!(config.originals(), props);
2186 assert_eq!(
2187 config.originals().get("partitioner.type").map(String::as_str),
2188 Some("RoundRobinPartitioner")
2189 );
2190 assert_eq!(config.originals().get("acks").map(String::as_str), Some("all"));
2191
2192 assert!(!config.originals().contains_key("client.id"));
2195 assert!(
2196 config.client_id.starts_with("producer-"),
2197 "expected a derived client id, got {}",
2198 config.client_id
2199 );
2200 }
2201
2202 #[test]
2205 fn test_originals_includes_user_set_client_id() {
2206 let props = props_with(&[("client.id", "my-client")]);
2207 let config = ProducerConfig::new(&props).expect("valid");
2208
2209 assert_eq!(config.originals().get("client.id").map(String::as_str), Some("my-client"));
2210 assert_eq!(config.client_id, "my-client");
2211 }
2212
2213 #[test]
2214 fn test_new_transactional_id() {
2215 let config = ProducerConfig::new(&props_with(&[("transactional.id", "my-txn")])).unwrap();
2216 assert_eq!(config.transactional_id, Some("my-txn".to_string()));
2217
2218 let config = ProducerConfig::new(&props_with(&[("transactional.id", " my-txn ")])).unwrap();
2221 assert_eq!(config.transactional_id, Some("my-txn".to_string()));
2222
2223 for raw in ["", " "] {
2228 let error = ProducerConfig::new(&props_with(&[("transactional.id", raw)]))
2229 .expect_err("an empty transactional.id must be rejected");
2230 assert_eq!(
2231 error.message(),
2232 "Invalid value for configuration transactional.id: String must be non-empty",
2233 "for transactional.id={raw:?}"
2234 );
2235 assert!(
2236 matches!(error, Error::Config(_)),
2237 "for transactional.id={raw:?}: expected Error::Config, got {error:?}"
2238 );
2239 assert!(
2240 error.is_kafka_error(),
2241 "for transactional.id={raw:?}: a config error is a Kafka error"
2242 );
2243 }
2244
2245 assert_eq!(ProducerConfig::new(&base_props()).unwrap().transactional_id, None);
2247 }
2248
2249 #[test]
2253 fn test_config_def_range_validators() {
2254 let cases: [(&str, i64); 20] = [
2255 ("batch.size", 0),
2256 ("linger.ms", 0),
2257 ("buffer.memory", 0),
2258 ("max.block.ms", 0),
2259 ("retries", 0),
2260 ("delivery.timeout.ms", 0),
2261 ("request.timeout.ms", 0),
2262 ("max.request.size", 0),
2263 ("max.in.flight.requests.per.connection", 1),
2264 ("reconnect.backoff.ms", 0),
2265 ("reconnect.backoff.max.ms", 0),
2266 ("retry.backoff.ms", 0),
2267 ("retry.backoff.max.ms", 0),
2268 ("bootstrap.resolve.timeout.ms", 0),
2269 ("send.buffer.bytes", -1),
2270 ("receive.buffer.bytes", -1),
2271 ("metadata.max.age.ms", 0),
2272 ("metadata.max.idle.ms", 5000),
2273 ("partitioner.availability.timeout.ms", 0),
2274 ("metrics.sample.window.ms", 0),
2275 ];
2276 for (key, min) in cases {
2277 let below = (min - 1).to_string();
2278 let error = ProducerConfig::new(&props_with(&[(key, &below)]))
2279 .expect_err(&format!("{key}={below} must be rejected"));
2280 assert!(
2281 matches!(error, Error::Config(_)),
2282 "{key}: expected Error::Config, got {error:?}"
2283 );
2284 assert_eq!(
2285 error.message(),
2286 format!("Invalid value {below} for configuration {key}: Value must be at least {min}"),
2287 );
2288 let at = min.to_string();
2289 ProducerConfig::new(&props_with(&[(key, &at)]))
2290 .unwrap_or_else(|e| panic!("{key}={at} must be accepted: {e}"));
2291 }
2292 }
2293
2294 #[test]
2301 fn test_acks_valid_strings() {
2302 for (acks, expected) in [("all", -1), (" all ", -1), ("-1", -1), ("0", 0), ("1", 1)] {
2303 let props = props_with(&[("acks", acks), ("enable.idempotence", "false")]);
2304 assert_eq!(ProducerConfig::new(&props).unwrap().acks, expected, "acks={acks:?}");
2305 }
2306 for acks in ["ALL", "2", "-2", "100", "invalid"] {
2309 let error = ProducerConfig::new(&props_with(&[("acks", acks)])).expect_err("rejected");
2310 assert!(matches!(error, Error::Config(_)), "expected Error::Config, got {error:?}");
2311 assert_eq!(
2312 error.message(),
2313 format!("Invalid value {acks} for configuration acks: String must be one of: all, -1, 0, 1"),
2314 );
2315 assert!(error.is_kafka_error(), "for acks={acks}: a config error is a Kafka error");
2316 }
2317 }
2318
2319 #[test]
2320 fn test_parse_acks() {
2321 assert_eq!(ProducerConfig::parse_acks("all").unwrap(), "-1");
2322 assert_eq!(ProducerConfig::parse_acks("ALL").unwrap(), "-1");
2323 assert_eq!(ProducerConfig::parse_acks("-1").unwrap(), "-1");
2324 assert_eq!(ProducerConfig::parse_acks("0").unwrap(), "0");
2325 assert_eq!(ProducerConfig::parse_acks("1").unwrap(), "1");
2326
2327 let error = ProducerConfig::parse_acks("invalid").expect_err("a non-numeric acks is a config error");
2332 assert!(matches!(error, Error::Config(_)), "expected Error::Config, got {error:?}");
2333 assert_eq!(error.message(), "Invalid configuration value for 'acks': invalid");
2334 assert!(error.is_kafka_error());
2335 }
2336
2337 #[test]
2343 fn test_parse_bool_matches_java_boolean() {
2344 for raw in ["true", "True", "TRUE"] {
2346 let config = ProducerConfig::new(&props_with(&[("enable.idempotence", raw)]))
2347 .unwrap_or_else(|e| panic!("enable.idempotence={raw} should be valid: {e:?}"));
2348 assert!(config.enable_idempotence, "for enable.idempotence={raw}");
2349 }
2350 for raw in ["false", "False", " false "] {
2351 let config = ProducerConfig::new(&props_with(&[("enable.idempotence", raw)]))
2352 .unwrap_or_else(|e| panic!("enable.idempotence={raw} should be valid: {e:?}"));
2353 assert!(!config.enable_idempotence, "for enable.idempotence={raw}");
2354 }
2355
2356 let error = ProducerConfig::new(&props_with(&[("enable.idempotence", "yes")]))
2358 .expect_err("a non-boolean enable.idempotence must be rejected");
2359 assert_eq!(
2360 error.message(),
2361 "Invalid value yes for configuration enable.idempotence: Expected value to be either true or false",
2362 );
2363 assert!(matches!(error, Error::Config(_)), "expected Error::Config, got {error:?}");
2364 assert!(error.is_kafka_error());
2365 }
2366
2367 #[test]
2377 fn config_errors_are_inside_the_kafka_error_hierarchy() {
2378 let cases: [(&[(&str, &str)], &str); 6] = [
2379 (
2380 &[("acks", "not-a-number")],
2381 "Invalid value not-a-number for configuration acks: String must be one of: all, -1, 0, 1",
2382 ),
2383 (
2384 &[("enable.idempotence", "true"), ("retries", "0")],
2385 "Must set retries to non-zero when using the idempotent producer.",
2386 ),
2387 (
2388 &[("enable.idempotence", "true"), ("acks", "1")],
2389 "Must set acks to all in order to use the idempotent producer. Otherwise we cannot guarantee idempotence.",
2390 ),
2391 (
2392 &[
2393 ("enable.idempotence", "true"),
2394 ("max.in.flight.requests.per.connection", "6"),
2395 ],
2396 "To use the idempotent producer, max.in.flight.requests.per.connection must be set to at most 5. \
2397 Current value is 6.",
2398 ),
2399 (
2400 &[("enable.idempotence", "false"), ("transactional.id", "txn-id")],
2401 "Cannot set a transactional.id without also enabling idempotence.",
2402 ),
2403 (
2404 &[
2405 ("transactional.id", "txn-id"),
2406 ("transaction.two.phase.commit.enable", "true"),
2407 ("transaction.timeout.ms", "1000"),
2408 ],
2409 "Cannot set transaction.timeout.ms when transaction.two.phase.commit.enable is set to true. \
2410 Transactions will not expire with two-phase commit enabled.",
2411 ),
2412 ];
2413
2414 for (props, expected_message) in cases {
2415 let error = ProducerConfig::new(&props_with(props)).expect_err("expected a config error");
2416 assert_eq!(error.message(), expected_message, "for {props:?}");
2417 assert!(
2418 matches!(error, Error::Config(_)),
2419 "for {props:?}: expected Error::Config, got {error:?}"
2420 );
2421 assert!(error.is_kafka_error(), "for {props:?}: a config error is a Kafka error");
2422 }
2423 }
2424
2425 #[test]
2428 fn test_client_dns_lookup() {
2429 assert_eq!(ProducerConfig::CLIENT_DNS_LOOKUP_CONFIG, "client.dns.lookup");
2430 let mut props = base_props();
2431 props.insert("bootstrap.servers".to_string(), "localhost:9092".to_string());
2432 assert_eq!(
2433 ProducerConfig::new(&props).unwrap().client_dns_lookup,
2434 ClientDnsLookup::UseAllDnsIps
2435 );
2436
2437 props.insert(
2438 CommonClientConfigs::CLIENT_DNS_LOOKUP_CONFIG.to_string(),
2439 "resolve_canonical_bootstrap_servers_only".to_string(),
2440 );
2441 assert_eq!(
2442 ProducerConfig::new(&props).unwrap().client_dns_lookup,
2443 ClientDnsLookup::ResolveCanonicalBootstrapServersOnly
2444 );
2445
2446 props.insert(CommonClientConfigs::CLIENT_DNS_LOOKUP_CONFIG.to_string(), "default".to_string());
2447 assert_eq!(
2448 ProducerConfig::new(&props).unwrap_err().message(),
2449 "Invalid value default for configuration client.dns.lookup: String must be one of: \
2450 use_all_dns_ips, resolve_canonical_bootstrap_servers_only"
2451 );
2452 }
2453
2454 #[test]
2456 #[doc(alias = "org.apache.kafka.clients.producer.ProducerConfigTest#testInvalidSecurityProtocol")]
2457 fn test_invalid_security_protocol() {
2458 let mut props = base_props();
2459 props.insert("security.protocol".to_string(), "abc".to_string());
2460 props.insert("bootstrap.servers".to_string(), "localhost:9092".to_string());
2461 let err = ProducerConfig::new(&props).unwrap_err();
2462 let msg = format!("{}", err);
2463 assert!(
2464 msg.contains("security.protocol"),
2465 "Error message should contain config key, got: {}",
2466 msg
2467 );
2468 }
2469
2470 #[test]
2472 #[doc(alias = "org.apache.kafka.clients.producer.ProducerConfigTest#testDefaultBufferMemoryAllocationStrategy")]
2473 fn test_default_buffer_memory_allocation_strategy() {
2474 let producer_config = ProducerConfig::new(&base_props()).unwrap();
2475 assert_eq!(
2476 ProducerConfig::BUFFER_MEMORY_ALLOCATION_STRATEGY_FULL,
2477 producer_config.buffer_memory_allocation_strategy
2478 );
2479 }
2480
2481 #[test]
2483 #[doc(alias = "org.apache.kafka.clients.producer.ProducerConfigTest#testValidBufferMemoryAllocationStrategy")]
2484 fn test_valid_buffer_memory_allocation_strategy() {
2485 let mut props = base_props();
2486 props.insert(
2487 ProducerConfig::BUFFER_MEMORY_ALLOCATION_STRATEGY_CONFIG.to_string(),
2488 ProducerConfig::BUFFER_MEMORY_ALLOCATION_STRATEGY_INCREMENTAL.to_string(),
2489 );
2490 let producer_config = ProducerConfig::new(&props).unwrap();
2491 assert_eq!(
2492 ProducerConfig::BUFFER_MEMORY_ALLOCATION_STRATEGY_INCREMENTAL,
2493 producer_config.buffer_memory_allocation_strategy
2494 );
2495
2496 props.insert(
2498 ProducerConfig::BUFFER_MEMORY_ALLOCATION_STRATEGY_CONFIG.to_string(),
2499 "INCREMENTAL".to_string(),
2500 );
2501 assert_eq!(
2502 "INCREMENTAL",
2503 ProducerConfig::new(&props).unwrap().buffer_memory_allocation_strategy
2504 );
2505 }
2506
2507 #[test]
2509 #[doc(alias = "org.apache.kafka.clients.producer.ProducerConfigTest#testInvalidBufferMemoryAllocationStrategy")]
2510 fn test_invalid_buffer_memory_allocation_strategy() {
2511 let mut props = base_props();
2512 props.insert(
2513 ProducerConfig::BUFFER_MEMORY_ALLOCATION_STRATEGY_CONFIG.to_string(),
2514 "abc".to_string(),
2515 );
2516 let err = ProducerConfig::new(&props).unwrap_err();
2517 assert!(matches!(err, Error::Config(_)), "expected Error::Config, got {err:?}");
2518 assert!(err.message().contains(ProducerConfig::BUFFER_MEMORY_ALLOCATION_STRATEGY_CONFIG));
2519 assert_eq!(
2521 err.message(),
2522 "Invalid value abc for configuration buffer.memory.allocation.strategy: \
2523 String must be one of (case insensitive): FULL, INCREMENTAL"
2524 );
2525 }
2526
2527 #[test]
2533 fn test_invalid_compression_type_is_a_config_error() {
2534 let mut props = base_props();
2535 props.insert("compression.type".to_string(), "gzipp".to_string());
2536 props.insert("bootstrap.servers".to_string(), "localhost:9092".to_string());
2537 let err = ProducerConfig::new(&props).expect_err("gzipp is not a compression type");
2538 assert_eq!(
2539 err.message(),
2540 "Invalid value gzipp for configuration compression.type: \
2541 String must be one of: none, gzip, snappy, lz4, zstd"
2542 );
2543 assert!(matches!(err, Error::Config(_)), "expected Error::Config, got {err:?}");
2544 assert!(err.is_kafka_error(), "a ConfigException is a KafkaException");
2546 assert!(!err.is_api_error(), "a ConfigException is not an ApiException");
2547 }
2548
2549 #[test]
2551 fn test_valid_compression_types_parse() {
2552 for name in CompressionType::names()
2553 .iter()
2554 .filter(|name| Compression::is_enabled(CompressionType::for_name(name).unwrap()))
2555 {
2556 let mut props = base_props();
2557 props.insert("compression.type".to_string(), name.to_string());
2558 props.insert("bootstrap.servers".to_string(), "localhost:9092".to_string());
2559 let config = ProducerConfig::new(&props)
2560 .unwrap_or_else(|e| panic!("compression.type={name} should be valid: {e:?}"));
2561 assert_eq!(config.compression_type.name(), *name);
2562 }
2563 }
2564
2565 #[test]
2569 fn test_disabled_compression_type_is_rejected() {
2570 for name in CompressionType::names()
2571 .iter()
2572 .filter(|name| !Compression::is_enabled(CompressionType::for_name(name).unwrap()))
2573 {
2574 let mut props = base_props();
2575 props.insert("compression.type".to_string(), name.to_string());
2576 props.insert("bootstrap.servers".to_string(), "localhost:9092".to_string());
2577 let error = ProducerConfig::new(&props).unwrap_err();
2578 assert!(error.is_config_error(), "{error:?}");
2579 assert_eq!(
2580 format!(
2581 "Invalid value {name} for configuration compression.type: Compression type {name} is not \
2582 available: the client was built without the `{name}` feature"
2583 ),
2584 error.message()
2585 );
2586 }
2587 }
2588
2589 #[test]
2591 #[doc(alias = "org.apache.kafka.clients.producer.ProducerConfigTest#testCaseInsensitiveSecurityProtocol")]
2592 fn test_case_insensitive_security_protocol() {
2593 let mut props = base_props();
2594 props.insert("security.protocol".to_string(), "sasl_ssl".to_string());
2595 props.insert("sasl.mechanism".to_string(), "PLAIN".to_string());
2597 props.insert("bootstrap.servers".to_string(), "localhost:9092".to_string());
2598 let config = ProducerConfig::new(&props).unwrap();
2599 assert_eq!(config.security_protocol, SecurityProtocol::SaslSsl);
2600 }
2601
2602 #[test]
2603 fn test_sasl_config_from_properties() {
2604 let mut props = base_props();
2605 props.insert("sasl.mechanism".to_string(), "PLAIN".to_string());
2606 props.insert(
2607 "sasl.jaas.config".to_string(),
2608 "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"alice\" password=\"secret\";"
2609 .to_string(),
2610 );
2611 props.insert("bootstrap.servers".to_string(), "localhost:9092".to_string());
2612 let config = ProducerConfig::new(&props).unwrap();
2613 assert_eq!(config.sasl_config.mechanism, "PLAIN");
2614 assert_eq!(config.sasl_config.resolve_username().as_deref(), Some("alice"));
2615 assert_eq!(config.sasl_config.resolve_password().as_deref(), Some("secret"));
2616 }
2617
2618 #[test]
2624 fn test_unsupported_sasl_mechanism_rejected() {
2625 let jaas = "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"u\" password=\"p\";";
2626 let build = |extra: &[(&str, &str)]| {
2627 let mut props = base_props();
2628 for (k, v) in extra {
2629 props.insert((*k).to_string(), (*v).to_string());
2630 }
2631 ProducerConfig::new(&props)
2632 };
2633
2634 let err = build(&[
2635 ("security.protocol", "SASL_PLAINTEXT"),
2636 ("sasl.mechanism", "SCRAM-SHA-256"),
2637 ("sasl.jaas.config", jaas),
2638 ])
2639 .unwrap_err();
2640 assert!(err.is_config_error(), "{err:?}");
2641 assert_eq!(
2642 err.to_string(),
2643 "ConfigError: Invalid value SCRAM-SHA-256 for configuration sasl.mechanism: only PLAIN is supported by \
2644 this client"
2645 );
2646
2647 let err = build(&[("security.protocol", "SASL_SSL")]).unwrap_err();
2649 assert!(err.is_config_error(), "{err:?}");
2650 assert_eq!(
2651 err.to_string(),
2652 "ConfigError: Invalid value GSSAPI for configuration sasl.mechanism: only PLAIN is supported by this client"
2653 );
2654
2655 let err = build(&[("security.protocol", "SASL_PLAINTEXT"), ("sasl.mechanism", "plain")]).unwrap_err();
2657 assert!(err.is_config_error(), "{err:?}");
2658
2659 build(&[("security.protocol", "PLAINTEXT")]).unwrap();
2660 build(&[
2661 ("security.protocol", "SASL_PLAINTEXT"),
2662 ("sasl.mechanism", "PLAIN"),
2663 ("sasl.jaas.config", jaas),
2664 ])
2665 .unwrap();
2666
2667 let err = build(&[("security.protocol", "SASL_PLAINTEXT"), ("sasl.mechanism", "")]).unwrap_err();
2669 assert!(err.is_config_error(), "{err:?}");
2670 assert_eq!(
2671 err.to_string(),
2672 "ConfigError: Invalid value null for configuration sasl.mechanism: When the security.protocol \
2673 configuration enables SASL, mechanism must be non-null and non-empty string."
2674 );
2675 }
2676
2677 #[test]
2678 fn test_ssl_config_from_properties() {
2679 let mut props = base_props();
2680 props.insert("ssl.truststore.location".to_string(), "/path/to/truststore.pem".to_string());
2681 props.insert("ssl.keystore.location".to_string(), "/path/to/keystore.pem".to_string());
2682 props.insert("ssl.endpoint.identification.algorithm".to_string(), String::new());
2683 props.insert("bootstrap.servers".to_string(), "localhost:9092".to_string());
2684 let config = ProducerConfig::new(&props).unwrap();
2685 assert_eq!(
2686 config.ssl_config.truststore_location.as_deref(),
2687 Some("/path/to/truststore.pem")
2688 );
2689 assert_eq!(config.ssl_config.keystore_location.as_deref(), Some("/path/to/keystore.pem"));
2690 assert_eq!(config.ssl_config.endpoint_identification_algorithm, "");
2691 }
2692
2693 fn base_properties() -> HashMap<String, String> {
2702 HashMap::from([("bootstrap.servers".to_string(), "localhost:9999".to_string())])
2703 }
2704
2705 #[test]
2709 fn test_partitioner_rack_aware_and_client_rack() {
2710 assert_eq!(ProducerConfig::PARTITIONER_RACK_AWARE_CONFIG, "partitioner.rack.aware");
2711 assert_eq!(ProducerConfig::CLIENT_RACK_CONFIG, "client.rack");
2712 assert_eq!(ProducerConfig::CLIENT_RACK_CONFIG, CommonClientConfigs::CLIENT_RACK_CONFIG);
2713 assert_eq!(ProducerConfig::DEFAULT_CLIENT_RACK, "");
2714
2715 let mut props = base_props();
2716 props.insert("partitioner.rack.aware".to_string(), "true".to_string());
2717 props.insert("client.rack".to_string(), " rack0 ".to_string());
2718 let config = ProducerConfig::new(&props).expect("valid config");
2719 assert!(config.partitioner_rack_aware);
2720 assert_eq!(config.client_rack, "rack0");
2721
2722 let mut props = base_props();
2723 props.insert("partitioner.rack.aware".to_string(), "yes".to_string());
2724 let err = ProducerConfig::new(&props).unwrap_err();
2725 assert!(matches!(err, Error::Config(_)), "got {err:?}");
2726 assert_eq!(
2727 err.message(),
2728 "Invalid value yes for configuration partitioner.rack.aware: Expected value to be either true or false"
2729 );
2730 }
2731
2732 fn props_with(extra: &[(&str, &str)]) -> HashMap<String, String> {
2733 let mut props = base_properties();
2734 for (key, value) in extra {
2735 props.insert((*key).to_string(), (*value).to_string());
2736 }
2737 props
2738 }
2739
2740 #[test]
2742 fn test_overwrite_acks_and_retries_for_idempotent_producers() {
2743 let props = props_with(&[("transactional.id", "transactionalId")]);
2744 let config = ProducerConfig::new(&props).expect("config should be valid");
2745
2746 assert!(config.enable_idempotence);
2747 assert_eq!(config.acks, -1);
2748 assert_eq!(config.retries, i32::MAX);
2749 assert_eq!(config.client_id, "producer-transactionalId");
2751 }
2752
2753 #[test]
2755 fn test_acks_and_idempotence_for_idempotent_producers() {
2756 let config =
2758 ProducerConfig::new(&props_with(&[("acks", "0"), ("enable.idempotence", "false")])).expect("valid");
2759 assert!(!config.enable_idempotence, "idempotence should be overwritten");
2760 assert_eq!(config.acks, 0, "acks should be overwritten");
2761
2762 let config = ProducerConfig::new(&props_with(&[("transactional.id", "transactionalId")])).expect("valid");
2764 assert!(config.enable_idempotence, "idempotence should be set with the default value");
2765 assert_eq!(config.acks, -1, "acks should be set with the default value");
2766
2767 let config =
2769 ProducerConfig::new(&props_with(&[("acks", "all"), ("enable.idempotence", "false")])).expect("valid");
2770 assert!(!config.enable_idempotence, "idempotence should be overwritten");
2771 assert_eq!(config.acks, -1, "acks should be overwritten");
2772
2773 let config = ProducerConfig::new(&props_with(&[("acks", "0")])).expect("valid");
2776 assert!(
2777 !config.enable_idempotence,
2778 "idempotence should be disabled when acks not set to all and `enable.idempotence` is unset"
2779 );
2780 assert_eq!(config.acks, 0, "acks should be set with overridden value");
2781
2782 let config = ProducerConfig::new(&props_with(&[("acks", "1")])).expect("valid");
2784 assert!(!config.enable_idempotence);
2785 assert_eq!(config.acks, 1);
2786
2787 let error = ProducerConfig::new(&props_with(&[
2789 ("acks", "0"),
2790 ("enable.idempotence", "false"),
2791 ("transactional.id", "transactionalId"),
2792 ]))
2793 .expect_err("transactional.id requires idempotence");
2794 assert_eq!(
2795 error.message(),
2796 "Cannot set a transactional.id without also enabling idempotence."
2797 );
2798
2799 let error = ProducerConfig::new(&props_with(&[("acks", "1"), ("enable.idempotence", "true")]))
2802 .expect_err("explicit idempotence with acks!=all must error");
2803 assert_eq!(
2804 error.message(),
2805 "Must set acks to all in order to use the idempotent producer. Otherwise we cannot guarantee idempotence."
2806 );
2807
2808 let error = ProducerConfig::new(&props_with(&[("acks", "0"), ("transactional.id", "transactionalId")]))
2811 .expect_err("transactional producer requires acks=all");
2812 assert_eq!(
2813 error.message(),
2814 "Cannot set a transactional.id without also enabling idempotence."
2815 );
2816 }
2817
2818 #[test]
2820 fn test_retries_and_idempotence_for_idempotent_producers() {
2821 let config =
2823 ProducerConfig::new(&props_with(&[("retries", "0"), ("enable.idempotence", "false")])).expect("valid");
2824 assert!(!config.enable_idempotence, "idempotence should be overwritten");
2825 assert_eq!(config.retries, 0, "retries should be overwritten");
2826
2827 let config = ProducerConfig::new(&props_with(&[("retries", "0")])).expect("valid");
2829 assert!(
2830 !config.enable_idempotence,
2831 "idempotence should be disabled when retries set to 0 and `enable.idempotence` is unset"
2832 );
2833 assert_eq!(config.retries, 0, "retries should be set with overridden value");
2834
2835 let error = ProducerConfig::new(&props_with(&[
2837 ("retries", "0"),
2838 ("enable.idempotence", "false"),
2839 ("transactional.id", "transactionalId"),
2840 ]))
2841 .expect_err("transactional.id requires idempotence");
2842 assert_eq!(
2843 error.message(),
2844 "Cannot set a transactional.id without also enabling idempotence."
2845 );
2846
2847 let error = ProducerConfig::new(&props_with(&[("retries", "0"), ("enable.idempotence", "true")]))
2849 .expect_err("explicit idempotence with retries=0 must error");
2850 assert_eq!(
2851 error.message(),
2852 "Must set retries to non-zero when using the idempotent producer."
2853 );
2854
2855 let error = ProducerConfig::new(&props_with(&[("retries", "0"), ("transactional.id", "transactionalId")]))
2857 .expect_err("transactional producer requires non-zero retries");
2858 assert_eq!(
2859 error.message(),
2860 "Cannot set a transactional.id without also enabling idempotence."
2861 );
2862 }
2863
2864 #[test]
2866 fn test_inflight_requests_and_idempotence_for_idempotent_producers() {
2867 let config = ProducerConfig::new(&props_with(&[
2869 ("max.in.flight.requests.per.connection", "6"),
2870 ("enable.idempotence", "false"),
2871 ]))
2872 .expect("valid");
2873 assert!(!config.enable_idempotence, "idempotence should be overwritten");
2874 assert_eq!(config.max_in_flight_requests_per_connection, 6);
2875
2876 let error = ProducerConfig::new(&props_with(&[("max.in.flight.requests.per.connection", "6")]))
2879 .expect_err("in-flight above 5 must error");
2880 assert_eq!(
2881 error.message(),
2882 "To use the idempotent producer, max.in.flight.requests.per.connection must be set to at most 5. Current value is 6."
2883 );
2884
2885 let error = ProducerConfig::new(&props_with(&[
2890 ("max.in.flight.requests.per.connection", "5"),
2891 ("enable.idempotence", "false"),
2892 ("transactional.id", "transactionalId"),
2893 ]))
2894 .expect_err("transactional.id without idempotence must error even at the in-flight cap");
2895 assert_eq!(
2896 error.message(),
2897 "Cannot set a transactional.id without also enabling idempotence."
2898 );
2899
2900 let error = ProducerConfig::new(&props_with(&[
2903 ("max.in.flight.requests.per.connection", "6"),
2904 ("enable.idempotence", "true"),
2905 ]))
2906 .expect_err("explicit idempotence above the cap must error");
2907 assert_eq!(
2908 error.message(),
2909 "To use the idempotent producer, max.in.flight.requests.per.connection must be set to at most 5. Current value is 6."
2910 );
2911
2912 let error = ProducerConfig::new(&props_with(&[
2915 ("max.in.flight.requests.per.connection", "6"),
2916 ("transactional.id", "transactionalId"),
2917 ]))
2918 .expect_err("transactional producer above the cap must error");
2919 assert_eq!(
2920 error.message(),
2921 "To use the idempotent producer, max.in.flight.requests.per.connection must be set to at most 5. Current value is 6."
2922 );
2923 }
2924
2925 #[test]
2927 #[doc(alias = "org.apache.kafka.clients.producer.ProducerConfigTest#testUpperboundCheckOfEnableIdempotence")]
2928 fn test_upperbound_check_of_enable_idempotence() {
2929 let error = ProducerConfig::new(&props_with(&[("max.in.flight.requests.per.connection", "6")]))
2930 .expect_err("6 exceeds the cap");
2931 assert_eq!(
2932 error.message(),
2933 "To use the idempotent producer, max.in.flight.requests.per.connection must be set to at most 5. Current value is 6."
2934 );
2935
2936 ProducerConfig::new(&props_with(&[("max.in.flight.requests.per.connection", "5")]))
2938 .expect("5 is at the cap and must be accepted");
2939 }
2940
2941 #[test]
2943 #[doc(
2944 alias = "org.apache.kafka.clients.producer.ProducerConfigTest#testTwoPhaseCommitIncompatibleWithTransactionTimeout"
2945 )]
2946 fn test_two_phase_commit_incompatible_with_transaction_timeout() {
2947 let both = props_with(&[
2948 ("enable.idempotence", "true"),
2949 ("transactional.id", "test-txn-id"),
2950 ("transaction.two.phase.commit.enable", "true"),
2951 ("transaction.timeout.ms", "60000"),
2952 ]);
2953 let error = ProducerConfig::new(&both).expect_err("2PC and timeout conflict");
2954 assert!(error.message().contains(ProducerConfig::TRANSACTION_TIMEOUT_CONFIG));
2955 assert!(
2956 error
2957 .message()
2958 .contains(ProducerConfig::TRANSACTION_TWO_PHASE_COMMIT_ENABLE_CONFIG)
2959 );
2960
2961 let only_2pc = props_with(&[
2963 ("enable.idempotence", "true"),
2964 ("transactional.id", "test-txn-id"),
2965 ("transaction.two.phase.commit.enable", "true"),
2966 ]);
2967 ProducerConfig::new(&only_2pc).expect("2PC alone is valid");
2968
2969 let only_timeout = props_with(&[
2970 ("enable.idempotence", "true"),
2971 ("transactional.id", "test-txn-id"),
2972 ("transaction.two.phase.commit.enable", "false"),
2973 ("transaction.timeout.ms", "60000"),
2974 ]);
2975 ProducerConfig::new(&only_timeout).expect("timeout alone is valid");
2976 }
2977
2978 #[test]
2982 fn test_explicit_client_id_is_preserved() {
2983 let config = ProducerConfig::new(&props_with(&[("client.id", "my-client")])).expect("valid");
2984 assert_eq!(config.client_id, "my-client");
2985 }
2986
2987 #[test]
2992 fn test_explicit_client_id_is_trimmed() {
2993 let config = ProducerConfig::new(&props_with(&[("client.id", " my-client ")])).expect("valid");
2994 assert_eq!(config.client_id, "my-client");
2995 let config = ProducerConfig::new(&props_with(&[("client.id", " ")])).expect("valid");
2996 assert_eq!(config.client_id, "");
2997 }
2998
2999 #[test]
3002 fn test_client_id_derived_from_sequence() {
3003 let first = ProducerConfig::new(&base_properties()).expect("valid");
3004 let second = ProducerConfig::new(&base_properties()).expect("valid");
3005
3006 assert!(
3007 first.client_id.starts_with("producer-"),
3008 "expected a derived client id, got {}",
3009 first.client_id
3010 );
3011 assert_ne!(first.client_id, second.client_id);
3014 let n: i32 = first.client_id.trim_start_matches("producer-").parse().expect("numeric suffix");
3015 assert!(n >= 1, "Java's sequence starts at 1");
3016 }
3017
3018 #[test]
3024 fn test_copy_for_client_keeps_the_resolved_client_id() {
3025 let first = ProducerConfig::new(&base_properties()).expect("valid");
3026 let copy = first.copy_for_client().expect("copied");
3027 let second_copy = first.copy_for_client().expect("copied");
3028 let next = ProducerConfig::new(&base_properties()).expect("valid");
3029 assert_eq!(copy.client_id, first.client_id);
3030 assert_eq!(second_copy.client_id, first.client_id);
3031 assert_eq!(
3032 copy.get_string(ProducerConfig::CLIENT_ID_CONFIG).unwrap(),
3033 Some(first.client_id.as_str())
3034 );
3035 assert!(!copy.user_configured(ProducerConfig::CLIENT_ID_CONFIG));
3036 let n1: i32 = first.client_id.trim_start_matches("producer-").parse().expect("numeric suffix");
3037 let n2: i32 = next.client_id.trim_start_matches("producer-").parse().expect("numeric suffix");
3038 assert!(n2 > n1, "{} then {}", first.client_id, next.client_id);
3040 let unused = first.unused();
3041 assert_eq!(second_copy.unused(), unused);
3042 first.ignore(ProducerConfig::BOOTSTRAP_SERVERS_CONFIG);
3043 assert_eq!(second_copy.unused(), unused, "the copy's used set is its own");
3044
3045 let config = ProducerConfig::new(&props_with(&[("client.id", "mine")])).expect("valid");
3046 assert_eq!(config.copy_for_client().expect("copied").client_id, "mine");
3047 let config = ProducerConfig::new(&props_with(&[("transactional.id", "t")])).expect("valid");
3048 assert_eq!(config.copy_for_client().expect("copied").client_id, "producer-t");
3049 }
3050
3051 #[test]
3057 fn test_copy_for_client_logs_no_configuration_line() {
3058 let props = props_with(&[
3059 (ProducerConfig::BOOTSTRAP_SERVERS_CONFIG, "localhost:9092,localhost:9092"),
3060 (ProducerConfig::RETRY_BACKOFF_MS_CONFIG, "2000"),
3061 (ProducerConfig::RETRY_BACKOFF_MAX_MS_CONFIG, "1000"),
3062 (ProducerConfig::RECONNECT_BACKOFF_MS_CONFIG, "100"),
3063 (CommonClientConfigs::SOCKET_CONNECTION_SETUP_TIMEOUT_MS_CONFIG, "40000"),
3064 (CommonClientConfigs::SOCKET_CONNECTION_SETUP_TIMEOUT_MAX_MS_CONFIG, "30000"),
3065 (ProducerConfig::RETRIES_CONFIG, "0"),
3066 (ProducerConfig::ACKS_CONFIG, "1"),
3067 ]);
3068 let (config, new_lines) = capture_logs(|| ProducerConfig::new(&props).expect("valid"));
3069 let lines = |lines: &[(Level, String)]| {
3070 lines
3071 .iter()
3072 .map(|(level, line)| (*level, line.split(' ').take(3).collect::<Vec<_>>().join(" ")))
3073 .collect::<Vec<_>>()
3074 };
3075 assert_eq!(
3076 lines(&new_lines),
3077 [
3078 (Level::Warn, "Configuration key \"bootstrap.servers\"".to_string()),
3079 (Level::Warn, "Configuration 'retry.backoff.ms' with".to_string()),
3080 (
3081 Level::Warn,
3082 "Configuration 'socket.connection.setup.timeout.ms' with".to_string()
3083 ),
3084 (Level::Warn, "Disabling exponential reconnect".to_string()),
3085 (Level::Info, "Idempotence will be".to_string()),
3086 (Level::Info, "Idempotence will be".to_string()),
3087 ],
3088 "{new_lines:?}"
3089 );
3090 let (copy, copy_lines) = capture_logs(|| config.copy_for_client().expect("copied"));
3091 assert!(copy_lines.is_empty(), "{copy_lines:?}");
3092 assert_eq!(copy.unused(), ProducerConfig::new(&props).expect("valid").unused());
3093 }
3094
3095 #[test]
3100 fn test_bootstrap_servers_list_parsing() {
3101 for value in [
3102 "localhost:1,localhost:2",
3103 "localhost:1, localhost:2",
3104 " localhost:1 ,localhost:2 ",
3105 ] {
3106 let props = HashMap::from([("bootstrap.servers".to_string(), value.to_string())]);
3107 let config = ProducerConfig::new(&props).unwrap();
3108 assert_eq!(
3109 config.bootstrap_servers,
3110 ["localhost:1".to_string(), "localhost:2".to_string()],
3111 "{value:?}"
3112 );
3113 let addresses =
3114 crate::ClientUtils::parse_and_validate_addresses(&config.bootstrap_servers, config.client_dns_lookup)
3115 .unwrap();
3116 assert_eq!(addresses.len(), 2, "{value:?}");
3117 }
3118 }
3119
3120 #[test]
3125 fn test_bootstrap_servers_valid_list() {
3126 let error_message = |value: &str| {
3127 let props = HashMap::from([("bootstrap.servers".to_string(), value.to_string())]);
3128 match ProducerConfig::new(&props) {
3129 Err(Error::Config(e)) => e.message().to_string(),
3130 other => panic!("expected a ConfigError for {value:?}, got {other:?}"),
3131 }
3132 };
3133 for value in ["localhost:9092,,localhost:9093", "a:1, ,b:1", "a:1,"] {
3134 assert_eq!(
3135 error_message(value),
3136 "Configuration 'bootstrap.servers' values must not be empty.",
3137 "{value:?}"
3138 );
3139 }
3140 let props = HashMap::from([("bootstrap.servers".to_string(), "a:1,a:1".to_string())]);
3142 assert_eq!(ProducerConfig::new(&props).unwrap().bootstrap_servers, ["a:1".to_string()]);
3143 assert_eq!(
3144 error_message(",,"),
3145 "Configuration 'bootstrap.servers' values must not be empty."
3146 );
3147 for value in ["", " "] {
3148 assert_eq!(
3149 error_message(value),
3150 "Configuration 'bootstrap.servers' must not be empty. Valid values include: any non-empty value",
3151 "{value:?}"
3152 );
3153 }
3154 let props = HashMap::from([("bootstrap.servers".to_string(), "a:1,b:1".to_string())]);
3155 assert_eq!(
3156 ProducerConfig::new(&props).unwrap().bootstrap_servers,
3157 ["a:1".to_string(), "b:1".to_string()]
3158 );
3159 }
3160
3161 #[test]
3174 fn test_debug_hides_secrets_and_renders_originals_as_key_set() {
3175 let secrets = [
3176 (
3177 "sasl.jaas.config",
3178 "org.apache.kafka.common.security.plain.PlainLoginModule required \
3179 username=\"jaas-user\" password=\"jaas-S3cr3t\";",
3180 ),
3181 ("ssl.truststore.password", "truststore-password-S3cr3t"),
3182 ("ssl.truststore.certificates", "truststore-certificates-S3cr3t"),
3183 ("ssl.keystore.password", "keystore-password-S3cr3t"),
3184 ("ssl.keystore.key", "keystore-key-S3cr3t"),
3185 ("ssl.keystore.certificate.chain", "keystore-certificate-chain-S3cr3t"),
3186 ("ssl.key.password", "key-password-S3cr3t"),
3187 (
3188 "sasl.oauthbearer.client.credentials.client.secret",
3189 "oauth-client-secret-S3cr3t",
3190 ),
3191 ("sasl.oauthbearer.assertion.private.key.passphrase", "oauth-passphrase-S3cr3t"),
3192 ("basic.auth.user.info", "registry-key:registry-S3cr3t"),
3193 ];
3194 let mut props: HashMap<String, String> = secrets
3195 .iter()
3196 .map(|(key, value)| (key.to_string(), value.to_string()))
3197 .collect();
3198 props.insert("bootstrap.servers".to_string(), "visible-host:9092".to_string());
3199 let config = ProducerConfig::new(&props).unwrap();
3200
3201 assert_eq!(config.sasl_config.resolve_password().as_deref(), Some("jaas-S3cr3t"));
3205 assert_eq!(
3206 config.ssl_config.keystore_key.as_ref().map(Password::value),
3207 Some("keystore-key-S3cr3t")
3208 );
3209 for (key, value) in secrets {
3210 assert_eq!(config.originals().get(key).map(String::as_str), Some(value), "{key}");
3211 }
3212
3213 for rendered in [format!("{config:?}"), format!("{config:#?}")] {
3214 for (key, value) in secrets {
3215 assert!(!rendered.contains(value), "the value of {key} leaked: {rendered}");
3216 assert!(rendered.contains(&format!("{key:?}")), "key {key} missing: {rendered}");
3219 }
3220 for fragment in ["S3cr3t", "jaas-user", "PlainLoginModule", "registry-key"] {
3221 assert!(!rendered.contains(fragment), "{fragment:?} leaked: {rendered}");
3222 }
3223 assert_eq!(rendered.matches(Password::HIDDEN).count(), 4, "{rendered}");
3225 assert_eq!(rendered.matches("visible-host:9092").count(), 1, "{rendered}");
3228 }
3229
3230 let rendered = format!("{config:?}");
3231 assert!(rendered.contains("bootstrap_servers: [\"visible-host:9092\"]"), "{rendered}");
3232 for field in [
3233 "jaas_config",
3234 "truststore_certificates",
3235 "keystore_key",
3236 "keystore_certificate_chain",
3237 ] {
3238 let hidden = format!("{field}: Some([hidden])");
3239 assert!(rendered.contains(&hidden), "{hidden} missing: {rendered}");
3240 }
3241 let expected_originals = concat!(
3244 "ProducerConfig { config: AbstractConfig { originals: [",
3245 "\"basic.auth.user.info\", ",
3246 "\"bootstrap.servers\", ",
3247 "\"sasl.jaas.config\", ",
3248 "\"sasl.oauthbearer.assertion.private.key.passphrase\", ",
3249 "\"sasl.oauthbearer.client.credentials.client.secret\", ",
3250 "\"ssl.key.password\", ",
3251 "\"ssl.keystore.certificate.chain\", ",
3252 "\"ssl.keystore.key\", ",
3253 "\"ssl.keystore.password\", ",
3254 "\"ssl.truststore.certificates\", ",
3255 "\"ssl.truststore.password\"",
3256 "], .. }, ",
3257 );
3258 assert!(rendered.starts_with(expected_originals), "{rendered}");
3259 }
3260
3261 #[test]
3271 fn test_default_metadata_recovery_strategy() {
3272 let config = ProducerConfig::new(&base_props()).unwrap();
3273 assert_eq!(
3274 Some("rebootstrap"),
3275 config
3276 .get_string(CommonClientConfigs::METADATA_RECOVERY_STRATEGY_CONFIG)
3277 .unwrap()
3278 );
3279 }
3280
3281 #[test]
3284 fn test_typed_getters() {
3285 let config = ProducerConfig::new(&props_with(&[
3286 (ProducerConfig::BATCH_SIZE_CONFIG, "7"),
3287 (ProducerConfig::ACKS_CONFIG, "all"),
3288 ]))
3289 .unwrap();
3290 assert_eq!(Some(7), config.get_int(ProducerConfig::BATCH_SIZE_CONFIG).unwrap());
3291 assert_eq!(Some(5), config.get_long(ProducerConfig::LINGER_MS_CONFIG).unwrap());
3292 assert_eq!(
3293 Some(true),
3294 config.get_boolean(ProducerConfig::ENABLE_IDEMPOTENCE_CONFIG).unwrap()
3295 );
3296 assert_eq!(Some("-1"), config.get_string(ProducerConfig::ACKS_CONFIG).unwrap());
3298 assert_eq!(None, config.get_string(ProducerConfig::TRANSACTIONAL_ID_CONFIG).unwrap());
3299 assert_eq!(
3300 Some(&["localhost:9999".to_string()][..]),
3301 config.get_list(ProducerConfig::BOOTSTRAP_SERVERS_CONFIG).unwrap()
3302 );
3303 assert_eq!(
3304 "Configuration 'batch.size' is of type INT, not LONG",
3305 config.get_long(ProducerConfig::BATCH_SIZE_CONFIG).unwrap_err().message()
3306 );
3307 assert_eq!(
3308 "Configuration 'enable.idempotence' is of type BOOLEAN, not SHORT",
3309 config
3310 .get_short(ProducerConfig::ENABLE_IDEMPOTENCE_CONFIG)
3311 .unwrap_err()
3312 .message()
3313 );
3314 assert_eq!(
3315 "Configuration 'acks' is of type STRING, not DOUBLE",
3316 config.get_double(ProducerConfig::ACKS_CONFIG).unwrap_err().message()
3317 );
3318 assert_eq!(
3319 "Unknown configuration 'no.such.key'",
3320 config.get_int("no.such.key").unwrap_err().message()
3321 );
3322 }
3323
3324 #[test]
3326 fn test_config_names() {
3327 let names = ProducerConfig::config_names();
3328 for key in [
3329 ProducerConfig::BOOTSTRAP_SERVERS_CONFIG,
3330 ProducerConfig::PARTITIONER_TYPE_CONFIG,
3331 ProducerConfig::COMPRESSION_GZIP_LEVEL_CONFIG,
3332 ProducerConfig::COMPRESSION_LZ4_LEVEL_CONFIG,
3333 ProducerConfig::COMPRESSION_ZSTD_LEVEL_CONFIG,
3334 SaslConfigs::SASL_MECHANISM,
3335 SslConfigs::SSL_KEYSTORE_TYPE_CONFIG,
3336 "ssl.cipher.suites",
3337 ] {
3338 assert!(names.contains(key), "{key}");
3339 }
3340 for key in [
3341 "partitioner.class",
3342 "key.serializer",
3343 "value.serializer",
3344 "metric.reporters",
3345 "interceptor.classes",
3346 "security.providers",
3347 "config.providers",
3348 "enable.metrics.push",
3349 "ssl.keystore.password",
3350 "ssl.key.password",
3351 "ssl.truststore.password",
3352 ] {
3353 assert!(!names.contains(key), "{key}");
3354 }
3355 }
3356
3357 #[test]
3360 fn test_unused_and_ignore() {
3361 let config = ProducerConfig::new(&props_with(&[
3362 (ProducerConfig::BATCH_SIZE_CONFIG, "2"),
3363 (ProducerConfig::LINGER_MS_CONFIG, "3"),
3364 (SslConfigs::SSL_KEYSTORE_TYPE_CONFIG, "PKCS12"),
3365 ("unknown.key", "x"),
3366 ]))
3367 .unwrap();
3368 let before = config.unused();
3370 for key in [
3371 ProducerConfig::BATCH_SIZE_CONFIG,
3372 ProducerConfig::LINGER_MS_CONFIG,
3373 SslConfigs::SSL_KEYSTORE_TYPE_CONFIG,
3374 "unknown.key",
3375 ] {
3376 assert!(before.contains(key), "{key}: {before:?}");
3377 }
3378 config.get_int(ProducerConfig::BATCH_SIZE_CONFIG).unwrap();
3379 config.ignore("unknown.key");
3380 let after = config.unused();
3381 assert!(!after.contains(ProducerConfig::BATCH_SIZE_CONFIG), "{after:?}");
3382 assert!(!after.contains("unknown.key"), "{after:?}");
3383 config.mark_read_keys_used();
3385 assert_eq!(
3386 HashSet::from([SslConfigs::SSL_KEYSTORE_TYPE_CONFIG.to_string()]),
3387 config.unused()
3388 );
3389 config.log_unused();
3390 }
3391
3392 #[test]
3395 fn test_enable_metrics_push_is_unused() {
3396 let config = ProducerConfig::new(&props_with(&[("enable.metrics.push", "true")])).unwrap();
3397 config.mark_read_keys_used();
3398 assert_eq!(HashSet::from(["enable.metrics.push".to_string()]), config.unused());
3399 assert_eq!(
3400 "Unknown configuration 'enable.metrics.push'",
3401 config.get_boolean("enable.metrics.push").unwrap_err().message()
3402 );
3403 }
3404
3405 #[test]
3407 fn test_equals_and_hash() {
3408 use std::collections::hash_map::DefaultHasher;
3409 let hash = |config: &ProducerConfig| {
3410 let mut hasher = DefaultHasher::new();
3411 config.hash(&mut hasher);
3412 hasher.finish()
3413 };
3414 let a = ProducerConfig::new(&props_with(&[(ProducerConfig::CLIENT_ID_CONFIG, "c")])).unwrap();
3415 let b = ProducerConfig::new(&props_with(&[(ProducerConfig::CLIENT_ID_CONFIG, "c")])).unwrap();
3416 let c = ProducerConfig::new(&props_with(&[(ProducerConfig::CLIENT_ID_CONFIG, "d")])).unwrap();
3417 assert_eq!(a, b);
3418 assert_eq!(hash(&a), hash(&b));
3419 assert_ne!(a, c);
3420 }
3421
3422 #[test]
3426 fn test_compression_levels() {
3427 let config = ProducerConfig::new(&base_props()).unwrap();
3428 assert_eq!(-1, config.compression_gzip_level);
3429 assert_eq!(9, config.compression_lz4_level);
3430 assert_eq!(3, config.compression_zstd_level);
3431
3432 let config = ProducerConfig::new(&props_with(&[
3433 (ProducerConfig::COMPRESSION_GZIP_LEVEL_CONFIG, "5"),
3434 (ProducerConfig::COMPRESSION_LZ4_LEVEL_CONFIG, "1"),
3435 (ProducerConfig::COMPRESSION_ZSTD_LEVEL_CONFIG, "-5"),
3436 ]))
3437 .unwrap();
3438 assert_eq!(5, config.compression_gzip_level);
3439 assert_eq!(1, config.compression_lz4_level);
3440 assert_eq!(-5, config.compression_zstd_level);
3441
3442 for (key, value, message) in [
3443 (
3444 ProducerConfig::COMPRESSION_GZIP_LEVEL_CONFIG,
3445 "0",
3446 "Invalid value 0 for configuration compression.gzip.level: Value must be between 1 and 9 or equal to -1",
3447 ),
3448 (
3449 ProducerConfig::COMPRESSION_GZIP_LEVEL_CONFIG,
3450 "10",
3451 "Invalid value 10 for configuration compression.gzip.level: Value must be between 1 and 9 or equal to -1",
3452 ),
3453 (
3454 ProducerConfig::COMPRESSION_LZ4_LEVEL_CONFIG,
3455 "18",
3456 "Invalid value 18 for configuration compression.lz4.level: Value must be no more than 17",
3457 ),
3458 (
3459 ProducerConfig::COMPRESSION_ZSTD_LEVEL_CONFIG,
3460 "23",
3461 "Invalid value 23 for configuration compression.zstd.level: Value must be no more than 22",
3462 ),
3463 ] {
3464 let error = ProducerConfig::new(&props_with(&[(key, value)])).unwrap_err();
3465 assert_eq!(message, error.message(), "{key}={value}");
3466 }
3467 }
3468}