ruvector-graph 2.0.6

Distributed Neo4j-compatible hypergraph database with SIMD optimization
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
//! Cross-cluster federation for distributed graph queries
//!
//! Enables querying across independent RuVector graph clusters:
//! - Cluster discovery and registration
//! - Remote query execution
//! - Result merging from multiple clusters
//! - Cross-cluster authentication and authorization

use crate::distributed::coordinator::{QueryPlan, QueryResult};
use crate::distributed::shard::ShardId;
use crate::{GraphError, Result};
use chrono::{DateTime, Utc};
use dashmap::DashMap;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{debug, info, warn};
use uuid::Uuid;

/// Unique identifier for a cluster
pub type ClusterId = String;

/// Remote cluster information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RemoteCluster {
    /// Unique cluster ID
    pub cluster_id: ClusterId,
    /// Cluster name
    pub name: String,
    /// Cluster endpoint URL
    pub endpoint: String,
    /// Cluster status
    pub status: ClusterStatus,
    /// Authentication token
    pub auth_token: Option<String>,
    /// Last health check timestamp
    pub last_health_check: DateTime<Utc>,
    /// Cluster metadata
    pub metadata: HashMap<String, String>,
    /// Number of shards in this cluster
    pub shard_count: u32,
    /// Cluster region/datacenter
    pub region: Option<String>,
}

impl RemoteCluster {
    /// Create a new remote cluster
    pub fn new(cluster_id: ClusterId, name: String, endpoint: String) -> Self {
        Self {
            cluster_id,
            name,
            endpoint,
            status: ClusterStatus::Unknown,
            auth_token: None,
            last_health_check: Utc::now(),
            metadata: HashMap::new(),
            shard_count: 0,
            region: None,
        }
    }

    /// Check if cluster is healthy
    pub fn is_healthy(&self) -> bool {
        matches!(self.status, ClusterStatus::Healthy)
    }
}

/// Cluster status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ClusterStatus {
    /// Cluster is healthy and available
    Healthy,
    /// Cluster is degraded but operational
    Degraded,
    /// Cluster is unreachable
    Unreachable,
    /// Cluster status unknown
    Unknown,
}

/// Cluster registry for managing federated clusters
pub struct ClusterRegistry {
    /// Registered clusters
    clusters: Arc<DashMap<ClusterId, RemoteCluster>>,
    /// Cluster discovery configuration
    discovery_config: DiscoveryConfig,
}

impl ClusterRegistry {
    /// Create a new cluster registry
    pub fn new(discovery_config: DiscoveryConfig) -> Self {
        Self {
            clusters: Arc::new(DashMap::new()),
            discovery_config,
        }
    }

    /// Register a remote cluster
    pub fn register_cluster(&self, cluster: RemoteCluster) -> Result<()> {
        info!(
            "Registering cluster: {} ({})",
            cluster.name, cluster.cluster_id
        );
        self.clusters.insert(cluster.cluster_id.clone(), cluster);
        Ok(())
    }

    /// Unregister a cluster
    pub fn unregister_cluster(&self, cluster_id: &ClusterId) -> Result<()> {
        info!("Unregistering cluster: {}", cluster_id);
        self.clusters.remove(cluster_id).ok_or_else(|| {
            GraphError::FederationError(format!("Cluster not found: {}", cluster_id))
        })?;
        Ok(())
    }

    /// Get a cluster by ID
    pub fn get_cluster(&self, cluster_id: &ClusterId) -> Option<RemoteCluster> {
        self.clusters.get(cluster_id).map(|c| c.value().clone())
    }

    /// List all registered clusters
    pub fn list_clusters(&self) -> Vec<RemoteCluster> {
        self.clusters.iter().map(|e| e.value().clone()).collect()
    }

    /// List healthy clusters only
    pub fn healthy_clusters(&self) -> Vec<RemoteCluster> {
        self.clusters
            .iter()
            .filter(|e| e.value().is_healthy())
            .map(|e| e.value().clone())
            .collect()
    }

