-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaxos.rs
217 lines (187 loc) · 5.82 KB
/
paxos.rs
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
use hydroflow_plus::*;
use stageleft::*;
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Clone)]
struct Ballot { // Note: Important that num comes before id, since Ord is defined lexicographically
num: u32,
id: u32,
}
#[derive(Serialize, Deserialize, Clone)]
struct P1a {
ballot: Ballot,
}
#[derive(Serialize, Deserialize, Clone)]
struct LogValue {
ballot: Ballot,
slot: u32,
value: u32,
}
#[derive(Serialize, Deserialize, Clone)]
struct P1b {
ballot: Ballot,
max_ballot: Ballot,
accepted: Vec<LogValue>,
}
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone)]
struct P2a {
ballot: Ballot,
slot: u32,
value: u32,
}
#[derive(Serialize, Deserialize, PartialEq, Eq, Clone)]
struct P2b {
ballot: Ballot,
max_ballot: Ballot,
slot: u32,
value: u32,
}
pub fn paxos<'a, D: Deploy<'a>>(
flow: &'a FlowBuilder<'a, D>,
process_spec: &impl ProcessSpec<'a, D>,
cluster_spec: &impl ClusterSpec<'a, D>
) {
let leader = flow.process(process_spec);
let acceptors = flow.cluster(cluster_spec);
/*
Proposer state
*/
//TODO: Find leader ID programmatically
let p_id = leader
.source_iter(q!([0]))
.all_ticks();
let p_ballot_nums = leader
.source_iter(q!([1]))
.all_ticks();
let p_ballot = p_ballot_nums
.reduce(q!(|a, b| if b > *a {
*a = b
}))
.cross_product(&p_id)
.map(q!(|(num, id)| Ballot {id, num}));
/*
Proposer client data input
*/
let data_input: Stream<'_, P2a, stream::Windowed, <D as Deploy>::Process> = leader
.source_iter(q!(0..10))
.cross_product(&p_ballot)
.map(q!(|(val, ballot)| P2a {
ballot: ballot,
slot: val,
value: val,
}));
let a_p2a = data_input
.broadcast_bincode(&acceptors);
/*
Proposer p1a
*/
/*
Acceptor state
*/
let a_ballots = acceptors
.source_iter(q!([Ballot{id: 0, num: 0}]))
.all_ticks();
let a_max_ballot = a_ballots
.reduce(q!(|a, b| if b > *a {
*a = b
}));
/*
Acceptor p1b
*/
/*
Acceptor p2a
*/
let a_p2a_and_max_ballot = a_p2a
.tick_batch()
.cross_product(&a_max_ballot);
// TODO: This log includes overwritten values
let a_log = a_p2a_and_max_ballot
.filter_map(q!(|(p2a, max_ballot): (P2a, Ballot)| if p2a.ballot >= max_ballot {
Some(LogValue {
ballot: p2a.ballot,
slot: p2a.slot,
value: p2a.value,
})
} else {
None
}));
let p_p2b = a_p2a_and_max_ballot
.map(q!(|(p2a, max_ballot): (P2a, Ballot)| P2b {
ballot: p2a.ballot,
max_ballot: max_ballot,
slot: p2a.slot,
value: p2a.value,
}))
.send_bincode_tagged(&leader);
/*
Proposer p2b
*/
// TODO: garbage collect p2bs where all replied & those whose ballots are no longer relevant
let p_p2b_relevant = p_p2b
.all_ticks()
.cross_product(&p_ballot)
.filter(q!(|((a_id, p2b), curr_ballot)| p2b.ballot == *curr_ballot));
let p_p2b_count = p_p2b_relevant
.map(q!(|((a_id, p2b), _curr_ballot)| ((p2b.slot, p2b.value), a_id))) // reformat so we can group by slot
.fold_keyed(q!(|| 0), q!(|accum, a_id| *accum += 1)) // TODO: count num unique a_ids
.filter_map(q!(|((slot, value), cnt)| if cnt >= 2 { // TODO: define f
Some((slot, value))
} else {
None
}))
.for_each(q!(|(slot, value): (u32, u32)| println!("Committed {}: {}", slot, value)));
}
use hydroflow_plus::util::cli::HydroCLI;
use hydroflow_plus_cli_integration::{CLIRuntime, HydroflowPlusMeta};
#[stageleft::entry]
pub fn paxos_runtime<'a>(
flow: &'a FlowBuilder<'a, CLIRuntime>,
cli: RuntimeData<&'a HydroCLI<HydroflowPlusMeta>>,
) -> impl Quoted<'a, Hydroflow<'a>> {
let _ = paxos(flow, &cli, &cli);
flow.build(q!(cli.meta.subgraph_id))
}
#[stageleft::runtime]
#[cfg(test)]
mod tests {
use hydro_deploy::{Deployment, HydroflowCrate};
use hydroflow_plus::futures::StreamExt;
use hydroflow_plus_cli_integration::{DeployCrateWrapper, DeployProcessSpec, DeployClusterSpec};
use std::cell::RefCell;
#[tokio::test]
async fn paxos() {
let bin = "paxos";
let profile = "dev";
let deployment = RefCell::new(Deployment::new());
let localhost = deployment.borrow_mut().Localhost();
let flow = hydroflow_plus::FlowBuilder::new();
let second_process = super::paxos(
&flow,
&DeployProcessSpec::new(|| {
let mut deployment = deployment.borrow_mut();
deployment.add_service(
HydroflowCrate::new(".", localhost.clone())
.bin(bin)
.profile(profile)
.display_name("leader"),
)
}),
&DeployClusterSpec::new(|| {
(0..=2) // TODO: define f
.map(|idx| {
let mut deployment = deployment.borrow_mut();
deployment.add_service(
HydroflowCrate::new(".", localhost.clone())
.bin(bin)
.profile(profile)
.display_name(format!("acceptor/{}", idx)),
)
})
.collect()
}),
);
let mut deployment = deployment.into_inner();
deployment.deploy().await.unwrap();
deployment.start().await.unwrap();
tokio::signal::ctrl_c().await.unwrap()
}
}