oxirs-fuseki 0.2.4

SPARQL 1.1/1.2 HTTP protocol server with Fuseki-compatible configuration
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
//! Store Health Monitoring
//!
//! Provides comprehensive health metrics for the RDF store, including:
//! - Store status and availability
//! - Performance metrics (query latency, throughput)
//! - Resource utilization (memory, connections)
//! - Error rates and recovery status

use crate::error::{FusekiError, FusekiResult};
use crate::store::{Store, StoreStats};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;
use tracing::{debug, warn};

/// Comprehensive store health status
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StoreHealth {
    /// Overall health status
    pub status: HealthStatus,
    /// Component-level health checks
    pub components: Vec<ComponentHealth>,
    /// Performance metrics
    pub performance: PerformanceMetrics,
    /// Resource utilization
    pub resources: ResourceMetrics,
    /// Error metrics
    pub errors: ErrorMetrics,
    /// Last check timestamp
    pub checked_at: chrono::DateTime<chrono::Utc>,
    /// Health score (0-100)
    pub health_score: u8,
}

/// Overall health status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum HealthStatus {
    /// All systems operational
    Healthy,
    /// Some non-critical issues detected
    Degraded,
    /// Critical issues detected
    Unhealthy,
    /// System unavailable
    Down,
}

/// Component health status
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComponentHealth {
    /// Component name
    pub name: String,
    /// Component status
    pub status: HealthStatus,
    /// Status message
    pub message: Option<String>,
    /// Last successful check
    pub last_success: Option<chrono::DateTime<chrono::Utc>>,
    /// Response time (milliseconds)
    pub response_time_ms: Option<u64>,
}

/// Performance metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceMetrics {
    /// Average query latency (milliseconds)
    pub avg_query_latency_ms: f64,
    /// P95 query latency (milliseconds)
    pub p95_query_latency_ms: f64,
    /// P99 query latency (milliseconds)
    pub p99_query_latency_ms: f64,
    /// Queries per second
    pub queries_per_second: f64,
    /// Cache hit rate (0-1)
    pub cache_hit_rate: f64,
    /// Active query count
    pub active_queries: u32,
}

/// Resource utilization metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceMetrics {
    /// Memory usage (bytes)
    pub memory_used_bytes: u64,
    /// Memory usage percentage (0-100)
    pub memory_usage_percent: f64,
    /// Active connections
    pub active_connections: u32,
    /// Maximum connections
    pub max_connections: u32,
    /// Triple count
    pub triple_count: usize,
    /// Dataset count
    pub dataset_count: usize,
}

/// Error metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorMetrics {
    /// Total errors in last hour
    pub errors_last_hour: u64,
    /// Query failures in last hour
    pub query_failures_last_hour: u64,
    /// Update failures in last hour
    pub update_failures_last_hour: u64,
    /// Error rate (errors per second)
    pub error_rate: f64,
    /// Last error timestamp
    pub last_error: Option<chrono::DateTime<chrono::Utc>>,
    /// Last error message
    pub last_error_message: Option<String>,
}

/// Health monitor for tracking store health over time
pub struct StoreHealthMonitor {
    /// Store reference
    store: Arc<Store>,
    /// Health history
    health_history: Arc<RwLock<Vec<StoreHealth>>>,
    /// Performance tracking
    performance_tracker: Arc<RwLock<PerformanceTracker>>,
    /// Error tracking
    error_tracker: Arc<RwLock<ErrorTracker>>,
    /// Configuration
    config: HealthMonitorConfig,
}

/// Health monitor configuration
#[derive(Debug, Clone)]
pub struct HealthMonitorConfig {
    /// Maximum health history entries
    pub max_history: usize,
    /// Health check interval
    pub check_interval: Duration,
    /// Performance window for metrics
    pub performance_window: Duration,
    /// Error window for metrics
    pub error_window: Duration,
    /// Memory threshold for warnings (bytes)
    pub memory_warning_threshold: u64,
    /// Memory threshold for critical (bytes)
    pub memory_critical_threshold: u64,
    /// Maximum connections (from server config)
    pub max_connections: usize,
}

impl Default for HealthMonitorConfig {
    fn default() -> Self {
        Self {
            max_history: 100,
            check_interval: Duration::from_secs(30),
            performance_window: Duration::from_secs(300),
            error_window: Duration::from_secs(3600),
            memory_warning_threshold: 2 * 1024 * 1024 * 1024, // 2GB
            memory_critical_threshold: 4 * 1024 * 1024 * 1024, // 4GB
            max_connections: 1000,                            // Default max connections
        }
    }
}

/// Performance tracking
#[derive(Debug, Clone, Default)]
struct PerformanceTracker {
    /// Query latencies (milliseconds)
    query_latencies: Vec<(Instant, f64)>,
    /// Query timestamps
    query_timestamps: Vec<Instant>,
    /// Active query count
    active_queries: u32,
    /// Active connection count (tracked from concurrent requests)
    active_connections: u32,
}

