scirs2_metrics/optimization/distributed/config/
mod.rs

1//! Configuration structures for advanced distributed systems
2//!
3//! This module provides comprehensive configuration options for:
4//! - Advanced cluster configuration
5//! - Consensus algorithm settings
6//! - Data sharding strategies
7//! - Fault tolerance parameters
8//! - Auto-scaling policies
9//! - Locality optimization
10//! - Performance optimization
11
12use serde::{Deserialize, Serialize};
13use std::time::Duration;
14
15/// Advanced distributed cluster configuration
16#[derive(Debug, Clone, Serialize, Deserialize, Default)]
17pub struct AdvancedClusterConfig {
18    /// Basic cluster settings
19    pub basic_config: super::super::distributed::DistributedConfig,
20    /// Consensus algorithm configuration
21    pub consensus_config: ConsensusConfig,
22    /// Data sharding strategy
23    pub sharding_config: ShardingConfig,
24    /// Fault tolerance settings
25    pub fault_tolerance: FaultToleranceConfig,
26    /// Auto-scaling configuration
27    pub auto_scaling: AutoScalingConfig,
28    /// Data locality optimization
29    pub locality_config: LocalityConfig,
30    /// Performance optimization settings
31    pub optimization_config: OptimizationConfig,
32}
33
34/// Consensus algorithm configuration
35#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct ConsensusConfig {
37    /// Algorithm to use
38    pub algorithm: ConsensusAlgorithm,
39    /// Minimum number of nodes for quorum
40    pub quorum_size: usize,
41    /// Election timeout (milliseconds)
42    pub election_timeout_ms: u64,
43    /// Heartbeat interval (milliseconds)
44    pub heartbeat_interval_ms: u64,
45    /// Maximum entries per append
46    pub max_entries_per_append: usize,
47    /// Log compaction threshold
48    pub log_compaction_threshold: usize,
49    /// Snapshot creation interval
50    pub snapshot_interval: Duration,
51    /// Node identifier (optional)
52    pub node_id: Option<String>,
53    /// List of peer nodes (optional)
54    pub peers: Option<Vec<String>>,
55}
56
57impl Default for ConsensusConfig {
58    fn default() -> Self {
59        Self {
60            algorithm: ConsensusAlgorithm::Raft,
61            quorum_size: 3,
62            election_timeout_ms: 5000,
63            heartbeat_interval_ms: 1000,
64            max_entries_per_append: 100,
65            log_compaction_threshold: 10000,
66            snapshot_interval: Duration::from_secs(3600),
67            node_id: None,
68            peers: None,
69        }
70    }
71}
72
73/// Consensus algorithms supported
74#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
75pub enum ConsensusAlgorithm {
76    /// Raft consensus algorithm
77    Raft,
78    /// Practical Byzantine Fault Tolerance
79    Pbft,
80    /// Proof of Stake consensus
81    ProofOfStake,
82    /// Delegated Proof of Stake
83    DelegatedProofOfStake,
84    /// Simple majority voting
85    SimpleMajority,
86    /// No consensus (for testing)
87    None,
88}
89
90/// Data sharding configuration
91#[derive(Debug, Clone, Serialize, Deserialize)]
92pub struct ShardingConfig {
93    /// Sharding strategy
94    pub strategy: ShardingStrategy,
95    /// Number of shards
96    pub shard_count: usize,
97    /// Replication factor per shard
98    pub replication_factor: usize,
99    /// Hash function for consistent hashing
100    pub hash_function: HashFunction,
101    /// Virtual nodes per physical node
102    pub virtual_nodes: usize,
103    /// Enable dynamic resharding
104    pub dynamic_resharding: bool,
105    /// Shard migration threshold
106    pub migration_threshold: f64,
107}
108
109impl Default for ShardingConfig {
110    fn default() -> Self {
111        Self {
112            strategy: ShardingStrategy::ConsistentHash,
113            shard_count: 16,
114            replication_factor: 3,
115            hash_function: HashFunction::Murmur3,
116            virtual_nodes: 256,
117            dynamic_resharding: true,
118            migration_threshold: 0.8,
119        }
120    }
121}
122
123/// Data sharding strategies
124#[derive(Debug, Clone, Serialize, Deserialize)]
125pub enum ShardingStrategy {
126    /// Hash-based sharding
127    Hash,
128    /// Range-based sharding
129    Range,
130    /// Directory-based sharding
131    Directory,
132    /// Consistent hashing
133    ConsistentHash,
134    /// Geographic sharding
135    Geographic,
136    /// Feature-based sharding
137    FeatureBased,
138    /// Custom sharding logic
139    Custom(String),
140}
141
142/// Hash functions for sharding
143#[derive(Debug, Clone, Serialize, Deserialize)]
144pub enum HashFunction {
145    /// MD5 hash
146    Md5,
147    /// SHA-256 hash
148    Sha256,
149    /// CRC32 hash
150    Crc32,
151    /// MurmurHash3
152    Murmur3,
153    /// xxHash
154    XxHash,
155    /// Custom hash function
156    Custom(String),
157}
158
159/// Fault tolerance configuration
160#[derive(Debug, Clone, Serialize, Deserialize)]
161pub struct FaultToleranceConfig {
162    /// Maximum node failures to tolerate
163    pub max_failures: usize,
164    /// Enable automatic recovery
165    pub auto_recovery: bool,
166    /// Recovery timeout (seconds)
167    pub recovery_timeout: u64,
168    /// Health check interval (seconds)
169    pub health_check_interval: u64,
170    /// Node replacement strategy
171    pub replacement_strategy: NodeReplacementStrategy,
172    /// Data backup interval
173    pub backup_interval: Duration,
174    /// Enable rolling updates
175    pub rolling_updates: bool,
176    /// Graceful shutdown timeout
177    pub graceful_shutdown_timeout: Duration,
178}
179
180impl Default for FaultToleranceConfig {
181    fn default() -> Self {
182        Self {
183            max_failures: 1,
184            auto_recovery: true,
185            recovery_timeout: 300,
186            health_check_interval: 30,
187            replacement_strategy: NodeReplacementStrategy::HotStandby,
188            backup_interval: Duration::from_secs(3600),
189            rolling_updates: true,
190            graceful_shutdown_timeout: Duration::from_secs(60),
191        }
192    }
193}
194
195/// Node replacement strategies
196#[derive(Debug, Clone, Serialize, Deserialize)]
197pub enum NodeReplacementStrategy {
198    /// Immediate replacement
199    Immediate,
200    /// Delayed replacement
201    Delayed { delay: Duration },
202    /// Manual replacement only
203    Manual,
204    /// Hot standby nodes
205    HotStandby,
206    /// Cold standby nodes
207    ColdStandby,
208}
209
210/// Auto-scaling configuration
211#[derive(Debug, Clone, Serialize, Deserialize)]
212pub struct AutoScalingConfig {
213    /// Enable auto-scaling
214    pub enabled: bool,
215    /// Minimum number of nodes
216    pub min_nodes: usize,
217    /// Maximum number of nodes
218    pub max_nodes: usize,
219    /// CPU threshold for scale-up
220    pub scale_up_cpu_threshold: f64,
221    /// CPU threshold for scale-down
222    pub scale_down_cpu_threshold: f64,
223    /// Memory threshold for scale-up
224    pub scale_up_memory_threshold: f64,
225    /// Memory threshold for scale-down
226    pub scale_down_memory_threshold: f64,
227    /// Cooldown period between scaling operations
228    pub cooldown_period: Duration,
229    /// Scaling policies
230    pub policies: Vec<ScalingPolicy>,
231}
232
233impl Default for AutoScalingConfig {
234    fn default() -> Self {
235        Self {
236            enabled: true,
237            min_nodes: 2,
238            max_nodes: 10,
239            scale_up_cpu_threshold: 80.0,
240            scale_down_cpu_threshold: 30.0,
241            scale_up_memory_threshold: 85.0,
242            scale_down_memory_threshold: 40.0,
243            cooldown_period: Duration::from_secs(300),
244            policies: vec![],
245        }
246    }
247}
248
249/// Scaling policy
250#[derive(Debug, Clone, Serialize, Deserialize)]
251pub struct ScalingPolicy {
252    /// Policy name
253    pub name: String,
254    /// Metric to monitor
255    pub metric: String,
256    /// Threshold value
257    pub threshold: f64,
258    /// Scaling action
259    pub action: ScalingAction,
260    /// Adjustment type
261    pub adjustment_type: AdjustmentType,
262    /// Adjustment value
263    pub adjustment_value: f64,
264    /// Cooldown period
265    pub cooldown: Duration,
266}
267
268/// Scaling actions
269#[derive(Debug, Clone, Serialize, Deserialize)]
270pub enum ScalingAction {
271    /// Scale up (add nodes)
272    ScaleUp,
273    /// Scale down (remove nodes)
274    ScaleDown,
275    /// No action
276    None,
277}
278
279/// Adjustment types for scaling
280#[derive(Debug, Clone, Serialize, Deserialize)]
281pub enum AdjustmentType {
282    /// Change by a specific count
283    ChangeInCapacity,
284    /// Change to exact capacity
285    ExactCapacity,
286    /// Change by percentage
287    PercentChangeInCapacity,
288}
289
290/// Data locality optimization configuration
291#[derive(Debug, Clone, Serialize, Deserialize)]
292pub struct LocalityConfig {
293    /// Enable locality optimization
294    pub enabled: bool,
295    /// Locality strategy
296    pub strategy: LocalityStrategy,
297    /// Network topology
298    pub topology: NetworkTopology,
299    /// Affinity rules
300    pub affinity_rules: Vec<AffinityRule>,
301    /// Cache configuration
302    pub cache_config: CacheConfig,
303}
304
305impl Default for LocalityConfig {
306    fn default() -> Self {
307        Self {
308            enabled: true,
309            strategy: LocalityStrategy::NetworkAware,
310            topology: NetworkTopology::default(),
311            affinity_rules: vec![],
312            cache_config: CacheConfig::default(),
313        }
314    }
315}
316
317/// Locality optimization strategies
318#[derive(Debug, Clone, Serialize, Deserialize)]
319pub enum LocalityStrategy {
320    /// Network-aware placement
321    NetworkAware,
322    /// Geographic placement
323    Geographic,
324    /// Latency-based placement
325    LatencyBased,
326    /// Bandwidth-aware placement
327    BandwidthAware,
328    /// Cost-optimized placement
329    CostOptimized,
330    /// Custom placement strategy
331    Custom(String),
332}
333
334/// Network topology representation
335#[derive(Debug, Clone, Serialize, Deserialize, Default)]
336pub struct NetworkTopology {
337    /// Topology levels (e.g., rack, datacenter, region)
338    pub levels: Vec<TopologyLevel>,
339    /// Node locations
340    pub node_locations: std::collections::HashMap<String, Location>,
341}
342
343/// Location in network topology
344#[derive(Debug, Clone, Serialize, Deserialize)]
345pub struct Location {
346    /// Location coordinates per topology level
347    pub coordinates: Vec<String>,
348    /// Geographic coordinates (latitude, longitude)
349    pub geo_coordinates: Option<(f64, f64)>,
350    /// Available bandwidth (Gbps)
351    pub bandwidth: f64,
352    /// Network latency characteristics
353    pub latency_ms: f64,
354}
355
356/// Topology level definition
357#[derive(Debug, Clone, Serialize, Deserialize)]
358pub struct TopologyLevel {
359    /// Level name (e.g., "rack", "datacenter")
360    pub name: String,
361    /// Level index (0 = lowest level)
362    pub index: usize,
363}
364
365/// Affinity rule for data/compute placement
366#[derive(Debug, Clone, Serialize, Deserialize)]
367pub struct AffinityRule {
368    /// Rule name
369    pub name: String,
370    /// Source entity pattern
371    pub source_pattern: String,
372    /// Target entity pattern
373    pub target_pattern: String,
374    /// Affinity type
375    pub affinity_type: AffinityType,
376    /// Rule weight (higher = more important)
377    pub weight: f64,
378}
379
380/// Affinity rule types
381#[derive(Debug, Clone, Serialize, Deserialize)]
382pub enum AffinityType {
383    /// Entities should be placed together
384    Attract,
385    /// Entities should be separated
386    Repel,
387    /// Entities should be in same topology level
388    SameLevel(usize),
389    /// Custom affinity logic
390    Custom(String),
391}
392
393/// Cache configuration for locality optimization
394#[derive(Debug, Clone, Serialize, Deserialize)]
395pub struct CacheConfig {
396    /// Enable distributed caching
397    pub enabled: bool,
398    /// Cache size per node (MB)
399    pub cache_size_mb: u64,
400    /// Cache eviction policy
401    pub eviction_policy: EvictionPolicy,
402    /// Cache coherence protocol
403    pub coherence_protocol: CoherenceProtocol,
404    /// Cache invalidation strategy
405    pub invalidation_strategy: InvalidationStrategy,
406}
407
408impl Default for CacheConfig {
409    fn default() -> Self {
410        Self {
411            enabled: true,
412            cache_size_mb: 1024,
413            eviction_policy: EvictionPolicy::Lru,
414            coherence_protocol: CoherenceProtocol::WriteInvalidate,
415            invalidation_strategy: InvalidationStrategy::LazyInvalidation,
416        }
417    }
418}
419
420/// Cache eviction policies
421#[derive(Debug, Clone, Serialize, Deserialize)]
422pub enum EvictionPolicy {
423    /// Least Recently Used
424    Lru,
425    /// Least Frequently Used
426    Lfu,
427    /// First In, First Out
428    Fifo,
429    /// Random eviction
430    Random,
431    /// Time-based eviction
432    Ttl,
433}
434
435/// Cache coherence protocols
436#[derive(Debug, Clone, Serialize, Deserialize)]
437pub enum CoherenceProtocol {
438    /// Write-invalidate protocol
439    WriteInvalidate,
440    /// Write-update protocol
441    WriteUpdate,
442    /// Directory-based coherence
443    DirectoryBased,
444    /// Snooping-based coherence
445    SnoopingBased,
446}
447
448/// Cache invalidation strategies
449#[derive(Debug, Clone, Serialize, Deserialize)]
450pub enum InvalidationStrategy {
451    /// Immediate invalidation
452    ImmediateInvalidation,
453    /// Lazy invalidation
454    LazyInvalidation,
455    /// Lease-based invalidation
456    LeaseBased,
457    /// Version-based invalidation
458    VersionBased,
459}
460
461/// Performance optimization configuration
462#[derive(Debug, Clone, Serialize, Deserialize)]
463pub struct OptimizationConfig {
464    /// Enable performance optimization
465    pub enabled: bool,
466    /// Batch optimization settings
467    pub batch_optimization: BatchOptimization,
468    /// Network optimization settings
469    pub network_optimization: NetworkOptimization,
470    /// Compute optimization settings
471    pub compute_optimization: ComputeOptimization,
472    /// Memory optimization settings
473    pub memory_optimization: MemoryOptimization,
474}
475
476impl Default for OptimizationConfig {
477    fn default() -> Self {
478        Self {
479            enabled: true,
480            batch_optimization: BatchOptimization::default(),
481            network_optimization: NetworkOptimization::default(),
482            compute_optimization: ComputeOptimization::default(),
483            memory_optimization: MemoryOptimization::default(),
484        }
485    }
486}
487
488/// Batch processing optimization
489#[derive(Debug, Clone, Serialize, Deserialize)]
490pub struct BatchOptimization {
491    /// Enable adaptive batching
492    pub adaptive_batching: bool,
493    /// Maximum batch size
494    pub max_batch_size: usize,
495    /// Minimum batch size
496    pub min_batch_size: usize,
497    /// Batch timeout (milliseconds)
498    pub batch_timeout_ms: u64,
499    /// Adaptive algorithm
500    pub algorithm: AdaptiveBatchingAlgorithm,
501}
502
503impl Default for BatchOptimization {
504    fn default() -> Self {
505        Self {
506            adaptive_batching: true,
507            max_batch_size: 1000,
508            min_batch_size: 10,
509            batch_timeout_ms: 100,
510            algorithm: AdaptiveBatchingAlgorithm::LoadBased,
511        }
512    }
513}
514
515/// Adaptive batching algorithms
516#[derive(Debug, Clone, Serialize, Deserialize)]
517pub enum AdaptiveBatchingAlgorithm {
518    /// Load-based batching
519    LoadBased,
520    /// Latency-based batching
521    LatencyBased,
522    /// Throughput-based batching
523    ThroughputBased,
524    /// Machine learning-based batching
525    MlBased,
526    /// Custom batching algorithm
527    Custom(String),
528}
529
530/// Network optimization settings
531#[derive(Debug, Clone, Serialize, Deserialize)]
532pub struct NetworkOptimization {
533    /// Enable compression
534    pub compression: CompressionConfig,
535    /// Enable prefetching
536    pub prefetching: bool,
537    /// Prefetching strategy
538    pub prefetching_strategy: PrefetchingStrategy,
539    /// Connection pooling
540    pub connection_pooling: bool,
541    /// Maximum connections per node
542    pub max_connections: usize,
543}
544
545impl Default for NetworkOptimization {
546    fn default() -> Self {
547        Self {
548            compression: CompressionConfig::default(),
549            prefetching: true,
550            prefetching_strategy: PrefetchingStrategy::AdaptivePrefetching,
551            connection_pooling: true,
552            max_connections: 100,
553        }
554    }
555}
556
557/// Compression configuration
558#[derive(Debug, Clone, Serialize, Deserialize)]
559pub struct CompressionConfig {
560    /// Enable compression
561    pub enabled: bool,
562    /// Compression algorithm
563    pub algorithm: CompressionAlgorithm,
564    /// Compression level (1-9)
565    pub level: u8,
566    /// Minimum data size for compression (bytes)
567    pub min_size_bytes: usize,
568}
569
570impl Default for CompressionConfig {
571    fn default() -> Self {
572        Self {
573            enabled: true,
574            algorithm: CompressionAlgorithm::Zstd,
575            level: 3,
576            min_size_bytes: 1024,
577        }
578    }
579}
580
581/// Compression algorithms
582#[derive(Debug, Clone, Serialize, Deserialize)]
583pub enum CompressionAlgorithm {
584    /// No compression
585    None,
586    /// Gzip compression
587    Gzip,
588    /// LZ4 compression
589    Lz4,
590    /// Zstandard compression
591    Zstd,
592    /// Snappy compression
593    Snappy,
594    /// Brotli compression
595    Brotli,
596}
597
598/// Prefetching strategies
599#[derive(Debug, Clone, Serialize, Deserialize)]
600pub enum PrefetchingStrategy {
601    /// No prefetching
602    None,
603    /// Sequential prefetching
604    Sequential,
605    /// Pattern-based prefetching
606    PatternBased,
607    /// Adaptive prefetching
608    AdaptivePrefetching,
609    /// Machine learning-based prefetching
610    MlBased,
611}
612
613/// Compute optimization settings
614#[derive(Debug, Clone, Serialize, Deserialize)]
615pub struct ComputeOptimization {
616    /// Enable algorithm selection
617    pub algorithm_selection: bool,
618    /// Selection strategy
619    pub selection_strategy: AlgorithmSelectionStrategy,
620    /// Precision optimization
621    pub precision_optimization: PrecisionOptimization,
622    /// Enable vectorization
623    pub vectorization: bool,
624    /// Enable parallelization
625    pub parallelization: bool,
626}
627
628impl Default for ComputeOptimization {
629    fn default() -> Self {
630        Self {
631            algorithm_selection: true,
632            selection_strategy: AlgorithmSelectionStrategy::PerformanceBased,
633            precision_optimization: PrecisionOptimization::default(),
634            vectorization: true,
635            parallelization: true,
636        }
637    }
638}
639
640/// Algorithm selection strategies
641#[derive(Debug, Clone, Serialize, Deserialize)]
642pub enum AlgorithmSelectionStrategy {
643    /// Performance-based selection
644    PerformanceBased,
645    /// Memory-based selection
646    MemoryBased,
647    /// Energy-based selection
648    EnergyBased,
649    /// Cost-based selection
650    CostBased,
651    /// Machine learning-based selection
652    MlBased,
653}
654
655/// Precision optimization settings
656#[derive(Debug, Clone, Serialize, Deserialize)]
657pub struct PrecisionOptimization {
658    /// Enable mixed precision
659    pub mixed_precision: bool,
660    /// Default precision level
661    pub default_precision: PrecisionLevel,
662    /// Adaptive precision adjustment
663    pub adaptive_precision: bool,
664}
665
666impl Default for PrecisionOptimization {
667    fn default() -> Self {
668        Self {
669            mixed_precision: true,
670            default_precision: PrecisionLevel::Float32,
671            adaptive_precision: true,
672        }
673    }
674}
675
676/// Precision levels
677#[derive(Debug, Clone, Serialize, Deserialize)]
678pub enum PrecisionLevel {
679    /// 16-bit floating point
680    Float16,
681    /// 32-bit floating point
682    Float32,
683    /// 64-bit floating point
684    Float64,
685    /// Brain float 16
686    BFloat16,
687    /// 8-bit integer
688    Int8,
689    /// 16-bit integer
690    Int16,
691    /// 32-bit integer
692    Int32,
693}
694
695/// Memory optimization settings
696#[derive(Debug, Clone, Serialize, Deserialize)]
697pub struct MemoryOptimization {
698    /// Enable memory pooling
699    pub memory_pooling: bool,
700    /// Pool size (MB)
701    pub pool_size_mb: u64,
702    /// Enable garbage collection optimization
703    pub gc_optimization: bool,
704    /// Memory allocation strategy
705    pub allocation_strategy: AllocationStrategy,
706}
707
708impl Default for MemoryOptimization {
709    fn default() -> Self {
710        Self {
711            memory_pooling: true,
712            pool_size_mb: 512,
713            gc_optimization: true,
714            allocation_strategy: AllocationStrategy::PoolBased,
715        }
716    }
717}
718
719/// Memory allocation strategies
720#[derive(Debug, Clone, Serialize, Deserialize)]
721pub enum AllocationStrategy {
722    /// Pool-based allocation
723    PoolBased,
724    /// Arena-based allocation
725    ArenaBased,
726    /// Stack-based allocation
727    StackBased,
728    /// Heap-based allocation
729    HeapBased,
730}