llm-optimizer-processor 0.1.0

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
//! Kafka-specific error types and error handling.
//!
//! This module provides comprehensive error types for all Kafka operations,
//! including connection errors, serialization/deserialization errors,
//! consumption errors, and production errors.

use std::fmt;
use thiserror::Error;

/// Result type alias for Kafka operations.
pub type Result<T> = std::result::Result<T, KafkaError>;

/// Comprehensive error type for Kafka operations.
///
/// This enum covers all possible error scenarios when working with Kafka,
/// providing detailed context for debugging and error handling.
///
/// # Examples
///
/// ```rust
/// use processor::kafka::error::{KafkaError, Result};
///
/// fn connect_to_kafka() -> Result<()> {
///     // Simulate connection error
///     Err(KafkaError::Connection {
///         message: "Failed to connect to broker".to_string(),
///         broker: "localhost:9092".to_string(),
///     })
/// }
///
/// match connect_to_kafka() {
///     Ok(_) => println!("Connected successfully"),
///     Err(KafkaError::Connection { message, broker }) => {
///         eprintln!("Connection error to {}: {}", broker, message);
///     }
///     Err(e) => eprintln!("Other error: {}", e),
/// }
/// ```
#[derive(Error, Debug)]
pub enum KafkaError {
    /// Error establishing connection to Kafka broker.
    #[error("Failed to connect to Kafka broker {broker}: {message}")]
    Connection {
        /// Error message describing the connection failure.
        message: String,
        /// Broker address that failed to connect.
        broker: String,
    },

    /// Error during message consumption.
    #[error("Failed to consume message from topic {topic}: {message}")]
    Consumption {
        /// Error message describing the consumption failure.
        message: String,
        /// Topic from which consumption failed.
        topic: String,
        /// Partition number if available.
        partition: Option<i32>,
        /// Offset if available.
        offset: Option<i64>,
    },

    /// Error during message production.
    #[error("Failed to produce message to topic {topic}: {message}")]
    Production {
        /// Error message describing the production failure.
        message: String,
        /// Topic to which production failed.
        topic: String,
        /// Partition number if available.
        partition: Option<i32>,
    },

    /// Error during serialization of messages.
    #[error("Serialization error: {message}")]
    Serialization {
        /// Error message describing the serialization failure.
        message: String,
        /// Optional context about what was being serialized.
        context: Option<String>,
    },

    /// Error during deserialization of messages.
    #[error("Deserialization error: {message}")]
    Deserialization {
        /// Error message describing the deserialization failure.
        message: String,
        /// Optional context about what was being deserialized.
        context: Option<String>,
        /// Raw bytes that failed to deserialize (truncated for display).
        payload_sample: Option<Vec<u8>>,
    },

    /// Configuration validation error.
    #[error("Invalid configuration: {message}")]
    InvalidConfiguration {
        /// Error message describing the configuration issue.
        message: String,
        /// Field name that has invalid configuration.
        field: Option<String>,
    },

    /// Error committing offsets.
    #[error("Failed to commit offsets for topic {topic}: {message}")]
    OffsetCommit {
        /// Error message describing the commit failure.
        message: String,
        /// Topic for which offset commit failed.
        topic: String,
        /// Partition for which offset commit failed.
        partition: i32,
        /// Offset that failed to commit.
        offset: i64,
    },

    /// Error during consumer group coordination.
    #[error("Consumer group coordination error for group {group_id}: {message}")]
    GroupCoordination {
        /// Error message describing the coordination failure.
        message: String,
        /// Consumer group ID.
        group_id: String,
    },

    /// Error during partition assignment.
    #[error("Partition assignment error: {message}")]
    PartitionAssignment {
        /// Error message describing the assignment failure.
        message: String,
        /// Topics involved in the assignment.
        topics: Vec<String>,
    },

    /// Authentication or authorization error.
    #[error("Authentication/Authorization error: {message}")]
    AuthError {
        /// Error message describing the auth failure.
        message: String,
        /// Security protocol being used.
        protocol: String,
    },