/// Error tracking
#[derive(Debug, Clone, Default)]
struct ErrorTracker {
    /// Error events
    errors: Vec<ErrorEvent>,
    /// Query failures
    query_failures: Vec<Instant>,
    /// Update failures
    update_failures: Vec<Instant>,
}

/// Error event
#[derive(Debug, Clone)]
struct ErrorEvent {
    timestamp: Instant,
    message: String,
    error_type: String,
}

impl StoreHealthMonitor {
    /// Create a new health monitor
    pub fn new(store: Arc<Store>) -> Self {
        Self::with_config(store, HealthMonitorConfig::default())
    }

    /// Create a new health monitor with custom configuration
    pub fn with_config(store: Arc<Store>, config: HealthMonitorConfig) -> Self {
        Self {
            store,
            health_history: Arc::new(RwLock::new(Vec::new())),
            performance_tracker: Arc::new(RwLock::new(PerformanceTracker::default())),
            error_tracker: Arc::new(RwLock::new(ErrorTracker::default())),
            config,
        }
    }

    /// Perform a comprehensive health check
    pub async fn check_health(&self) -> FusekiResult<StoreHealth> {
        let check_start = Instant::now();
        let checked_at = chrono::Utc::now();

        debug!("Performing comprehensive store health check");

        // Check store statistics
        let stats = self.store.get_stats(None).unwrap_or(StoreStats {
            triple_count: 0,
            dataset_count: 0,
            total_queries: 0,
            total_updates: 0,
            cache_hit_ratio: 0.0,
            uptime_seconds: 0,
            change_log_size: 0,
            latest_change_id: 0,
        });

        // Check components
        let components = self.check_components(&stats).await;

        // Get performance metrics
        let performance = self.get_performance_metrics(&stats).await?;

        // Get resource metrics
        let resources = self.get_resource_metrics(&stats).await?;

        // Get error metrics
        let errors = self.get_error_metrics().await?;

        // Calculate overall health status and score
        let (status, health_score) =
            self.calculate_health_status(&components, &performance, &resources, &errors);

        let health = StoreHealth {
            status,
            components,
            performance,
            resources,
            errors,
            checked_at,
            health_score,
        };

        // Store in history
        self.add_to_history(health.clone()).await;

        debug!("Health check completed in {:?}", check_start.elapsed());

        Ok(health)
    }

    /// Check individual components
    async fn check_components(&self, stats: &StoreStats) -> Vec<ComponentHealth> {
        let mut components = Vec::new();

        // Check default store
        let store_check_start = Instant::now();
        let store_status = if stats.triple_count == 0 && stats.total_queries == 0 {
            HealthStatus::Degraded
        } else {
            HealthStatus::Healthy
        };

        components.push(ComponentHealth {
            name: "default_store".to_string(),
            status: store_status,
            message: Some(format!("{} triples", stats.triple_count)),
            last_success: Some(chrono::Utc::now()),
            response_time_ms: Some(store_check_start.elapsed().as_millis() as u64),
        });

        // Check query engine
        components.push(ComponentHealth {
            name: "query_engine".to_string(),
            status: HealthStatus::Healthy,
            message: Some(format!("{} queries executed", stats.total_queries)),
            last_success: Some(chrono::Utc::now()),
            response_time_ms: Some(1),
        });

        // Check datasets
        let dataset_status = if stats.dataset_count > 0 {
            HealthStatus::Healthy
        } else {
            HealthStatus::Degraded
        };

        components.push(ComponentHealth {
            name: "datasets".to_string(),
            status: dataset_status,
            message: Some(format!("{} datasets", stats.dataset_count)),
            last_success: Some(chrono::Utc::now()),
            response_time_ms: Some(1),
        });

        components
    }

    /// Get performance metrics
    async fn get_performance_metrics(
        &self,
        stats: &StoreStats,
    ) -> FusekiResult<PerformanceMetrics> {
        let tracker = self.performance_tracker.read().await;
        let now = Instant::now();
        let window_start = now - self.config.performance_window;

        // Filter recent latencies
        let recent_latencies: Vec<f64> = tracker
            .query_latencies
            .iter()
            .filter(|(ts, _)| *ts >= window_start)
            .map(|(_, latency)| *latency)
            .collect();

        // Calculate latency metrics
        let avg_query_latency_ms = if !recent_latencies.is_empty() {
            recent_latencies.iter().sum::<f64>() / recent_latencies.len() as f64
        } else {
            0.0
        };

        let mut sorted_latencies = recent_latencies.clone();
        sorted_latencies.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let p95_query_latency_ms = if !sorted_latencies.is_empty() {
            let idx = (sorted_latencies.len() as f64 * 0.95) as usize;
            sorted_latencies.get(idx).copied().unwrap_or(0.0)
        } else {
            0.0
        };

        let p99_query_latency_ms = if !sorted_latencies.is_empty() {
            let idx = (sorted_latencies.len() as f64 * 0.99) as usize;
            sorted_latencies.get(idx).copied().unwrap_or(0.0)
        } else {
            0.0
        };

        // Calculate queries per second
        let recent_queries = tracker
            .query_timestamps
            .iter()
            .filter(|ts| **ts >= window_start)
            .count();

        let queries_per_second =
            recent_queries as f64 / self.config.performance_window.as_secs_f64();

        Ok(PerformanceMetrics {
            avg_query_latency_ms,
            p95_query_latency_ms,
            p99_query_latency_ms,
            queries_per_second,
            cache_hit_rate: stats.cache_hit_ratio,
            active_queries: tracker.active_queries,
        })
    }