    /// Perform health check on a cluster
    pub async fn health_check(&self, cluster_id: &ClusterId) -> Result<ClusterStatus> {
        let cluster = self.get_cluster(cluster_id).ok_or_else(|| {
            GraphError::FederationError(format!("Cluster not found: {}", cluster_id))
        })?;

        // In production, make actual HTTP/gRPC health check request
        // For now, simulate health check
        let status = ClusterStatus::Healthy;

        // Update cluster status
        if let Some(mut entry) = self.clusters.get_mut(cluster_id) {
            entry.status = status;
            entry.last_health_check = Utc::now();
        }

        debug!("Health check for cluster {}: {:?}", cluster_id, status);
        Ok(status)
    }

    /// Perform health checks on all clusters
    pub async fn health_check_all(&self) -> HashMap<ClusterId, ClusterStatus> {
        let mut results = HashMap::new();

        for cluster in self.list_clusters() {
            match self.health_check(&cluster.cluster_id).await {
                Ok(status) => {
                    results.insert(cluster.cluster_id, status);
                }
                Err(e) => {
                    warn!(
                        "Health check failed for cluster {}: {}",
                        cluster.cluster_id, e
                    );
                    results.insert(cluster.cluster_id, ClusterStatus::Unreachable);
                }
            }
        }

        results
    }

    /// Discover clusters automatically (if enabled)
    pub async fn discover_clusters(&self) -> Result<Vec<RemoteCluster>> {
        if !self.discovery_config.auto_discovery {
            return Ok(Vec::new());
        }

        info!("Discovering clusters...");

        // In production, implement actual cluster discovery:
        // - mDNS/DNS-SD for local network
        // - Consul/etcd for service discovery
        // - Static configuration file
        // - Cloud provider APIs (AWS, GCP, Azure)

        // For now, return empty list
        Ok(Vec::new())
    }
}

/// Cluster discovery configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiscoveryConfig {
    /// Enable automatic cluster discovery
    pub auto_discovery: bool,
    /// Discovery method
    pub discovery_method: DiscoveryMethod,
    /// Discovery interval in seconds
    pub discovery_interval_seconds: u64,
    /// Health check interval in seconds
    pub health_check_interval_seconds: u64,
}

impl Default for DiscoveryConfig {
    fn default() -> Self {
        Self {
            auto_discovery: false,
            discovery_method: DiscoveryMethod::Static,
            discovery_interval_seconds: 60,
            health_check_interval_seconds: 30,
        }
    }
}

/// Cluster discovery method
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum DiscoveryMethod {
    /// Static configuration
    Static,
    /// DNS-based discovery
    Dns,
    /// Consul service discovery
    Consul,
    /// etcd service discovery
    Etcd,
    /// Kubernetes service discovery
    Kubernetes,
}

/// Federated query spanning multiple clusters
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FederatedQuery {
    /// Query ID
    pub query_id: String,
    /// Original query
    pub query: String,
    /// Target clusters
    pub target_clusters: Vec<ClusterId>,
    /// Query execution strategy
    pub strategy: FederationStrategy,
    /// Created timestamp
    pub created_at: DateTime<Utc>,
}

/// Federation strategy
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum FederationStrategy {
    /// Execute on all clusters in parallel
    Parallel,
    /// Execute on clusters sequentially
    Sequential,
    /// Execute on primary cluster, fallback to others
    PrimaryWithFallback,
    /// Execute on nearest/fastest cluster only
    Nearest,
}

/// Federation engine for cross-cluster queries
pub struct Federation {
    /// Cluster registry
    registry: Arc<ClusterRegistry>,
    /// Federation configuration
    config: FederationConfig,
    /// Active federated queries
    active_queries: Arc<DashMap<String, FederatedQuery>>,
}

impl Federation {
    /// Create a new federation engine
    pub fn new(config: FederationConfig) -> Self {
        let discovery_config = DiscoveryConfig::default();
        Self {
            registry: Arc::new(ClusterRegistry::new(discovery_config)),
            config,
            active_queries: Arc::new(DashMap::new()),
        }
    }

