saorsa_core/placement/mod.rs
1// Copyright (c) 2025 Saorsa Labs Limited
2//
3// This file is part of the Saorsa P2P network.
4//
5// Licensed under the AGPL-3.0 license:
6// <https://www.gnu.org/licenses/agpl-3.0.html>
7
8//! Placement Loop & Storage Orchestration System
9//!
10//! This module implements the core placement system for optimal distribution
11//! of erasure-coded shards across the network, integrating EigenTrust reputation,
12//! churn prediction, capacity constraints, and diversity rules.
13//!
14//! ## Core Concepts
15//!
16//! ### Weighted Selection Algorithm
17//!
18//! The placement system uses Efraimidis-Spirakis weighted sampling with the formula:
19//!
20//! ```text
21//! w_i = (τ_i^α) * (p_i^β) * (c_i^γ) * d_i
22//! ```
23//!
24//! Where:
25//! - `τ_i`: EigenTrust reputation score (0.0-1.0)
26//! - `p_i`: Node performance score (0.0-1.0)
27//! - `c_i`: Available capacity score (0.0-1.0)
28//! - `d_i`: Geographic/network diversity bonus (1.0-2.0)
29//! - `α, β, γ`: Configurable weight exponents
30//!
31//! ### Byzantine Fault Tolerance
32//!
33//! Implements configurable f-out-of-3f+1 Byzantine fault tolerance:
34//! - Tolerates up to f Byzantine (malicious) nodes
35//! - Requires minimum 3f+1 nodes for safety
36//! - Automatically adjusts replication based on network size
37//!
38//! ### Geographic Diversity
39//!
40//! Ensures optimal shard distribution across:
41//! - Geographic regions (7 major regions)
42//! - Autonomous System Numbers (ASNs)
43//! - Network operators and data centers
44//!
45//! ## Usage Examples
46//!
47//! ### Basic Placement
48//!
49//! ```rust,no_run
50//! use saorsa_core::placement::{PlacementEngine, PlacementConfig};
51//! use std::time::Duration;
52//!
53//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
54//! let config = PlacementConfig {
55//! replication_factor: (3, 8).into(),
56//! byzantine_tolerance: 2.into(),
57//! placement_timeout: Duration::from_secs(30),
58//! geographic_diversity: true,
59//! ..Default::default()
60//! };
61//!
62//! let mut engine = PlacementEngine::new(config);
63//!
64//! // Select optimal nodes for shard placement
65//! let decision = engine.select_nodes(
66//! &available_nodes,
67//! 8, // replication factor
68//! &trust_system,
69//! &performance_monitor,
70//! &node_metadata,
71//! ).await?;
72//!
73//! println!("Selected {} nodes with {:.2}% reliability",
74//! decision.selected_nodes.len(),
75//! decision.estimated_reliability * 100.0);
76//! # Ok(())
77//! # }
78//! ```
79//!
80//! ### Advanced Configuration
81//!
82//! ```rust,no_run
83//! use saorsa_core::placement::{
84//! PlacementConfig, OptimizationWeights, PlacementConstraint
85//! };
86//! use std::time::Duration;
87//!
88//! let config = PlacementConfig {
89//! weights: OptimizationWeights {
90//! trust_weight: 0.5, // High trust emphasis
91//! performance_weight: 0.25,
92//! capacity_weight: 0.15,
93//! diversity_bonus: 0.1,
94//! },
95//! constraints: vec![
96//! PlacementConstraint::MinimumTrustScore(0.7),
97//! PlacementConstraint::MaximumLatency(Duration::from_millis(500)),
98//! PlacementConstraint::RequireGeographicDiversity,
99//! ],
100//! ..Default::default()
101//! };
102//! ```
103//!
104//! ### Storage Orchestration
105//!
106//! ```rust,no_run
107//! use saorsa_core::placement::PlacementOrchestrator;
108//!
109//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
110//! let orchestrator = PlacementOrchestrator::new(
111//! config,
112//! dht_engine,
113//! trust_system,
114//! performance_monitor,
115//! churn_predictor,
116//! ).await?;
117//!
118//! // Start audit and repair systems
119//! orchestrator.start().await?;
120//!
121//! // Place data with optimal distribution
122//! let decision = orchestrator.place_data(
123//! data,
124//! 8, // replication factor
125//! Some(NetworkRegion::Europe),
126//! ).await?;
127//! # Ok(())
128//! # }
129//! ```
130//!
131//! ## Architecture
132//!
133//! The placement system consists of several key components:
134//!
135//! - **PlacementEngine**: Main orchestrator for placement decisions
136//! - **WeightedPlacementStrategy**: Implements the weighted selection algorithm
137//! - **StorageOrchestrator**: Manages shard storage and retrieval
138//! - **AuditSystem**: Continuous monitoring of shard health
139//! - **RepairSystem**: Automatic repair with hysteresis control
140//! - **DiversityEnforcer**: Geographic and network diversity constraints
141//!
142//! ## Performance Characteristics
143//!
144//! - **Selection Speed**: <1 second for 8-node selection from 1000+ candidates
145//! - **Memory Usage**: O(n) where n is candidate node count
146//! - **Audit Frequency**: Every 5 minutes with concurrent limits
147//! - **Repair Latency**: <1 hour detection, immediate repair initiation
148//!
149//! ## Security Features
150//!
151//! - EigenTrust integration for reputation-based selection
152//! - Byzantine fault tolerance with configurable parameters
153//! - Proof-of-work for DHT records (~18 bits difficulty)
154//! - Cryptographic verification of all operations
155//! - Secure random selection with cryptographic entropy
156
157pub mod algorithms;
158pub mod dht_records;
159pub mod errors;
160pub mod orchestrator;
161pub mod traits;
162pub mod types;
163
164// Re-export core types for convenience
165pub use algorithms::{DiversityEnforcer, WeightedPlacementStrategy, WeightedSampler};
166pub use dht_records::{
167 DataPointer, DhtRecord, GroupBeacon, NatType, NodeAd, NodeCapabilities, OsSignature,
168 RegisterPointer,
169};
170pub use errors::{PlacementError, PlacementResult};
171pub use orchestrator::{AuditSystem, PlacementOrchestrator, RepairSystem, StorageOrchestrator};
172pub use traits::{
173 NetworkTopology, NodePerformanceMetrics, PerformanceEstimator, PlacementConstraint,
174 PlacementStrategy, PlacementValidator,
175};
176pub use types::{
177 ByzantineTolerance, GeographicLocation, NetworkRegion, OptimizationWeights, PlacementConfig,
178 PlacementDecision, PlacementMetrics, ReplicationFactor,
179};
180
181use std::collections::HashSet;
182use std::time::Instant;
183
184use crate::adaptive::{NodeId, performance::PerformanceMonitor, trust::EigenTrustEngine};
185
186/// Main placement engine that orchestrates the entire placement process
187#[derive(Debug)]
188pub struct PlacementEngine {
189 config: PlacementConfig,
190 strategy: Box<dyn PlacementStrategy + Send + Sync>,
191}
192
193impl PlacementEngine {
194 /// Create new placement engine with default weighted strategy
195 pub fn new(config: PlacementConfig) -> Self {
196 let strategy = Box::new(algorithms::WeightedPlacementStrategy::new(config.clone()));
197
198 Self { config, strategy }
199 }
200
201 /// Create placement engine with custom strategy
202 pub fn with_strategy(
203 config: PlacementConfig,
204 strategy: Box<dyn PlacementStrategy + Send + Sync>,
205 ) -> Self {
206 Self { config, strategy }
207 }
208
209 /// Select optimal nodes for shard placement
210 pub async fn select_nodes(
211 &mut self,
212 available_nodes: &HashSet<NodeId>,
213 replication_factor: u8,
214 trust_system: &EigenTrustEngine,
215 performance_monitor: &PerformanceMonitor,
216 node_metadata: &std::collections::HashMap<NodeId, (GeographicLocation, u32, NetworkRegion)>,
217 ) -> PlacementResult<PlacementDecision> {
218 let start_time = Instant::now();
219
220 // Validate inputs
221 if available_nodes.is_empty() {
222 return Err(PlacementError::InsufficientNodes {
223 required: replication_factor as usize,
224 available: 0,
225 });
226 }
227
228 if replication_factor < self.config.replication_factor.min_value() {
229 return Err(PlacementError::InvalidReplicationFactor(replication_factor));
230 }
231
232 // Apply placement timeout
233 let timeout_future = async {
234 tokio::time::sleep(self.config.placement_timeout).await;
235 Err(PlacementError::PlacementTimeout)
236 };
237
238 let placement_future = self.strategy.select_nodes(
239 available_nodes,
240 replication_factor,
241 trust_system,
242 performance_monitor,
243 node_metadata,
244 );
245
246 // Race placement against timeout
247 let result = tokio::select! {
248 result = placement_future => result?,
249 timeout_result = timeout_future => timeout_result?,
250 };
251 let mut decision = result;
252
253 // Update timing information
254 decision.selection_time = start_time.elapsed();
255
256 // Validate against configuration constraints
257 self.validate_decision(&decision)?;
258
259 Ok(decision)
260 }
261
262 /// Validate placement decision against configuration constraints
263 fn validate_decision(&self, decision: &PlacementDecision) -> PlacementResult<()> {
264 // Check minimum nodes
265 if decision.selected_nodes.len() < self.config.replication_factor.min_value() as usize {
266 return Err(PlacementError::InsufficientNodes {
267 required: self.config.replication_factor.min_value() as usize,
268 available: decision.selected_nodes.len(),
269 });
270 }
271
272 // Check Byzantine fault tolerance
273 let required_for_byzantine = self.config.byzantine_tolerance.required_nodes();
274 if decision.selected_nodes.len() < required_for_byzantine {
275 return Err(PlacementError::ByzantineToleranceViolation {
276 required: required_for_byzantine,
277 available: decision.selected_nodes.len(),
278 });
279 }
280
281 // Check reliability threshold
282 if decision.estimated_reliability < 0.8 {
283 return Err(PlacementError::ReliabilityTooLow {
284 estimated: decision.estimated_reliability,
285 minimum: 0.8,
286 });
287 }
288
289 Ok(())
290 }
291
292 /// Get current configuration
293 pub fn config(&self) -> &PlacementConfig {
294 &self.config
295 }
296
297 /// Update configuration
298 pub fn update_config(&mut self, config: PlacementConfig) {
299 self.config = config;
300 }
301
302 /// Get strategy name
303 pub fn strategy_name(&self) -> &str {
304 self.strategy.name()
305 }
306}