-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathutils.rs
More file actions
170 lines (155 loc) · 4.43 KB
/
utils.rs
File metadata and controls
170 lines (155 loc) · 4.43 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use anyhow::Result;
use axum::http::Method;
use rivet_api_builder::ApiCtx;
use rivet_api_util::request_remote_datacenter;
use rivet_types::actors::Actor;
use rivet_util::Id;
use std::collections::HashMap;
/// Helper function to fetch an actor by ID, automatically routing to the correct datacenter
/// based on the actor ID's label.
#[tracing::instrument(skip_all)]
pub async fn fetch_actor_by_id(ctx: &ApiCtx, actor_id: Id, namespace: String) -> Result<Actor> {
let list_query = rivet_api_types::actors::list::ListQuery {
namespace,
actor_ids: Some(actor_id.to_string()),
..Default::default()
};
if actor_id.label() == ctx.config().dc_label() {
// Local datacenter - use peer API directly
let res = rivet_api_peer::actors::list::list(ctx.clone().into(), (), list_query).await?;
let actor = res
.actors
.into_iter()
.next()
.ok_or_else(|| pegboard::errors::Actor::NotFound.build())?;
Ok(actor)
} else {
// Remote datacenter - make HTTP request
let res = request_remote_datacenter::<rivet_api_types::actors::list::ListResponse>(
ctx.config(),
actor_id.label(),
"/actors",
Method::GET,
Some(&list_query),
Option::<&()>::None,
)
.await?;
let actor = res
.actors
.into_iter()
.next()
.ok_or_else(|| pegboard::errors::Actor::NotFound.build())?;
Ok(actor)
}
}
/// Helper function to fetch multiple actors by their IDs, automatically routing to the correct datacenters
/// based on each actor ID's label. This function batches requests by datacenter for efficiency.
#[tracing::instrument(skip_all)]
pub async fn fetch_actors_by_ids(
ctx: &ApiCtx,
actor_ids: Vec<Id>,
namespace: String,
include_destroyed: Option<bool>,
limit: Option<usize>,
cursor: Option<String>,
) -> Result<Vec<Actor>> {
if actor_ids.is_empty() {
return Ok(Vec::new());
}
// Group actor IDs by datacenter
let mut actors_by_dc = HashMap::<u16, Vec<Id>>::new();
for actor_id in actor_ids {
actors_by_dc
.entry(actor_id.label())
.or_default()
.push(actor_id);
}
// Fetch actors in batch from each datacenter
let fetch_futures = actors_by_dc.into_iter().map(|(dc_label, dc_actor_ids)| {
let ctx = ctx.clone();
let namespace = namespace.clone();
let include_destroyed = include_destroyed;
let limit = limit;
let cursor = cursor.clone();
async move {
// Prepare peer query with actor_ids
let peer_query = rivet_api_types::actors::list::ListQuery {
namespace: namespace.clone(),
name: None,
key: None,
actor_ids: None,
actor_id: dc_actor_ids,
include_destroyed,
limit,
cursor,
};
if dc_label == ctx.config().dc_label() {
// Local datacenter - use peer API directly
let res = rivet_api_peer::actors::list::list(ctx.into(), (), peer_query).await?;
Ok::<Vec<Actor>, anyhow::Error>(res.actors)
} else {
// Remote datacenter - make HTTP request
let res = request_remote_datacenter::<rivet_api_types::actors::list::ListResponse>(
ctx.config(),
dc_label,
"/actors",
Method::GET,
Some(&peer_query),
Option::<&()>::None,
)
.await?;
Ok(res.actors)
}
}
});
// Execute all requests in parallel
let results = futures_util::future::join_all(fetch_futures).await;
// Aggregate results
let mut actors = Vec::new();
for res in results {
match res {
Ok(dc_actors) => actors.extend(dc_actors),
Err(err) => tracing::error!(?err, "failed to fetch actors from datacenter"),
}
}
// Sort by create ts desc
actors.sort_by_cached_key(|x| std::cmp::Reverse(x.create_ts));
Ok(actors)
}
/// Determine the datacenter label to create the actor in.
#[tracing::instrument(skip_all)]
pub async fn find_dc_for_actor_creation(
ctx: &ApiCtx,
namespace_id: Id,
namespace_name: &str,
runner_name: &str,
dc_name: Option<&str>,
) -> Result<u16> {
let target_dc_label = if let Some(dc_name) = &dc_name {
// Use user-configured DC
ctx.config()
.dc_for_name(dc_name)
.ok_or_else(|| rivet_api_util::errors::Datacenter::NotFound.build())?
.datacenter_label
} else {
// Find the nearest DC with runners
let res = ctx
.op(
pegboard::ops::runner::list_runner_config_enabled_dcs::Input {
namespace_id,
runner_name: runner_name.into(),
},
)
.await?;
if let Some(dc_label) = res.dc_labels.into_iter().next() {
dc_label
} else {
return Err(pegboard::errors::Actor::NoRunnerConfigConfigured {
namespace: namespace_name.into(),
pool_name: runner_name.into(),
}
.build());
}
};
Ok(target_dc_label)
}