-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmanager.rs
574 lines (470 loc) · 17.8 KB
/
manager.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
// Copyright (c) Meta Platforms, Inc. and affiliates.
// All rights reserved.
//
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree.
use core::net::SocketAddr;
use std::collections::HashMap;
use std::collections::HashSet;
use std::sync::Arc;
use std::time::Duration;
use anyhow::Result;
use gethostname::gethostname;
use tokio::sync::broadcast;
use tokio::sync::Mutex;
use tokio::task::JoinSet;
use tokio::time::sleep;
use tonic::transport::server::TcpIncoming;
use tonic::transport::Server;
use tonic::transport::{Channel, Endpoint};
use tonic::{Request, Response, Status};
use crate::torchftpb::lighthouse_service_client::LighthouseServiceClient;
use crate::torchftpb::manager_service_client::ManagerServiceClient;
use crate::torchftpb::{
manager_service_server::{ManagerService, ManagerServiceServer},
CheckpointAddressRequest, CheckpointAddressResponse, KillRequest, KillResponse,
LighthouseHeartbeatRequest, LighthouseQuorumRequest, ManagerQuorumRequest,
ManagerQuorumResponse, Quorum, QuorumMember, ShouldCommitRequest, ShouldCommitResponse,
};
#[cfg(not(test))]
use log::{info, warn};
#[cfg(test)]
use std::{println as info, println as warn};
struct RoomState {
channel: broadcast::Sender<Quorum>,
participants: HashSet<i64>,
}
struct ManagerState {
checkpoint_servers: HashMap<i64, String>,
rooms: HashMap<String, RoomState>,
should_commit_channel: broadcast::Sender<bool>,
should_commit_failures: HashSet<i64>,
should_commit_count: HashSet<i64>,
}
pub struct Manager {
replica_id: String,
lighthouse_addr: String,
address: String,
store_address: String,
world_size: u64,
state: Mutex<ManagerState>,
listener: Mutex<Option<tokio::net::TcpListener>>,
local_addr: SocketAddr,
}
pub async fn manager_client_new(
addr: String,
timeout: Duration,
) -> Result<ManagerServiceClient<Channel>> {
// TODO add retries + backoff so other nodes can start before the rank0 comes up
info!("ManagerClient: establishing connection to {}", &addr);
let conn = Endpoint::new(addr.clone())?
.timeout(timeout)
.connect_timeout(Duration::from_secs(60))
.connect()
.await?;
Ok(ManagerServiceClient::new(conn))
}
impl Manager {
pub async fn new(
replica_id: String,
lighthouse_addr: String,
address: String,
bind: String,
store_addr: String,
world_size: u64,
) -> Result<Arc<Self>> {
let listener = tokio::net::TcpListener::bind(&bind).await?;
let (should_commit_tx, _) = broadcast::channel(16);
Ok(Arc::new(Self {
replica_id: replica_id,
lighthouse_addr: lighthouse_addr,
address: address,
store_address: store_addr,
world_size: world_size,
state: Mutex::new(ManagerState {
checkpoint_servers: HashMap::new(),
rooms: HashMap::new(),
should_commit_channel: should_commit_tx,
should_commit_count: HashSet::new(),
should_commit_failures: HashSet::new(),
}),
local_addr: listener.local_addr()?,
listener: Mutex::new(Some(listener)),
}))
}
pub async fn run(self: Arc<Self>) -> Result<()> {
let mut set = JoinSet::new();
set.spawn(self.clone()._run_heartbeat());
set.spawn(self.clone()._run_grpc());
while let Some(res) = set.join_next().await {
res??;
}
Ok(())
}
pub fn address(&self) -> String {
format!(
"http://{}:{}",
gethostname().into_string().unwrap(),
self.local_addr.port()
)
}
async fn _run_grpc(self: Arc<Self>) -> Result<()> {
info!(
"Manager {} listening on {}",
self.replica_id,
self.address()
);
let listener = self.listener.lock().await.take().unwrap();
let incoming =
TcpIncoming::from_listener(listener, true, None).map_err(|e| anyhow::anyhow!(e))?;
Server::builder()
.add_service(ManagerServiceServer::new(self))
.serve_with_incoming(incoming)
.await
.map_err(|e| e.into())
}
async fn _run_heartbeat(self: Arc<Self>) -> Result<()> {
let mut client = self.lighthouse_client_new().await?;
loop {
let request = tonic::Request::new(LighthouseHeartbeatRequest {
replica_id: self.replica_id.clone(),
});
let _response = client.heartbeat(request).await;
sleep(Duration::from_millis(100)).await;
}
}
async fn lighthouse_client_new(&self) -> Result<LighthouseServiceClient<Channel>> {
info!(
"Manager: connecting to lighthouse at {}",
&self.lighthouse_addr
);
let conn = Endpoint::new(self.lighthouse_addr.clone())?
.connect_timeout(Duration::from_secs(60))
.connect()
.await?;
Ok(LighthouseServiceClient::new(conn))
}
}
#[tonic::async_trait]
impl ManagerService for Arc<Manager> {
async fn quorum(
&self,
request: Request<ManagerQuorumRequest>,
) -> Result<Response<ManagerQuorumResponse>, Status> {
let req = request.get_ref();
let rank = req.rank;
let room_id = &req.room_id;
info!("{}: got quorum request for rank {}", room_id, rank);
let mut rx = {
let mut state = self.state.lock().await;
// save checkpoint server info for healing process
// TODO: make separate call to set?
state
.checkpoint_servers
.insert(req.rank, req.checkpoint_server_addr.clone());
if !state.rooms.contains_key(room_id) {
let (tx, _) = broadcast::channel(16);
state.rooms.insert(
room_id.clone(),
RoomState {
channel: tx,
participants: HashSet::new(),
},
);
}
let room = state.rooms.get_mut(room_id).unwrap();
// TODO check step
room.participants.insert(rank);
let rx = room.channel.subscribe();
if room.participants.len() as u64 >= self.world_size {
room.participants.clear();
info!("{}: all workers joined -- starting quorum", room_id);
// TODO: don't hold the lock during quorum
let mut client = self
.lighthouse_client_new()
.await
.map_err(|e| Status::from_error(e.into()))?;
let mut lighthouse_request = tonic::Request::new(LighthouseQuorumRequest {
room_id: room_id.clone(),
requester: Some(QuorumMember {
replica_id: self.replica_id.clone(),
address: self.address.clone(),
store_address: self.store_address.clone(),
step: req.step,
world_size: self.world_size,
}),
});
// propagate timeout from request to lighthouse
let timeout = request
.metadata()
.get("grpc-timeout")
.ok_or_else(|| Status::internal("grpc-timeout not set"))?;
lighthouse_request
.metadata_mut()
.insert("grpc-timeout", timeout.clone());
let response = client.quorum(lighthouse_request).await.unwrap();
let resp = response.into_inner();
info!("{}: got lighthouse quorum {:?}", room_id, resp);
room.channel
.send(
resp.quorum
.ok_or_else(|| Status::internal("missing quorum"))?,
)
.map_err(|e| Status::from_error(e.into()))?;
}
rx
};
let quorum = rx
.recv()
.await
.map_err(|e| Status::internal(e.to_string()))?;
let mut participants = quorum.participants.clone();
participants.sort_by(|a, b| a.replica_id.cmp(&b.replica_id));
let mut replica_rank = 10000000000;
for (i, p) in participants.iter().enumerate() {
if p.replica_id == self.replica_id {
replica_rank = i;
break;
}
}
let max_step = participants.iter().map(|p| p.step).max().unwrap();
let max_participants: Vec<&QuorumMember> =
participants.iter().filter(|p| p.step == max_step).collect();
let primary = max_participants[rank as usize % max_participants.len()];
let mut max_rank = None;
for (i, p) in max_participants.iter().enumerate() {
if p.replica_id == self.replica_id {
max_rank = Some(i as i64);
break;
}
}
// Decide whether we should be healing:
// 1. if we're not at the max step
// 2. if everyone is at the first step and we're not the primary
let heal = max_step != req.step || max_step == 0 && primary.replica_id != self.replica_id;
if heal {
info!(
"{}: healing is required step={}, max_step={}",
room_id, req.step, max_step
);
}
let reply = ManagerQuorumResponse {
quorum_id: quorum.quorum_id,
// address is used for looking up the checkpoint server address.
address: primary.address.clone(),
store_address: primary.store_address.clone(),
max_step: max_step,
max_rank: max_rank,
max_world_size: max_participants.len() as i64,
replica_rank: replica_rank as i64,
replica_world_size: participants.len() as i64,
heal: heal,
};
info!("{}: returning quorum for rank {}", room_id, rank);
Ok(Response::new(reply))
}
async fn checkpoint_address(
&self,
request: Request<CheckpointAddressRequest>,
) -> Result<Response<CheckpointAddressResponse>, Status> {
let state = self.state.lock().await;
let req = request.into_inner();
let address = state
.checkpoint_servers
.get(&req.rank)
.ok_or_else(|| Status::invalid_argument("rank not found"))?;
let reply = CheckpointAddressResponse {
checkpoint_server_address: address.clone(),
};
Ok(Response::new(reply))
}
async fn should_commit(
&self,
request: Request<ShouldCommitRequest>,
) -> Result<Response<ShouldCommitResponse>, Status> {
let req = request.into_inner();
let rank = req.rank;
info!(
"should_commit request from {} should_commit={}",
rank, req.should_commit
);
// TODO: check step count
let mut rx = {
let mut state = self.state.lock().await;
if !req.should_commit {
state.should_commit_failures.insert(rank);
}
state.should_commit_count.insert(rank);
let rx = state.should_commit_channel.subscribe();
if state.should_commit_count.len() == self.world_size as usize {
let decision = state.should_commit_failures.len() == 0;
info!("should_commit completed should_commit={}", decision);
state
.should_commit_channel
.send(decision)
.map_err(|e| Status::from_error(e.into()))?;
// reset state
state.should_commit_count.clear();
state.should_commit_failures.clear();
let (should_commit_tx, _) = broadcast::channel(16);
state.should_commit_channel = should_commit_tx;
}
rx
};
let should_commit = rx
.recv()
.await
.map_err(|e| Status::internal(e.to_string()))?;
let reply = ShouldCommitResponse {
should_commit: should_commit,
};
Ok(Response::new(reply))
}
async fn kill(&self, request: Request<KillRequest>) -> Result<Response<KillResponse>, Status> {
let req = request.into_inner();
warn!("got kill request: {}", req.msg);
std::process::exit(1);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::lighthouse::{Lighthouse, LighthouseOpt};
async fn should_commit(rank: i64, should_commit: bool) -> Result<ShouldCommitResponse> {
let mut client = manager_client_new(
"http://localhost:29531".to_string(),
Duration::from_secs(10),
)
.await?;
let request = tonic::Request::new(ShouldCommitRequest {
rank: rank,
step: 1,
should_commit: should_commit,
});
let resp = client.should_commit(request).await?;
Ok(resp.into_inner())
}
#[tokio::test]
async fn test_should_commit() -> Result<()> {
let manager = Manager::new(
"rep_id".to_string(),
"lighthouse".to_string(),
"addr".to_string(),
"[::]:29531".to_string(),
"store_addr".to_string(),
2,
)
.await?;
let manager_fut = tokio::spawn(manager._run_grpc());
let fut_a = tokio::spawn(should_commit(0, true));
let fut_b = tokio::spawn(should_commit(1, true));
let resp_a = fut_a.await??;
let resp_b = fut_b.await??;
assert!(resp_a.should_commit);
assert!(resp_b.should_commit);
let fut_a = tokio::spawn(should_commit(0, true));
let fut_b = tokio::spawn(should_commit(1, false));
let resp_a = fut_a.await??;
let resp_b = fut_b.await??;
assert!(!resp_a.should_commit);
assert!(!resp_b.should_commit);
manager_fut.abort();
Ok(())
}
#[tokio::test]
async fn test_get_quorum() -> Result<()> {
let lighthouse = Lighthouse::new(LighthouseOpt {
bind: "[::]:0".to_string(),
join_timeout_ms: 100,
min_replicas: 1,
quorum_tick_ms: 100,
})
.await?;
let lighthouse_fut = tokio::spawn(lighthouse.clone().run());
let manager = Manager::new(
"rep_id".to_string(),
lighthouse.address(),
"addr".to_string(),
"[::]:0".to_string(),
"store_addr".to_string(),
1, // world size
)
.await?;
let manager_fut = tokio::spawn(manager.clone().run());
let mut client = manager_client_new(manager.address(), Duration::from_secs(10)).await?;
let mut request = tonic::Request::new(ManagerQuorumRequest {
room_id: "room".to_string(),
rank: 0,
step: 123,
checkpoint_server_addr: "addr".to_string(),
});
request.set_timeout(Duration::from_secs(10));
let resp = client.quorum(request).await?.into_inner();
manager_fut.abort();
lighthouse_fut.abort();
assert_eq!(resp.quorum_id, 1);
assert_eq!(resp.address, "addr".to_string());
assert_eq!(resp.store_address, "store_addr".to_string());
assert_eq!(resp.max_step, 123);
assert_eq!(resp.max_rank, Some(0));
assert_eq!(resp.max_world_size, 1);
assert_eq!(resp.replica_rank, 0);
assert_eq!(resp.replica_world_size, 1);
assert_eq!(resp.heal, false);
Ok(())
}
#[tokio::test]
async fn test_get_quorum_heal_first_step() -> Result<()> {
let lighthouse = Lighthouse::new(LighthouseOpt {
bind: "[::]:0".to_string(),
join_timeout_ms: 100,
min_replicas: 2,
quorum_tick_ms: 100,
})
.await?;
let lighthouse_fut = tokio::spawn(lighthouse.clone().run());
let mut manager_futs: Vec<tokio::task::JoinHandle<Result<ManagerQuorumResponse>>> =
Vec::new();
for replica_id in 0..2 {
let lighthouse_addr = lighthouse.address();
manager_futs.push(tokio::spawn(async move {
let manager = Manager::new(
format!("rep_{}", replica_id),
lighthouse_addr,
"addr".to_string(),
"[::]:0".to_string(),
"store_addr".to_string(),
1, // world size
)
.await?;
let manager_fut = tokio::spawn(manager.clone().run());
let mut client =
manager_client_new(manager.address(), Duration::from_secs(10)).await?;
let mut request = tonic::Request::new(ManagerQuorumRequest {
room_id: "room".to_string(),
rank: 0,
step: 0,
checkpoint_server_addr: "addr".to_string(),
});
request.set_timeout(Duration::from_secs(10));
let result = client.quorum(request).await?.into_inner();
manager_fut.abort();
Ok(result)
}));
}
let resp_a = manager_futs.swap_remove(0).await??;
let resp_b = manager_futs.swap_remove(0).await??;
lighthouse_fut.abort();
assert_eq!(resp_a.quorum_id, 1);
assert_eq!(resp_a.max_step, 0);
assert_eq!(resp_a.replica_rank, 0);
assert_eq!(resp_a.replica_world_size, 2);
assert_eq!(resp_a.heal, false);
assert_eq!(resp_b.quorum_id, 1);
assert_eq!(resp_b.max_step, 0);
assert_eq!(resp_b.replica_rank, 1);
assert_eq!(resp_b.replica_world_size, 2);
assert_eq!(resp_b.heal, true);
Ok(())
}
}