Skip to main content

quantrs2_device/quantum_network/network_optimization/
roundrobinbalancer_traits.rs

1//! # RoundRobinBalancer - Trait Implementations
2//!
3//! This module contains trait implementations for `RoundRobinBalancer`.
4//!
5//! ## Implemented Traits
6//!
7//! - `LoadBalancer`
8//! - `LoadBalancer`
9//!
10//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
11
12use async_trait::async_trait;
13use std::collections::HashMap;
14use std::time::Duration;
15use uuid::Uuid;
16
17use super::type_definitions::*;
18use crate::quantum_network::distributed_protocols::{NodeId, NodeInfo, PerformanceMetrics};
19
20#[async_trait]
21impl LoadBalancer for RoundRobinBalancer {
22    async fn select_node(
23        &self,
24        available_nodes: &[NodeInfo],
25        _requirements: &ResourceRequirements,
26    ) -> Result<NodeId> {
27        if available_nodes.is_empty() {
28            return Err(NetworkOptimizationError::TopologyOptimizationFailed(
29                "No available nodes".to_string(),
30            ));
31        }
32        let index = self
33            .current_index
34            .fetch_add(1, std::sync::atomic::Ordering::Relaxed)
35            % available_nodes.len();
36        Ok(available_nodes[index].node_id.clone())
37    }
38    async fn update_node_metrics(
39        &self,
40        _node_id: &NodeId,
41        _metrics: &PerformanceMetrics,
42    ) -> Result<()> {
43        Ok(())
44    }
45    fn get_balancer_metrics(&self) -> LoadBalancerMetrics {
46        LoadBalancerMetrics {
47            total_decisions: 0,
48            average_decision_time: Duration::from_millis(1),
49            prediction_accuracy: 1.0,
50            load_distribution_variance: 0.0,
51        }
52    }
53}
54
55#[async_trait]
56impl crate::quantum_network::distributed_protocols::LoadBalancer for RoundRobinBalancer {
57    fn select_nodes(
58        &self,
59        partitions: &[crate::quantum_network::distributed_protocols::CircuitPartition],
60        available_nodes: &HashMap<NodeId, crate::quantum_network::distributed_protocols::NodeInfo>,
61        _requirements: &crate::quantum_network::distributed_protocols::ExecutionRequirements,
62    ) -> std::result::Result<
63        HashMap<Uuid, NodeId>,
64        crate::quantum_network::distributed_protocols::DistributedComputationError,
65    > {
66        let mut allocation = HashMap::new();
67        let nodes: Vec<_> = available_nodes.keys().cloned().collect();
68        if nodes.is_empty() {
69            return Err(
70                crate::quantum_network::distributed_protocols::DistributedComputationError::ResourceAllocation(
71                    "No available nodes".to_string(),
72                ),
73            );
74        }
75        for partition in partitions {
76            let index = self
77                .current_index
78                .fetch_add(1, std::sync::atomic::Ordering::Relaxed)
79                % nodes.len();
80            allocation.insert(partition.partition_id, nodes[index].clone());
81        }
82        Ok(allocation)
83    }
84    fn rebalance_load(
85        &self,
86        _current_allocation: &HashMap<Uuid, NodeId>,
87        _nodes: &HashMap<NodeId, crate::quantum_network::distributed_protocols::NodeInfo>,
88    ) -> Option<HashMap<Uuid, NodeId>> {
89        None
90    }
91    fn predict_execution_time(
92        &self,
93        partition: &crate::quantum_network::distributed_protocols::CircuitPartition,
94        _node: &crate::quantum_network::distributed_protocols::NodeInfo,
95    ) -> Duration {
96        partition.estimated_execution_time
97    }
98    async fn select_node(
99        &self,
100        available_nodes: &[crate::quantum_network::distributed_protocols::NodeInfo],
101        _requirements: &crate::quantum_network::distributed_protocols::ResourceRequirements,
102    ) -> std::result::Result<
103        NodeId,
104        crate::quantum_network::distributed_protocols::DistributedComputationError,
105    > {
106        if available_nodes.is_empty() {
107            return Err(
108                crate::quantum_network::distributed_protocols::DistributedComputationError::ResourceAllocation(
109                    "No available nodes".to_string(),
110                ),
111            );
112        }
113        let index = self
114            .current_index
115            .fetch_add(1, std::sync::atomic::Ordering::Relaxed)
116            % available_nodes.len();
117        Ok(available_nodes[index].node_id.clone())
118    }
119    async fn update_node_metrics(
120        &self,
121        _node_id: &NodeId,
122        _metrics: &crate::quantum_network::distributed_protocols::PerformanceMetrics,
123    ) -> std::result::Result<
124        (),
125        crate::quantum_network::distributed_protocols::DistributedComputationError,
126    > {
127        Ok(())
128    }
129    fn get_balancer_metrics(
130        &self,
131    ) -> crate::quantum_network::distributed_protocols::LoadBalancerMetrics {
132        crate::quantum_network::distributed_protocols::LoadBalancerMetrics {
133            total_decisions: 0,
134            average_decision_time: Duration::from_millis(1),
135            prediction_accuracy: 1.0,
136            load_distribution_variance: 0.0,
137            total_requests: 0,
138            successful_allocations: 0,
139            failed_allocations: 0,
140            average_response_time: Duration::from_millis(0),
141            node_utilization: HashMap::new(),
142        }
143    }
144}