Skip to main content

hyperactor/
lib.rs

1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree.
7 */
8
9//! Hyperactor is an actor system intended for managing large scale compute.
10//!
11//! # Actor data model
12//!
13//! Hyperactor is designed to support large scale (millions of nodes)
14//! machine learning workloads where actor topologies communicate through
15//! high fanout multicast messaging.
16//!
17//! Supporting this scale requires us to impose additional structure
18//! at the level of the framework, so that we can efficiently refer to
19//! _meshes_ of actors that implement the same worker runtimes.
20//!
21//! Similarly, Hyperactor must co-schedule actors in order to support
22//! collective communicaton between actors.
23//!
24//! Hyperactor is organized into a hierarchy:
25//!
26//! * Each _proc_ represents a single actor runtime instance, and hosts
27//!   zero or more actors.
28//! * Actors are _spawned_ into procs, and assigned a global name.
29//!   Actors spawned in this way are assigned a local PID (pid) of 0.
30//!   Actors in turn can spawn local actors. These inherit the global pid
31//!   of their parent, but receive a unique pid.
32//!
33//! This scheme confers several benefits:
34//!
35//! * Routing of messages can be performed by prefix. For example, we
36//!   can identify the _proc_ of the actor and send the message to it,
37//!   which can then in turn be routed locally.
38//!
39//! * We can represent meshes of actors in a uniform and compact way.
40//!   This is the basis on which we implement efficient multicasting
41//!   within the system.
42//!
43//!
44//! | Entity    | Identifier                    |
45//! |-----------|-------------------------------|
46//! | Proc      | `addr,proc_name`              |
47//! | Actor     | `addr,proc_name,name[pid]`    |
48
49#![feature(anonymous_lifetime_in_impl_trait)]
50#![feature(associated_type_defaults)]
51#![feature(box_patterns)]
52#![feature(btree_cursors)]
53#![feature(error_reporter)]
54#![feature(exact_size_is_empty)]
55#![feature(impl_trait_in_assoc_type)]
56#![feature(never_type)]
57#![feature(panic_update_hook)]
58#![feature(type_alias_impl_trait)]
59#![feature(trait_alias)]
60#![deny(missing_docs)]
61
62pub mod accum;
63pub mod actor;
64pub mod actor_local;
65pub mod addr;
66pub mod channel;
67pub mod client;
68pub mod config;
69pub mod context;
70pub mod endpoint;
71/// Gateway management for proc connectivity.
72pub mod gateway;
73pub mod id;
74mod init;
75pub mod introspect;
76pub mod mailbox;
77pub mod message;
78pub mod metrics;
79pub mod ordering;
80pub mod panic_handler;
81mod parse;
82pub mod port;
83pub mod proc;
84pub mod ref_;
85pub mod remote;
86pub mod runtime_identity;
87pub(crate) mod sequenced;
88mod signal_handler;
89mod stdio_redirect;
90pub mod subject;
91pub mod supervision;
92pub mod sync;
93/// Test utilities.
94pub mod testing;
95pub mod time;
96pub mod value_mesh;
97
98#[cfg(fbcode_build)]
99pub mod meta;
100
101/// Re-exports of external crates used by hyperactor_macros codegen.
102/// This module is not part of the public API and should not be used directly.
103#[doc(hidden)]
104pub mod internal_macro_support {
105    pub use anyhow;
106    pub use async_trait;
107    pub use inventory;
108    pub use opentelemetry;
109    pub use paste::paste;
110    pub use serde_json;
111    pub use tracing;
112    pub use typeuri;
113}
114
115pub use actor::Actor;
116pub use actor::ActorGuard;
117pub use actor::ActorHandle;
118pub use actor::AnyActorGuard;
119pub use actor::AnyActorHandle;
120pub use actor::Handler;
121pub use actor::HandlerInfo;
122pub use actor::RemoteHandles;
123pub use actor::RemoteSpawn;
124pub use actor_local::ActorLocal;
125pub use addr::ActorAddr;
126pub use addr::Addr;
127pub use addr::AddrParseError;
128pub use addr::Location;
129pub use addr::PortAddr;
130pub use addr::ProcAddr;
131pub use client::Client;
132pub use endpoint::Endpoint;
133pub use endpoint::EndpointLocation;
134pub use endpoint::RemoteEndpoint;
135pub use gateway::Gateway;
136#[doc(inline)]
137pub use hyperactor_macros::HandleClient;
138#[doc(inline)]
139pub use hyperactor_macros::Handler;
140#[doc(inline)]
141pub use hyperactor_macros::RefClient;
142#[doc(inline)]
143pub use hyperactor_macros::behavior;
144#[doc(inline)]
145pub use hyperactor_macros::export;
146#[doc(inline)]
147pub use hyperactor_macros::handle;
148#[doc(inline)]
149pub use hyperactor_macros::instrument;
150#[doc(inline)]
151pub use hyperactor_macros::instrument_infallible;
152pub use hyperactor_macros::observe_async;
153pub use hyperactor_macros::observe_result;
154#[doc(inline)]
155pub use hyperactor_macros::spawnable;
156#[doc(inline)]
157pub use hyperactor_macros::uid;
158pub use hyperactor_telemetry::declare_static_counter;
159pub use hyperactor_telemetry::declare_static_gauge;
160pub use hyperactor_telemetry::declare_static_histogram;
161pub use hyperactor_telemetry::declare_static_timer;
162pub use hyperactor_telemetry::key_value;
163pub use hyperactor_telemetry::kv_pairs;
164pub use id::ActorId;
165pub use id::Id;
166pub use id::Label;
167pub use id::PortId;
168pub use id::ProcId;
169pub use id::Uid;
170#[doc(inline)]
171pub use init::initialize;
172#[doc(inline)]
173pub use init::initialize_with_current_runtime;
174#[doc(inline)]
175pub use init::initialize_with_log_prefix;
176pub use mailbox::Data;
177pub use mailbox::Mailbox;
178pub use mailbox::Message;
179pub use mailbox::OncePortHandle;
180pub use mailbox::PortHandle;
181pub use mailbox::RemoteMessage;
182pub use port::ControlPort;
183pub use port::Port;
184pub use proc::Context;
185pub use proc::Instance;
186pub use proc::InstanceCell;
187pub use proc::Proc;
188pub use proc::StatusMessage;
189pub use proc::WeakProc;
190pub use ref_::ActorRef;
191pub use ref_::OncePortRef;
192#[doc(hidden)]
193pub use ref_::OncePortRefRepr;
194pub use ref_::PortRef;
195#[doc(hidden)]
196pub use ref_::PortRefRepr;
197pub use remote::Accepts;
198/// Rank or position index used by distributed mesh helpers.
199pub type Index = usize;
200#[doc(inline)]
201pub use signal_handler::SignalCleanupGuard;
202#[doc(inline)]
203pub use signal_handler::SignalDisposition;
204#[doc(inline)]
205pub use signal_handler::query_signal_disposition;
206#[doc(inline)]
207pub use signal_handler::register_signal_cleanup;
208#[doc(inline)]
209pub use signal_handler::register_signal_cleanup_scoped;
210#[doc(inline)]
211pub use signal_handler::sigpipe_disposition;
212#[doc(inline)]
213pub use signal_handler::unregister_signal_cleanup;
214
215/// Serve the current gateway on the provided channel address.
216pub fn serve(
217    addr: channel::ChannelAddr,
218) -> Result<gateway::GatewayServeHandle, channel::ChannelError> {
219    Gateway::current().serve(addr)
220}
221
222/// Spawn a root actor with a fresh uid labeled from the actor type on the current proc.
223pub fn spawn<A: Actor>(actor: A) -> ActorHandle<A> {
224    Proc::current().spawn(actor)
225}
226
227/// Spawn a root actor with a fresh uid carrying a display label on the current proc.
228pub fn spawn_with_label<A: Actor>(label: &str, actor: A) -> ActorHandle<A> {
229    Proc::current().spawn_with_label(label, actor)
230}
231
232/// Spawn a root actor using an explicit uid on the current proc.
233pub fn spawn_with_uid<A: Actor>(uid: Uid, actor: A) -> anyhow::Result<ActorHandle<A>> {
234    Proc::current().spawn_with_uid(uid, actor)
235}
236
237/// Create a client actor on the current proc.
238pub fn client(label: &str) -> Client {
239    Proc::current().client(label)
240}
241
242mod private {
243    /// Public trait in a private module for sealing traits within this crate:
244    /// [Sealed trait pattern](https://rust-lang.github.io/api-guidelines/future-proofing.html#sealed-traits-protect-against-downstream-implementations-c-sealed).
245    pub trait Sealed {}
246
247    // These two implement context capabilities:
248    impl<A: crate::Actor> Sealed for crate::proc::Instance<A> {}
249    impl<A: crate::Actor> Sealed for &crate::proc::Instance<A> {}
250    impl<A: crate::Actor> Sealed for crate::proc::Context<'_, A> {}
251    impl<A: crate::Actor> Sealed for &crate::proc::Context<'_, A> {}
252    impl Sealed for crate::client::Client {}
253    impl Sealed for &crate::client::Client {}
254    impl Sealed for crate::mailbox::Mailbox {}
255    impl Sealed for &crate::mailbox::Mailbox {}
256    impl<A: crate::Actor> Sealed for &crate::actor::ActorHandle<A> {}
257    impl<M: crate::Message> Sealed for &crate::mailbox::PortHandle<M> {}
258    impl<M: crate::Message> Sealed for crate::mailbox::OncePortHandle<M> {}
259    impl<A: crate::actor::Referable> Sealed for &crate::ref_::ActorRef<A> {}
260    impl<M: crate::RemoteMessage> Sealed for &crate::ref_::PortRef<M> {}
261    impl<M: crate::RemoteMessage> Sealed for crate::ref_::OncePortRef<M> {}
262}