llm-optimizer-processor 0.1.1

Data processing and transformation pipeline
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
//! Kafka source consumer implementation
//!
//! This module provides a robust Kafka consumer implementation for ingesting
//! events from Kafka topics with support for:
//! - Async message consumption with tokio
//! - Automatic reconnection on errors
//! - Configurable commit strategies (auto, manual, periodic)
//! - Multiple partition support
//! - Graceful shutdown
//! - Message deserialization with error handling
//! - Offset management integration with state backend
//! - Comprehensive metrics tracking

use chrono::{DateTime, Utc};
use rdkafka::config::ClientConfig;
use rdkafka::consumer::{Consumer, ConsumerContext, Rebalance, StreamConsumer};
use rdkafka::error::{KafkaError, KafkaResult};
use rdkafka::message::{BorrowedMessage, Headers};
use rdkafka::{ClientContext, Message, Offset, TopicPartitionList};
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::{mpsc, RwLock};
use tokio::time;
use tracing::{debug, error, info, warn};

use crate::error::{ProcessorError, Result};
use crate::state::StateBackend;

/// Default poll timeout for Kafka consumer
const DEFAULT_POLL_TIMEOUT: Duration = Duration::from_millis(100);

/// Default commit interval for periodic commits
const DEFAULT_COMMIT_INTERVAL: Duration = Duration::from_secs(5);

/// Default max retries for message consumption
const DEFAULT_MAX_RETRIES: u32 = 3;

/// Default backoff for retries
const DEFAULT_RETRY_BACKOFF: Duration = Duration::from_secs(1);

/// Commit strategy for offset management
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CommitStrategy {
    /// Automatic commits by rdkafka (not recommended for exactly-once)
    Auto,
    /// Manual commit after each message
    Manual,
    /// Periodic commit at specified interval
    Periodic,
    /// Commit on checkpoint (integration with state backend)
    OnCheckpoint,
}

/// Configuration for Kafka source consumer
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KafkaSourceConfig {
    /// Kafka brokers (comma-separated list)
    pub brokers: String,
    /// Consumer group ID
    pub group_id: String,
    /// Topics to subscribe to
    pub topics: Vec<String>,
    /// Commit strategy
    pub commit_strategy: CommitStrategy,
    /// Commit interval for periodic commits
    pub commit_interval_secs: Option<u64>,
    /// Enable auto offset reset to earliest
    pub auto_offset_reset: Option<String>,
    /// Additional Kafka consumer configuration
    pub extra_config: HashMap<String, String>,
    /// Maximum retries for message consumption
    pub max_retries: Option<u32>,
    /// Retry backoff duration in milliseconds
    pub retry_backoff_ms: Option<u64>,
    /// Poll timeout in milliseconds
    pub poll_timeout_ms: Option<u64>,
}

impl Default for KafkaSourceConfig {
    fn default() -> Self {
        Self {
            brokers: "localhost:9092".to_string(),
            group_id: "llm-optimizer-processor".to_string(),
            topics: vec![],
            commit_strategy: CommitStrategy::Periodic,
            commit_interval_secs: Some(5),
            auto_offset_reset: Some("earliest".to_string()),
            extra_config: HashMap::new(),
            max_retries: Some(DEFAULT_MAX_RETRIES),
            retry_backoff_ms: Some(DEFAULT_RETRY_BACKOFF.as_millis() as u64),
            poll_timeout_ms: Some(DEFAULT_POLL_TIMEOUT.as_millis() as u64),
        }
    }
}

/// Metrics tracked by the Kafka source
#[derive(Debug, Clone)]
pub struct KafkaSourceMetrics {
    /// Total messages consumed
    pub messages_consumed: u64,
    /// Total messages failed
    pub messages_failed: u64,
    /// Messages consumed per partition
    pub messages_per_partition: HashMap<i32, u64>,
    /// Current lag per partition (estimated)
    pub lag_per_partition: HashMap<i32, i64>,
    /// Total bytes consumed
    pub bytes_consumed: u64,
    /// Messages per second (last minute)
    pub throughput_msgs_per_sec: f64,
    /// Bytes per second (last minute)
    pub throughput_bytes_per_sec: f64,
    /// Average message size
    pub avg_message_size: f64,
    /// Last commit timestamp
    pub last_commit_time: Option<DateTime<Utc>>,
    /// Total commits
    pub total_commits: u64,
    /// Rebalance count
    pub rebalance_count: u64,
    /// Current partition assignment
    pub assigned_partitions: Vec<i32>,
}

