Skip to content

Commit d5f2781

Browse files
authored
Conversion implementations for awaited action db structs (TraceMachina#1243)
Implements a number of convenience implementations to simplify compatibility with fred and conversions into redis keys/values.
1 parent 3eadab0 commit d5f2781

File tree

5 files changed

+89
-7
lines changed

5 files changed

+89
-7
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nativelink-scheduler/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ rust_library(
3737
"//nativelink-store",
3838
"//nativelink-util",
3939
"@crates//:async-lock",
40+
"@crates//:bytes",
4041
"@crates//:futures",
4142
"@crates//:lru",
4243
"@crates//:parking_lot",

nativelink-scheduler/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ nativelink-metric = { path = "../nativelink-metric" }
1515
nativelink-store = { path = "../nativelink-store" }
1616
async-lock = { version = "3.4.0", features = ["std"], default-features = false }
1717
async-trait = "0.1.81"
18+
bytes = { version = "1.6.1", default-features = false }
1819
prost = { version = "0.13.1", default-features = false }
1920
uuid = { version = "1.8.0", default-features = false, features = ["v4", "serde"] }
2021
futures = { version = "0.3.30", default-features = false }

nativelink-scheduler/src/awaited_action_db/awaited_action.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use std::sync::Arc;
1616
use std::time::{SystemTime, UNIX_EPOCH};
1717

18+
use bytes::Bytes;
1819
use nativelink_error::{make_input_err, Error, ResultExt};
1920
use nativelink_metric::{
2021
MetricFieldData, MetricKind, MetricPublishKnownKindData, MetricsComponent,
@@ -149,12 +150,13 @@ impl AwaitedAction {
149150
}
150151
}
151152

152-
impl TryInto<Vec<u8>> for AwaitedAction {
153+
impl TryInto<bytes::Bytes> for AwaitedAction {
153154
type Error = Error;
154-
fn try_into(self) -> Result<Vec<u8>, Self::Error> {
155-
serde_json::to_vec(&self)
155+
fn try_into(self) -> Result<Bytes, Self::Error> {
156+
serde_json::to_string(&self)
157+
.map(Bytes::from)
156158
.map_err(|e| make_input_err!("{}", e.to_string()))
157-
.err_tip(|| "In AwaitedAction::TryInto::<Vec<u8>>")
159+
.err_tip(|| "In AwaitedAction::TryInto::Bytes")
158160
}
159161
}
160162

@@ -213,6 +215,10 @@ impl AwaitedActionSortKey {
213215
.as_secs() as u32;
214216
Self::new(priority, timestamp)
215217
}
218+
219+
pub fn as_u64(&self) -> u64 {
220+
self.0
221+
}
216222
}
217223

218224
// Ensure the size of the sort key is the same as a `u64`.

nativelink-scheduler/src/awaited_action_db/mod.rs

+76-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ use std::sync::Arc;
1818

1919
pub use awaited_action::{AwaitedAction, AwaitedActionSortKey};
2020
use futures::{Future, Stream};
21-
use nativelink_error::Error;
21+
use nativelink_error::{make_input_err, Error, ResultExt};
2222
use nativelink_metric::MetricsComponent;
23-
use nativelink_util::action_messages::{ActionInfo, OperationId};
23+
use nativelink_util::action_messages::{ActionInfo, ActionStage, OperationId};
24+
use serde::{Deserialize, Serialize};
2425

2526
mod awaited_action;
2627

@@ -33,8 +34,38 @@ pub enum SortedAwaitedActionState {
3334
Completed,
3435
}
3536

37+
impl TryFrom<&ActionStage> for SortedAwaitedActionState {
38+
type Error = Error;
39+
fn try_from(value: &ActionStage) -> Result<Self, Error> {
40+
match value {
41+
ActionStage::CacheCheck => Ok(Self::CacheCheck),
42+
ActionStage::Executing => Ok(Self::Executing),
43+
ActionStage::Completed(_) => Ok(Self::Completed),
44+
ActionStage::Queued => Ok(Self::Queued),
45+
_ => Err(make_input_err!("Invalid State")),
46+
}
47+
}
48+
}
49+
50+
impl TryFrom<ActionStage> for SortedAwaitedActionState {
51+
type Error = Error;
52+
fn try_from(value: ActionStage) -> Result<Self, Error> {
53+
Self::try_from(&value)
54+
}
55+
}
56+
57+
impl std::fmt::Display for SortedAwaitedActionState {
58+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59+
match self {
60+
SortedAwaitedActionState::CacheCheck => write!(f, "CacheCheck"),
61+
SortedAwaitedActionState::Queued => write!(f, "Queued"),
62+
SortedAwaitedActionState::Executing => write!(f, "Executing"),
63+
SortedAwaitedActionState::Completed => write!(f, "Completed"),
64+
}
65+
}
66+
}
3667
/// A struct pointing to an AwaitedAction that can be sorted.
37-
#[derive(Debug, Clone, MetricsComponent)]
68+
#[derive(Debug, Clone, Serialize, Deserialize, MetricsComponent)]
3869
pub struct SortedAwaitedAction {
3970
#[metric(help = "The sort key of the AwaitedAction")]
4071
pub sort_key: AwaitedActionSortKey,
@@ -64,6 +95,48 @@ impl Ord for SortedAwaitedAction {
6495
}
6596
}
6697

98+
impl std::fmt::Display for SortedAwaitedAction {
99+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
100+
std::fmt::write(
101+
f,
102+
format_args!("{}-{}", self.sort_key.as_u64(), self.operation_id),
103+
)
104+
}
105+
}
106+
107+
impl From<&AwaitedAction> for SortedAwaitedAction {
108+
fn from(value: &AwaitedAction) -> Self {
109+
Self {
110+
operation_id: value.operation_id().clone(),
111+
sort_key: value.sort_key(),
112+
}
113+
}
114+
}
115+
116+
impl From<AwaitedAction> for SortedAwaitedAction {
117+
fn from(value: AwaitedAction) -> Self {
118+
Self::from(&value)
119+
}
120+
}
121+
122+
impl TryInto<Vec<u8>> for SortedAwaitedAction {
123+
type Error = Error;
124+
fn try_into(self) -> Result<Vec<u8>, Self::Error> {
125+
serde_json::to_vec(&self)
126+
.map_err(|e| make_input_err!("{}", e.to_string()))
127+
.err_tip(|| "In SortedAwaitedAction::TryInto::<Vec<u8>>")
128+
}
129+
}
130+
131+
impl TryFrom<&[u8]> for SortedAwaitedAction {
132+
type Error = Error;
133+
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
134+
serde_json::from_slice(value)
135+
.map_err(|e| make_input_err!("{}", e.to_string()))
136+
.err_tip(|| "In AwaitedAction::TryFrom::&[u8]")
137+
}
138+
}
139+
67140
/// Subscriber that can be used to monitor when AwaitedActions change.
68141
pub trait AwaitedActionSubscriber: Send + Sync + Sized + 'static {
69142
/// Wait for AwaitedAction to change.

0 commit comments

Comments
 (0)