rustfs-kafka 1.2.0

Rust client for Apache Kafka
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
//! Transactional producer support for exactly-once semantics.
//!
//! Provides [`TransactionalProducer`] for atomic writes across multiple
//! topic-partitions using Kafka's transaction protocol.

use std::collections::{HashMap, HashSet};

use tracing::{debug, info};

use crate::client::{KafkaClient, KafkaClientInternals, ProduceMessage, RequiredAcks};
use crate::error::{Error, KafkaCode, Result};
use crate::protocol::init_producer_id;
use crate::protocol::transaction::{self, TxnPartition};

use super::{AsBytes, DefaultPartitioner, Partitioner, Record, State};

/// A producer that supports Kafka transactions.
///
/// Transactions allow atomic writes across multiple topic-partitions,
/// enabling exactly-once semantics for consume-transform-produce patterns.
///
/// # Example
///
/// ```no_run
/// use rustfs_kafka::producer::{TransactionalProducer, Record};
///
/// let mut producer = TransactionalProducer::from_hosts(vec!["localhost:9092".to_owned()])
///     .with_transactional_id("my-txn-id".to_owned())
///     .create()
///     .unwrap();
///
/// producer.begin().unwrap();
/// producer.send(&Record::from_value("topic-a", &b"msg1"[..])).unwrap();
/// producer.send(&Record::from_value("topic-b", &b"msg2"[..])).unwrap();
/// producer.commit().unwrap();
/// ```
pub struct TransactionalProducer<P = DefaultPartitioner> {
    client: KafkaClient,
    state: State<P>,
    producer_id: i64,
    producer_epoch: i16,
    transactional_id: String,
    sequence_numbers: HashMap<(String, i32), i32>,
    current_txn_partitions: HashSet<(String, i32)>,
    in_transaction: bool,
    txn_epoch: u64,
}

impl TransactionalProducer {
    /// Starts building a new transactional producer from the given hosts.
    #[must_use]
    pub fn from_hosts(hosts: Vec<String>) -> TransactionalBuilder<DefaultPartitioner> {
        TransactionalBuilder::new(None, hosts)
    }

    /// Starts building a new transactional producer from an existing client.
    #[must_use]
    pub fn from_client(client: KafkaClient) -> TransactionalBuilder<DefaultPartitioner> {
        TransactionalBuilder::new(Some(client), Vec::new())
    }

    /// Borrows the underlying kafka client.
    #[must_use]
    pub fn client(&self) -> &KafkaClient {
        &self.client
    }

    /// Borrows the underlying kafka client as mut.
    pub fn client_mut(&mut self) -> &mut KafkaClient {
        &mut self.client
    }

    /// Returns the current producer ID.
    #[must_use]
    pub fn producer_id(&self) -> i64 {
        self.producer_id
    }

    /// Returns the current producer epoch.
    #[must_use]
    pub fn producer_epoch(&self) -> i16 {
        self.producer_epoch
    }

    /// Returns the transactional ID.
    #[must_use]
    pub fn transactional_id(&self) -> &str {
        &self.transactional_id
    }

    /// Returns whether a transaction is currently active.
    #[must_use]
    pub fn in_transaction(&self) -> bool {
        self.in_transaction
    }
}

impl<P: Partitioner> TransactionalProducer<P> {
    /// Begins a new transaction.
    ///
    /// Clears any previous transaction state and prepares for new
    /// transactional writes.
    ///
    /// # Errors
    ///
    /// Returns an error if transactional state preparation fails.
    pub fn begin(&mut self) -> Result<()> {
        self.txn_epoch += 1;
        self.current_txn_partitions.clear();
        self.in_transaction = true;
        debug!(
            "Transaction began (txn_id: {}, epoch: {})",
            self.transactional_id, self.txn_epoch
        );
        Ok(())
    }