impl Default for KafkaSourceMetrics {
    fn default() -> Self {
        Self {
            messages_consumed: 0,
            messages_failed: 0,
            messages_per_partition: HashMap::new(),
            lag_per_partition: HashMap::new(),
            bytes_consumed: 0,
            throughput_msgs_per_sec: 0.0,
            throughput_bytes_per_sec: 0.0,
            avg_message_size: 0.0,
            last_commit_time: None,
            total_commits: 0,
            rebalance_count: 0,
            assigned_partitions: vec![],
        }
    }
}

/// Custom consumer context for handling callbacks
struct SourceConsumerContext {
    metrics: Arc<RwLock<KafkaSourceMetrics>>,
    state_backend: Option<Arc<dyn StateBackend>>,
}

impl ClientContext for SourceConsumerContext {}

impl ConsumerContext for SourceConsumerContext {
    fn pre_rebalance(&self, rebalance: &Rebalance) {
        match rebalance {
            Rebalance::Revoke(tpl) => {
                info!("Partition revocation: {:?}", tpl);
                let metrics = self.metrics.clone();
                tokio::spawn(async move {
                    let mut metrics = metrics.write().await;
                    metrics.rebalance_count += 1;
                });
            }
            Rebalance::Assign(tpl) => {
                info!("Partition assignment: {:?}", tpl);
            }
            Rebalance::Error(err) => {
                error!("Rebalance error: {}", err);
            }
        }
    }

    fn post_rebalance(&self, rebalance: &Rebalance) {
        match rebalance {
            Rebalance::Assign(tpl) => {
                let partitions: Vec<i32> = tpl
                    .elements()
                    .iter()
                    .map(|elem| elem.partition())
                    .collect();
                info!("Successfully assigned partitions: {:?}", partitions);

                let metrics = self.metrics.clone();
                tokio::spawn(async move {
                    let mut metrics = metrics.write().await;
                    metrics.assigned_partitions = partitions;
                });
            }
            Rebalance::Revoke(_) => {
                info!("Partitions revoked successfully");
            }
            Rebalance::Error(err) => {
                error!("Post-rebalance error: {}", err);
            }
        }
    }

    fn commit_callback(&self, result: KafkaResult<()>, _offsets: &TopicPartitionList) {
        match result {
            Ok(_) => {
                let metrics = self.metrics.clone();
                tokio::spawn(async move {
                    let mut metrics = metrics.write().await;
                    metrics.total_commits += 1;
                    metrics.last_commit_time = Some(Utc::now());
                });
            }
            Err(err) => {
                error!("Commit callback error: {}", err);
            }
        }
    }
}

/// Message envelope with metadata
#[derive(Debug, Clone)]
pub struct KafkaMessage<T> {
    /// Deserialized payload
    pub payload: T,
    /// Message key (if present)
    pub key: Option<Vec<u8>>,
    /// Topic name
    pub topic: String,
    /// Partition
    pub partition: i32,
    /// Offset
    pub offset: i64,
    /// Timestamp (if available)
    pub timestamp: Option<DateTime<Utc>>,
    /// Headers (if present)
    pub headers: HashMap<String, Vec<u8>>,
}

/// Kafka source consumer for ingesting events
pub struct KafkaSource<T> {
    /// Kafka consumer
    consumer: Arc<StreamConsumer<SourceConsumerContext>>,
    /// Configuration
    config: KafkaSourceConfig,
    /// Metrics
    metrics: Arc<RwLock<KafkaSourceMetrics>>,
    /// State backend for offset management
    state_backend: Option<Arc<dyn StateBackend>>,
    /// Shutdown signal
    shutdown: Arc<AtomicBool>,
    /// Throughput tracking
    throughput_tracker: Arc<RwLock<ThroughputTracker>>,
    /// Phantom data for type parameter
    _phantom: std::marker::PhantomData<T>,
}