    /// Get the cluster registry
    pub fn registry(&self) -> Arc<ClusterRegistry> {
        Arc::clone(&self.registry)
    }

    /// Execute a federated query across multiple clusters
    pub async fn execute_federated(
        &self,
        query: &str,
        target_clusters: Option<Vec<ClusterId>>,
    ) -> Result<FederatedQueryResult> {
        let query_id = Uuid::new_v4().to_string();
        let start = std::time::Instant::now();

        // Determine target clusters
        let clusters = if let Some(targets) = target_clusters {
            targets
                .into_iter()
                .filter_map(|id| self.registry.get_cluster(&id))
                .collect()
        } else {
            self.registry.healthy_clusters()
        };

        if clusters.is_empty() {
            return Err(GraphError::FederationError(
                "No healthy clusters available".to_string(),
            ));
        }

        info!(
            "Executing federated query {} across {} clusters",
            query_id,
            clusters.len()
        );

        let federated_query = FederatedQuery {
            query_id: query_id.clone(),
            query: query.to_string(),
            target_clusters: clusters.iter().map(|c| c.cluster_id.clone()).collect(),
            strategy: self.config.default_strategy,
            created_at: Utc::now(),
        };

        self.active_queries
            .insert(query_id.clone(), federated_query.clone());

        // Execute query on each cluster based on strategy
        let mut cluster_results = HashMap::new();

        match self.config.default_strategy {
            FederationStrategy::Parallel => {
                // Execute on all clusters in parallel
                let mut handles = Vec::new();

                for cluster in &clusters {
                    let cluster_id = cluster.cluster_id.clone();
                    let query_str = query.to_string();
                    let cluster_clone = cluster.clone();

                    let handle = tokio::spawn(async move {
                        Self::execute_on_cluster(&cluster_clone, &query_str).await
                    });

                    handles.push((cluster_id, handle));
                }

                // Collect results
                for (cluster_id, handle) in handles {
                    match handle.await {
                        Ok(Ok(result)) => {
                            cluster_results.insert(cluster_id, result);
                        }
                        Ok(Err(e)) => {
                            warn!("Query failed on cluster {}: {}", cluster_id, e);
                        }
                        Err(e) => {
                            warn!("Task failed for cluster {}: {}", cluster_id, e);
                        }
                    }
                }
            }
            FederationStrategy::Sequential => {
                // Execute on clusters sequentially
                for cluster in &clusters {
                    match Self::execute_on_cluster(cluster, query).await {
                        Ok(result) => {
                            cluster_results.insert(cluster.cluster_id.clone(), result);
                        }
                        Err(e) => {
                            warn!("Query failed on cluster {}: {}", cluster.cluster_id, e);
                        }
                    }
                }
            }
            FederationStrategy::Nearest | FederationStrategy::PrimaryWithFallback => {
                // Execute on first healthy cluster
                if let Some(cluster) = clusters.first() {
                    match Self::execute_on_cluster(cluster, query).await {
                        Ok(result) => {
                            cluster_results.insert(cluster.cluster_id.clone(), result);
                        }
                        Err(e) => {
                            warn!("Query failed on cluster {}: {}", cluster.cluster_id, e);
                        }
                    }
                }
            }
        }

        // Merge results from all clusters
        let merged_result = self.merge_results(cluster_results)?;

        let execution_time_ms = start.elapsed().as_millis() as u64;

        // Remove from active queries
        self.active_queries.remove(&query_id);

        Ok(FederatedQueryResult {
            query_id,
            merged_result,
            clusters_queried: clusters.len(),
            execution_time_ms,
        })
    }

    /// Execute query on a single remote cluster
    async fn execute_on_cluster(cluster: &RemoteCluster, query: &str) -> Result<QueryResult> {
        debug!("Executing query on cluster: {}", cluster.cluster_id);

        // In production, make actual HTTP/gRPC call to remote cluster
        // For now, return empty result
        Ok(QueryResult {
            query_id: Uuid::new_v4().to_string(),
            nodes: Vec::new(),
            edges: Vec::new(),
            aggregates: HashMap::new(),
            stats: crate::distributed::coordinator::QueryStats {
                execution_time_ms: 0,
                shards_queried: 0,
                nodes_scanned: 0,
                edges_scanned: 0,
                cached: false,
            },
        })
    }