    /// Sends a message within the current transaction.
    ///
    /// If the target partition hasn't been added to the transaction yet,
    /// an `AddPartitionsToTxn` request is sent first. The message is then
    /// produced with the transactional producer's ID and epoch.
    ///
    /// # Errors
    ///
    /// Returns an error if not currently in a transaction, or if the
    /// broker reports an error.
    pub fn send<K, V>(&mut self, rec: &Record<'_, K, V>) -> Result<()>
    where
        K: AsBytes,
        V: AsBytes,
    {
        if !self.in_transaction {
            return Err(Error::Config(
                "not in a transaction; call begin() first".into(),
            ));
        }

        let key = if rec.key.as_bytes().is_empty() {
            None
        } else {
            Some(rec.key.as_bytes())
        };
        let value = if rec.value.as_bytes().is_empty() {
            None
        } else {
            Some(rec.value.as_bytes())
        };

        let mut msg = ProduceMessage {
            key,
            value,
            topic: rec.topic,
            partition: rec.partition,
            headers: &rec.headers.0,
        };

        // Partition the message (only uses self.state)
        {
            let topics = super::partitioner::Topics::new(&self.state.partitions);
            self.state.partitioner.partition(topics, &mut msg);
        }

        let tp = (msg.topic.to_owned(), msg.partition);

        // Add partition to transaction if needed
        if !self.current_txn_partitions.contains(&tp) {
            self.add_partition_to_txn(&tp.0, tp.1)?;
            self.current_txn_partitions.insert(tp.clone());
        }

        let seq = self.next_sequence_number(&tp);

        let confirms = self.client.internal_produce_messages(
            RequiredAcks::All as i16,
            30_000,
            std::iter::once(msg),
        )?;

        if let Some(confirm) = confirms.first()
            && let Some(p_confirm) = confirm.partition_confirms.first()
            && p_confirm.offset.is_err()
        {
            return Err(Error::Kafka(KafkaCode::Unknown));
        }

        debug!("Sent message to {}:{} (seq: {})", tp.0, tp.1, seq);

        Ok(())
    }

    /// Commits the current transaction.
    ///
    /// Sends an `EndTxn` request with `committed=true` to the transaction
    /// coordinator, making all messages sent since `begin()` visible to
    /// consumers.
    ///
    /// # Errors
    ///
    /// Returns an error if no transaction is active or broker commit calls fail.
    pub fn commit(&mut self) -> Result<()> {
        if !self.in_transaction {
            return Err(Error::Config(
                "not in a transaction; call begin() first".into(),
            ));
        }

        self.end_txn(true)?;

        info!(
            "Transaction committed (txn_id: {}, epoch: {})",
            self.transactional_id, self.txn_epoch
        );

        self.in_transaction = false;
        self.current_txn_partitions.clear();
        Ok(())
    }

    /// Aborts the current transaction.
    ///
    /// Sends an `EndTxn` request with `committed=false` to the transaction
    /// coordinator. All messages sent since `begin()` will be discarded.
    ///
    /// # Errors
    ///
    /// Returns an error if no transaction is active or broker abort calls fail.
    pub fn abort(&mut self) -> Result<()> {
        if !self.in_transaction {
            return Err(Error::Config(
                "not in a transaction; call begin() first".into(),
            ));
        }

        self.end_txn(false)?;

        info!(
            "Transaction aborted (txn_id: {}, epoch: {})",
            self.transactional_id, self.txn_epoch
        );

        self.in_transaction = false;
        self.current_txn_partitions.clear();
        Ok(())
    }

    fn end_txn(&mut self, committed: bool) -> Result<()> {
        let coordinator_host = self.find_transaction_coordinator()?;
        let correlation_id = self.client.next_correlation_id();
        let client_id = self.client.client_id().to_owned();

        let resp = transaction::fetch_end_txn(
            self.client.get_conn_mut(&coordinator_host)?,
            correlation_id,
            &client_id,
            self.producer_id,
            self.producer_epoch,
            &self.transactional_id,
            committed,
        )?;

        if resp.throttle_time_ms > 0 {
            debug!(
                "EndTxn throttled by coordinator: {} ms (txn_id: {})",
                resp.throttle_time_ms, self.transactional_id
            );
        }

        if resp.error_code != 0 {
            let err =
                Error::from_protocol(resp.error_code).unwrap_or(Error::Kafka(KafkaCode::Unknown));
            return Err(err);
        }

        Ok(())
    }

    fn add_partition_to_txn(&mut self, topic: &str, partition: i32) -> Result<()> {
        let coordinator_host = self.find_transaction_coordinator()?;
        let correlation_id = self.client.next_correlation_id();
        let client_id = self.client.client_id().to_owned();

        let txn_partitions = vec![TxnPartition {
            topic: topic.to_owned(),
            partitions: vec![partition],
        }];

        let resp = transaction::fetch_add_partitions_to_txn(
            self.client.get_conn_mut(&coordinator_host)?,
            correlation_id,
            &client_id,
            self.producer_id,
            self.producer_epoch,
            &self.transactional_id,
            &txn_partitions,
        )?;

        if resp.throttle_time_ms > 0 {
            debug!(
                "AddPartitionsToTxn throttled by coordinator: {} ms (txn_id: {})",
                resp.throttle_time_ms, self.transactional_id
            );
        }

        if resp.error_code != 0 {
            let err =
                Error::from_protocol(resp.error_code).unwrap_or(Error::Kafka(KafkaCode::Unknown));
            return Err(err);
        }

        for result in &resp.results {
            if result.error_code != 0 {
                return Err(Error::TopicPartitionError {
                    topic_name: result.topic.clone(),
                    partition_id: partition,
                    error_code: KafkaCode::from_protocol(result.error_code)
                        .unwrap_or(KafkaCode::Unknown),
                });
            }
        }

        debug!(
            "Added {}:{} to transaction (txn_id: {})",
            topic, partition, self.transactional_id
        );

        Ok(())
    }

    fn next_sequence_number(&mut self, tp: &(String, i32)) -> i32 {
        let seq = self.sequence_numbers.entry(tp.clone()).or_insert(0);
        let current = *seq;
        *seq += 1;
        current
    }

    fn find_transaction_coordinator(&mut self) -> Result<String> {
        self.client.load_metadata_all()?;

        self.client
            .group_coordinator_host(&self.transactional_id)
            .ok_or(Error::Kafka(KafkaCode::GroupCoordinatorNotAvailable))
    }
}

// --------------------------------------------------------------------
// Builder
// --------------------------------------------------------------------

/// Builder for constructing a [`TransactionalProducer`].
pub struct TransactionalBuilder<P = DefaultPartitioner> {
    client: Option<KafkaClient>,
    hosts: Vec<String>,
    transactional_id: Option<String>,
    client_id: Option<String>,
    partitioner: P,
    ack_timeout_ms: i32,
}

impl TransactionalBuilder {
    pub(crate) fn new(client: Option<KafkaClient>, hosts: Vec<String>) -> Self {
        Self {
            client,
            hosts,
            transactional_id: None,
            client_id: None,
            partitioner: DefaultPartitioner::default(),
            ack_timeout_ms: 30_000,
        }
    }
}

impl TransactionalBuilder<DefaultPartitioner> {
    /// Sets the transactional ID (required).
    #[must_use]
    pub fn with_transactional_id(mut self, id: impl Into<String>) -> Self {
        self.transactional_id = Some(id.into());
        self
    }

