Skip to content

Commit 017938c

Browse files
committed
[hyperactor] mesh: define resource::Resource, and mesh::Mesh in terms of it
Pull Request resolved: #1841 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. ghstack-source-id: 323217472 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/)!
1 parent 09c4909 commit 017938c

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

hyperactor_mesh/src/resource.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,23 @@ where
359359
}
360360
}
361361

362+
/// A trait that bundles a set of types that together define a resource.
363+
pub trait Resource {
364+
/// The spec specification for this resource.
365+
type Spec: Named + Serialize + for<'de> Deserialize<'de> + Send + Sync + std::fmt::Debug;
366+
367+
/// The state for this resource.
368+
type State: Named + Serialize + for<'de> Deserialize<'de> + Send + Sync + std::fmt::Debug;
369+
}
370+
371+
// A behavior defining the interface for a mesh controller.
372+
hyperactor::behavior!(
373+
Controller<R: Resource>,
374+
CreateOrUpdate<R::Spec>,
375+
GetState<R::State>,
376+
Stop,
377+
);
378+
362379
/// RankedValues compactly represents rank-indexed values of type T.
363380
/// It stores contiguous values in a set of intervals; thus it is
364381
/// efficient and compact when the cardinality of T-typed values is

hyperactor_mesh/src/resource/mesh.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ use ndslice::Extent;
1919
use serde::Deserialize;
2020
use serde::Serialize;
2121

22-
use crate::resource::CreateOrUpdate;
23-
use crate::resource::GetState;
22+
use crate::resource::Resource;
2423
use crate::resource::Status;
25-
use crate::resource::Stop;
2624
use crate::v1::ValueMesh;
2725

2826
/// Mesh specs
@@ -49,17 +47,14 @@ pub trait Mesh {
4947
/// The mesh-specific specification for this resource.
5048
type Spec: Named + Serialize + for<'de> Deserialize<'de> + Send + Sync + std::fmt::Debug;
5149

52-
/// The mesh-specific state for thsi resource.
50+
/// The mesh-specific state for this resource.
5351
type State: Named + Serialize + for<'de> Deserialize<'de> + Send + Sync + std::fmt::Debug;
5452
}
5553

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-
);
54+
impl<M: Mesh> Resource for M {
55+
type Spec = Spec<M::Spec>;
56+
type State = State<M::State>;
57+
}
6358

6459
#[cfg(test)]
6560
mod test {
@@ -68,6 +63,10 @@ mod test {
6863
use hyperactor::Handler;
6964

7065
use super::*;
66+
use crate::resource::Controller;
67+
use crate::resource::CreateOrUpdate;
68+
use crate::resource::GetState;
69+
use crate::resource::Stop;
7170

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

0 commit comments

Comments
 (0)