    /// Network or I/O error.
    #[error("Network I/O error: {message}")]
    IoError {
        /// Error message describing the I/O failure.
        message: String,
        /// Optional underlying I/O error.
        #[source]
        source: Option<std::io::Error>,
    },

    /// Timeout error during Kafka operations.
    #[error("Operation timed out after {timeout_ms}ms: {operation}")]
    Timeout {
        /// Description of the operation that timed out.
        operation: String,
        /// Timeout duration in milliseconds.
        timeout_ms: u64,
    },

    /// Error related to topic metadata.
    #[error("Topic metadata error for topic {topic}: {message}")]
    TopicMetadata {
        /// Error message describing the metadata failure.
        message: String,
        /// Topic name.
        topic: String,
    },

    /// Error when a topic partition doesn't exist.
    #[error("Topic partition {topic}:{partition} does not exist")]
    PartitionNotFound {
        /// Topic name.
        topic: String,
        /// Partition number.
        partition: i32,
    },

    /// Error when reaching resource limits.
    #[error("Resource limit exceeded: {message}")]
    ResourceExhausted {
        /// Error message describing the resource exhaustion.
        message: String,
        /// Resource type (e.g., "buffer", "connections").
        resource: String,
    },

    /// Error from the underlying rdkafka library.
    #[error("Kafka client error: {message} (error code: {code:?})")]
    KafkaClient {
        /// Error message from rdkafka.
        message: String,
        /// rdkafka error code.
        code: Option<i32>,
    },

    /// Generic error for cases not covered by specific variants.
    #[error("Kafka error: {0}")]
    Other(String),
}

impl KafkaError {
    /// Create a connection error.
    pub fn connection(message: impl Into<String>, broker: impl Into<String>) -> Self {
        Self::Connection {
            message: message.into(),
            broker: broker.into(),
        }
    }

    /// Create a consumption error.
    pub fn consumption(
        message: impl Into<String>,
        topic: impl Into<String>,
        partition: Option<i32>,
        offset: Option<i64>,
    ) -> Self {
        Self::Consumption {
            message: message.into(),
            topic: topic.into(),
            partition,
            offset,
        }
    }

    /// Create a production error.
    pub fn production(
        message: impl Into<String>,
        topic: impl Into<String>,
        partition: Option<i32>,
    ) -> Self {
        Self::Production {
            message: message.into(),
            topic: topic.into(),
            partition,
        }
    }

    /// Create a serialization error.
    pub fn serialization(message: impl Into<String>, context: Option<String>) -> Self {
        Self::Serialization {
            message: message.into(),
            context,
        }
    }

    /// Create a deserialization error.
    pub fn deserialization(
        message: impl Into<String>,
        context: Option<String>,
        payload_sample: Option<Vec<u8>>,
    ) -> Self {
        Self::Deserialization {
            message: message.into(),
            context,
            payload_sample,
        }
    }

    /// Create an invalid configuration error.
    pub fn invalid_config(message: impl Into<String>, field: Option<String>) -> Self {
        Self::InvalidConfiguration {
            message: message.into(),
            field,
        }
    }

    /// Create an offset commit error.
    pub fn offset_commit(
        message: impl Into<String>,
        topic: impl Into<String>,
        partition: i32,
        offset: i64,
    ) -> Self {
        Self::OffsetCommit {
            message: message.into(),
            topic: topic.into(),
            partition,
            offset,
        }
    }

    /// Create a group coordination error.
    pub fn group_coordination(message: impl Into<String>, group_id: impl Into<String>) -> Self {
        Self::GroupCoordination {
            message: message.into(),
            group_id: group_id.into(),
        }
    }

    /// Create a partition assignment error.
    pub fn partition_assignment(message: impl Into<String>, topics: Vec<String>) -> Self {
        Self::PartitionAssignment {
            message: message.into(),
            topics,
        }
    }

