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
//! How the circuit of a pipeline is laid out across hosts. The controller of the pipeline and
//! the engine that runs the circuit share this layout.
use std::{
collections::HashSet,
error::Error as StdError,
fmt::{self, Debug, Display, Error as FmtError, Formatter},
iter::empty,
net::SocketAddr,
ops::Range,
};
use itertools::Either;
/// A host for some workers in the [`Layout`] for a multi-host circuit.
#[allow(clippy::manual_non_exhaustive)]
#[derive(Clone, serde::Serialize, serde::Deserialize)]
pub struct Host {
/// The IP address and TCP port on which the host listens and to which the
/// other hosts connect.
pub address: SocketAddr,
/// The worker thread IDs implemented on this host. Worker thread IDs start
/// with 0 in the first host and increase sequentially from there. A host
/// has `workers.len()` workers.
pub workers: Range<usize>,
/// Prevents `Host` and `Layout::Multihost` from being instantiated without
/// using the constructor (which checks the invariants).
_private: (),
}
impl Debug for Host {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("Host")
.field("address", &self.address)
.field("workers", &self.workers)
.finish()
}
}
/// How a circuit is laid out across one or more machines.
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub enum Layout {
/// A layout whose workers run on a single host.
Solo {
/// The number of worker threads.
n_workers: usize,
},
/// A layout across multiple machines.
Multihost {
/// Each host in the layout. There should be two or more, each with a
/// unique network address.
hosts: Vec<Host>,
/// The index within `hosts` of the current host.
local_host_idx: usize,
},
}
impl Layout {
/// Returns a new solo layout with `n_workers` worker threads.
pub fn new_solo(n_workers: usize) -> Layout {
assert_ne!(n_workers, 0);
Layout::Solo { n_workers }
}
/// Returns a new multihost layout with as many hosts as specified in
/// `params`. Each tuple in `params` specifies a host's unique network
/// address and the number of workers to run on that host. `local_address`
/// must be one of the addresses in `params`.
///
/// To execute such a multihost circuit, one must create a `Runtime` for it
/// on every host in `params`, passing the same `params` in each case. Each
/// host must pass its own `local_address`. The `Runtime` on each host
/// listens on its own address and connects to all the other addresses.
pub fn new_multihost(
params: &[(SocketAddr, usize)],
local_address: SocketAddr,
) -> Result<Layout, LayoutError> {
// Check that the addresses are unique.
let mut uniq = HashSet::new();
if let Some((duplicate, _)) = params.iter().find(|(address, _)| !uniq.insert(address)) {
return Err(LayoutError::DuplicateAddress(*duplicate));
}
// Find `local_address` in `params`.
let local_host_idx = params
.iter()
.position(|(address, _)| *address == local_address)
.ok_or(LayoutError::NoSuchAddress(local_address))?;
if params.len() == 1 {
Ok(Self::new_solo(params[0].1))
} else {
let mut hosts = Vec::with_capacity(params.len());
let mut total_workers = 0;
for (address, n_workers) in params {
assert_ne!(*n_workers, 0);
hosts.push(Host {
address: *address,
workers: total_workers..total_workers + *n_workers,
_private: (),
});
total_workers += *n_workers;
}
Ok(Layout::Multihost {
hosts,
local_host_idx,
})
}
}
/// Returns the range of IDs for the workers on the local machine.
pub fn local_workers(&self) -> Range<usize> {
match self {
Self::Solo { n_workers } => 0..*n_workers,
Self::Multihost {
hosts,
local_host_idx,
..
} => hosts[*local_host_idx].workers.clone(),
}
}
/// Returns an iterator over `Host`s in this layout other than this one. If
/// this is a single-host layout, this will be an empty iterator.
pub fn other_hosts(&self) -> impl Iterator<Item = &Host> {
match self {
Self::Solo { .. } => Either::Left(empty()),
Self::Multihost {
hosts,
local_host_idx,
} => Either::Right(
hosts
.iter()
.enumerate()
.filter_map(|(i, host)| (i != *local_host_idx).then_some(host)),
),
}
}
/// Returns an iterator across the ranges of workers on all the hosts in
/// this layout. A single-host layout will have a single range with all the
/// workers, a two-host layout will have two ranges, and so on.
pub fn all_hosts(&self) -> impl Iterator<Item = Range<usize>> {
match self {
Self::Solo { n_workers } => Either::Left(std::iter::once(0..*n_workers)),
Self::Multihost { hosts, .. } => {
Either::Right(hosts.iter().map(|host| host.workers.clone()))
}
}
}
/// Returns the network address for the current machine, or `None` if this
/// is a solo layout.
pub fn local_address(&self) -> Option<SocketAddr> {
match self {
Self::Solo { .. } => None,
Self::Multihost {
hosts,
local_host_idx,
..
} => Some(hosts[*local_host_idx].address),
}
}
/// Returns the total number of worker threads in this layout.
pub fn n_workers(&self) -> usize {
match self {
Self::Solo { n_workers } => *n_workers,
Self::Multihost { hosts, .. } => hosts.iter().map(|host| host.workers.len()).sum(),
}
}
pub fn is_multihost(&self) -> bool {
matches!(self, Self::Multihost { .. })
}
pub fn is_solo(&self) -> bool {
matches!(self, Self::Solo { .. })
}
pub fn n_hosts(&self) -> usize {
match self {
Layout::Solo { .. } => 1,
Layout::Multihost { hosts, .. } => hosts.len(),
}
}
pub fn local_host_idx(&self) -> usize {
match self {
Layout::Solo { .. } => 0,
Layout::Multihost { local_host_idx, .. } => *local_host_idx,
}
}
/// Returns the host index corresponding to the given `worker_idx`.
pub fn worker_idx_to_host_idx(&self, worker_idx: usize) -> Option<usize> {
self.all_hosts()
.position(|workers| workers.contains(&worker_idx))
}
/// Returns [`HostInfo`] for this host when running in multihost mode,
/// or `None` for solo pipelines.
///
/// [`HostInfo`]: crate::checkpoint::HostInfo
pub fn host_info(&self) -> Option<crate::checkpoint::HostInfo> {
match self {
Layout::Solo { .. } => None,
Layout::Multihost {
hosts,
local_host_idx,
} => Some(crate::checkpoint::HostInfo {
host_idx: *local_host_idx,
n_hosts: hosts.len(),
}),
}
}
}
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)]
pub enum LayoutError {
/// The socket address passed to `new_multihost()` isn't in the list of
/// hosts.
NoSuchAddress(SocketAddr),
/// The list of socket addresses passed to `new_multihost()` contains a
/// duplicate.
DuplicateAddress(SocketAddr),
}
impl Display for LayoutError {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError> {
match self {
Self::NoSuchAddress(address) => write!(f, "address {address} not in list of hosts"),
Self::DuplicateAddress(address) => {
write!(f, "duplicate address {address} in list of hosts")
}
}
}
}
impl StdError for LayoutError {}