Skip to main content

confluent_kafka/producer/
producer_config.rs

1// Copyright 2025 Confluent Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Configuration for the Kafka Producer.
16//!
17//! Corresponds to `org.apache.kafka.clients.producer.ProducerConfig`: the keys
18//! are defined on a `ConfigDef` in Java's order (`ProducerConfig`)
19//! and parsed and validated by it, `postProcessParsedConfig` runs Java's steps
20//! (`ProducerConfig#postProcessParsedConfig`), and the configuration answers Java's
21//! `AbstractConfig` getters.
22//!
23//! Not defined, and so reported by [`ProducerConfig::unused`] and the
24//! client's [`ProducerConfig::log_unused`] line when set:
25//!
26//! - `key.serializer`, `value.serializer`, `partitioner.class`,
27//!   `metric.reporters` and `interceptor.classes`: classes or lists of
28//!   classes, which a configuration never takes (CLAUDE.md §2). A partitioner
29//!   is chosen with the Rust-only `partitioner.type` key or set with
30//!   [`ProducerConfig::set_partitioner`].
31//! - `security.providers`: Java's JCA security providers have no counterpart.
32//! - `config.providers`: config providers are not translated, so the key is
33//!   left undefined and a value set for it is reported as unused, rather than
34//!   accepted and ignored.
35//! - `enable.metrics.push`: KIP-714 client telemetry is not implemented, so
36//!   the key is left undefined and a value set for it is reported as unused,
37//!   rather than accepted and ignored.
38//!
39//! `AbstractConfig`'s `getPassword`, `typeOf`, `originals*()` and `values*()`
40//! are not translated yet: each needs a public type the crate does not have
41//! (`Password`, `ConfigDef.Type`, Java's `RecordingMap`). `documentationOf`
42//! is not translated either: the documentation strings are not kept, since
43//! only it and Java's documentation renderers read them. See `CHANGELOG.md`.
44
45use 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
68/// Process-wide counter for deriving a default `client.id`.
69///
70/// Corresponds to Java's `static AtomicInteger PRODUCER_CLIENT_ID_SEQUENCE`
71/// (`ProducerConfig#PRODUCER_CLIENT_ID_SEQUENCE`), which starts at 1. Crate-level (not
72/// per-config) to match Java's static scope, so successive producers in one
73/// process get distinct ids.
74static PRODUCER_CLIENT_ID_SEQUENCE: AtomicI32 = AtomicI32::new(1);
75
76/// Maximum number of in-flight requests per connection when idempotence is enabled.
77/// Aligned with `ProducerStateEntry.NUM_BATCHES_TO_RETAIN` on the broker.
78const MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION_FOR_IDEMPOTENCE: i32 = 5;
79
80/// The validator of `partitioner.type`: a `null` value or one of the three
81/// built-in partitioner names.
82fn 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
120/// The `ConfigDef` level validator of a compression type that has levels.
121fn 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
127/// The default level of a compression type that has levels.
128fn 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        // Internal until the incremental strategy is fully implemented
161        .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        // Java defines `enable.metrics.push` here (`ProducerConfig`);
323        // it is left undefined until KIP-714 client telemetry is implemented.
324        .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        // `metric.reporters` is a list of classes (CLAUDE.md §2): not defined.
374        .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        // `key.serializer` and `value.serializer` are classes (CLAUDE.md §2):
382        // not defined.
383        .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        // default is set to be a bit lower than the server default (10 min), to avoid both client and server closing
396        // connection at same time
397        .define(
398            ProducerConfig::CONNECTIONS_MAX_IDLE_MS_CONFIG,
399            Type::Long,
400            9 * 60 * 1000_i64,
401            Importance::Medium,
402        )
403        // `partitioner.class` is a class (CLAUDE.md §2): the Rust-only
404        // `partitioner.type` names a built-in partitioner in its place.
405        .define_with_validator(
406            ProducerConfig::PARTITIONER_TYPE_CONFIG,
407            Type::String,
408            DefaultValue::Null,
409            partitioner_type_validator(),
410            Importance::Medium,
411        )
412        // `interceptor.classes` is a list of classes (CLAUDE.md §2): not defined.
413        .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        // `security.providers` names JCA security providers, which have no
421        // counterpart: not defined.
422        .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    // Java ends the chain with `config.providers` (the static initializer of
460    // `org.apache.kafka.clients.producer.ProducerConfig`); it is left undefined until config providers are translated.
461});
462
463/// Configuration for the Kafka Producer. Documentation for these
464/// configurations can be found in the
465/// [Kafka documentation](http://kafka.apache.org/documentation.html#producerconfigs).
466//
467// As in Java, the configuration is built from the property map by
468// `ProducerConfig::new`, which parses every key to its `ConfigDef` type,
469// applies its validator and runs Java's `postProcessParsedConfig`. Values are
470// read back with Java's `AbstractConfig` getters (`get_int`, `get_string`,
471// ...). Keys it does not define are accepted, as Java accepts them; the
472// producer reports them, with the defined keys it does not read, in one line
473// when it is built (`log_unused`). Equality and hashing are Java's `equals` /
474// `hashCode`: two configurations are equal when they were built from equal
475// properties.
476//
477// `Debug` is derived: every secret the typed fields hold is a `Password`
478// inside `sasl_config` / `ssl_config`, which renders as `[hidden]`, and
479// `AbstractConfig`'s `Debug` renders the properties as their key set only: the
480// map also holds keys this client does not define, and Java never prints a raw
481// original value. Not `Clone`: the partitioner set with `set_partitioner` is
482// owned, as the producer built from the configuration takes it.
483#[derive(Debug)]
484#[doc(alias = "org.apache.kafka.clients.producer.ProducerConfig")]
485pub struct ProducerConfig {
486    /// The parsed configuration, Java's `AbstractConfig` state.
487    pub(crate) config: AbstractConfig,
488    /// The keys the typed fields below were read from, marked used when the
489    /// producer is built ([`Self::mark_read_keys_used`]).
490    pub(crate) read_keys: Vec<&'static str>,
491
492    // --- Connection ---
493    /// `bootstrap.servers` - A list of host/port pairs to use for establishing the
494    /// initial connection to the Kafka cluster.
495    pub(crate) bootstrap_servers: Vec<String>,
496
497    /// `client.dns.lookup` - Controls how the client uses DNS lookups.
498    /// Default: [`ClientDnsLookup::UseAllDnsIps`].
499    pub(crate) client_dns_lookup: ClientDnsLookup,
500
501    /// `client.id` - An id string to pass to the server when making requests.
502    pub(crate) client_id: String,
503
504    /// `client.rack` - A rack identifier for this client. This can be any string
505    /// value which indicates where this client is physically located. It
506    /// corresponds with the broker config `broker.rack`. Default:
507    /// [`ProducerConfig::DEFAULT_CLIENT_RACK`] (`""`). Used by the built-in
508    /// partitioner when `partitioner.rack.aware` is enabled (KIP-1123).
509    pub(crate) client_rack: String,
510
511    // --- Security ---
512    /// `security.protocol` - Protocol used to communicate with brokers.
513    /// Default: `SecurityProtocol::Plaintext`.
514    pub(crate) security_protocol: SecurityProtocol,
515
516    /// SASL configuration (mechanism, JAAS config, credentials).
517    pub(crate) sasl_config: SaslConfigs,
518
519    /// SSL/TLS configuration.
520    pub(crate) ssl_config: SslConfigs,
521
522    // --- Batching ---
523    /// `batch.size` - The producer will attempt to batch records together into fewer
524    /// requests whenever multiple records are being sent to the same partition.
525    /// Default: 16384 bytes.
526    pub(crate) batch_size: i32,
527
528    /// `linger.ms` - Upper bound on the delay for batching.
529    /// Default: 5 ms (changed from 0 in Apache Kafka 4.0).
530    pub(crate) linger_ms: i64,
531
532    // --- Buffering ---
533    /// `buffer.memory` - Total bytes of memory the producer can use to buffer records
534    /// waiting to be sent. Default: 32 MiB.
535    pub(crate) buffer_memory: i64,
536
537    /// `buffer.memory.allocation.strategy` - Controls how the producer allocates memory from
538    /// `buffer.memory` for record batches. The following values are supported:
539    ///
540    /// - `full`: reserves a full `batch.size` up front when a batch is created, regardless of how
541    ///   much data it ends up holding. Pool memory therefore scales with the number of active
542    ///   partitions.
543    /// - `incremental`: allocates memory on demand as records are appended, growing a batch up to
544    ///   `batch.size`. Pool memory therefore scales with the data actually buffered rather than
545    ///   the number of active partitions, allowing larger `batch.size` values (e.g. for
546    ///   high-latency clusters) without reserving `batch.size` for every active partition.
547    ///
548    /// Default: `full`. Internal (Java's `defineInternal`) until the incremental strategy is fully
549    /// implemented. Validated case-insensitively and kept as given; `KafkaProducer` lower-cases it.
550    pub(crate) buffer_memory_allocation_strategy: String,
551
552    /// `max.block.ms` - Maximum time `send()` and `partitionsFor()` will block.
553    /// Default: 60000 ms.
554    pub(crate) max_block_ms: i64,
555
556    // --- Reliability ---
557    /// `acks` - Number of acknowledgments the producer requires the leader to have
558    /// received. Allowed values: 0, 1, -1 (all). Default: -1 (all).
559    pub(crate) acks: i16,
560
561    /// `retries` - Number of times to retry a request that fails with a transient error.
562    /// Default: `i32::MAX` (effectively infinite retries bounded by delivery timeout).
563    pub(crate) retries: i32,
564
565    /// `delivery.timeout.ms` - Upper bound on time to report success or failure after
566    /// `send()` returns. Default: 120000 ms.
567    pub(crate) delivery_timeout_ms: i32,
568
569    /// `request.timeout.ms` - Maximum time the client will wait for a response.
570    /// Default: 30000 ms.
571    pub(crate) request_timeout_ms: i32,
572
573    /// `enable.idempotence` - When true, exactly-once delivery semantics are enabled.
574    /// Default: true.
575    pub(crate) enable_idempotence: bool,
576
577    // --- Sizing ---
578    /// `max.request.size` - Maximum size of a request in bytes.
579    /// Default: 1048576 (1 MiB).
580    pub(crate) max_request_size: i32,
581
582    /// `max.in.flight.requests.per.connection` - Maximum number of unacknowledged
583    /// requests per connection. Default: 5.
584    pub(crate) max_in_flight_requests_per_connection: i32,
585
586    // --- Compression ---
587    /// `compression.type` - Compression type for all data generated by the producer.
588    /// Default: `CompressionType::None`.
589    pub(crate) compression_type: CompressionType,
590
591    /// `compression.gzip.level` - The compression level to use if
592    /// `compression.type` is `gzip`.
593    pub(crate) compression_gzip_level: i32,
594
595    /// `compression.lz4.level` - The compression level to use if
596    /// `compression.type` is `lz4`.
597    pub(crate) compression_lz4_level: i32,
598
599    /// `compression.zstd.level` - The compression level to use if
600    /// `compression.type` is `zstd`.
601    pub(crate) compression_zstd_level: i32,
602
603    // --- Connections ---
604    /// `connections.max.idle.ms` - Close idle connections after this duration.
605    /// Default: 540000 ms (9 minutes).
606    pub(crate) connections_max_idle_ms: i64,
607
608    /// `reconnect.backoff.ms` - Base wait time before attempting to reconnect.
609    /// Default: 50 ms.
610    pub(crate) reconnect_backoff_ms: i64,
611
612    /// `reconnect.backoff.max.ms` - Maximum wait time before reconnection attempts.
613    /// Default: 1000 ms.
614    pub(crate) reconnect_backoff_max_ms: i64,
615
616    /// `retry.backoff.ms` - Wait time before retrying a failed request.
617    /// Default: 100 ms.
618    pub(crate) retry_backoff_ms: i64,
619
620    /// `retry.backoff.max.ms` - Maximum wait time before retrying a failed request.
621    /// Default: 1000 ms.
622    pub(crate) retry_backoff_max_ms: i64,
623
624    /// `bootstrap.resolve.timeout.ms` - Selects the bootstrap DNS resolution
625    /// mode (KIP-909): `0` resolves `bootstrap.servers` synchronously at
626    /// construction; a positive value resolves asynchronously for at most this
627    /// long. Default: 0. See
628    /// [`ProducerConfig::BOOTSTRAP_RESOLVE_TIMEOUT_MS_CONFIG`].
629    pub(crate) bootstrap_resolve_timeout_ms: i64,
630
631    // --- Socket ---
632    /// `send.buffer.bytes` - TCP send buffer size. Default: 131072 (128 KiB).
633    pub(crate) send_buffer_bytes: i32,
634
635    /// `receive.buffer.bytes` - TCP receive buffer size. Default: 32768 (32 KiB).
636    pub(crate) receive_buffer_bytes: i32,
637
638    /// `socket.connection.setup.timeout.ms` - Timeout for socket connection setup.
639    /// Default: 10000 ms.
640    pub(crate) socket_connection_setup_timeout_ms: i64,
641
642    /// `socket.connection.setup.timeout.max.ms` - Maximum timeout for socket connection
643    /// setup. Default: 30000 ms.
644    pub(crate) socket_connection_setup_timeout_max_ms: i64,
645
646    // --- Metadata ---
647    /// `metadata.max.age.ms` - Period after which metadata is force-refreshed.
648    /// Default: 300000 ms (5 minutes).
649    pub(crate) metadata_max_age_ms: i64,
650
651    /// `metadata.max.idle.ms` - Controls how long the producer will cache metadata for
652    /// an idle topic. Default: 300000 ms (5 minutes).
653    pub(crate) metadata_max_idle_ms: i64,
654
655    /// `metadata.recovery.strategy` - How the client recovers when none of the
656    /// brokers known to it is available. Default: `rebootstrap`
657    /// (`ProducerConfig`).
658    pub(crate) metadata_recovery_strategy: MetadataRecoveryStrategy,
659
660    /// `metadata.recovery.rebootstrap.trigger.ms` - How long a client configured
661    /// to rebootstrap waits without obtaining metadata before it rebootstraps.
662    /// Default: 300000 ms (`ProducerConfig`).
663    pub(crate) metadata_recovery_rebootstrap_trigger_ms: i64,
664
665    /// `metadata.cluster.check.enable` (KIP-1242) - Whether the client sends the
666    /// cluster id and node id it expects in ApiVersions so the broker can detect
667    /// a misrouted connection; ignored when `metadata.recovery.strategy=none`.
668    /// Default: true (`ProducerConfig`).
669    pub(crate) metadata_cluster_check_enable: bool,
670
671    // --- Partitioning ---
672    /// `partitioner.adaptive.partitioning.enable` - Adapt to broker performance.
673    /// Default: true.
674    pub(crate) partitioner_adaptive_partitioning_enable: bool,
675
676    /// `partitioner.availability.timeout.ms` - Timeout to mark a partition as
677    /// unavailable. Default: 0 (disabled).
678    pub(crate) partitioner_availability_timeout_ms: i64,
679
680    /// `partitioner.ignore.keys` - Ignore record keys for partitioning.
681    /// Default: false.
682    pub(crate) partitioner_ignore_keys: bool,
683
684    /// `partitioner.rack.aware` - Controls whether the default partitioner is
685    /// rack-aware. This has no effect when a custom partitioner is used.
686    /// Default: false.
687    ///
688    /// When `client.rack` is specified and `partitioner.rack.aware=true`, the
689    /// sticky partition is chosen from partitions with the leader broker in the
690    /// same rack, if at least one is available. If none are available, it falls
691    /// back on selecting from all available partitions (KIP-1123). Enabling it
692    /// with a blank `client.rack` makes the producer constructor fail with
693    /// `"client.rack must be provided if partitioner.rack.aware is enabled"`.
694    pub(crate) partitioner_rack_aware: bool,
695
696    /// `partitioner.type` - Selects the partitioning strategy. Default: `None`
697    /// (unset), which uses the built-in default partitioner keyed by IEEE CRC-32
698    /// ([`KeyHasher::Crc32`], librdkafka `consistent_random` parity).
699    ///
700    /// Accepted values:
701    /// - `ConsistentRandomPartitioner` — the default built-in partitioner with a
702    ///   CRC-32 key hash; identical to the unset default.
703    /// - `Murmur2RandomPartitioner` — the default built-in partitioner with a
704    ///   murmur2 key hash, identical to the Java client's built-in partitioner
705    ///   (exact Java parity).
706    /// - `RoundRobinPartitioner` — the
707    ///   [`RoundRobinPartitioner`](crate::producer::RoundRobinPartitioner), which
708    ///   distributes writes evenly across a topic's available partitions and
709    ///   ignores the record key. Selecting it **disables adaptive partitioning**,
710    ///   exactly as Java does when a `partitioner.class` is configured.
711    ///
712    /// Any other value is rejected by [`new`](Self::new) as not supported. Java's
713    /// `partitioner.class` names a class to load reflectively; Rust has no
714    /// reflection, so this key names one of the built-in types above, and a
715    /// user-written
716    /// [`Partitioner`](crate::producer::Partitioner) is instead set with
717    /// [`set_partitioner`](Self::set_partitioner). Unlike Java, this
718    /// Rust client's default key hash is CRC-32, not murmur2 — see
719    /// `design/current/partitioner.md`.
720    pub(crate) partitioner_type: Option<String>,
721
722    /// The [`Partitioner`] set with [`set_partitioner`](Self::set_partitioner),
723    /// the value of Java's `partitioner.class`. Its record types are erased,
724    /// because `ProducerConfig` is not generic over them;
725    /// [`resolve_partitioner`](Self::resolve_partitioner) recovers them for the
726    /// producer being built.
727    pub(crate) partitioner: Option<ConfiguredPartitioner>,
728
729    // --- Transactions ---
730    /// `transactional.id` - TransactionalId for transactional delivery.
731    /// Default: `None` (no transactions).
732    pub(crate) transactional_id: Option<String>,
733
734    /// `transaction.timeout.ms` - Maximum time a transaction will remain open.
735    /// Default: 60000 ms.
736    pub(crate) transaction_timeout_ms: i32,
737
738    // --- Metrics ---
739    /// `metrics.sample.window.ms` - The window of time a metrics sample is
740    /// computed over. Default: 30000 ms.
741    pub(crate) metrics_sample_window_ms: i64,
742
743    /// `metrics.num.samples` - The number of samples maintained to compute
744    /// metrics. Default: 2.
745    pub(crate) metrics_num_samples: i32,
746
747    /// `metrics.recording.level` - The highest recording level for metrics.
748    /// One of `INFO`, `DEBUG`, `TRACE`. Default: `INFO`.
749    pub(crate) metrics_recording_level: String,
750
751    /// `transaction.two.phase.commit.enable` - Whether the client participates
752    /// in two-phase commit (KIP-939), where an external coordinator decides when
753    /// to finalize. Default: `false`.
754    pub(crate) two_phase_commit_enable: bool,
755}
756
757/// The [`Partitioner`] set with [`ProducerConfig::set_partitioner`], with its
758/// record types erased.
759///
760/// No Java counterpart: Java holds the `partitioner.class` value in the config
761/// map as an `Object`. It is needed because [`ProducerConfig`] is not generic
762/// over the record types the partitioner is written for.
763#[derive(Debug)]
764pub(crate) struct ConfiguredPartitioner {
765    /// A `Box<dyn Partitioner<K, V>>`.
766    partitioner: Box<dyn Any + Send + Sync>,
767    /// `dyn Partitioner<K, V>`, named in the type mismatch error.
768    type_name: &'static str,
769}
770
771impl PartialEq for ProducerConfig {
772    // Java's `AbstractConfig.equals`: the originals are equal.
773    fn eq(&self, other: &Self) -> bool {
774        self.config == other.config
775    }
776}
777
778impl Eq for ProducerConfig {}
779
780impl Hash for ProducerConfig {
781    // Java's `AbstractConfig.hashCode`, over the originals.
782    fn hash<H: Hasher>(&self, state: &mut H) {
783        self.config.hash(state);
784    }
785}
786
787impl ProducerConfig {
788    /// A configuration holding every key's `ConfigDef` default, for the
789    /// crate's own tests, which then write the fields they need.
790    ///
791    /// It is parsed from `bootstrap.servers` alone, without
792    /// `postProcessParsedConfig`, so `client.id` stays empty; `bootstrap_servers`
793    /// is then emptied.
794    ///
795    /// Crate-private, and not `Default`: Java builds a configuration only
796    /// through its constructor, and a public default would hand out one that
797    /// skipped `new`'s validation, including the required `bootstrap.servers`.
798    #[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    /// Reads the typed fields from the parsed values, recording the keys read
809    /// in `read_keys`.
810    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        // `postProcessParsedConfig` replaced the value with `parseAcks`'s
823        // number, which `KafkaProducer` reads with `Short.parseShort`; reading
824        // it through `parse_acks` again, a no-op on that number, also covers
825        // the test-only `defaults()`, which skips the post-processing.
826        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        // The validator accepted only the compression type names.
837        let compression_type = CompressionType::for_name(values.string(Self::COMPRESSION_TYPE_CONFIG)?)?;
838        // A codec left out of the build (its Cargo feature off) would fail every
839        // batch, so the producer is refused at construction instead.
840        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        // The validator accepted the strategy case-insensitively, as `forName`
864        // reads it.
865        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        // The validator accepted `protocol` case-insensitively.
886        let security_protocol = SecurityProtocol::for_name(protocol)
887            .ok_or_else(|| Error::local_illegal_state(format!("Unknown security protocol {protocol}")))?;
888
889        // The `ssl.*` and `sasl.*` keys are read by Java's channel builder only
890        // for a protocol that uses them (`ChannelBuilders.clientChannelBuilder`),
891        // so only then are they marked used.
892        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    // --- Config key constants (matching Java string keys) ---
963
964    /// Config key: `bootstrap.servers`
965    pub const BOOTSTRAP_SERVERS_CONFIG: &'static str = "bootstrap.servers";
966    /// Config key: `client.dns.lookup`. Java's `ProducerConfig.java` declares it as its
967    /// own public alias of `CommonClientConfigs.CLIENT_DNS_LOOKUP_CONFIG`.
968    pub const CLIENT_DNS_LOOKUP_CONFIG: &'static str = CommonClientConfigs::CLIENT_DNS_LOOKUP_CONFIG;
969    /// Config key: `client.id`
970    pub const CLIENT_ID_CONFIG: &'static str = "client.id";
971    /// Config key: `client.rack`
972    pub const CLIENT_RACK_CONFIG: &'static str = CommonClientConfigs::CLIENT_RACK_CONFIG;
973    /// Default value of `client.rack`: no rack.
974    pub const DEFAULT_CLIENT_RACK: &'static str = CommonClientConfigs::DEFAULT_CLIENT_RACK;
975    /// Config key: `batch.size`
976    pub const BATCH_SIZE_CONFIG: &'static str = "batch.size";
977    /// Config key: `linger.ms`
978    pub const LINGER_MS_CONFIG: &'static str = "linger.ms";
979    /// Config key: `buffer.memory`
980    pub const BUFFER_MEMORY_CONFIG: &'static str = "buffer.memory";
981    /// Config key: `buffer.memory.allocation.strategy` (KIP-1332). Internal until the incremental
982    /// strategy is fully implemented.
983    pub const BUFFER_MEMORY_ALLOCATION_STRATEGY_CONFIG: &'static str = "buffer.memory.allocation.strategy";
984    /// `buffer.memory.allocation.strategy` value: reserve a full `batch.size` per batch up front
985    /// (the default).
986    pub const BUFFER_MEMORY_ALLOCATION_STRATEGY_FULL: &'static str = "full";
987    /// `buffer.memory.allocation.strategy` value: allocate memory on demand as records are
988    /// appended.
989    pub const BUFFER_MEMORY_ALLOCATION_STRATEGY_INCREMENTAL: &'static str = "incremental";
990    /// Config key: `max.block.ms`
991    pub const MAX_BLOCK_MS_CONFIG: &'static str = "max.block.ms";
992    /// Config key: `acks`
993    pub const ACKS_CONFIG: &'static str = "acks";
994    /// Config key: `retries`
995    pub const RETRIES_CONFIG: &'static str = "retries";
996    /// Config key: `delivery.timeout.ms`
997    pub const DELIVERY_TIMEOUT_MS_CONFIG: &'static str = "delivery.timeout.ms";
998    /// Config key: `request.timeout.ms`
999    pub const REQUEST_TIMEOUT_MS_CONFIG: &'static str = "request.timeout.ms";
1000    /// Config key: `enable.idempotence`
1001    pub const ENABLE_IDEMPOTENCE_CONFIG: &'static str = "enable.idempotence";
1002    /// Config key: `max.request.size`
1003    pub const MAX_REQUEST_SIZE_CONFIG: &'static str = "max.request.size";
1004    /// Config key: `max.in.flight.requests.per.connection`
1005    pub const MAX_IN_FLIGHT_REQUESTS_PER_CONNECTION: &'static str = "max.in.flight.requests.per.connection";
1006    /// Config key: `compression.type`
1007    pub const COMPRESSION_TYPE_CONFIG: &'static str = "compression.type";
1008    /// Config key: `connections.max.idle.ms`
1009    pub const CONNECTIONS_MAX_IDLE_MS_CONFIG: &'static str = "connections.max.idle.ms";
1010    /// Config key: `reconnect.backoff.ms`
1011    pub const RECONNECT_BACKOFF_MS_CONFIG: &'static str = "reconnect.backoff.ms";
1012    /// Config key: `reconnect.backoff.max.ms`
1013    pub const RECONNECT_BACKOFF_MAX_MS_CONFIG: &'static str = "reconnect.backoff.max.ms";
1014    /// Config key: `retry.backoff.ms`
1015    pub const RETRY_BACKOFF_MS_CONFIG: &'static str = "retry.backoff.ms";
1016    /// Config key: `retry.backoff.max.ms`
1017    pub const RETRY_BACKOFF_MAX_MS_CONFIG: &'static str = "retry.backoff.max.ms";
1018    /// Config key: `bootstrap.resolve.timeout.ms` (KIP-909)
1019    ///
1020    /// Selects the client's bootstrap DNS resolution mode. When set to `0` (the
1021    /// default), DNS is resolved synchronously during client construction; any
1022    /// failure surfaces as a config error and no client instance is created.
1023    /// When set to a positive value, DNS is resolved asynchronously and this is
1024    /// the maximum amount of time the client will spend retrying resolution
1025    /// before failing with an unrecoverable
1026    /// [`BootstrapResolutionError`](crate::common::errors::BootstrapResolutionError)
1027    /// from subsequent API calls (the client must then be closed and re-created
1028    /// after fixing the underlying DNS or `bootstrap.servers` configuration
1029    /// issue). Setting this config to a positive value enables an evolving
1030    /// feature whose compatibility may be broken in a minor release.
1031    pub const BOOTSTRAP_RESOLVE_TIMEOUT_MS_CONFIG: &'static str =
1032        CommonClientConfigs::BOOTSTRAP_RESOLVE_TIMEOUT_MS_CONFIG;
1033    /// Config key: `send.buffer.bytes`
1034    pub const SEND_BUFFER_CONFIG: &'static str = "send.buffer.bytes";
1035    /// Config key: `receive.buffer.bytes`
1036    pub const RECEIVE_BUFFER_CONFIG: &'static str = "receive.buffer.bytes";
1037    /// Config key: `metadata.max.age.ms`
1038    pub const METADATA_MAX_AGE_CONFIG: &'static str = "metadata.max.age.ms";
1039    /// Config key: `metadata.max.idle.ms`
1040    pub const METADATA_MAX_IDLE_CONFIG: &'static str = "metadata.max.idle.ms";
1041    /// Config key: `partitioner.adaptive.partitioning.enable`
1042    pub const PARTITIONER_ADAPTIVE_PARTITIONING_ENABLE_CONFIG: &'static str =
1043        "partitioner.adaptive.partitioning.enable";
1044    /// Config key: `partitioner.availability.timeout.ms`
1045    pub const PARTITIONER_AVAILABILITY_TIMEOUT_MS_CONFIG: &'static str = "partitioner.availability.timeout.ms";
1046    /// Config key: `partitioner.ignore.keys`
1047    pub const PARTITIONER_IGNORE_KEYS_CONFIG: &'static str = "partitioner.ignore.keys";
1048    /// Config key: `partitioner.rack.aware`
1049    pub const PARTITIONER_RACK_AWARE_CONFIG: &'static str = "partitioner.rack.aware";
1050    /// Config key: `partitioner.type`
1051    // selects a built-in partitioner in place of Java's class-valued `partitioner.class` (CLAUDE.md §2)
1052    #[doc(alias = "rust-only")]
1053    pub const PARTITIONER_TYPE_CONFIG: &'static str = "partitioner.type";
1054    /// Accepted `partitioner.type` value selecting the CRC-32 key hash
1055    /// (the crate-internal `KeyHasher::Crc32`) — the default, librdkafka `consistent_random` parity.
1056    // selects a built-in partitioner in place of Java's class-valued `partitioner.class` (CLAUDE.md §2)
1057    #[doc(alias = "rust-only")]
1058    pub const CONSISTENT_RANDOM_PARTITIONER: &'static str = "ConsistentRandomPartitioner";
1059    /// Accepted `partitioner.type` value selecting the murmur2 key hash
1060    /// (the crate-internal `KeyHasher::Murmur2`) — exact Java-client parity.
1061    // selects a built-in partitioner in place of Java's class-valued `partitioner.class` (CLAUDE.md §2)
1062    #[doc(alias = "rust-only")]
1063    pub const MURMUR2_RANDOM_PARTITIONER: &'static str = "Murmur2RandomPartitioner";
1064    /// Accepted `partitioner.type` value selecting the
1065    /// [`RoundRobinPartitioner`](crate::producer::RoundRobinPartitioner).
1066    // selects a built-in partitioner in place of Java's class-valued `partitioner.class` (CLAUDE.md §2)
1067    #[doc(alias = "rust-only")]
1068    pub const ROUND_ROBIN_PARTITIONER: &'static str = "RoundRobinPartitioner";
1069    /// Config key: `transactional.id`
1070    pub const TRANSACTIONAL_ID_CONFIG: &'static str = "transactional.id";
1071    /// Config key: `transaction.timeout.ms`
1072    pub const TRANSACTION_TIMEOUT_CONFIG: &'static str = "transaction.timeout.ms";
1073    /// Config key: `metrics.sample.window.ms`
1074    pub const METRICS_SAMPLE_WINDOW_MS_CONFIG: &'static str = "metrics.sample.window.ms";
1075    /// Config key: `metrics.num.samples`
1076    pub const METRICS_NUM_SAMPLES_CONFIG: &'static str = "metrics.num.samples";
1077    /// Config key: `metrics.recording.level`
1078    pub const METRICS_RECORDING_LEVEL_CONFIG: &'static str = "metrics.recording.level";
1079    /// Config key: `transaction.two.phase.commit.enable`
1080    pub const TRANSACTION_TWO_PHASE_COMMIT_ENABLE_CONFIG: &'static str = "transaction.two.phase.commit.enable";
1081    /// Config key: `compression.gzip.level`
1082    pub const COMPRESSION_GZIP_LEVEL_CONFIG: &'static str = "compression.gzip.level";
1083    /// Config key: `compression.lz4.level`
1084    pub const COMPRESSION_LZ4_LEVEL_CONFIG: &'static str = "compression.lz4.level";
1085    /// Config key: `compression.zstd.level`
1086    pub const COMPRESSION_ZSTD_LEVEL_CONFIG: &'static str = "compression.zstd.level";
1087    /// Config key: `socket.connection.setup.timeout.ms`
1088    pub const SOCKET_CONNECTION_SETUP_TIMEOUT_MS_CONFIG: &'static str = "socket.connection.setup.timeout.ms";
1089    /// Config key: `socket.connection.setup.timeout.max.ms`
1090    pub const SOCKET_CONNECTION_SETUP_TIMEOUT_MAX_MS_CONFIG: &'static str = "socket.connection.setup.timeout.max.ms";
1091
1092    /// Constructs a new ProducerConfig with the given configuration map.
1093    ///
1094    /// # Arguments
1095    ///
1096    /// - `props` - The producer configuration map
1097    ///
1098    /// # Errors
1099    ///
1100    /// - `Error::is_config_error` if a value is invalid, the settings
1101    ///   conflict, or `sasl.mechanism` is not `PLAIN` under a SASL
1102    ///   `security.protocol`.
1103    ///
1104    /// # Translation notes
1105    ///
1106    /// WARNING: `sasl.mechanism` must be `PLAIN`.
1107    // Covers Java's `ProducerConfig(Map)` and `ProducerConfig(Properties)`.
1108    // Every value is parsed to its `ConfigDef` type and checked by its
1109    // validator, then Java's `postProcessParsedConfig` runs:
1110    // `postValidateSaslMechanismConfig`, `warnDisablingExponentialBackoff`,
1111    // `postProcessReconnectBackoffConfigs`,
1112    // `postProcessAndValidateIdempotenceConfigs` (an incompatible `retries` or
1113    // `acks` disables idempotence unless it was set explicitly) and
1114    // `maybeOverrideClientId` (an unset `client.id` becomes
1115    // `producer-<transactional.id or a process-wide sequence>`).
1116    //
1117    // The config errors: a value fails to parse or validate, with
1118    // `ConfigDef`'s message; `bootstrap.servers` is missing; a SASL
1119    // `security.protocol` has an empty `sasl.mechanism`; the idempotence
1120    // settings conflict (`enable.idempotence=true` with `retries=0`, with
1121    // `acks` other than `all`, or `max.in.flight.requests.per.connection`
1122    // above 5 with idempotence enabled); `transactional.id` is set without
1123    // idempotence; `transaction.timeout.ms` is set with
1124    // `transaction.two.phase.commit.enable=true`.
1125    //
1126    // A SASL `security.protocol` with a `sasl.mechanism` other than `PLAIN`
1127    // fails with a config error: Java accepts any mechanism name, but this
1128    // client implements only PLAIN, and a client built with another mechanism
1129    // would fail only later, at authentication.
1130    #[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    /// The configuration of one more client given this one: the same
1136    /// properties and this configuration's resolved `client.id`, with its
1137    /// own record of the keys used and no partitioner, as [`Self::new`]
1138    /// leaves it.
1139    ///
1140    /// Java hands each client the `ProducerConfig` it is given, which took
1141    /// its one number from the `client.id` sequence when it was built
1142    /// (`KafkaProducer#KafkaProducer` reads it), so clients given one
1143    /// configuration share its id. Building the configuration again would
1144    /// take a second number; this copy does not. It is not `Clone`
1145    /// (CLAUDE.md §3).
1146    ///
1147    /// Java's client logs nothing about the configuration it is given, so the
1148    /// copy logs none of the lines [`Self::new`] logs: it makes the same parse
1149    /// and the same reads, marking the same keys used and failing the same
1150    /// way, but logs neither the parse's warning that a list's duplicate
1151    /// values are removed, nor the post-processing's lines (the reconnect and
1152    /// retry backoff and connection setup timeout warnings, the INFO lines
1153    /// saying idempotence will be disabled). The client given the copy logs its one
1154    /// unused-keys line ([`Self::log_unused`]), as Java's client constructor
1155    /// does.
1156    ///
1157    /// # Errors
1158    ///
1159    /// Fails as [`Self::new`] does; this configuration's properties already
1160    /// passed it, so it does not in practice.
1161    #[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    /// Java's `ProducerConfig(Map)` without its logging: parses `originals`
1170    /// and post-processes them, `resolved_client_id` standing in for the
1171    /// sequence number `client.id` would take. With a `resolved_client_id`
1172    /// (a copy) nothing is logged: not the parse's duplicate-values warning,
1173    /// not the post-processing's lines.
1174    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    /// Java's `ProducerConfig.postProcessParsedConfig`: the values that
1189    /// replace the parsed ones. A `resolved_client_id` is the id a derived
1190    /// `client.id` takes, in place of a new one; it marks a copy for one more
1191    /// client ([`Self::copy_for_client`]), whose reads are the same but which
1192    /// logs none of the lines: its original logged them once.
1193    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    /// Java's private `maybeOverrideClientId`: a `client.id` the user did not
1209    /// set becomes `producer-<transactional.id>` when a transactional id is
1210    /// set, otherwise `producer-<n>` from the process-wide sequence.
1211    ///
1212    /// A `resolved_client_id` (the id of the configuration this one copies)
1213    /// is put in `configs` instead of a derived one, after the same reads.
1214    #[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    /// Validates and post-processes the idempotence-dependent configs: Java's
1239    /// private `postProcessAndValidateIdempotenceConfigs`.
1240    ///
1241    /// Java's asymmetry is deliberate and preserved: for `retries` and `acks`,
1242    /// an incompatible value **silently disables** idempotence unless the user
1243    /// asked for idempotence explicitly, in which case it is an error. For
1244    /// `max.in.flight.requests.per.connection` it is **always** an error. The
1245    /// silent-disable path is what keeps existing non-idempotent configurations
1246    /// working, so removing it would be a breaking change.
1247    ///
1248    /// With `log_lines` false it decides and fails the same way but logs
1249    /// neither INFO line.
1250    #[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        // For idempotence producers, values for `retries` and `acks` and `max.in.flight.requests.per.connection` need validation
1264        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            // `parseAcks` produced a number: "-1", "0" or "1" after the
1280            // validator.
1281            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        // validate `transaction.id` after validating idempotence dependant configs because `enable.idempotence` config might be overridden
1318        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        // Validate that transaction.timeout.ms is not set when transaction.two.phase.commit.enable is true
1327        // In standard Kafka transactions, the broker enforces transaction.timeout.ms and aborts any
1328        // transaction that isn't completed in time. With two-phase commit (2PC), an external coordinator
1329        // decides when to finalize, so broker-side timeouts don't apply. Disallow using both.
1330        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    /// Java's private `parseAcks`: `all` becomes `-1`, any other value its
1345    /// `short` value, as a string.
1346    ///
1347    /// # Errors
1348    ///
1349    /// [`Error::Config`] with `Invalid configuration value for 'acks': <value>`
1350    /// when the value is not `all` and not a `short`. Java's `parseAcks` turns
1351    /// the `NumberFormatException` into that `ConfigException`
1352    /// (`ProducerConfig#parseAcks`). The `in("all", "-1", "0", "1")`
1353    /// validator runs first, so the error is reachable only when this is
1354    /// called directly, as in Java.
1355    #[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    /// The `SHORT` configuration `key`. Reading a key marks it used
1369    /// ([`Self::unused`]).
1370    ///
1371    /// # Arguments
1372    ///
1373    /// - `key` - the configuration name
1374    ///
1375    /// # Returns
1376    ///
1377    /// The value, `None` for Java's `null`.
1378    ///
1379    /// # Errors
1380    ///
1381    /// - `Error::is_config_error` with Java's `Unknown configuration '<key>'`
1382    ///   if `key` is not a configuration this class defines.
1383    /// - `Error::is_local_illegal_argument_error` if `key` is a configuration
1384    ///   of another type, where Java's cast fails with a `ClassCastException`.
1385    #[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    /// The `INT` configuration `key`. Reading a key marks it used
1391    /// ([`Self::unused`]).
1392    ///
1393    /// # Arguments
1394    ///
1395    /// - `key` - the configuration name
1396    ///
1397    /// # Returns
1398    ///
1399    /// The value, `None` for Java's `null`.
1400    ///
1401    /// # Errors
1402    ///
1403    /// - `Error::is_config_error` with Java's `Unknown configuration '<key>'`
1404    ///   if `key` is not a configuration this class defines.
1405    /// - `Error::is_local_illegal_argument_error` if `key` is a configuration
1406    ///   of another type, where Java's cast fails with a `ClassCastException`.
1407    #[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    /// The `LONG` configuration `key`. Reading a key marks it used
1413    /// ([`Self::unused`]).
1414    ///
1415    /// # Arguments
1416    ///
1417    /// - `key` - the configuration name
1418    ///
1419    /// # Returns
1420    ///
1421    /// The value, `None` for Java's `null`.
1422    ///
1423    /// # Errors
1424    ///
1425    /// - `Error::is_config_error` with Java's `Unknown configuration '<key>'`
1426    ///   if `key` is not a configuration this class defines.
1427    /// - `Error::is_local_illegal_argument_error` if `key` is a configuration
1428    ///   of another type, where Java's cast fails with a `ClassCastException`.
1429    #[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    /// The `DOUBLE` configuration `key`. Reading a key marks it used
1435    /// ([`Self::unused`]).
1436    ///
1437    /// # Arguments
1438    ///
1439    /// - `key` - the configuration name
1440    ///
1441    /// # Returns
1442    ///
1443    /// The value, `None` for Java's `null`.
1444    ///
1445    /// # Errors
1446    ///
1447    /// - `Error::is_config_error` with Java's `Unknown configuration '<key>'`
1448    ///   if `key` is not a configuration this class defines.
1449    /// - `Error::is_local_illegal_argument_error` if `key` is a configuration
1450    ///   of another type, where Java's cast fails with a `ClassCastException`.
1451    #[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    /// The `LIST` configuration `key`. Reading a key marks it used
1457    /// ([`Self::unused`]).
1458    ///
1459    /// # Arguments
1460    ///
1461    /// - `key` - the configuration name
1462    ///
1463    /// # Returns
1464    ///
1465    /// The value, borrowed from the parsed values, `None` for Java's `null`.
1466    ///
1467    /// # Errors
1468    ///
1469    /// - `Error::is_config_error` with Java's `Unknown configuration '<key>'`
1470    ///   if `key` is not a configuration this class defines.
1471    /// - `Error::is_local_illegal_argument_error` if `key` is a configuration
1472    ///   of another type, where Java's cast fails with a `ClassCastException`.
1473    #[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    /// The `BOOLEAN` configuration `key`. Reading a key marks it used
1479    /// ([`Self::unused`]).
1480    ///
1481    /// `enable.idempotence` answers the value `postProcessParsedConfig`
1482    /// settled on: `false` when an incompatible `retries` or `acks` disabled
1483    /// it.
1484    ///
1485    /// # Arguments
1486    ///
1487    /// - `key` - the configuration name
1488    ///
1489    /// # Returns
1490    ///
1491    /// The value, `None` for Java's `null`.
1492    ///
1493    /// # Errors
1494    ///
1495    /// - `Error::is_config_error` with Java's `Unknown configuration '<key>'`
1496    ///   if `key` is not a configuration this class defines.
1497    /// - `Error::is_local_illegal_argument_error` if `key` is a configuration
1498    ///   of another type, where Java's cast fails with a `ClassCastException`.
1499    #[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    /// The `STRING` configuration `key`. Reading a key marks it used
1505    /// ([`Self::unused`]).
1506    ///
1507    /// `client.id` answers the resolved id: when the property is unset, `new`
1508    /// derives it the way Java's `maybeOverrideClientId` does, so this is the
1509    /// id the producer reports to the brokers. `acks` answers the number
1510    /// Java's `parseAcks` settled on (`"-1"` for `all`). `security.protocol`
1511    /// answers the value as written, trimmed (`"sasl_ssl"`), as Java's does:
1512    /// its validator accepts it case-insensitively without changing it. A
1513    /// `PASSWORD` configuration that is not set answers `None`, as Java's
1514    /// `null` default casts to `String` (`SslConfigs`, `SaslConfigs`).
1515    ///
1516    /// # Arguments
1517    ///
1518    /// - `key` - the configuration name
1519    ///
1520    /// # Returns
1521    ///
1522    /// The value, trimmed, or `None` when it has no value (Java's `null`,
1523    /// e.g. an unset `transactional.id`).
1524    ///
1525    /// # Errors
1526    ///
1527    /// - `Error::is_config_error` with Java's `Unknown configuration '<key>'`
1528    ///   if `key` is not a configuration this class defines.
1529    /// - `Error::is_local_illegal_argument_error` if `key` is a configuration
1530    ///   of another type, where Java's cast fails with a `ClassCastException`.
1531    #[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    /// Returns the keys of the properties this configuration was built from
1537    /// that were never read: through a getter, or by the producer built from
1538    /// it.
1539    #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#unused")]
1540    pub fn unused(&self) -> HashSet<String> {
1541        self.config.unused()
1542    }
1543
1544    /// Marks the configuration `key` used without reading it.
1545    ///
1546    /// # Arguments
1547    ///
1548    /// - `key` - the configuration name
1549    #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#ignore")]
1550    pub fn ignore(&self, key: &str) {
1551        self.config.ignore(key);
1552    }
1553
1554    /// Info level log for any unused configurations
1555    // One line, `These configurations '[a, b]' were supplied but are not used
1556    // yet.`, and nothing when every key was used.
1557    #[doc(alias = "org.apache.kafka.common.config.AbstractConfig#logUnused")]
1558    pub fn log_unused(&self) {
1559        self.config.log_unused();
1560    }
1561
1562    /// The names of the configurations this class defines.
1563    ///
1564    /// # Returns
1565    ///
1566    /// The set of configuration names
1567    #[doc(alias = "org.apache.kafka.clients.producer.ProducerConfig#configNames")]
1568    pub fn config_names() -> HashSet<String> {
1569        CONFIG.names()
1570    }
1571
1572    /// Marks used the keys the typed settings were read from, as the producer
1573    /// does where Java's reads them through the getters.
1574    pub(crate) fn mark_read_keys_used(&self) {
1575        for key in &self.read_keys {
1576            self.config.ignore(key);
1577        }
1578    }
1579
1580    /// Java's `logAll()`, which Java runs when the producer builds the
1581    /// configuration with `doLog = true`: every parsed value at INFO.
1582    pub(crate) fn log_values(&self) {
1583        self.config.log_all("ProducerConfig");
1584    }
1585
1586    /// The properties the configuration was built from, Java's `originals()`,
1587    /// as strings: the map a configured partitioner's `configure` receives.
1588    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    /// Resolves `partitioner.type` to the [`KeyHasher`] used on the built-in
1596    /// default partitioner's keyed partition path.
1597    ///
1598    /// This helper has no direct Java counterpart (Java resolves a `Partitioner`
1599    /// instance instead); it exists only to map the validated
1600    /// [`partitioner_type`](Self::partitioner_type) string to the internal
1601    /// [`KeyHasher`] enum. Only `Murmur2RandomPartitioner` maps to murmur2;
1602    /// every other accepted value — unset (`None`), `ConsistentRandomPartitioner`,
1603    /// and `RoundRobinPartitioner` — maps to the CRC-32
1604    /// default.
1605    ///
1606    /// For a `RoundRobinPartitioner` config the hash is moot: the producer uses
1607    /// the [`RoundRobinPartitioner`](crate::producer::RoundRobinPartitioner)
1608    /// instance (which ignores the key) rather than the built-in default
1609    /// partitioner, so this value is never consulted. It is defined here only to
1610    /// keep the accessor total.
1611    pub(crate) fn key_hasher(&self) -> KeyHasher {
1612        match self.partitioner_type.as_deref() {
1613            Some(Self::MURMUR2_RANDOM_PARTITIONER) => KeyHasher::Murmur2,
1614            // Unset (`None`), `ConsistentRandomPartitioner`, and
1615            // `RoundRobinPartitioner` all use the CRC-32 default. `new` rejects
1616            // every other value, so no other string can reach here.
1617            _ => KeyHasher::Crc32,
1618        }
1619    }
1620
1621    /// Resolves `partitioner.type` to a built-in [`Partitioner`] instance, if
1622    /// the configured value names one that has a dedicated partitioner type.
1623    ///
1624    /// This is the Rust stand-in for Java's
1625    /// `config.getConfiguredInstance(PARTITIONER_CLASS_CONFIG, Partitioner.class,
1626    /// ...)` (`KafkaProducer#KafkaProducer`), which reflectively loads and
1627    /// instantiates the named class. Rust has no reflection, so only the
1628    /// built-in names resolve here:
1629    ///
1630    /// - [`ROUND_ROBIN_PARTITIONER`](Self::ROUND_ROBIN_PARTITIONER) →
1631    ///   `Some(Box::new(RoundRobinPartitioner::new()))`;
1632    /// - every other accepted value (unset, `ConsistentRandomPartitioner`,
1633    ///   `Murmur2RandomPartitioner`) → `None`, meaning the built-in default
1634    ///   partitioner's keyed path is used (see [`key_hasher`](Self::key_hasher)).
1635    ///
1636    /// The partitioner set with [`set_partitioner`](Self::set_partitioner)
1637    /// takes precedence over a `partitioner.type` name.
1638    ///
1639    /// `new` has already rejected any value that is not a built-in type, so no
1640    /// unknown string reaches here.
1641    ///
1642    /// # Errors
1643    ///
1644    /// Returns [`Error::KafkaError`] when the partitioner set with
1645    /// `set_partitioner` partitions records of other types than the producer's
1646    /// `K` / `V`: Java's `getConfiguredInstance` throws
1647    /// `KafkaException(c.getName() + " is not an instance of " + t.getName())`
1648    /// when the configured value does not implement the requested type.
1649    ///
1650    /// Takes `&mut self` because the partitioner set with `set_partitioner` is
1651    /// moved out: the producer being built owns it, as a Java producer owns
1652    /// the instance it creates from `partitioner.class`.
1653    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    /// Sets the [`Partitioner`] that determines which partition each record
1673    /// goes to: the value of Java's `partitioner.class`.
1674    ///
1675    /// Java names a class, which the producer instantiates by reflection; here
1676    /// the partitioner itself is the value. [`KafkaProducer::new`](crate::producer::KafkaProducer::new)
1677    /// `configure`s it with the user configs plus the resolved `client.id`, as
1678    /// Java does (`KafkaProducer#KafkaProducer`), and closes it on `close`. As
1679    /// with any configured partitioner, adaptive partitioning is then disabled.
1680    /// The producer built from this config takes ownership of the partitioner.
1681    ///
1682    /// This replaces a `partitioner.type` given in the properties passed to
1683    /// [`new`](Self::new), as a later value for the same key would.
1684    ///
1685    /// The partitioner's `K` / `V` must be the record types of the producer built
1686    /// from this config; otherwise `KafkaProducer::new` fails with
1687    /// "`<partitioner>` is not an instance of `<expected>`".
1688    ///
1689    /// # Arguments
1690    ///
1691    /// - `partitioner` - the partitioner the producer uses
1692    ///
1693    /// # Returns
1694    ///
1695    /// This configuration, with the partitioner set.
1696    // the partitioner set through a setter, not a class config (CLAUDE.md §2)
1697    #[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    /// Whether the user set `key` explicitly.
1708    ///
1709    /// Java's `config.originals().containsKey(key)`: a key is "user
1710    /// configured" iff it appears in the properties the configuration was
1711    /// built from, not merely because it has a (possibly derived) value.
1712    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    /// `postValidateSaslMechanismConfig` runs when the config is built, so an unusable
1720    /// `sasl.mechanism` fails here and not later, when a channel builder is created.
1721    #[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    /// Props holding only `bootstrap.servers`, which the `ConfigDef` defines
1757    /// with `NO_DEFAULT_VALUE` and so every valid config must carry.
1758    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        // The default (unset) key hash is CRC-32 (librdkafka parity), NOT the
1795        // Java-client murmur2 default.
1796        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        // The test-only defaults are parsed from `bootstrap.servers` alone.
1803        assert_eq!(config.originals(), base_props());
1804    }
1805
1806    /// `metadata.recovery.strategy` is a case-insensitive `none` / `rebootstrap`
1807    /// string (`ProducerConfig`) and
1808    /// `metadata.recovery.rebootstrap.trigger.ms` a long (`ProducerConfig`). Before
1809    /// these keys were parsed the producer silently ignored them and never
1810    /// rebootstrapped (`ClientRebootstrapTest.testProducerRebootstrap`).
1811    #[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        // `CaseInsensitiveValidString.ensureValid`'s message, which names the
1829        // valid strings (`ConfigDef.java`).
1830        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    /// `metrics.num.samples` is `atLeast(1)` (Java ProducerConfig /
1838    /// CommonClientConfigs). A value below 1 is rejected asserting the bound.
1839    #[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    /// `metadata.recovery.rebootstrap.trigger.ms` is `atLeast(0)` (Java
1861    /// `ProducerConfig`): 0 is accepted, -1 is rejected with
1862    /// Java's `ConfigDef.Range.atLeast` message.
1863    #[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    /// `metadata.cluster.check.enable` (KIP-1242): `Type.BOOLEAN`, default `true`
1884    /// (`ProducerConfig`).
1885    #[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    /// `bootstrap.resolve.timeout.ms` (KIP-909): `Type.LONG`, default `0`
1908    /// (synchronous resolution), `atLeast(0L)` since KAFKA-20939.
1909    #[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    /// `metrics.sample.window.ms` is `atLeast(0)` (Java ProducerConfig /
1934    /// CommonClientConfigs). A negative value is rejected asserting the bound.
1935    #[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    /// `metrics.recording.level` accepts exactly `INFO`/`DEBUG`/`TRACE`
1958    /// (case-sensitive) and rejects anything else. Java uses
1959    /// `ConfigDef.ValidString.in("INFO", "DEBUG", "TRACE")`, an exact
1960    /// case-sensitive membership check, so lowercase `debug` is rejected
1961    /// with a `ConfigException` while `DEBUG` is accepted.
1962    #[test]
1963    fn test_metrics_recording_level_validator() {
1964        // Uppercase enum values are accepted.
1965        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        // Lowercase is rejected (Java is case-sensitive here) with the exact
1973        // `ConfigException` wording.
1974        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        // A wholly unknown value is likewise rejected with the same wording.
1986        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    /// `bootstrap.servers` has `NO_DEFAULT_VALUE` (`ProducerConfig`), so a config
2021    /// without it fails at parse time with `ConfigDef.parseValue`'s message.
2022    #[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        // All fields should have default values
2039        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    /// `partitioner.type=ConsistentRandomPartitioner` selects the CRC-32 hash
2062    /// (identical to the unset default, librdkafka `consistent_random` parity).
2063    #[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    /// `partitioner.type=Murmur2RandomPartitioner` selects the murmur2 hash
2073    /// (exact Java-client parity).
2074    #[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    /// An unrecognised `partitioner.type` is rejected with the exact message
2084    /// naming the supported types (DoD §3: error messages are the contract).
2085    /// A Java fully-qualified class name is not a type, so it is rejected too.
2086    #[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        // `err.to_string()` prepends the variant tag, so assert on the exact
2092        // inner text with `ends_with`, matching
2093        // `test_metrics_recording_level_validator` above.
2094        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    /// `partitioner.type=RoundRobinPartitioner` (the simple name) is accepted
2121    /// and stored verbatim. The key hash is the CRC-32 default (moot: a
2122    /// RoundRobin producer uses the partitioner instance, not the key hash).
2123    #[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    /// [`resolve_partitioner`](ProducerConfig::resolve_partitioner) returns a
2137    /// built-in [`RoundRobinPartitioner`] instance for the simple name — the
2138    /// Rust stand-in for Java's reflective `getConfiguredInstance` of
2139    /// `partitioner.class` (`KafkaProducer#KafkaProducer`).
2140    #[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    /// With no `partitioner.type`, `resolve_partitioner` returns `None`: the
2149    /// built-in default partitioner's keyed path is used, not a partitioner
2150    /// instance.
2151    #[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    /// The other accepted `partitioner.type` values name Java's built-in
2159    /// random/sticky partitioners, which in this client are handled by the
2160    /// key-hash path (see [`key_hasher`](ProducerConfig::key_hasher)), not by a
2161    /// [`Partitioner`] instance. So `resolve_partitioner` returns `None` for
2162    /// both — only `RoundRobinPartitioner` maps to a dedicated partitioner type.
2163    #[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    /// Java's `originals()` holds the user-supplied map verbatim and never
2177    /// contains a *derived* default. Here `client.id` is not user-set, so the
2178    /// generated `producer-<n>` id lands on `client_id` but NOT in `originals`.
2179    #[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        // Verbatim: originals equals the input map exactly (keys and values).
2185        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        // The generated client.id is present on the struct but absent from
2193        // originals — mirroring Java, where a derived default is not an original.
2194        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    /// The other direction of the `originals` contract: a *user-set* `client.id`
2203    /// IS an original (and is preserved as the client id).
2204    #[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        // Java trims before validating (`ConfigDef.parseType(STRING)`), so a padded
2219        // id is stored trimmed.
2220        let config = ProducerConfig::new(&props_with(&[("transactional.id", " my-txn ")])).unwrap();
2221        assert_eq!(config.transactional_id, Some("my-txn".to_string()));
2222
2223        // Java's `ConfigDef.NonEmptyString` validator (`ProducerConfig`
2224        // → `ConfigDef`) rejects an empty string; a whitespace-only
2225        // id trims to empty and is rejected the same way. The error prints the
2226        // trimmed (empty) value, so "Invalid value " is followed by two spaces.
2227        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        // Only an absent key means "no transactional id" (Java's `null` default).
2246        assert_eq!(ProducerConfig::new(&base_props()).unwrap().transactional_id, None);
2247    }
2248
2249    /// Java's `ProducerConfig` `ConfigDef` validators (`ProducerConfig`):
2250    /// each bound rejects the value just below it with `ConfigDef.Range.atLeast`'s
2251    /// message and accepts the bound itself.
2252    #[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    /// `acks` accepts exactly `all`, `-1`, `0`, `1` (after trimming) and rejects
2295    /// anything else, mirroring Java's `ConfigDef.ValidString.in("all", "-1",
2296    /// "0", "1")` (`ProducerConfig`, `ConfigDef`), which
2297    /// runs before `parseAcks`. The error names the trimmed value because the
2298    /// validator runs on the parsed (trimmed) value; the check is exact-match, so
2299    /// `ALL` and `2` are rejected while a padded ` all ` is accepted.
2300    #[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        // Rejected: wrong case, out-of-range numbers and non-numbers. Each is a
2307        // `ConfigException` (a Kafka error) naming the value.
2308        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        // Java's `parseAcks` catches `NumberFormatException` and throws
2328        // `ConfigException` (`ProducerConfig#parseAcks`), so the class matters
2329        // as much as the message: `ConfigException extends KafkaException`, and a
2330        // caller validating a config map with `is_kafka_error()` must see it.
2331        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    /// Boolean config values are parsed case-insensitively after trimming, and an
2338    /// unparseable value is rejected with Java's message and the original,
2339    /// untrimmed value. Mirrors `ConfigDef.parseType(BOOLEAN)`
2340    /// (`ConfigDef#parseType`): `equalsIgnoreCase("true")` / `("false")`, else
2341    /// `ConfigException(name, value, "Expected value to be either true or false")`.
2342    #[test]
2343    fn test_parse_bool_matches_java_boolean() {
2344        // Accepted regardless of case, and after trimming surrounding whitespace.
2345        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        // Rejected: a non-boolean string. Java prints the original (untrimmed) value.
2357        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    /// Every `ConfigException` this config raises must answer `true` to
2368    /// `is_kafka_error()`, matching `ConfigException extends KafkaException`.
2369    ///
2370    /// The six sites are the `acks` `ConfigDef.ValidString` validator
2371    /// (which runs before `ProducerConfig#parseAcks`) and the five throws in
2372    /// `ProducerConfig#postProcessAndValidateIdempotenceConfigs`. They all used to be `IllegalArgumentException`, which sits
2373    /// *beside* `KafkaException` rather than below it, so `is_kafka_error()`
2374    /// answered `false` and a bad `acks` slipped past a caller that a bad
2375    /// `linger.ms` did not.
2376    #[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    /// `client.dns.lookup` defaults to `use_all_dns_ips` and parses into the
2426    /// typed [`ClientDnsLookup`], as `ProducerConfig`'s `ConfigDef` defines it.
2427    #[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    /// Translated from `ProducerConfigTest.testInvalidSecurityProtocol`.
2455    #[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    /// Translated from `ProducerConfigTest.testDefaultBufferMemoryAllocationStrategy`.
2471    #[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    /// Translated from `ProducerConfigTest.testValidBufferMemoryAllocationStrategy`.
2482    #[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        // Rust-only: `CaseInsensitiveValidString` accepts any case, and the value is kept as given.
2497        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    /// Translated from `ProducerConfigTest.testInvalidBufferMemoryAllocationStrategy`.
2508    #[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        // Rust-only: the whole `CaseInsensitiveValidString` message.
2520        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    /// An unrecognised `compression.type` is a `ConfigException`, not the
2528    /// `IllegalArgumentException` `CompressionType.forName` would raise: Java
2529    /// validates the key with `in(Utils.enumOptions(CompressionType.class))`
2530    /// (`ProducerConfig`), so `ConfigDef.ValidString.ensureValid`
2531    /// (`ConfigDef.ValidString#ensureValid`) rejects the value before the enum lookup runs.
2532    #[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        // `ConfigException extends KafkaException` but is not an `ApiException`.
2545        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    /// Every valid `compression.type` name still parses.
2550    #[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    /// A compression type whose feature is off is refused when the config is
2566    /// parsed, so the producer fails at construction rather than at its first
2567    /// batch.
2568    #[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    /// Translated from `ProducerConfigTest.testCaseInsensitiveSecurityProtocol`.
2590    #[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        // Only `PLAIN` is accepted under a SASL protocol (`post_validate_supported_sasl_mechanism`).
2596        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    /// Rust-only (`CommonClientConfigs::post_validate_supported_sasl_mechanism`):
2619    /// a SASL `security.protocol` needs `sasl.mechanism=PLAIN`; any other
2620    /// mechanism, the `GSSAPI` default included, fails the build with
2621    /// `LocalUnsupportedOperationError`, while an empty one keeps Java's
2622    /// `ConfigError`. Under `PLAINTEXT` the mechanism is inert, as in Java.
2623    #[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        // The default mechanism is Java's `GSSAPI`.
2648        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        // Case-sensitive, as Java's mechanism names are.
2656        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        // An empty mechanism fails Java's check first.
2668        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    // -- Idempotence config validation --------------------------------------
2694    //
2695    // Translated from `ProducerConfigTest` and the four pure-config tests in
2696    // `KafkaProducerTest`. The latter construct a `ProducerConfig` from
2697    // `Properties` and assert on values without creating a producer or touching
2698    // the network, so they belong here rather than in a producer test.
2699
2700    /// Java's `KafkaProducerTest.baseProperties()`.
2701    fn base_properties() -> HashMap<String, String> {
2702        HashMap::from([("bootstrap.servers".to_string(), "localhost:9999".to_string())])
2703    }
2704
2705    /// KIP-1123 (KAFKA-19193): `partitioner.rack.aware` is a `Type.BOOLEAN` and the
2706    /// producer's `client.rack` a `Type.STRING` (trimmed by `ConfigDef.parseType`),
2707    /// defined with the keys and defaults Java's `ProducerConfig` gives them.
2708    #[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    /// Translated from `KafkaProducerTest.testOverwriteAcksAndRetriesForIdempotentProducers`.
2741    #[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        // The derived client id form Java asserts on.
2750        assert_eq!(config.client_id, "producer-transactionalId");
2751    }
2752
2753    /// Translated from `KafkaProducerTest.testAcksAndIdempotenceForIdempotentProducers`.
2754    #[test]
2755    fn test_acks_and_idempotence_for_idempotent_producers() {
2756        // Valid: acks=0 with idempotence explicitly off.
2757        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        // Valid: transactional.id alone leaves the idempotence/acks defaults.
2763        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        // Valid: acks=all with idempotence explicitly off.
2768        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        // Valid: acks=0 with idempotence UNSET silently disables idempotence.
2774        // This is the path that keeps existing configurations working.
2775        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        // Same for acks=1.
2783        let config = ProducerConfig::new(&props_with(&[("acks", "1")])).expect("valid");
2784        assert!(!config.enable_idempotence);
2785        assert_eq!(config.acks, 1);
2786
2787        // Invalid: transactional.id without idempotence.
2788        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        // Invalid: explicitly enabling idempotence with acks=1 still errors,
2800        // rather than silently disabling.
2801        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        // Invalid: acks=0 with a transactional id — idempotence is silently
2809        // disabled by the acks arm, and the transactional.id check then fails.
2810        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    /// Translated from `KafkaProducerTest.testRetriesAndIdempotenceForIdempotentProducers`.
2819    #[test]
2820    fn test_retries_and_idempotence_for_idempotent_producers() {
2821        // Valid: retries=0 with idempotence explicitly off.
2822        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        // Valid: retries=0 with idempotence UNSET silently disables idempotence.
2828        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        // Invalid: transactional.id without idempotence.
2836        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        // Invalid: explicitly enabling idempotence with retries=0.
2848        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        // Invalid: retries=0 with a transactional id.
2856        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    /// Translated from `KafkaProducerTest.testInflightRequestsAndIdempotenceForIdempotentProducers`.
2865    #[test]
2866    fn test_inflight_requests_and_idempotence_for_idempotent_producers() {
2867        // Valid: in-flight above the cap is fine when idempotence is off.
2868        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        // Invalid: with idempotence on (by default), exceeding the cap is ALWAYS
2877        // an error — this arm never silently disables, unlike acks and retries.
2878        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        // Invalid: exactly at the cap, idempotence explicitly off, transactional
2886        // id set. Pins that the in-flight arm does not mask the transactional-id
2887        // arm — the in-flight value is legal here, so the error must come from
2888        // the transactional-id check.
2889        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        // Invalid: above the cap with idempotence explicitly on. Pins that the
2901        // in-flight arm fires regardless of how idempotence came to be enabled.
2902        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        // Invalid: above the cap with a transactional id. Pins that the in-flight
2913        // arm fires ahead of the transactional-id arm.
2914        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    /// Translated from `ProducerConfigTest.testUpperboundCheckOfEnableIdempotence`.
2926    #[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        // Exactly at the cap is allowed.
2937        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    /// Translated from `ProducerConfigTest.testTwoPhaseCommitIncompatibleWithTransactionTimeout`.
2942    #[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        // Setting one but not the other is valid.
2962        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    // -- client.id derivation ------------------------------------------------
2979
2980    /// Java's `maybeOverrideClientId`: an explicit client.id is preserved.
2981    #[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    /// `ConfigDef.parseType` trims `client.id`. The producer keys generation
2988    /// on the key being present in the originals, not on emptiness
2989    /// (`ProducerConfig`), so a blank explicit id stays empty,
2990    /// as in Java. The consumer and admin client generate one instead.
2991    #[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    /// Without an explicit client.id or transactional.id, the derived form is
3000    /// `producer-<n>` from the process-wide counter.
3001    #[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        // The counter is process-wide, so successive configs differ. Compare
3012        // rather than asserting absolute values, since test order is arbitrary.
3013        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    /// A copy keeps the configuration's resolved `client.id` and takes no
3019    /// sequence number, while the next configuration built takes the next
3020    /// one, as Java's next `new ProducerConfig(props)` does. The generated id
3021    /// stays out of the copy's originals, and its used set is its own, as
3022    /// `new` leaves it.
3023    #[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        // Other tests take numbers concurrently, so the next one is only later.
3039        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    /// `new` logs each configuration line once (the parse's duplicate values,
3052    /// the post-processing's); a copy for one
3053    /// more client logs none of them, as Java's client logs nothing about the
3054    /// config it is given, but makes the same reads, so its used set is
3055    /// `new`'s.
3056    #[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    /// `bootstrap.servers` is a `Type.LIST`: `ConfigDef.parseType` trims the
3096    /// value and splits it on `\\s*,\\s*`, so whitespace around the commas
3097    /// and at the ends never reaches `ClientUtils.parseAndValidateAddresses`
3098    /// (which does not trim, and rejects it).
3099    #[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    /// `bootstrap.servers` is validated with Java's
3121    /// `ValidList.anyNonDuplicateValues(false, false)` (`ProducerConfig`): an empty
3122    /// element is rejected with `ConfigDef`'s exact message and duplicates are removed
3123    /// (single-message `ConfigException`, no `Invalid value` prefix). An empty list is rejected too.
3124    #[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        // `ConfigDef.parseValue` removes duplicates (with a warning) before validating.
3141        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    // -- Debug redaction -----------------------------------------------------
3162
3163    /// `{:?}` renders no secret the user passed, and `originals` only as its
3164    /// key set. The props hold the four `Type.PASSWORD` keys this client
3165    /// parses (`SslConfigs`, `SaslConfigs`), the three
3166    /// SSL store and key password keys it does not define
3167    /// (`SslConfigs`), the two OAuth `Type.PASSWORD` keys
3168    /// it defines but does not parse (`SaslConfigs`), and an
3169    /// unknown key carrying a serializer credential: Java
3170    /// prints none of those values. The parsed secrets render as `[hidden]`
3171    /// through their typed fields, and the non-secret `bootstrap.servers` value
3172    /// stays visible through its typed field only.
3173    #[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        // The secrets did reach the config, typed and raw (so `originals` can
3202        // still be forwarded, e.g. to a `Partitioner`); they are only hidden
3203        // from `Debug`.
3204        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                // Only `originals` renders key names, so this proves the key
3217                // set is there.
3218                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            // `[hidden]` comes from the four typed `Password` fields only.
3224            assert_eq!(rendered.matches(Password::HIDDEN).count(), 4, "{rendered}");
3225            // The non-secret value renders once, through its typed field and
3226            // not through `originals`.
3227            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        // `originals` renders as its key set, sorted, with no value at all,
3242        // through the `AbstractConfig` the configuration holds.
3243        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    // Skipped `ProducerConfigTest` tests:
3262    // - `testAppendSerializerToConfig`, `testAppendSerializerToConfigWithException`
3263    //   exercise the static `appendSerializerToConfig`, which writes serializer
3264    //   classes into the config map; serializers are constructor arguments here,
3265    //   and the config takes no class (CLAUDE.md §2).
3266    // - `testValidateConfigPropertiesFile` reads `config/producer.properties` from
3267    //   the Apache Kafka source tree, which is not part of the Rust client.
3268
3269    /// Translated from `ProducerConfigTest.testDefaultMetadataRecoveryStrategy`.
3270    #[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    /// `AbstractConfig`'s typed getters answer every key's `ConfigDef` type,
3282    /// including the values `postProcessParsedConfig` rewrote.
3283    #[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        // `parseAcks` turned `all` into `-1`.
3297        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    /// `configNames`.
3325    #[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    /// `unused` reports the supplied keys nobody read; the getters, `ignore`
3358    /// and the producer's own reads mark keys used.
3359    #[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        // `postProcessParsedConfig` read neither of these.
3369        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        // The producer's reads: the `ssl.*` keys only for an SSL protocol.
3384        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    /// `enable.metrics.push` is not defined until KIP-714 client telemetry is
3393    /// implemented, so a value set for it is only reported as unused.
3394    #[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    /// Java's `equals` / `hashCode` compare the originals.
3406    #[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    /// The per-codec levels (`compression.gzip.level`, `.lz4.level`,
3423    /// `.zstd.level`) default to each codec's default level and are checked
3424    /// by `CompressionType.levelValidator()`.
3425    #[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}