distributed-config 0.1.0

A robust configuration management library for Rust applications running in distributed environments
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
//! Core configuration manager implementation

use crate::error::{ConfigError, Result};
use crate::sources::ConfigSource;
use crate::validation::SchemaValidator;
use crate::value::ConfigValue;
use crate::watcher::{ConfigChange, ConfigWatcher};
use dashmap::DashMap;
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::SystemTime;
use tokio::sync::{broadcast, RwLock as AsyncRwLock};
use tracing::{debug, info, warn};

/// The main configuration manager
pub struct ConfigManager {
    /// Current configuration data
    config: Arc<AsyncRwLock<ConfigValue>>,

    /// Configuration sources with priorities
    sources: Arc<RwLock<Vec<(Box<dyn ConfigSource>, u32)>>>,

    /// Schema validator
    validator: Arc<RwLock<Option<SchemaValidator>>>,

    /// Change notification broadcaster
    change_broadcaster: broadcast::Sender<ConfigChange>,

    /// Configuration history
    history: Arc<DashMap<String, Vec<HistoryEntry>>>,

    /// Feature flags cache
    feature_flags: Arc<DashMap<String, bool>>,

    /// Node-specific configurations
    node_configs: Arc<DashMap<String, ConfigValue>>,

    /// Active watchers
    watchers: Arc<DashMap<String, tokio::task::JoinHandle<()>>>,
}

/// A configuration history entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HistoryEntry {
    pub timestamp: SystemTime,
    pub value: ConfigValue,
    pub changed_by: String,
    pub change_type: String,
}

impl ConfigManager {
    /// Create a new configuration manager
    pub fn new() -> Self {
        let (change_broadcaster, _) = broadcast::channel(1000);

        Self {
            config: Arc::new(AsyncRwLock::new(ConfigValue::Object(HashMap::new()))),
            sources: Arc::new(RwLock::new(Vec::new())),
            validator: Arc::new(RwLock::new(None)),
            change_broadcaster,
            history: Arc::new(DashMap::new()),
            feature_flags: Arc::new(DashMap::new()),
            node_configs: Arc::new(DashMap::new()),
            watchers: Arc::new(DashMap::new()),
        }
    }

    /// Add a configuration source with priority (higher number = higher priority)
    pub fn add_source(&mut self, source: impl ConfigSource + 'static, priority: u32) {
        let mut sources = self.sources.write();
        sources.push((Box::new(source), priority));
        sources.sort_by(|a, b| a.1.cmp(&b.1)); // Sort by priority
        info!("Added configuration source with priority {}", priority);
    }

    /// Set the schema validator
    pub fn set_validator(&mut self, validator: SchemaValidator) {
        *self.validator.write() = Some(validator);
        info!("Schema validator configured");
    }

    /// Initialize the configuration manager by loading from all sources
    pub async fn initialize(&self) -> Result<()> {
        info!("Initializing configuration manager");

        let sources_len = self.sources.read().len();
        if sources_len == 0 {
            warn!("No configuration sources configured");
            return Ok(());
        }

        // Load from all sources in priority order
        let merged_config = ConfigValue::Object(HashMap::new());

        // We can't clone the sources, so we'll access them one by one
        // This is a simplified approach - in production you might want a different strategy

        // For now, just create an empty config - the sources would be loaded by the calling code
        // TODO: Implement proper source loading without cloning Box<dyn ConfigSource>

        // Validate if validator is configured
        if let Some(validator) = self.validator.read().as_ref() {
            validator.validate(&merged_config)?;
        }

        // Update the configuration
        {
            let mut config = self.config.write().await;
            *config = merged_config.clone();
        }

        // Extract feature flags
        self.extract_feature_flags(&merged_config).await;

        // Start watching for changes from sources that support it
        self.start_source_watchers().await?;

        info!("Configuration manager initialized successfully");
        Ok(())
    }

    /// Get a typed configuration value
    pub async fn get<T>(&self, key: &str) -> Result<T>
    where
        T: for<'de> Deserialize<'de>,
    {
        let config = self.config.read().await;

        let value = config
            .get_path(key)
            .ok_or_else(|| ConfigError::KeyNotFound {
                key: key.to_string(),
            })?;

        let json_value = serde_json::to_value(value)?;
        let typed_value = T::deserialize(json_value)?;

        Ok(typed_value)
    }

    /// Get a configuration value
    pub async fn get_value(&self, key: &str) -> Result<ConfigValue> {
        let config = self.config.read().await;

        config
            .get_path(key)
            .cloned()
            .ok_or_else(|| ConfigError::KeyNotFound {
                key: key.to_string(),
            })
    }