    /// Sets the client ID sent with each request.
    #[must_use]
    pub fn with_client_id(mut self, id: impl Into<String>) -> Self {
        self.client_id = Some(id.into());
        self
    }

    /// Sets the acknowledgement timeout in milliseconds.
    #[must_use]
    pub fn with_ack_timeout_ms(mut self, timeout_ms: i32) -> Self {
        self.ack_timeout_ms = timeout_ms;
        self
    }
}

impl<P: Partitioner> TransactionalBuilder<P> {
    /// Sets a custom partitioner.
    #[must_use]
    pub fn with_partitioner<Q: Partitioner>(self, partitioner: Q) -> TransactionalBuilder<Q> {
        TransactionalBuilder {
            client: self.client,
            hosts: self.hosts,
            transactional_id: self.transactional_id,
            client_id: self.client_id,
            partitioner,
            ack_timeout_ms: self.ack_timeout_ms,
        }
    }

    /// Builds the [`TransactionalProducer`].
    ///
    /// # Errors
    ///
    /// Returns an error if `transactional_id` is not set, if metadata
    /// loading fails, or if the producer ID initialization fails.
    pub fn create(self) -> Result<TransactionalProducer<P>> {
        let transactional_id = self
            .transactional_id
            .ok_or_else(|| Error::Config("transactional_id is required".into()))?;

        let mut client = match self.client {
            Some(client) => client,
            None => KafkaClient::new(self.hosts),
        };

        if let Some(client_id) = self.client_id {
            client.set_client_id(client_id);
        }

        client.load_metadata_all()?;

        let producer_id = init_producer_id_for_txn(&mut client, &transactional_id)?;

        let state = State::new(&mut client, self.partitioner);

        info!(
            "TransactionalProducer created (txn_id: {}, producer_id: {}, epoch: {})",
            transactional_id, producer_id.producer_id, producer_id.producer_epoch
        );

        Ok(TransactionalProducer {
            client,
            state,
            producer_id: producer_id.producer_id,
            producer_epoch: producer_id.producer_epoch,
            transactional_id,
            sequence_numbers: HashMap::new(),
            current_txn_partitions: HashSet::new(),
            in_transaction: false,
            txn_epoch: 0,
        })
    }
}

fn init_producer_id_for_txn(
    client: &mut KafkaClient,
    transactional_id: &str,
) -> Result<init_producer_id::InitProducerIdResponseData> {
    let coordinator_host = client
        .group_coordinator_host(transactional_id)
        .ok_or(Error::Kafka(KafkaCode::GroupCoordinatorNotAvailable))?;

    let correlation_id = client.next_correlation_id();
    let client_id = client.client_id().to_owned();

    let resp = init_producer_id::fetch_init_producer_id(
        client.get_conn_mut(&coordinator_host)?,
        correlation_id,
        &client_id,
        Some(transactional_id),
    )?;

    if resp.error_code != 0 {
        let err = Error::from_protocol(resp.error_code).unwrap_or(Error::Kafka(KafkaCode::Unknown));
        return Err(err);
    }

    Ok(resp)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_transactional_builder_requires_transactional_id() {
        let result = TransactionalProducer::from_hosts(vec!["localhost:9092".to_owned()])
            .with_client_id("test".to_owned())
            .create();

        match result {
            Err(ref e) => {
                let msg = e.to_string();
                assert!(msg.contains("transactional_id"), "got: {msg}");
            }
            Ok(_) => panic!("expected error when transactional_id is not set"),
        }
    }

    #[test]
    fn test_transactional_state_transitions() {
        let mut producer_state = TransactionState::new();
        assert!(!producer_state.in_transaction);

        producer_state.begin();
        assert!(producer_state.in_transaction);

        let err = producer_state.commit();
        assert!(err.is_ok());
        assert!(!producer_state.in_transaction);

        producer_state.begin();
        let err = producer_state.abort();
        assert!(err.is_ok());
        assert!(!producer_state.in_transaction);
    }

    #[test]
    fn test_cannot_commit_without_transaction() {
        let mut producer_state = TransactionState::new();
        let err = producer_state.commit();
        assert!(err.is_err());
    }

    #[test]
    fn test_cannot_abort_without_transaction() {
        let mut producer_state = TransactionState::new();
        let err = producer_state.abort();
        assert!(err.is_err());
    }

    /// Minimal state machine mirroring `TransactionalProducer` for unit tests
    /// without requiring a broker connection.
    struct TransactionState {
        in_transaction: bool,
    }

    impl TransactionState {
        fn new() -> Self {
            Self {
                in_transaction: false,
            }
        }

        fn begin(&mut self) {
            self.in_transaction = true;
        }

        fn commit(&mut self) -> Result<()> {
            if !self.in_transaction {
                return Err(Error::Config("not in a transaction".into()));
            }
            self.in_transaction = false;
            Ok(())
        }

        fn abort(&mut self) -> Result<()> {
            if !self.in_transaction {
                return Err(Error::Config("not in a transaction".into()));
            }
            self.in_transaction = false;
            Ok(())
        }
    }
}