-
Notifications
You must be signed in to change notification settings - Fork 187
Expand file tree
/
Copy pathrunner_pool_backfill.rs
More file actions
153 lines (129 loc) · 3.7 KB
/
runner_pool_backfill.rs
File metadata and controls
153 lines (129 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//! Backfills runner pool keys in epoxy.
use std::time::{Duration, Instant};
use epoxy::ops::propose::{Command, CommandKind, Proposal, SetCommand};
use futures_util::{FutureExt, StreamExt, TryStreamExt};
use gas::prelude::*;
use universaldb::prelude::*;
use crate::keys;
use crate::workflows::actor_runner_name_selector_backfill::MarkCompleteInput;
const EARLY_TXN_TIMEOUT: Duration = Duration::from_millis(2500);
pub const BACKFILL_NAME: &str = "epoxy_runner_pools";
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Input {}
#[workflow]
pub async fn pegboard_runner_pool_backfill(ctx: &mut WorkflowCtx, _input: &Input) -> Result<()> {
ctx.loope(Vec::<u8>::new(), |ctx, last_key| {
async move {
let res = ctx
.activity(BackfillChunkInput {
last_key: last_key.clone(),
})
.await?;
match res {
BackfillOutput::Continue(new_last_key) => {
*last_key = new_last_key;
Ok(Loop::Continue)
}
BackfillOutput::Complete => Ok(Loop::Break(())),
}
}
.boxed()
})
.await?;
ctx.activity(MarkCompleteInput {
name: BACKFILL_NAME.to_string(),
})
.await?;
Ok(())
}
#[derive(Debug, Clone, Serialize, Deserialize, Hash)]
struct BackfillChunkInput {
last_key: Vec<u8>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
enum BackfillOutput {
Continue(Vec<u8>),
Complete,
}
#[activity(BackfillChunk)]
async fn backfill_chunk(ctx: &ActivityCtx, input: &BackfillChunkInput) -> Result<BackfillOutput> {
let (entries, new_last_key) = ctx
.udb()?
.run(|tx| async move {
let start = Instant::now();
let tx = tx.with_subspace(namespace::keys::subspace());
let mut new_last_key = Vec::new();
let mut entries = Vec::new();
let runner_config_subspace = namespace::keys::subspace()
.subspace(&keys::runner_config::DataKey::entire_subspace());
let range = runner_config_subspace.range();
let range_start = if input.last_key.is_empty() {
&range.0
} else {
&input.last_key
};
let range_end = &runner_config_subspace.range().1;
let mut stream = tx.get_ranges_keyvalues(
universaldb::RangeOption {
mode: StreamingMode::WantAll,
..(range_start.as_slice(), range_end.as_slice()).into()
},
Snapshot,
);
loop {
if start.elapsed() > EARLY_TXN_TIMEOUT {
tracing::warn!("timed out processing pending actors metrics");
break;
}
let Some(entry) = stream.try_next().await? else {
new_last_key = Vec::new();
break;
};
new_last_key = [entry.key(), &[0xff]].concat();
if let Ok((_, _, leaf)) =
runner_config_subspace.unpack::<(Id, String, usize)>(entry.key())
{
if leaf == universaldb::utils::keys::PROTOCOL_VERSION {
continue;
}
}
entries.push(tx.read_entry::<keys::runner_config::DataKey>(&entry)?);
}
Ok((entries, new_last_key))
})
.custom_instrument(tracing::info_span!("read_runner_pools_tx"))
.await?;
// Save to epoxy
futures_util::stream::iter(entries)
.map(|(key, value)| async move {
let global_runner_config_key = keys::runner_config::GlobalDataKey::new(
ctx.config().dc_label(),
key.namespace_id,
key.name,
);
ctx.op(epoxy::ops::propose::Input {
proposal: Proposal {
commands: vec![Command {
kind: CommandKind::SetCommand(SetCommand {
key: namespace::keys::subspace().pack(&global_runner_config_key),
value: Some(global_runner_config_key.serialize(value)?),
}),
}],
},
purge_cache: true,
mutable: true,
target_replicas: None,
})
.await?;
anyhow::Ok(())
})
.buffer_unordered(512)
.try_collect::<()>()
.await?;
if new_last_key.is_empty() {
Ok(BackfillOutput::Complete)
} else {
Ok(BackfillOutput::Continue(new_last_key))
}
}