    /// Merge results from multiple clusters
    fn merge_results(&self, results: HashMap<ClusterId, QueryResult>) -> Result<QueryResult> {
        if results.is_empty() {
            return Err(GraphError::FederationError(
                "No results to merge".to_string(),
            ));
        }

        let mut merged = QueryResult {
            query_id: Uuid::new_v4().to_string(),
            nodes: Vec::new(),
            edges: Vec::new(),
            aggregates: HashMap::new(),
            stats: crate::distributed::coordinator::QueryStats {
                execution_time_ms: 0,
                shards_queried: 0,
                nodes_scanned: 0,
                edges_scanned: 0,
                cached: false,
            },
        };

        for (cluster_id, result) in results {
            debug!("Merging results from cluster: {}", cluster_id);

            // Merge nodes (deduplicating by ID)
            for node in result.nodes {
                if !merged.nodes.iter().any(|n| n.id == node.id) {
                    merged.nodes.push(node);
                }
            }

            // Merge edges (deduplicating by ID)
            for edge in result.edges {
                if !merged.edges.iter().any(|e| e.id == edge.id) {
                    merged.edges.push(edge);
                }
            }

            // Merge aggregates
            for (key, value) in result.aggregates {
                merged
                    .aggregates
                    .insert(format!("{}_{}", cluster_id, key), value);
            }

            // Aggregate stats
            merged.stats.execution_time_ms = merged
                .stats
                .execution_time_ms
                .max(result.stats.execution_time_ms);
            merged.stats.shards_queried += result.stats.shards_queried;
            merged.stats.nodes_scanned += result.stats.nodes_scanned;
            merged.stats.edges_scanned += result.stats.edges_scanned;
        }

        Ok(merged)
    }

    /// Get configuration
    pub fn config(&self) -> &FederationConfig {
        &self.config
    }
}

/// Federation configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FederationConfig {
    /// Default federation strategy
    pub default_strategy: FederationStrategy,
    /// Maximum number of clusters to query
    pub max_clusters: usize,
    /// Query timeout in seconds
    pub query_timeout_seconds: u64,
    /// Enable result caching
    pub enable_caching: bool,
}

impl Default for FederationConfig {
    fn default() -> Self {
        Self {
            default_strategy: FederationStrategy::Parallel,
            max_clusters: 10,
            query_timeout_seconds: 30,
            enable_caching: true,
        }
    }
}

/// Federated query result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FederatedQueryResult {
    /// Query ID
    pub query_id: String,
    /// Merged result from all clusters
    pub merged_result: QueryResult,
    /// Number of clusters queried
    pub clusters_queried: usize,
    /// Total execution time
    pub execution_time_ms: u64,
}

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

    #[test]
    fn test_cluster_registry() {
        let config = DiscoveryConfig::default();
        let registry = ClusterRegistry::new(config);

        let cluster = RemoteCluster::new(
            "cluster-1".to_string(),
            "Test Cluster".to_string(),
            "http://localhost:8080".to_string(),
        );

        registry.register_cluster(cluster.clone()).unwrap();

        assert_eq!(registry.list_clusters().len(), 1);
        assert!(registry.get_cluster(&"cluster-1".to_string()).is_some());
    }

    #[tokio::test]
    async fn test_federation() {
        let config = FederationConfig::default();
        let federation = Federation::new(config);

        let cluster = RemoteCluster::new(
            "cluster-1".to_string(),
            "Test Cluster".to_string(),
            "http://localhost:8080".to_string(),
        );

        federation.registry().register_cluster(cluster).unwrap();

        // Test would execute federated query in production
    }

    #[test]
    fn test_remote_cluster() {
        let cluster = RemoteCluster::new(
            "test".to_string(),
            "Test".to_string(),
            "http://localhost".to_string(),
        );

        assert!(!cluster.is_healthy());
    }
}