/// Tracks throughput over time windows
struct ThroughputTracker {
    /// Messages in current window
    messages_in_window: u64,
    /// Bytes in current window
    bytes_in_window: u64,
    /// Window start time
    window_start: Instant,
    /// Window duration
    window_duration: Duration,
}

impl ThroughputTracker {
    fn new() -> Self {
        Self {
            messages_in_window: 0,
            bytes_in_window: 0,
            window_start: Instant::now(),
            window_duration: Duration::from_secs(60),
        }
    }

    fn record(&mut self, bytes: usize) {
        self.messages_in_window += 1;
        self.bytes_in_window += bytes as u64;
    }

    fn calculate(&mut self) -> (f64, f64) {
        let elapsed = self.window_start.elapsed();

        if elapsed >= self.window_duration {
            let secs = elapsed.as_secs_f64();
            let msgs_per_sec = self.messages_in_window as f64 / secs;
            let bytes_per_sec = self.bytes_in_window as f64 / secs;

            // Reset for next window
            self.messages_in_window = 0;
            self.bytes_in_window = 0;
            self.window_start = Instant::now();

            (msgs_per_sec, bytes_per_sec)
        } else {
            (0.0, 0.0)
        }
    }
}

impl<T> KafkaSource<T>
where
    T: DeserializeOwned + Send + Sync + 'static,
{
    /// Create a new Kafka source consumer
    ///
    /// # Arguments
    ///
    /// * `config` - Kafka source configuration
    /// * `state_backend` - Optional state backend for offset management
    ///
    /// # Returns
    ///
    /// * `Result<Self>` - New Kafka source instance
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use processor::kafka::{KafkaSource, KafkaSourceConfig};
    /// use serde::{Deserialize, Serialize};
    ///
    /// #[derive(Debug, Deserialize, Serialize)]
    /// struct MyEvent {
    ///     id: String,
    ///     value: f64,
    /// }
    ///
    /// #[tokio::main]
    /// async fn main() -> anyhow::Result<()> {
    ///     let config = KafkaSourceConfig {
    ///         brokers: "localhost:9092".to_string(),
    ///         group_id: "my-consumer-group".to_string(),
    ///         topics: vec!["my-topic".to_string()],
    ///         ..Default::default()
    ///     };
    ///
    ///     let source = KafkaSource::<MyEvent>::new(config, None).await?;
    ///     Ok(())
    /// }
    /// ```
    pub async fn new(
        config: KafkaSourceConfig,
        state_backend: Option<Arc<dyn StateBackend>>,
    ) -> Result<Self> {
        let metrics = Arc::new(RwLock::new(KafkaSourceMetrics::default()));

        let context = SourceConsumerContext {
            metrics: metrics.clone(),
            state_backend: state_backend.clone(),
        };

        let consumer = Self::create_consumer(&config, context)?;

        Ok(Self {
            consumer: Arc::new(consumer),
            config,
            metrics,
            state_backend,
            shutdown: Arc::new(AtomicBool::new(false)),
            throughput_tracker: Arc::new(RwLock::new(ThroughputTracker::new())),
            _phantom: std::marker::PhantomData,
        })
    }

    /// Create the underlying Kafka consumer
    fn create_consumer(
        config: &KafkaSourceConfig,
        context: SourceConsumerContext,
    ) -> Result<StreamConsumer<SourceConsumerContext>> {
        let mut client_config = ClientConfig::new();

        client_config
            .set("bootstrap.servers", &config.brokers)
            .set("group.id", &config.group_id)
            .set("enable.auto.commit", "false"); // We handle commits manually

        // Set auto offset reset
        if let Some(ref reset) = config.auto_offset_reset {
            client_config.set("auto.offset.reset", reset);
        }

        // Apply extra configuration
        for (key, value) in &config.extra_config {
            client_config.set(key, value);
        }

        // Create consumer
        let consumer: StreamConsumer<SourceConsumerContext> = client_config
            .create_with_context(context)
            .map_err(|e| ProcessorError::Configuration {
                source: Box::new(e),
            })?;

        Ok(consumer)
    }

    /// Subscribe to topics
    ///
    /// # Arguments
    ///
    /// * `topics` - Topics to subscribe to
    ///
    /// # Returns
    ///
    /// * `Result<()>` - Success or error
    pub async fn subscribe(&self, topics: &[String]) -> Result<()> {
        let topic_refs: Vec<&str> = topics.iter().map(|s| s.as_str()).collect();

        self.consumer
            .subscribe(&topic_refs)
            .map_err(|e| ProcessorError::Configuration {
                source: Box::new(e),
            })?;

        info!("Subscribed to topics: {:?}", topics);
        Ok(())
    }

    /// Poll for a single message
    ///
    /// # Returns
    ///
    /// * `Result<Option<KafkaMessage<T>>>` - Message or None if timeout/shutdown
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use processor::kafka::{KafkaSource, KafkaSourceConfig};
    /// # use serde::{Deserialize, Serialize};
    /// # #[derive(Debug, Deserialize, Serialize)]
    /// # struct MyEvent { id: String }
    /// # #[tokio::main]
    /// # async fn main() -> anyhow::Result<()> {
    /// # let config = KafkaSourceConfig::default();
    /// # let source = KafkaSource::<MyEvent>::new(config, None).await?;
    /// while let Some(msg) = source.poll().await? {
    ///     println!("Received: {:?}", msg.payload);
    ///     source.commit().await?;
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn poll(&self) -> Result<Option<KafkaMessage<T>>> {
        if self.shutdown.load(Ordering::Relaxed) {
            return Ok(None);
        }

        let _timeout = Duration::from_millis(
            self.config.poll_timeout_ms.unwrap_or(DEFAULT_POLL_TIMEOUT.as_millis() as u64)
        );

        match self.consumer.recv().await {
            Ok(msg) => {
                let kafka_msg = self.process_message(&msg).await?;

                // Update metrics
                self.update_metrics(&msg).await;

                Ok(Some(kafka_msg))
            }
            Err(KafkaError::PartitionEOF(_)) => {
                debug!("Reached end of partition");
                Ok(None)
            }
            Err(e) => {
                error!("Error polling message: {}", e);
                let mut metrics = self.metrics.write().await;
                metrics.messages_failed += 1;

                Err(ProcessorError::Execution {
                    source: Box::new(e),
                })
            }
        }
    }

    /// Process a borrowed message into a typed message
    async fn process_message(&self, msg: &BorrowedMessage<'_>) -> Result<KafkaMessage<T>> {
        // Extract payload
        let payload_bytes = msg.payload().ok_or_else(|| ProcessorError::Unexpected(
            "Message has no payload".to_string(),
        ))?;

        // Deserialize payload
        let payload: T = serde_json::from_slice(payload_bytes)
            .map_err(|e| ProcessorError::Serialization(format!("Failed to deserialize message: {}", e)))?;

        // Extract key
        let key = msg.key().map(|k| k.to_vec());

        // Extract headers
        let mut headers = HashMap::new();
        if let Some(msg_headers) = msg.headers() {
            for idx in 0..msg_headers.count() {
                let header = msg_headers.get(idx);
                headers.insert(header.key.to_string(), header.value.map(|v| v.to_vec()).unwrap_or_default());
            }
        }

        // Extract timestamp
        let timestamp = msg.timestamp().to_millis().and_then(|millis| {
            DateTime::from_timestamp_millis(millis)
        });

        Ok(KafkaMessage {
            payload,
            key,
            topic: msg.topic().to_string(),
            partition: msg.partition(),
            offset: msg.offset(),
            timestamp,
            headers,
        })
    }

    /// Update metrics after processing a message
    async fn update_metrics(&self, msg: &BorrowedMessage<'_>) {
        let partition = msg.partition();
        let payload_size = msg.payload().map(|p| p.len()).unwrap_or(0);

        let mut metrics = self.metrics.write().await;

        metrics.messages_consumed += 1;
        metrics.bytes_consumed += payload_size as u64;

        *metrics.messages_per_partition.entry(partition).or_insert(0) += 1;

        // Update average message size
        if metrics.messages_consumed > 0 {
            metrics.avg_message_size = metrics.bytes_consumed as f64 / metrics.messages_consumed as f64;
        }

        drop(metrics);

        // Update throughput
        let mut tracker = self.throughput_tracker.write().await;
        tracker.record(payload_size);
        let (msgs_per_sec, bytes_per_sec) = tracker.calculate();

        if msgs_per_sec > 0.0 {
            let mut metrics = self.metrics.write().await;
            metrics.throughput_msgs_per_sec = msgs_per_sec;
            metrics.throughput_bytes_per_sec = bytes_per_sec;
        }
    }

    /// Commit current offsets
    ///
    /// # Returns
    ///
    /// * `Result<()>` - Success or error
    pub async fn commit(&self) -> Result<()> {
        self.consumer
            .commit_consumer_state(rdkafka::consumer::CommitMode::Async)
            .map_err(|e| ProcessorError::Execution {
                source: Box::new(e),
            })?;

        debug!("Committed offsets");
        Ok(())
    }

    /// Seek to a specific offset for a partition
    ///
    /// # Arguments
    ///
    /// * `topic` - Topic name
    /// * `partition` - Partition number
    /// * `offset` - Offset to seek to
    ///
    /// # Returns
    ///
    /// * `Result<()>` - Success or error
    pub async fn seek(&self, topic: &str, partition: i32, offset: i64) -> Result<()> {
        self.consumer
            .seek(topic, partition, Offset::Offset(offset), Duration::from_secs(10))
            .map_err(|e| ProcessorError::Execution {
                source: Box::new(e),
            })?;

        info!("Seeked to offset {} for {}/{}", offset, topic, partition);
        Ok(())
    }

    /// Get current consumption metrics
    ///
    /// # Returns
    ///
    /// * `KafkaSourceMetrics` - Current metrics snapshot
    pub async fn metrics(&self) -> KafkaSourceMetrics {
        self.metrics.read().await.clone()
    }

    /// Start consuming messages and send them to a channel
    ///
    /// This method runs in a loop, consuming messages and sending them to the
    /// provided channel. It handles automatic commits based on the configured
    /// commit strategy.
    ///
    /// # Arguments
    ///
    /// * `tx` - Channel sender for messages
    ///
    /// # Returns
    ///
    /// * `Result<()>` - Success or error
    pub async fn start(&self, tx: mpsc::Sender<KafkaMessage<T>>) -> Result<()> {
        // Subscribe to configured topics
        self.subscribe(&self.config.topics.clone()).await?;

        let mut commit_interval = time::interval(Duration::from_secs(
            self.config.commit_interval_secs.unwrap_or(5),
        ));

        loop {
            tokio::select! {
                _ = commit_interval.tick() => {
                    if self.config.commit_strategy == CommitStrategy::Periodic {
                        if let Err(e) = self.commit().await {
                            warn!("Failed to commit offsets: {}", e);
                        }
                    }
                }

                result = self.poll() => {
                    match result {
                        Ok(Some(msg)) => {
                            // Send message to channel
                            if tx.send(msg).await.is_err() {
                                warn!("Channel closed, stopping consumer");
                                break;
                            }

                            // Commit if strategy is manual
                            if self.config.commit_strategy == CommitStrategy::Manual {
                                if let Err(e) = self.commit().await {
                                    warn!("Failed to commit offset: {}", e);
                                }
                            }
                        }
                        Ok(None) => {
                            // Timeout or shutdown
                            if self.shutdown.load(Ordering::Relaxed) {
                                info!("Shutdown signal received");
                                break;
                            }
                        }
                        Err(e) => {
                            error!("Error consuming message: {}", e);

                            // Implement retry logic
                            let _max_retries = self.config.max_retries.unwrap_or(DEFAULT_MAX_RETRIES);
                            let backoff = Duration::from_millis(
                                self.config.retry_backoff_ms.unwrap_or(DEFAULT_RETRY_BACKOFF.as_millis() as u64)
                            );

                            tokio::time::sleep(backoff).await;
                        }
                    }
                }
            }
        }

        // Final commit before shutdown
        if let Err(e) = self.commit().await {
            warn!("Failed to commit offsets on shutdown: {}", e);
        }

        info!("Kafka source consumer stopped");
        Ok(())
    }

    /// Stop the consumer gracefully
    pub async fn close(&self) -> Result<()> {
        info!("Stopping Kafka source consumer");
        self.shutdown.store(true, Ordering::Relaxed);

        // Final commit
        self.commit().await?;

        info!("Kafka source consumer stopped gracefully");
        Ok(())
    }

    /// Get current lag for all assigned partitions
    ///
    /// This fetches watermarks from Kafka to calculate lag
    pub async fn get_lag(&self) -> Result<HashMap<i32, i64>> {
        let metadata = self.consumer
            .fetch_metadata(None, Duration::from_secs(10))
            .map_err(|e| ProcessorError::Execution {
                source: Box::new(e),
            })?;

        let mut lag_map = HashMap::new();

        for topic in metadata.topics() {
            for partition in topic.partitions() {
                let partition_id = partition.id();

                // Get current position
                let position = self.consumer
                    .position()
                    .map_err(|e| ProcessorError::Execution {
                        source: Box::new(e),
                    })?;

                let current_offset = position
                    .elements_for_topic(topic.name())
                    .iter()
                    .find(|e| e.partition() == partition_id)
                    .and_then(|e| e.offset().to_raw())
                    .unwrap_or(0);

                // Get high watermark
                let (_, high_watermark) = self.consumer
                    .fetch_watermarks(topic.name(), partition_id, Duration::from_secs(10))
                    .map_err(|e| ProcessorError::Execution {
                        source: Box::new(e),
                    })?;

                let lag = high_watermark - current_offset;
                lag_map.insert(partition_id, lag);
            }
        }

        // Update metrics
        let mut metrics = self.metrics.write().await;
        metrics.lag_per_partition = lag_map.clone();

        Ok(lag_map)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde::{Deserialize, Serialize};

    #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
    struct TestEvent {
        id: String,
        value: f64,
        timestamp: i64,
    }

    #[test]
    fn test_kafka_source_config_default() {
        let config = KafkaSourceConfig::default();
        assert_eq!(config.brokers, "localhost:9092");
        assert_eq!(config.group_id, "llm-optimizer-processor");
        assert_eq!(config.commit_strategy, CommitStrategy::Periodic);
    }

    #[test]
    fn test_commit_strategy_serialization() {
        let strategy = CommitStrategy::Periodic;
        let json = serde_json::to_string(&strategy).unwrap();
        assert_eq!(json, "\"periodic\"");

        let deserialized: CommitStrategy = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized, CommitStrategy::Periodic);
    }

    #[test]
    fn test_throughput_tracker() {
        let mut tracker = ThroughputTracker::new();

        // Record some messages
        for _ in 0..100 {
            tracker.record(1024);
        }

        // Not enough time has passed
        let (msgs, bytes) = tracker.calculate();
        assert_eq!(msgs, 0.0);
        assert_eq!(bytes, 0.0);
    }

    #[tokio::test]
    async fn test_kafka_message_structure() {
        let event = TestEvent {
            id: "test-123".to_string(),
            value: 42.0,
            timestamp: 1234567890,
        };

        let msg = KafkaMessage {
            payload: event.clone(),
            key: Some(b"key1".to_vec()),
            topic: "test-topic".to_string(),
            partition: 0,
            offset: 100,
            timestamp: Some(Utc::now()),
            headers: HashMap::new(),
        };

        assert_eq!(msg.payload.id, "test-123");
        assert_eq!(msg.partition, 0);
        assert_eq!(msg.offset, 100);
    }

    #[tokio::test]
    async fn test_metrics_default() {
        let metrics = KafkaSourceMetrics::default();
        assert_eq!(metrics.messages_consumed, 0);
        assert_eq!(metrics.messages_failed, 0);
        assert_eq!(metrics.bytes_consumed, 0);
        assert_eq!(metrics.total_commits, 0);
        assert_eq!(metrics.rebalance_count, 0);
    }
}