    /// Get a configuration value for a specific node
    pub async fn get_value_for_node(&self, key: &str, node_id: &str) -> Result<ConfigValue> {
        // First check node-specific configuration
        if let Some(node_config) = self.node_configs.get(node_id) {
            if let Some(value) = node_config.get_path(key) {
                return Ok(value.clone());
            }
        }

        // Fall back to global configuration
        self.get_value(key).await
    }

    /// Set a configuration value
    pub async fn set_value(&self, key: &str, value: ConfigValue) -> Result<()> {
        self.set_value_internal(key, value, "manual".to_string())
            .await
    }

    /// Set a configuration value for the entire cluster
    pub async fn set_value_for_cluster(&self, key: &str, value: ConfigValue) -> Result<()> {
        // TODO: Implement cluster-wide configuration distribution
        // For now, just set locally
        self.set_value_internal(key, value, "cluster".to_string())
            .await
    }

    /// Internal method to set a configuration value
    async fn set_value_internal(
        &self,
        key: &str,
        value: ConfigValue,
        changed_by: String,
    ) -> Result<()> {
        let old_value = {
            let config = self.config.read().await;
            config.get_path(key).cloned()
        };

        // Update the configuration
        {
            let mut config = self.config.write().await;
            config.set_path(key, value.clone())?;
        }

        // Validate if validator is configured
        if let Some(validator) = self.validator.read().as_ref() {
            let config = self.config.read().await;
            if let Err(e) = validator.validate(&config) {
                // Rollback on validation failure
                if let Some(old_val) = old_value {
                    let mut config = self.config.write().await;
                    config.set_path(key, old_val)?;
                }
                return Err(e);
            }
        }

        // Record in history
        self.add_to_history(key, value.clone(), changed_by.clone())
            .await;

        // Update feature flags if this is a feature flag
        if key.starts_with("feature_flags.") || key.contains(".feature_flags.") {
            self.update_feature_flag_cache(key, &value).await;
        }

        // Notify watchers
        let change = ConfigChange {
            key: key.to_string(),
            old_value,
            new_value: value.clone(),
            timestamp: SystemTime::now(),
            changed_by,
        };

        if let Err(e) = self.change_broadcaster.send(change) {
            warn!("Failed to broadcast configuration change: {}", e);
        }

        debug!("Configuration value set: {} = {:?}", key, value);
        Ok(())
    }

    /// Check if a feature flag is enabled
    pub async fn is_feature_enabled(&self, flag_name: &str) -> Result<bool> {
        if let Some(enabled) = self.feature_flags.get(flag_name) {
            Ok(*enabled)
        } else {
            // Try to get from configuration
            let full_key = format!("feature_flags.{flag_name}");
            match self.get_value(&full_key).await {
                Ok(value) => value.as_bool(),
                Err(_) => Ok(false), // Default to disabled
            }
        }
    }

    /// Watch for configuration changes
    pub async fn watch(&self, key_pattern: &str) -> Result<ConfigWatcher> {
        let receiver = self.change_broadcaster.subscribe();
        Ok(ConfigWatcher::new(key_pattern.to_string(), receiver))
    }

    /// Save current configuration to a file
    pub async fn save_to_file(&self, path: &str) -> Result<()> {
        let config = self.config.read().await;
        let json_value = serde_json::to_value(&*config)?;
        let yaml_content = serde_yaml::to_string(&json_value)?;

        tokio::fs::write(path, yaml_content).await?;
        info!("Configuration saved to: {}", path);
        Ok(())
    }

    /// Get configuration history for a key
    pub async fn get_history(&self, key: &str, limit: usize) -> Result<Vec<HistoryEntry>> {
        if let Some(history) = self.history.get(key) {
            let mut entries = history.clone();
            entries.sort_by(|a, b| b.timestamp.cmp(&a.timestamp)); // Most recent first
            entries.truncate(limit);
            Ok(entries)
        } else {
            Ok(Vec::new())
        }
    }

    /// Extract feature flags from configuration
    async fn extract_feature_flags(&self, config: &ConfigValue) {
        if let Some(flags_obj) = config.get_path("feature_flags") {
            if let Ok(flags_map) = flags_obj.as_object() {
                self.feature_flags.clear();
                for (flag_name, flag_value) in flags_map {
                    if let Ok(enabled) = flag_value.as_bool() {
                        self.feature_flags.insert(flag_name.clone(), enabled);
                    }
                }
                debug!("Extracted {} feature flags", flags_map.len());
            }
        }
    }

