Skip to content

Commit 6241515

Browse files
committed
[hyperactor] mesh: define resource::Resource, and mesh::Mesh in terms of it
We formalize a resource controller behavior: ``` /// A trait that bundles a set of types that together define a resource. pub trait Resource { /// The spec specification for this resource. type Spec: Named + Serialize + for<'de> Deserialize<'de> + Send + Sync + std::fmt::Debug; /// The state for this resource. type State: Named + Serialize + for<'de> Deserialize<'de> + Send + Sync + std::fmt::Debug; } // A behavior defining the interface for a mesh controller. hyperactor::behavior!( Controller<R: Resource>, CreateOrUpdate<R::Spec>, GetState<R::State>, Stop, ); ``` Anything that is a controller should behave-as a `resource::Controller`. We then formalize the mesh controller behavior as a specialization of a resource controller, by implementing `Resource` for any mesh: ``` impl<M: Mesh> Resource for M { type Spec = Spec<M::Spec>; type State = State<M::State>; } ``` This resolves to the same set of bindings and aliases (except we use `resource::Controller<Mesh>` rather than `mesh::Controller<Mesh>`) -- the existing behaviors continue to assert. Differential Revision: [D86905562](https://our.internmc.facebook.com/intern/diff/D86905562/) **NOTE FOR REVIEWERS**: This PR has internal Meta-specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D86905562/)! ghstack-source-id: 322844163 Pull Request resolved: #1841
1 parent 5f11cd2 commit 6241515

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

hyperactor_mesh/src/resource.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,23 @@ where
334334
}
335335
}
336336

337+
/// A trait that bundles a set of types that together define a resource.
338+
pub trait Resource {
339+
/// The spec specification for this resource.
340+
type Spec: Named + Serialize + for<'de> Deserialize<'de> + Send + Sync + std::fmt::Debug;
341+
342+
/// The state for this resource.
343+
type State: Named + Serialize + for<'de> Deserialize<'de> + Send + Sync + std::fmt::Debug;
344+
}
345+
346+
// A behavior defining the interface for a mesh controller.
347+
hyperactor::behavior!(
348+
Controller<R: Resource>,
349+
CreateOrUpdate<R::Spec>,
350+
GetState<R::State>,
351+
Stop,
352+
);
353+
337354
/// RankedValues compactly represents rank-indexed values of type T.
338355
/// It stores contiguous values in a set of intervals; thus it is
339356
/// efficient and compact when the cardinality of T-typed values is

hyperactor_mesh/src/resource/mesh.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use serde::Serialize;
2121

2222
use crate::resource::CreateOrUpdate;
2323
use crate::resource::GetState;
24+
use crate::resource::Resource;
2425
use crate::resource::Status;
2526
use crate::resource::Stop;
2627
use crate::v1::ValueMesh;
@@ -49,17 +50,14 @@ pub trait Mesh {
4950
/// The mesh-specific specification for this resource.
5051
type Spec: Named + Serialize + for<'de> Deserialize<'de> + Send + Sync + std::fmt::Debug;
5152

52-
/// The mesh-specific state for thsi resource.
53+
/// The mesh-specific state for this resource.
5354
type State: Named + Serialize + for<'de> Deserialize<'de> + Send + Sync + std::fmt::Debug;
5455
}
5556

56-
// A behavior defining the interface for a mesh controller.
57-
hyperactor::behavior!(
58-
Controller<M: Mesh>,
59-
CreateOrUpdate<Spec<M::Spec>>,
60-
GetState<State<M::State>>,
61-
Stop,
62-
);
57+
impl<M: Mesh> Resource for M {
58+
type Spec = Spec<M::Spec>;
59+
type State = State<M::State>;
60+
}
6361

6462
#[cfg(test)]
6563
mod test {
@@ -68,6 +66,7 @@ mod test {
6866
use hyperactor::Handler;
6967

7068
use super::*;
69+
use crate::resource::Controller;
7170

7271
// Consider upstreaming this into `hyperactor` -- lightweight handler definitions
7372
// can be quite useful.

0 commit comments

Comments
 (0)