    /// Get resource metrics
    async fn get_resource_metrics(&self, stats: &StoreStats) -> FusekiResult<ResourceMetrics> {
        // Get system memory info
        use sysinfo::System;
        let mut sys = System::new();
        sys.refresh_memory();

        let memory_used_bytes = sys.used_memory();
        let total_memory = sys.total_memory();
        let memory_usage_percent = if total_memory > 0 {
            (memory_used_bytes as f64 / total_memory as f64) * 100.0
        } else {
            0.0
        };

        // Get active connections from performance tracker
        // This is tracked based on concurrent query/update requests
        let active_connections = self.performance_tracker.read().await.active_connections;

        Ok(ResourceMetrics {
            memory_used_bytes,
            memory_usage_percent,
            active_connections,
            max_connections: self.config.max_connections as u32,
            triple_count: stats.triple_count,
            dataset_count: stats.dataset_count,
        })
    }

    /// Get error metrics
    async fn get_error_metrics(&self) -> FusekiResult<ErrorMetrics> {
        let tracker = self.error_tracker.read().await;
        let now = Instant::now();
        let window_start = now - self.config.error_window;

        // Count recent errors
        let recent_errors = tracker
            .errors
            .iter()
            .filter(|e| e.timestamp >= window_start)
            .count();

        let query_failures_last_hour = tracker
            .query_failures
            .iter()
            .filter(|ts| **ts >= window_start)
            .count();

        let update_failures_last_hour = tracker
            .update_failures
            .iter()
            .filter(|ts| **ts >= window_start)
            .count();

        let error_rate = recent_errors as f64 / self.config.error_window.as_secs_f64();

        let (last_error, last_error_message) = tracker
            .errors
            .last()
            .map(|e| {
                (
                    Some(
                        chrono::Utc::now()
                            - chrono::Duration::seconds((now - e.timestamp).as_secs() as i64),
                    ),
                    Some(e.message.clone()),
                )
            })
            .unwrap_or((None, None));

        Ok(ErrorMetrics {
            errors_last_hour: recent_errors as u64,
            query_failures_last_hour: query_failures_last_hour as u64,
            update_failures_last_hour: update_failures_last_hour as u64,
            error_rate,
            last_error,
            last_error_message,
        })
    }

    /// Calculate overall health status and score
    fn calculate_health_status(
        &self,
        components: &[ComponentHealth],
        performance: &PerformanceMetrics,
        resources: &ResourceMetrics,
        errors: &ErrorMetrics,
    ) -> (HealthStatus, u8) {
        let mut score = 100u8;

        // Check component health
        let unhealthy_components = components
            .iter()
            .filter(|c| c.status == HealthStatus::Unhealthy)
            .count();
        let degraded_components = components
            .iter()
            .filter(|c| c.status == HealthStatus::Degraded)
            .count();

        if unhealthy_components > 0 {
            score = score.saturating_sub(40);
        }
        score = score.saturating_sub((degraded_components * 10) as u8);

        // Check performance
        if performance.avg_query_latency_ms > 1000.0 {
            score = score.saturating_sub(15);
        } else if performance.avg_query_latency_ms > 500.0 {
            score = score.saturating_sub(10);
        }

        if performance.cache_hit_rate < 0.5 {
            score = score.saturating_sub(5);
        }

        // Check resources
        if resources.memory_usage_percent > 90.0 {
            score = score.saturating_sub(20);
        } else if resources.memory_usage_percent > 80.0 {
            score = score.saturating_sub(10);
        }

        // Check errors
        if errors.errors_last_hour > 100 {
            score = score.saturating_sub(20);
        } else if errors.errors_last_hour > 10 {
            score = score.saturating_sub(10);
        }

        // Determine overall status
        let status = if score >= 80 {
            HealthStatus::Healthy
        } else if score >= 60 {
            HealthStatus::Degraded
        } else if score >= 30 {
            HealthStatus::Unhealthy
        } else {
            HealthStatus::Down
        };

        (status, score)
    }