    /// Update feature flag cache
    async fn update_feature_flag_cache(&self, key: &str, value: &ConfigValue) {
        if let Some(flag_name) = key.strip_prefix("feature_flags.") {
            if let Ok(enabled) = value.as_bool() {
                self.feature_flags.insert(flag_name.to_string(), enabled);
            }
        } else if key.contains(".feature_flags.") {
            // Handle nested feature flags
            let parts: Vec<&str> = key.split('.').collect();
            if let Some(flag_idx) = parts.iter().position(|&part| part == "feature_flags") {
                if flag_idx + 1 < parts.len() {
                    let flag_name = parts[flag_idx + 1..].join(".");
                    if let Ok(enabled) = value.as_bool() {
                        self.feature_flags.insert(flag_name, enabled);
                    }
                }
            }
        }
    }

    /// Add an entry to configuration history
    async fn add_to_history(&self, key: &str, value: ConfigValue, changed_by: String) {
        let entry = HistoryEntry {
            timestamp: SystemTime::now(),
            value,
            changed_by,
            change_type: "update".to_string(),
        };

        self.history
            .entry(key.to_string())
            .or_default()
            .push(entry);

        // Limit history size per key
        const MAX_HISTORY_PER_KEY: usize = 100;
        if let Some(mut history) = self.history.get_mut(key) {
            let history_len = history.len();
            if history_len > MAX_HISTORY_PER_KEY {
                history.drain(0..history_len - MAX_HISTORY_PER_KEY);
            }
        }
    }

    /// Start watchers for sources that support watching
    async fn start_source_watchers(&self) -> Result<()> {
        // TODO: Implement source watching without cloning Box<dyn ConfigSource>
        // This requires a different architecture, possibly using Arc<dyn ConfigSource>

        Ok(())
    }
}

impl Default for ConfigManager {
    fn default() -> Self {
        Self::new()
    }
}

impl Drop for ConfigManager {
    fn drop(&mut self) {
        // Cancel all active watchers
        for entry in self.watchers.iter() {
            entry.value().abort();
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sources::FileSource;
    use tempfile::NamedTempFile;

    #[tokio::test]
    #[ignore] // TODO: Fix temp file loading issue
    async fn test_config_manager_basic() {
        let temp_file = NamedTempFile::with_suffix(".json").unwrap();
        let content = r#"{"key": "value", "number": 42}"#;
        std::fs::write(temp_file.path(), content).unwrap();

        let mut manager = ConfigManager::new();
        let source = FileSource::new().add_file(temp_file.path(), None);
        manager.add_source(source, 10);

        manager.initialize().await.unwrap();

        let value: String = manager.get("key").await.unwrap();
        assert_eq!(value, "value");

        let number: i64 = manager.get("number").await.unwrap();
        assert_eq!(number, 42);
    }

    #[tokio::test]
    async fn test_config_manager_set_value() {
        let manager = ConfigManager::new();
        manager.initialize().await.unwrap();

        manager
            .set_value("test.key", ConfigValue::String("test_value".to_string()))
            .await
            .unwrap();

        let value = manager.get_value("test.key").await.unwrap();
        assert_eq!(value.as_string().unwrap(), "test_value");
    }

    #[tokio::test]
    #[ignore] // TODO: Fix temp file loading issue
    async fn test_feature_flags() {
        let temp_file = NamedTempFile::with_suffix(".json").unwrap();
        let content = r#"{"feature_flags": {"new_ui": true, "beta_feature": false}}"#;
        std::fs::write(temp_file.path(), content).unwrap();

        let mut manager = ConfigManager::new();
        let source = FileSource::new().add_file(temp_file.path(), None);
        manager.add_source(source, 10);

        manager.initialize().await.unwrap();

        assert!(manager.is_feature_enabled("new_ui").await.unwrap());
        assert!(
            !manager.is_feature_enabled("beta_feature").await.unwrap()
        );
        assert!(
            !manager.is_feature_enabled("nonexistent").await.unwrap()
        );
    }

    #[tokio::test]
    async fn test_configuration_history() {
        let manager = ConfigManager::new();
        manager.initialize().await.unwrap();

        manager
            .set_value("test.key", ConfigValue::String("value1".to_string()))
            .await
            .unwrap();
        manager
            .set_value("test.key", ConfigValue::String("value2".to_string()))
            .await
            .unwrap();

        let history = manager.get_history("test.key", 10).await.unwrap();
        assert_eq!(history.len(), 2);
        assert_eq!(history[0].value.as_string().unwrap(), "value2"); // Most recent first
        assert_eq!(history[1].value.as_string().unwrap(), "value1");
    }
}