Skip to content

(3/n) Specialize state actor bootstrap for each alloc #459

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions hyperactor_mesh/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,15 +223,7 @@ pub trait Alloc {
/// It allows remote processes to stream stdout and stderr back to the client.
/// A client can connect to the log source to obtain the streamed logs.
/// A log source is allocation specific. Each allocator can decide how to stream the logs back.
async fn log_source(&self) -> Result<LogSource, AllocatorError> {
// TODO: this should be implemented based on different allocators.
// Having this temporarily here so that the client can connect to the log source.
// But the client will not get anything.
// The following diffs will gradually implement this for different allocators.
LogSource::new_with_local_actor()
.await
.map_err(AllocatorError::from)
}
async fn log_source(&self) -> Result<LogSource, AllocatorError>;

/// Stop this alloc, shutting down all of its procs. A clean
/// shutdown should result in Stop events from all allocs,
Expand Down Expand Up @@ -367,6 +359,10 @@ pub mod test_utils {
self.alloc.transport()
}

async fn log_source(&self) -> Result<LogSource, AllocatorError> {
self.alloc.log_source().await
}

async fn stop(&mut self) -> Result<(), AllocatorError> {
self.alloc.stop().await
}
Expand Down
9 changes: 9 additions & 0 deletions hyperactor_mesh/src/alloc/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::alloc::AllocSpec;
use crate::alloc::Allocator;
use crate::alloc::AllocatorError;
use crate::alloc::ProcState;
use crate::log_source::LogSource;
use crate::proc_mesh::mesh_agent::MeshAgent;
use crate::shortuuid::ShortUuid;

Expand Down Expand Up @@ -252,6 +253,14 @@ impl Alloc for LocalAlloc {
ChannelTransport::Local
}

async fn log_source(&self) -> Result<LogSource, AllocatorError> {
// Local alloc does not need to stream logs back.
// The client can subscribe to it but local actors will not stream logs into it.
LogSource::new_with_local_actor()
.await
.map_err(AllocatorError::from)
}

async fn stop(&mut self) -> Result<(), AllocatorError> {
for rank in 0..self.size() {
self.todo_tx
Expand Down
46 changes: 27 additions & 19 deletions hyperactor_mesh/src/alloc/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ use hyperactor::channel::ChannelTx;
use hyperactor::channel::Rx;
use hyperactor::channel::Tx;
use hyperactor::channel::TxStatus;
use hyperactor::id;
use hyperactor::sync::flag;
use hyperactor::sync::monitor;
use hyperactor_state::state_actor::StateActor;
Expand All @@ -53,6 +52,8 @@ use crate::bootstrap;
use crate::bootstrap::Allocator2Process;
use crate::bootstrap::Process2Allocator;
use crate::bootstrap::Process2AllocatorMessage;
use crate::log_source::LogSource;
use crate::log_source::StateServerInfo;
use crate::shortuuid::ShortUuid;

/// The maximum number of log lines to tail keep for managed processes.
Expand Down Expand Up @@ -89,6 +90,9 @@ impl Allocator for ProcessAllocator {
let (bootstrap_addr, rx) = channel::serve(ChannelAddr::any(ChannelTransport::Unix))
.await
.map_err(anyhow::Error::from)?;
let log_source = LogSource::new_with_local_actor()
.await
.map_err(AllocatorError::from)?;

let name = ShortUuid::generate();
let n = spec.shape.slice().len();
Expand All @@ -97,6 +101,7 @@ impl Allocator for ProcessAllocator {
world_id: WorldId(name.to_string()),
spec: spec.clone(),
bootstrap_addr,
log_source,
rx,
index: 0,
active: HashMap::new(),
Expand All @@ -115,6 +120,7 @@ pub struct ProcessAlloc {
world_id: WorldId, // to provide storage
spec: AllocSpec,
bootstrap_addr: ChannelAddr,
log_source: LogSource,
rx: channel::ChannelRx<Process2Allocator>,
index: usize,
active: HashMap<usize, Child>,
Expand Down Expand Up @@ -145,6 +151,7 @@ struct Child {
impl Child {
fn monitored(
mut process: tokio::process::Child,
state_server_info: StateServerInfo,
) -> (Self, impl Future<Output = ProcStopReason>) {
let (group, handle) = monitor::group();
let (exit_flag, exit_guard) = flag::guarded();
Expand All @@ -161,24 +168,20 @@ impl Child {

// If state actor is enabled, try to set up LogWriter instances
if use_state_actor {
let state_actor_ref = ActorRef::<StateActor>::attest(id!(state_server[0].state[0]));
// Parse the state actor address
if let Ok(state_actor_addr) = "tcp![::]:3000".parse::<ChannelAddr>() {
// Use the helper function to create both writers at once
match hyperactor_state::log_writer::create_log_writers(
state_actor_addr,
state_actor_ref,
) {
Ok((stdout_writer, stderr_writer)) => {
stdout_tee = stdout_writer;
stderr_tee = stderr_writer;
}
Err(e) => {
tracing::error!("failed to create log writers: {}", e);
}
let state_actor_ref = ActorRef::<StateActor>::attest(state_server_info.state_actor_id);
let state_actor_addr = state_server_info.state_proc_addr;
// Use the helper function to create both writers at once
match hyperactor_state::log_writer::create_log_writers(
state_actor_addr,
state_actor_ref,
) {
Ok((stdout_writer, stderr_writer)) => {
stdout_tee = stdout_writer;
stderr_tee = stderr_writer;
}
Err(e) => {
tracing::error!("failed to create log writers: {}", e);
}
} else {
tracing::error!("failed to parse state actor address");
}
}

Expand Down Expand Up @@ -394,7 +397,8 @@ impl ProcessAlloc {
None
}
Ok(rank) => {
let (handle, monitor) = Child::monitored(process);
let (handle, monitor) =
Child::monitored(process, self.log_source.server_info());
self.children.spawn(async move { (index, monitor.await) });
self.active.insert(index, handle);
// Adjust for shape slice offset for non-zero shapes (sub-shapes).
Expand Down Expand Up @@ -498,6 +502,10 @@ impl Alloc for ProcessAlloc {
ChannelTransport::Unix
}

async fn log_source(&self) -> Result<LogSource, AllocatorError> {
Ok(self.log_source.clone())
}

async fn stop(&mut self) -> Result<(), AllocatorError> {
// We rely on the teardown here, and that the process should
// exit on its own. We should have a hard timeout here as well,
Expand Down
Loading
Loading