danube-connect-core 0.5.0

Core SDK for building Danube connectors
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
//! Configuration management for connectors.

use crate::route::{SinkRoute, SourceRoute};
use crate::{ConnectorError, ConnectorResult};
use danube_client::SubType;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::env;
use std::fs;
use std::path::{Path, PathBuf};

/// Main configuration for connectors
///
/// Can be created via:
/// - `ConnectorConfig::from_env()` - load from environment variables
/// - `ConnectorConfig::from_file()` - load from TOML file
/// - Direct construction in code for full programmatic control
///
/// # Structure
/// - **Mandatory fields**: `danube_service_url`, `connector_name`
/// - **Optional fields**: `retry`, `processing`, `schemas` (with defaults)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConnectorConfig {
    /// Danube broker service URL (mandatory, from DANUBE_SERVICE_URL env var)
    pub danube_service_url: String,

    /// Connector name (mandatory, from CONNECTOR_NAME env var, must be unique)
    pub connector_name: String,

    /// Retry settings (optional, from config file or defaults)
    #[serde(default)]
    pub retry: RetrySettings,

    /// Processing and runtime settings (optional, from config file or defaults)
    #[serde(default)]
    pub processing: ProcessingSettings,

    /// Schema mappings for topics (optional, for source connectors using schema registry)
    #[serde(default)]
    pub schemas: Vec<SchemaMapping>,
}

/// Trait for applying environment-variable overrides after deserialization.
///
/// Implementors can override this trait to apply environment-specific settings
/// to their configuration values.
pub trait ConfigEnvOverrides {
    /// Apply environment-specific overrides to a deserialized configuration value.
    fn apply_env_overrides(&mut self) -> ConnectorResult<()> {
        Ok(())
    }
}

/// Trait for validating configuration values after loading.
///
/// Implementors can override this trait to validate their configuration values
/// and return an error for invalid settings.
pub trait ConfigValidate {
    /// Validate the loaded configuration and return an error for invalid settings.
    fn validate_config(&self) -> ConnectorResult<()> {
        Ok(())
    }
}

/// Helper for loading connector configuration from TOML files and environment variables.
///
/// This loader provides a flexible way to load configuration values from various sources.
#[derive(Debug, Clone)]
pub struct ConnectorConfigLoader {
    config_path_env: String,
}

impl Default for ConnectorConfigLoader {
    fn default() -> Self {
        Self {
            config_path_env: "CONNECTOR_CONFIG_PATH".to_string(),
        }
    }
}

impl ConnectorConfigLoader {
    /// Create a loader that reads the configuration path from `CONNECTOR_CONFIG_PATH`.
    pub fn new() -> Self {
        Self::default()
    }

    /// Override the environment variable used to locate the configuration file.
    pub fn with_path_env(mut self, env_var: impl Into<String>) -> Self {
        self.config_path_env = env_var.into();
        self
    }

    /// Load a configuration value from the configured path environment variable.
    pub fn load<T>(&self) -> ConnectorResult<T>
    where
        T: DeserializeOwned + ConfigEnvOverrides + ConfigValidate,
    {
        let config_path = env::var(&self.config_path_env).map_err(|_| {
            ConnectorError::config(format!(
                "{} environment variable must be set to the path of the TOML configuration file",
                self.config_path_env
            ))
        })?;

        self.from_file(config_path)
    }

    /// Load a configuration value from a TOML file path.
    pub fn from_file<T>(&self, path: impl AsRef<Path>) -> ConnectorResult<T>
    where
        T: DeserializeOwned + ConfigEnvOverrides + ConfigValidate,
    {
        let path = path.as_ref();
        let content = fs::read_to_string(path).map_err(|e| {
            ConnectorError::config(format!(
                "Failed to read config file {}: {}",
                path.display(),
                e
            ))
        })?;

        let source_name = path.display().to_string();
        self.parse_str(&content, &source_name)
    }

    /// Parse a configuration value directly from TOML content.
    pub fn parse_str<T>(&self, content: &str, source_name: &str) -> ConnectorResult<T>
    where
        T: DeserializeOwned + ConfigEnvOverrides + ConfigValidate,
    {
        let mut config: T = toml::from_str(content).map_err(|e| {
            ConnectorError::config(format!(
                "Failed to parse config file {}: {}",
                source_name, e
            ))
        })?;

        config.apply_env_overrides()?;
        config.validate_config()?;
        Ok(config)
    }
}