    /// Add health check to history
    async fn add_to_history(&self, health: StoreHealth) {
        let mut history = self.health_history.write().await;
        history.push(health);

        // Trim history if needed
        let history_len = history.len();
        if history_len > self.config.max_history {
            let drain_count = history_len - self.config.max_history;
            history.drain(0..drain_count);
        }
    }

    /// Get health history
    pub async fn get_health_history(&self) -> Vec<StoreHealth> {
        self.health_history.read().await.clone()
    }

    /// Record query execution
    pub async fn record_query(&self, latency_ms: f64) {
        let mut tracker = self.performance_tracker.write().await;
        let now = Instant::now();

        tracker.query_latencies.push((now, latency_ms));
        tracker.query_timestamps.push(now);

        // Clean old entries
        let window_start = now - self.config.performance_window;
        tracker
            .query_latencies
            .retain(|(ts, _)| *ts >= window_start);
        tracker.query_timestamps.retain(|ts| *ts >= window_start);
    }

    /// Increment active connection count
    pub async fn connection_started(&self) {
        let mut tracker = self.performance_tracker.write().await;
        tracker.active_connections = tracker.active_connections.saturating_add(1);
    }

    /// Decrement active connection count
    pub async fn connection_ended(&self) {
        let mut tracker = self.performance_tracker.write().await;
        tracker.active_connections = tracker.active_connections.saturating_sub(1);
    }

    /// Increment active query count
    pub async fn query_started(&self) {
        let mut tracker = self.performance_tracker.write().await;
        tracker.active_queries = tracker.active_queries.saturating_add(1);
    }

    /// Decrement active query count
    pub async fn query_ended(&self) {
        let mut tracker = self.performance_tracker.write().await;
        tracker.active_queries = tracker.active_queries.saturating_sub(1);
    }

    /// Record query error
    pub async fn record_query_error(&self, message: String) {
        let mut tracker = self.error_tracker.write().await;
        let now = Instant::now();

        tracker.errors.push(ErrorEvent {
            timestamp: now,
            message: message.clone(),
            error_type: "query".to_string(),
        });

        tracker.query_failures.push(now);

        // Clean old entries
        let window_start = now - self.config.error_window;
        tracker.errors.retain(|e| e.timestamp >= window_start);
        tracker.query_failures.retain(|ts| *ts >= window_start);

        warn!("Query error recorded: {}", message);
    }

    /// Record update error
    pub async fn record_update_error(&self, message: String) {
        let mut tracker = self.error_tracker.write().await;
        let now = Instant::now();

        tracker.errors.push(ErrorEvent {
            timestamp: now,
            message: message.clone(),
            error_type: "update".to_string(),
        });

        tracker.update_failures.push(now);

        // Clean old entries
        let window_start = now - self.config.error_window;
        tracker.errors.retain(|e| e.timestamp >= window_start);
        tracker.update_failures.retain(|ts| *ts >= window_start);

        warn!("Update error recorded: {}", message);
    }

    /// Start background health monitoring
    pub fn start_monitoring(self: Arc<Self>) -> tokio::task::JoinHandle<()> {
        tokio::spawn(async move {
            let mut interval = tokio::time::interval(self.config.check_interval);

            loop {
                interval.tick().await;

                if let Err(e) = self.check_health().await {
                    warn!("Health check failed: {}", e);
                }
            }
        })
    }
}

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

    #[tokio::test]
    async fn test_health_monitor_creation() {
        let store = Arc::new(Store::new().unwrap());
        let monitor = StoreHealthMonitor::new(store);

        let health = monitor.check_health().await.unwrap();
        assert_eq!(health.status, HealthStatus::Degraded); // No data yet
        assert!(health.health_score <= 100);
    }

    #[tokio::test]
    async fn test_record_query() {
        let store = Arc::new(Store::new().unwrap());
        let monitor = StoreHealthMonitor::new(store);

        monitor.record_query(50.0).await;
        monitor.record_query(75.0).await;

        let health = monitor.check_health().await.unwrap();
        assert!(health.performance.avg_query_latency_ms > 0.0);
    }

    #[tokio::test]
    async fn test_record_error() {
        let store = Arc::new(Store::new().unwrap());
        let monitor = StoreHealthMonitor::new(store);

        monitor.record_query_error("Test error".to_string()).await;

        let health = monitor.check_health().await.unwrap();
        assert!(health.errors.errors_last_hour > 0);
    }

    #[tokio::test]
    async fn test_health_history() {
        let store = Arc::new(Store::new().unwrap());
        let monitor = StoreHealthMonitor::new(store);

        monitor.check_health().await.unwrap();
        monitor.check_health().await.unwrap();

        let history = monitor.get_health_history().await;
        assert_eq!(history.len(), 2);
    }
}