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
use std::rc::Rc;
use std::cell::RefCell;
use std::collections::{VecDeque, HashMap};
use std::sync::mpsc::{Sender, Receiver};
use bytes::arc::Bytes;
use crate::networking::MessageHeader;
use crate::{Allocate, Message, Data, Push, Pull};
use crate::allocator::{AllocateBuilder, Event};
use crate::allocator::canary::Canary;
use super::bytes_exchange::{BytesPull, SendEndpoint, MergeQueue};
use super::push_pull::{Pusher, Puller};
pub struct ProcessBuilder {
index: usize,
peers: usize,
pushers: Vec<Receiver<MergeQueue>>,
pullers: Vec<Sender<MergeQueue>>,
}
impl ProcessBuilder {
pub fn new_vector(count: usize) -> Vec<ProcessBuilder> {
let (pullers_vec, pushers_vec) = crate::promise_futures(count, count);
pushers_vec
.into_iter()
.zip(pullers_vec)
.enumerate()
.map(|(index, (pushers, pullers))|
ProcessBuilder {
index,
peers: count,
pushers,
pullers,
}
)
.collect()
}
pub fn build(self) -> ProcessAllocator {
let mut recvs = Vec::with_capacity(self.peers);
for puller in self.pullers.into_iter() {
let buzzer = crate::buzzer::Buzzer::new();
let queue = MergeQueue::new(buzzer);
puller.send(queue.clone()).expect("Failed to send MergeQueue");
recvs.push(queue.clone());
}
let mut sends = Vec::with_capacity(self.peers);
for pusher in self.pushers.into_iter() {
let queue = pusher.recv().expect("Failed to receive MergeQueue");
let sendpoint = SendEndpoint::new(queue);
sends.push(Rc::new(RefCell::new(sendpoint)));
}
ProcessAllocator {
index: self.index,
peers: self.peers,
events: Rc::new(RefCell::new(VecDeque::new())),
canaries: Rc::new(RefCell::new(Vec::new())),
staged: Vec::new(),
sends,
recvs,
to_local: HashMap::new(),
}
}
}
impl AllocateBuilder for ProcessBuilder {
type Allocator = ProcessAllocator;
fn build(self) -> Self::Allocator {
self.build()
}
}
pub struct ProcessAllocator {
index: usize,
peers: usize,
events: Rc<RefCell<VecDeque<(usize, Event)>>>,
canaries: Rc<RefCell<Vec<usize>>>,
staged: Vec<Bytes>,
sends: Vec<Rc<RefCell<SendEndpoint<MergeQueue>>>>,
recvs: Vec<MergeQueue>,
to_local: HashMap<usize, Rc<RefCell<VecDeque<Bytes>>>>,
}
impl Allocate for ProcessAllocator {
fn index(&self) -> usize { self.index }
fn peers(&self) -> usize { self.peers }
fn allocate<T: Data>(&mut self, identifier: usize) -> (Vec<Box<dyn Push<Message<T>>>>, Box<dyn Pull<Message<T>>>) {
let mut pushes = Vec::<Box<dyn Push<Message<T>>>>::new();
for target_index in 0 .. self.peers() {
let header = MessageHeader {
channel: identifier,
source: self.index,
target: target_index,
length: 0,
seqno: 0,
};
pushes.push(Box::new(Pusher::new(header, self.sends[target_index].clone())));
}
let channel =
self.to_local
.entry(identifier)
.or_insert_with(|| Rc::new(RefCell::new(VecDeque::new())))
.clone();
use crate::allocator::counters::Puller as CountPuller;
let canary = Canary::new(identifier, self.canaries.clone());
let puller = Box::new(CountPuller::new(Puller::new(channel, canary), identifier, self.events().clone()));
(pushes, puller)
}
#[inline(never)]
fn receive(&mut self) {
let mut canaries = self.canaries.borrow_mut();
for dropped_channel in canaries.drain(..) {
let dropped =
self.to_local
.remove(&dropped_channel)
.expect("non-existent channel dropped");
assert!(dropped.borrow().is_empty());
}
std::mem::drop(canaries);
let mut events = self.events.borrow_mut();
for recv in self.recvs.iter_mut() {
recv.drain_into(&mut self.staged);
}
for mut bytes in self.staged.drain(..) {
while bytes.len() > 0 {
if let Some(header) = MessageHeader::try_read(&mut bytes[..]) {
let mut peel = bytes.extract_to(header.required_bytes());
let _ = peel.extract_to(40);
events.push_back((header.channel, Event::Pushed(1)));
self.to_local
.entry(header.channel)
.or_insert_with(|| Rc::new(RefCell::new(VecDeque::new())))
.borrow_mut()
.push_back(peel);
}
else {
println!("failed to read full header!");
}
}
}
}
fn release(&mut self) {
for send in self.sends.iter_mut() {
send.borrow_mut().publish();
}
}
fn events(&self) -> &Rc<RefCell<VecDeque<(usize, Event)>>> {
&self.events
}
fn await_events(&self, duration: Option<std::time::Duration>) {
if self.events.borrow().is_empty() {
if let Some(duration) = duration {
std::thread::park_timeout(duration);
}
else {
std::thread::park();
}
}
}
}