impl ConnectorConfig {
    /// Load `ConnectorConfig` using `ConnectorConfigLoader` defaults.
    pub fn load() -> ConnectorResult<Self> {
        ConnectorConfigLoader::new().load()
    }

    /// Load mandatory configuration from environment variables
    ///
    /// Only reads mandatory fields:
    /// - `DANUBE_SERVICE_URL`: Danube broker URL (required)
    /// - `CONNECTOR_NAME`: Unique connector name (required)
    ///
    /// All retry and processing settings use defaults.
    /// To customize these, load from a config file or set them explicitly.
    pub fn from_env() -> ConnectorResult<Self> {
        let danube_service_url = env::var("DANUBE_SERVICE_URL")
            .map_err(|_| ConnectorError::config("DANUBE_SERVICE_URL is required"))?;

        let connector_name = env::var("CONNECTOR_NAME")
            .map_err(|_| ConnectorError::config("CONNECTOR_NAME is required"))?;

        Ok(Self {
            danube_service_url,
            connector_name,
            retry: RetrySettings::default(),
            processing: ProcessingSettings::default(),
            schemas: Vec::new(),
        })
    }

    /// Load configuration from a TOML file
    pub fn from_file(path: &str) -> ConnectorResult<Self> {
        let content = std::fs::read_to_string(path).map_err(|e| {
            ConnectorError::config(format!("Failed to read config file {}: {}", path, e))
        })?;

        toml::from_str(&content).map_err(|e| {
            ConnectorError::config(format!("Failed to parse config file {}: {}", path, e))
        })
    }

    /// Apply environment variable overrides to mandatory fields only
    ///
    /// This only overrides `danube_service_url` and `connector_name`.
    /// Retry and processing settings should come from config files, not env vars.
    pub fn apply_env_overrides(&mut self) {
        if let Ok(val) = env::var("DANUBE_SERVICE_URL") {
            self.danube_service_url = val;
        }
        if let Ok(val) = env::var("CONNECTOR_NAME") {
            self.connector_name = val;
        }
    }

    /// Validate the configuration
    ///
    /// Called internally by the runtime. Users can also call this for early validation.
    pub fn validate(&self) -> ConnectorResult<()> {
        if self.danube_service_url.is_empty() {
            return Err(ConnectorError::config("danube_service_url cannot be empty"));
        }

        if self.connector_name.is_empty() {
            return Err(ConnectorError::config("connector_name cannot be empty"));
        }

        if self.retry.max_retries > 100 {
            return Err(ConnectorError::config("max_retries too high (max 100)"));
        }

        if self.processing.batch_size == 0 {
            return Err(ConnectorError::config("batch_size must be > 0"));
        }

        Ok(())
    }
}

impl ConfigEnvOverrides for ConnectorConfig {
    fn apply_env_overrides(&mut self) -> ConnectorResult<()> {
        ConnectorConfig::apply_env_overrides(self);
        Ok(())
    }
}

impl ConfigValidate for ConnectorConfig {
    fn validate_config(&self) -> ConnectorResult<()> {
        self.validate()
    }
}

impl Default for ConnectorConfig {
    fn default() -> Self {
        Self {
            danube_service_url: "http://localhost:6650".to_string(),
            connector_name: "default-connector".to_string(),
            retry: RetrySettings::default(),
            processing: ProcessingSettings::default(),
            schemas: Vec::new(),
        }
    }
}

/// Retry configuration settings
///
/// Can be used via:
/// - TOML config file: `[retry]` section
/// - Direct construction in code for programmatic control
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RetrySettings {
    /// Maximum number of retries for failed operations
    #[serde(default = "default_max_retries")]
    pub max_retries: u32,

    /// Base backoff duration in milliseconds
    #[serde(default = "default_retry_backoff_ms")]
    pub retry_backoff_ms: u64,

    /// Maximum backoff duration in milliseconds
    #[serde(default = "default_max_backoff_ms")]
    pub max_backoff_ms: u64,
}

fn default_max_retries() -> u32 {
    3
}

fn default_retry_backoff_ms() -> u64 {
    1000
}

fn default_max_backoff_ms() -> u64 {
    30000
}

impl Default for RetrySettings {
    fn default() -> Self {
        Self {
            max_retries: 3,
            retry_backoff_ms: 1000,
            max_backoff_ms: 30000,
        }
    }
}

