Main loop handling communication with the master.
(
mut socket: TcpStream,
client: SocketAddr,
mut context: WorkerContext<G::Shardable>,
)
| 297 | |
| 298 | /// Main loop handling communication with the master. |
| 299 | async fn handle_master_client( |
| 300 | mut socket: TcpStream, |
| 301 | client: SocketAddr, |
| 302 | mut context: WorkerContext<G::Shardable>, |
| 303 | ) -> Result<()> { |
| 304 | // Authenticate if cluster key is set |
| 305 | if let Some(ref cluster_key) = context.context.args.cluster_key { |
| 306 | super::auth::authenticate_as_worker(&mut socket, cluster_key) |
| 307 | .await |
| 308 | .map_err(|e| anyhow!("[{}] authentication failed: {}", &client, e))?; |
| 309 | log::debug!("[{}] authenticated", &client); |
| 310 | } |
| 311 | |
| 312 | // read first message: expect Hello, but handle LayerAssignment for master restarts |
| 313 | let (latency, _size, first_msg) = Self::read_message_timed(&mut socket).await?; |
| 314 | match first_msg { |
| 315 | Message::Hello => { /* normal inference handshake, continue below */ } |
| 316 | Message::LayerAssignment { ref layers, .. } => { |
| 317 | // Master restarted and is re-running setup against an already-running worker. |
| 318 | // Ack the assignment (we already have cached data) and signal ready, |
| 319 | // then close this connection so the master can reconnect for inference. |
| 320 | log::info!( |
| 321 | "[{}] master re-setup: accepting {} layer assignment(s)", |
| 322 | &client, |
| 323 | layers.len() |
| 324 | ); |
| 325 | let ack = Message::LayerAssignmentAck { needs_data: false }; |
| 326 | ack.to_writer(&mut socket).await?; |
| 327 | Message::WorkerReady.to_writer(&mut socket).await?; |
| 328 | log::info!("[{}] re-setup complete, closing setup connection", &client); |
| 329 | return Ok(()); |
| 330 | } |
| 331 | other => { |
| 332 | return Err(anyhow!( |
| 333 | "[{}] unexpected first message (expected Hello): {:?}", |
| 334 | &client, |
| 335 | other |
| 336 | )); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | // send info |
| 341 | if let Err(e) = Self::write_message_timed( |
| 342 | &mut socket, |
| 343 | Message::WorkerInfo(context.to_info(latency.as_millis())), |
| 344 | ) |
| 345 | .await |
| 346 | { |
| 347 | return Err(anyhow!("[{}] could not send worker info: {:?}", &client, e)); |
| 348 | } |
| 349 | |
| 350 | let mut msg_idx = 0; |
| 351 | let mut avg_ops = 0; |
| 352 | let mut avg_write = 0; |
| 353 | let mut avg_read = 0; |
| 354 | let mut read_buf = Vec::with_capacity(64 * 1024); |
| 355 | let mut write_buf = Vec::with_capacity(64 * 1024); |
| 356 |
nothing calls this directly
no test coverage detected