    /// Create an authentication/authorization error.
    pub fn auth_error(message: impl Into<String>, protocol: impl Into<String>) -> Self {
        Self::AuthError {
            message: message.into(),
            protocol: protocol.into(),
        }
    }

    /// Create a timeout error.
    pub fn timeout(operation: impl Into<String>, timeout_ms: u64) -> Self {
        Self::Timeout {
            operation: operation.into(),
            timeout_ms,
        }
    }

    /// Create a topic metadata error.
    pub fn topic_metadata(message: impl Into<String>, topic: impl Into<String>) -> Self {
        Self::TopicMetadata {
            message: message.into(),
            topic: topic.into(),
        }
    }

    /// Create a partition not found error.
    pub fn partition_not_found(topic: impl Into<String>, partition: i32) -> Self {
        Self::PartitionNotFound {
            topic: topic.into(),
            partition,
        }
    }

    /// Create a resource exhausted error.
    pub fn resource_exhausted(message: impl Into<String>, resource: impl Into<String>) -> Self {
        Self::ResourceExhausted {
            message: message.into(),
            resource: resource.into(),
        }
    }

    /// Create a Kafka client error.
    pub fn kafka_client(message: impl Into<String>, code: Option<i32>) -> Self {
        Self::KafkaClient {
            message: message.into(),
            code,
        }
    }

    /// Check if the error is retryable.
    ///
    /// Returns `true` if the operation that caused this error can be retried.
    pub fn is_retryable(&self) -> bool {
        matches!(
            self,
            KafkaError::Timeout { .. }
                | KafkaError::IoError { .. }
                | KafkaError::Connection { .. }
                | KafkaError::GroupCoordination { .. }
                | KafkaError::ResourceExhausted { .. }
        )
    }

    /// Check if the error is fatal and requires system shutdown.
    ///
    /// Returns `true` if the error indicates an unrecoverable state.
    pub fn is_fatal(&self) -> bool {
        matches!(
            self,
            KafkaError::AuthError { .. } | KafkaError::InvalidConfiguration { .. }
        )
    }

    /// Get error severity level.
    ///
    /// Returns a severity level suitable for logging and monitoring.
    pub fn severity(&self) -> ErrorSeverity {
        match self {
            KafkaError::InvalidConfiguration { .. } | KafkaError::AuthError { .. } => {
                ErrorSeverity::Critical
            }
            KafkaError::Connection { .. }
            | KafkaError::GroupCoordination { .. }
            | KafkaError::Production { .. } => ErrorSeverity::Error,
            KafkaError::Timeout { .. }
            | KafkaError::ResourceExhausted { .. }
            | KafkaError::Consumption { .. } => ErrorSeverity::Warning,
            _ => ErrorSeverity::Info,
        }
    }
}

/// Error severity levels.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum ErrorSeverity {
    /// Informational messages.
    Info,
    /// Warning messages for potential issues.
    Warning,
    /// Error messages for failures.
    Error,
    /// Critical errors requiring immediate attention.
    Critical,
}

impl fmt::Display for ErrorSeverity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ErrorSeverity::Info => write!(f, "INFO"),
            ErrorSeverity::Warning => write!(f, "WARNING"),
            ErrorSeverity::Error => write!(f, "ERROR"),
            ErrorSeverity::Critical => write!(f, "CRITICAL"),
        }
    }
}

// Conversions from common error types

impl From<std::io::Error> for KafkaError {
    fn from(err: std::io::Error) -> Self {
        KafkaError::IoError {
            message: err.to_string(),
            source: Some(err),
        }
    }
}

impl From<serde_json::Error> for KafkaError {
    fn from(err: serde_json::Error) -> Self {
        KafkaError::Serialization {
            message: err.to_string(),
            context: Some("JSON serialization".to_string()),
        }
    }
}

impl From<bincode::Error> for KafkaError {
    fn from(err: bincode::Error) -> Self {
        KafkaError::Serialization {
            message: err.to_string(),
            context: Some("Bincode serialization".to_string()),
        }
    }
}