/// Processing and runtime configuration settings
///
/// Can be used via:
/// - TOML config file: `[processing]` section
/// - Direct construction in code for programmatic control
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProcessingSettings {
    /// Batch size for batch processing
    #[serde(default = "default_batch_size")]
    pub batch_size: usize,

    /// Batch timeout in milliseconds
    #[serde(default = "default_batch_timeout_ms")]
    pub batch_timeout_ms: u64,

    /// Poll interval in milliseconds for source connectors
    #[serde(default = "default_poll_interval_ms")]
    pub poll_interval_ms: u64,

    /// Metrics export port
    #[serde(default = "default_metrics_port")]
    pub metrics_port: u16,

    /// Log level
    #[serde(default = "default_log_level")]
    pub log_level: String,

    /// Interval between health checks, in milliseconds.
    #[serde(default = "default_health_check_interval_ms")]
    pub health_check_interval_ms: u64,

    /// Number of consecutive failed health checks before reporting unhealthy status.
    #[serde(default = "default_health_check_failure_threshold")]
    pub health_check_failure_threshold: usize,
}

fn default_batch_size() -> usize {
    1000
}

fn default_batch_timeout_ms() -> u64 {
    1000
}

fn default_poll_interval_ms() -> u64 {
    100
}

fn default_metrics_port() -> u16 {
    9090
}

fn default_log_level() -> String {
    "info".to_string()
}

fn default_health_check_interval_ms() -> u64 {
    30000
}

fn default_health_check_failure_threshold() -> usize {
    3
}

impl Default for ProcessingSettings {
    fn default() -> Self {
        Self {
            batch_size: 1000,
            batch_timeout_ms: 1000,
            poll_interval_ms: 100,
            metrics_port: 9090,
            log_level: "info".to_string(),
            health_check_interval_ms: 30000,
            health_check_failure_threshold: 3,
        }
    }
}

/// Schema mapping configuration for topics
///
/// Maps a topic to its schema definition for source connectors using schema registry.
///
/// Can be used via:
/// - TOML config file: `[[schemas]]` array
/// - Direct construction in code for programmatic control
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SchemaMapping {
    /// Danube topic name (format: /{namespace}/{topic_name})
    pub topic: String,

    /// Schema subject name in the registry
    pub subject: String,

    /// Schema type (e.g., "json_schema", "avro", "protobuf")
    pub schema_type: String,

    /// Path to schema definition file
    pub schema_file: PathBuf,

    /// Auto-register schema on startup if it doesn't exist
    #[serde(default = "default_auto_register")]
    pub auto_register: bool,

    /// Version strategy for this schema
    #[serde(default)]
    pub version_strategy: VersionStrategy,
}

fn default_auto_register() -> bool {
    true
}

/// Subscription type for configuration
///
/// **Mandatory public API** - required for `ConsumerConfig`.
///
/// Mirrors `SubType` from danube-client but with Serialize/Deserialize for config files.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SubscriptionType {
    /// Only one consumer on the subscription receives messages.
    Exclusive,
    /// Multiple consumers can share message delivery.
    Shared,
    /// A standby consumer takes over when the active consumer fails.
    FailOver,
}

impl From<SubscriptionType> for SubType {
    fn from(st: SubscriptionType) -> Self {
        match st {
            SubscriptionType::Exclusive => SubType::Exclusive,
            SubscriptionType::Shared => SubType::Shared,
            SubscriptionType::FailOver => SubType::FailOver,
        }
    }
}

/// Configuration for a Danube consumer
///
/// **Mandatory public API** - required by `SinkConnector::consumer_configs()` trait.
///
/// Specifies how to create a consumer for a specific topic, including subscription settings.
#[derive(Debug, Clone)]
pub struct ConsumerConfig {
    /// Danube topic to consume from (format: /{namespace}/{topic_name})
    pub topic: String,
    /// Consumer name (for identification)
    pub consumer_name: String,
    /// Subscription name (shared across consumer instances)
    pub subscription: String,
    /// Subscription type (Exclusive, Shared, FailOver)
    pub subscription_type: SubscriptionType,
    /// Optional: Expected schema subject for validation
    /// If set, runtime will validate that incoming messages match this schema
    pub expected_schema_subject: Option<String>,
}

impl ConsumerConfig {
    /// Convert this consumer configuration into the route representation used by the runtime.
    pub fn route(&self) -> SinkRoute {
        self.clone().into()
    }

    /// Build a consumer configuration from a sink route definition.
    pub fn from_route(route: SinkRoute) -> Self {
        route.into()
    }
}

