|
| 1 | +use kanal::{AsyncReceiver, AsyncSender}; |
| 2 | +use monoio::io::stream::Stream; |
| 3 | +use std::{collections::VecDeque, fmt::Debug, sync::Arc}; |
| 4 | + |
| 5 | +pub fn add(left: u64, right: u64) -> u64 { |
| 6 | + left + right |
| 7 | +} |
| 8 | + |
| 9 | +struct IncomingNetworkMessage; |
| 10 | +struct OutgoingNetworkMessage; |
| 11 | + |
| 12 | +enum Action {} |
| 13 | + |
| 14 | +#[repr(i32)] |
| 15 | +#[derive(PartialEq, Eq, PartialOrd, Ord)] |
| 16 | +enum MessagePriority { |
| 17 | + Low = -1, |
| 18 | + Normal = 0, |
| 19 | + High = 1, |
| 20 | +} |
| 21 | + |
| 22 | +impl Default for MessagePriority { |
| 23 | + fn default() -> Self { |
| 24 | + MessagePriority::Normal |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +/// Queues |
| 29 | +struct JobQueues { |
| 30 | + /// Network jobs are prioritized over other jobs. They're cheap to execute. |
| 31 | + net_jobs: VecDeque<Box<dyn Job>>, |
| 32 | + garbling_jobs: VecDeque<Box<dyn Job>>, |
| 33 | + other_jobs: VecDeque<Box<dyn Job>>, |
| 34 | +} |
| 35 | + |
| 36 | +trait NetworkSendingClient { |
| 37 | + async fn send_and_wait_for_ack( |
| 38 | + &self, |
| 39 | + to: PeerId, |
| 40 | + priority: Option<MessagePriority>, |
| 41 | + message: OutgoingNetworkMessage, |
| 42 | + ) -> Result<(), Error>; |
| 43 | +} |
| 44 | + |
| 45 | +trait NetworkReceivingClient { |
| 46 | + type Stream: Stream<Item = (IncomingNetworkMessage,)>; |
| 47 | + async fn receive(&self) -> Result<Option, Error>; |
| 48 | +} |
| 49 | + |
| 50 | +trait Acker { |
| 51 | + async fn ack(&self) -> Result<(), Error>; |
| 52 | +} |
| 53 | + |
| 54 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] |
| 55 | +struct Utilisation { |
| 56 | + /// Memory usage in bytes |
| 57 | + memory: usize, |
| 58 | + /// Threads used for job execution |
| 59 | + threads: usize, |
| 60 | +} |
| 61 | + |
| 62 | +impl Utilisation { |
| 63 | + fn checked_sub(&self, other: &Utilisation) -> Option<Utilisation> { |
| 64 | + if self.memory < other.memory || self.threads < other.threads { |
| 65 | + None |
| 66 | + } else { |
| 67 | + Some(Utilisation { |
| 68 | + memory: self.memory - other.memory, |
| 69 | + threads: self.threads - other.threads, |
| 70 | + }) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + fn checked_add(&self, other: &Utilisation, max: &Utilisation) -> Option<Utilisation> { |
| 75 | + if self.memory + other.memory > max.memory || self.threads + other.threads > max.threads { |
| 76 | + None |
| 77 | + } else { |
| 78 | + Some(Utilisation { |
| 79 | + memory: self.memory + other.memory, |
| 80 | + threads: self.threads + other.threads, |
| 81 | + }) |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +pub struct JobScheduler { |
| 87 | + /// Immutable context shared with all jobs. |
| 88 | + /// |
| 89 | + /// We use an Arc as jobs are sent to individual threads |
| 90 | + context: Arc<JobContext>, |
| 91 | + max_util: Utilisation, |
| 92 | + cur_util: Utilisation, |
| 93 | + |
| 94 | + workers: Vec<SchedulerToWorker>, |
| 95 | +} |
| 96 | + |
| 97 | +struct SchedulerToWorker { |
| 98 | + to: AsyncSender<Job>, |
| 99 | +} |
| 100 | + |
| 101 | +impl JobScheduler { |
| 102 | + fn available_resources(&self) -> Utilisation { |
| 103 | + self.max_util |
| 104 | + .checked_sub(&self.cur_util) |
| 105 | + .expect("Utilisation underflow") |
| 106 | + } |
| 107 | + |
| 108 | + fn release_resources(&mut self, util: Utilisation) { |
| 109 | + self.cur_util = self |
| 110 | + .cur_util |
| 111 | + .checked_sub(&util) |
| 112 | + .expect("Utilisation overflow"); |
| 113 | + } |
| 114 | + |
| 115 | + fn use_resources(&mut self, util: Utilisation) { |
| 116 | + self.cur_util = self |
| 117 | + .cur_util |
| 118 | + .checked_add(&util, &self.max_util) |
| 119 | + .expect("Utilisation overflow"); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +struct JobContext { |
| 124 | + net_in: AsyncReceiver<IncomingNetworkMessage>, |
| 125 | + net_out: AsyncSender<OutgoingNetworkMessage>, |
| 126 | + |
| 127 | + actions_in: AsyncReceiver<Action>, |
| 128 | + action_results_out: AsyncSender<ActionResult>, |
| 129 | +} |
| 130 | + |
| 131 | +// SMExecutors send Actions to the JobScheduler |
| 132 | +// JobScheduler transforms each Action into a Job and queues it for execution |
| 133 | + |
| 134 | +trait Job { |
| 135 | + type Output; |
| 136 | + |
| 137 | + /// Returns an estimated memory requirement of the job in bytes. |
| 138 | + /// |
| 139 | + /// This helps the JobScheduler budget memory usage. |
| 140 | + fn memory_requirement(&self) -> Option<usize>; |
| 141 | + |
| 142 | + /// Executes the job with a global job context. |
| 143 | + async fn execute(&self, context: &JobContext) -> Result<Self::Output, Box<dyn Debug>>; |
| 144 | +} |
| 145 | + |
| 146 | +#[cfg(test)] |
| 147 | +mod tests { |
| 148 | + use super::*; |
| 149 | + |
| 150 | + #[test] |
| 151 | + fn it_works() { |
| 152 | + let result = add(2, 2); |
| 153 | + assert_eq!(result, 4); |
| 154 | + } |
| 155 | +} |
0 commit comments