// Conversion from rdkafka errors
impl From<rdkafka::error::KafkaError> for KafkaError {
    fn from(err: rdkafka::error::KafkaError) -> Self {
        use rdkafka::error::KafkaError as RdKafkaError;

        match err {
            // Connection-related errors
            RdKafkaError::ClientCreation(msg) => KafkaError::connection(msg, "unknown"),

            // Metadata errors
            RdKafkaError::MetadataFetch(err) => {
                KafkaError::topic_metadata(err.to_string(), "unknown")
            }

            // Subscription errors
            RdKafkaError::Subscription(msg) => {
                KafkaError::partition_assignment(msg, Vec::new())
            }

            // Group coordination errors
            RdKafkaError::GroupListFetch(err) => {
                KafkaError::group_coordination(err.to_string(), "unknown")
            }

            // Message production errors
            RdKafkaError::MessageProduction(code) => KafkaError::production(
                format!("Message production failed with code: {:?}", code),
                "unknown",
                None,
            ),

            // Message consumption errors
            RdKafkaError::MessageConsumption(code) => KafkaError::consumption(
                format!("Message consumption failed with code: {:?}", code),
                "unknown",
                None,
                None,
            ),

            // Offset store/commit errors
            RdKafkaError::StoreOffset(err) | RdKafkaError::OffsetFetch(err) => {
                KafkaError::offset_commit(err.to_string(), "unknown", -1, -1)
            }

            // Partition EOF (not really an error, but can be handled)
            RdKafkaError::PartitionEOF(partition) => KafkaError::consumption(
                format!("Reached end of partition {}", partition),
                "unknown",
                Some(partition),
                None,
            ),

            // Generic error
            _ => KafkaError::kafka_client(err.to_string(), None),
        }
    }
}

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

    #[test]
    fn test_error_creation() {
        let err = KafkaError::connection("Connection refused", "localhost:9092");
        assert!(matches!(err, KafkaError::Connection { .. }));
    }

    #[test]
    fn test_error_is_retryable() {
        let timeout_err = KafkaError::timeout("poll", 1000);
        assert!(timeout_err.is_retryable());

        let auth_err = KafkaError::auth_error("Invalid credentials", "SASL_SSL");
        assert!(!auth_err.is_retryable());
    }

    #[test]
    fn test_error_is_fatal() {
        let auth_err = KafkaError::auth_error("Invalid credentials", "SASL_SSL");
        assert!(auth_err.is_fatal());

        let timeout_err = KafkaError::timeout("poll", 1000);
        assert!(!timeout_err.is_fatal());
    }

    #[test]
    fn test_error_severity() {
        let critical = KafkaError::invalid_config("Missing bootstrap servers", None);
        assert_eq!(critical.severity(), ErrorSeverity::Critical);

        let error = KafkaError::connection("Connection refused", "localhost:9092");
        assert_eq!(error.severity(), ErrorSeverity::Error);

        let warning = KafkaError::timeout("poll", 1000);
        assert_eq!(warning.severity(), ErrorSeverity::Warning);
    }

    #[test]
    fn test_error_display() {
        let err = KafkaError::connection("Connection refused", "localhost:9092");
        let display = format!("{}", err);
        assert!(display.contains("localhost:9092"));
        assert!(display.contains("Connection refused"));
    }

    #[test]
    fn test_io_error_conversion() {
        let io_err = std::io::Error::new(std::io::ErrorKind::ConnectionRefused, "test error");
        let kafka_err: KafkaError = io_err.into();
        assert!(matches!(kafka_err, KafkaError::IoError { .. }));
    }

    #[test]
    fn test_serde_error_conversion() {
        let json_err = serde_json::from_str::<serde_json::Value>("invalid json").unwrap_err();
        let kafka_err: KafkaError = json_err.into();
        assert!(matches!(kafka_err, KafkaError::Serialization { .. }));
    }
}