/// Configuration for a Danube producer
///
/// **Mandatory public API** - required by `SourceConnector::producer_configs()` trait.
///
/// Specifies how to create a producer for a specific topic, including partitioning
/// and reliability settings.
///
/// Note: The `schema_config` field is internal and populated by the runtime from `SchemaMapping`.
/// Always use `ProducerConfig::new()` or set `schema_config` to `None` when constructing manually.
#[derive(Debug, Clone)]
pub struct ProducerConfig {
    /// Danube topic name (format: /{namespace}/{topic_name})
    pub topic: String,
    /// Number of partitions (0 = non-partitioned)
    pub partitions: usize,
    /// Use reliable dispatch (WAL + Cloud persistence)
    pub reliable_dispatch: bool,
    /// Internal: Schema configuration (populated by runtime from SchemaMapping)
    /// Users should not set this - always use None
    pub schema_config: Option<SchemaConfig>,
}

impl ProducerConfig {
    /// Create a new ProducerConfig
    pub fn new(topic: impl Into<String>, partitions: usize, reliable_dispatch: bool) -> Self {
        Self {
            topic: topic.into(),
            partitions,
            reliable_dispatch,
            schema_config: None,
        }
    }

    /// Convert this producer configuration into the route representation used by the runtime.
    pub fn route(&self) -> SourceRoute {
        self.clone().into()
    }

    /// Build a producer configuration from a source route definition.
    pub fn from_route(route: SourceRoute) -> Self {
        route.into()
    }
}

/// Schema configuration for a topic
///
/// Can be used via:
/// - TOML config file: Use `SchemaMapping` in `[[schemas]]` (recommended)
/// - Direct construction in code for advanced programmatic control
///
/// Note: Usually populated automatically from `SchemaMapping` by the runtime.
///
/// The runtime converts `SchemaMapping` → `SchemaConfig` automatically when loading from files.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SchemaConfig {
    /// Schema subject name in the registry
    pub subject: String,

    /// Schema type (JsonSchema, Avro, Protobuf, etc.)
    pub schema_type: String,

    /// Path to schema definition file
    pub schema_file: PathBuf,

    /// Auto-register schema on startup if it doesn't exist
    #[serde(default = "default_schema_auto_register")]
    pub auto_register: bool,

    /// Version strategy for producers
    #[serde(default)]
    pub version_strategy: VersionStrategy,
}

fn default_schema_auto_register() -> bool {
    true
}

/// Strategy for selecting schema version
///
/// Used in `SchemaMapping` and `SchemaConfig` to control which schema version producers use.
///
/// Can be used via:
/// - TOML config file: `version_strategy` field in `[[schemas]]`
/// - Direct construction in code for programmatic control
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum VersionStrategy {
    /// Use the latest schema version (default)
    #[default]
    Latest,

    /// Pin to a specific schema version
    Pinned(u32),

    /// Use minimum version or newer
    Minimum(u32),
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::NamedTempFile;

    #[test]
    fn test_config_default() {
        let config = ConnectorConfig::default();
        assert_eq!(config.danube_service_url, "http://localhost:6650");
        assert_eq!(config.connector_name, "default-connector");
        assert_eq!(config.retry.max_retries, 3);
        assert_eq!(config.processing.batch_size, 1000);
        assert_eq!(config.processing.health_check_interval_ms, 30000);
        assert_eq!(config.processing.health_check_failure_threshold, 3);
    }

    #[test]
    fn test_config_validation() {
        let mut config = ConnectorConfig::default();
        assert!(config.validate().is_ok());

        config.danube_service_url = "".to_string();
        assert!(config.validate().is_err());

        config.danube_service_url = "http://localhost:6650".to_string();
        config.processing.batch_size = 0;
        assert!(config.validate().is_err());
    }

    #[derive(Debug, Deserialize)]
    struct LoaderTestConfig {
        value: String,
    }

    impl ConfigValidate for LoaderTestConfig {
        fn validate_config(&self) -> ConnectorResult<()> {
            if self.value.is_empty() {
                return Err(ConnectorError::config("value cannot be empty"));
            }

            Ok(())
        }
    }

    impl ConfigEnvOverrides for LoaderTestConfig {}

    #[test]
    fn test_config_loader_from_file() {
        let mut file = NamedTempFile::new().unwrap();
        writeln!(file, "value = \"loaded\"").unwrap();

        let config: LoaderTestConfig = ConnectorConfigLoader::new().from_file(file.path()).unwrap();

        assert_eq!(config.value, "loaded");
    }
}