-
Notifications
You must be signed in to change notification settings - Fork 124
/
grpc_scheduler.rs
262 lines (248 loc) · 9.79 KB
/
grpc_scheduler.rs
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// Copyright 2023-2024 The NativeLink Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::collections::HashMap;
use std::future::Future;
use std::sync::Arc;
use std::time::Duration;
use async_trait::async_trait;
use futures::stream::unfold;
use futures::TryFutureExt;
use nativelink_error::{make_err, Code, Error, ResultExt};
use nativelink_proto::build::bazel::remote::execution::v2::capabilities_client::CapabilitiesClient;
use nativelink_proto::build::bazel::remote::execution::v2::execution_client::ExecutionClient;
use nativelink_proto::build::bazel::remote::execution::v2::{
digest_function, ExecuteRequest, ExecutionPolicy, GetCapabilitiesRequest, WaitExecutionRequest,
};
use nativelink_proto::google::longrunning::Operation;
use nativelink_util::action_messages::{
ActionInfo, ActionInfoHashKey, ActionState, DEFAULT_EXECUTION_PRIORITY,
};
use nativelink_util::grpc_utils::ConnectionManager;
use nativelink_util::retry::{Retrier, RetryResult};
use nativelink_util::tls_utils;
use parking_lot::Mutex;
use rand::rngs::OsRng;
use rand::Rng;
use tokio::select;
use tokio::sync::watch;
use tokio::time::sleep;
use tonic::{Request, Streaming};
use tracing::{error, info, warn};
use crate::action_scheduler::ActionScheduler;
use crate::platform_property_manager::PlatformPropertyManager;
pub struct GrpcScheduler {
platform_property_managers: Mutex<HashMap<String, Arc<PlatformPropertyManager>>>,
retrier: Retrier,
connection_manager: ConnectionManager,
}
impl GrpcScheduler {
pub fn new(config: &nativelink_config::schedulers::GrpcScheduler) -> Result<Self, Error> {
let jitter_amt = config.retry.jitter;
Self::new_with_jitter(
config,
Box::new(move |delay: Duration| {
if jitter_amt == 0. {
return delay;
}
let min = 1. - (jitter_amt / 2.);
let max = 1. + (jitter_amt / 2.);
delay.mul_f32(OsRng.gen_range(min..max))
}),
)
}
pub fn new_with_jitter(
config: &nativelink_config::schedulers::GrpcScheduler,
jitter_fn: Box<dyn Fn(Duration) -> Duration + Send + Sync>,
) -> Result<Self, Error> {
let endpoint = tls_utils::endpoint(&config.endpoint)?;
Ok(Self {
platform_property_managers: Mutex::new(HashMap::new()),
retrier: Retrier::new(
Arc::new(|duration| Box::pin(sleep(duration))),
Arc::new(jitter_fn),
config.retry.to_owned(),
),
connection_manager: ConnectionManager::new(
std::iter::once(endpoint),
config.connections_per_endpoint,
config.max_concurrent_requests,
),
})
}
async fn perform_request<F, Fut, R, I>(&self, input: I, mut request: F) -> Result<R, Error>
where
F: FnMut(I) -> Fut + Send + Copy,
Fut: Future<Output = Result<R, Error>> + Send,
R: Send,
I: Send + Clone,
{
self.retrier
.retry(unfold(input, move |input| async move {
let input_clone = input.clone();
Some((
request(input_clone)
.await
.map_or_else(RetryResult::Retry, RetryResult::Ok),
input,
))
}))
.await
}
async fn stream_state(
mut result_stream: Streaming<Operation>,
) -> Result<watch::Receiver<Arc<ActionState>>, Error> {
if let Some(initial_response) = result_stream
.message()
.await
.err_tip(|| "Recieving response from upstream scheduler")?
{
let (tx, rx) = watch::channel(Arc::new(initial_response.try_into()?));
tokio::spawn(async move {
loop {
select!(
_ = tx.closed() => {
info!("Client disconnected in GrpcScheduler");
return;
}
response = result_stream.message() => {
// When the upstream closes the channel, close the
// downstream too.
let Ok(Some(response)) = response else {
return;
};
match response.try_into() {
Ok(response) => {
if let Err(err) = tx.send(Arc::new(response)) {
info!("Client disconnected in GrpcScheduler: {}", err);
return;
}
}
Err(err) => error!("Error converting response to ActionState in GrpcScheduler: {}", err),
}
}
)
}
});
return Ok(rx);
}
Err(make_err!(
Code::Internal,
"Upstream scheduler didn't accept action."
))
}
}
#[async_trait]
impl ActionScheduler for GrpcScheduler {
async fn get_platform_property_manager(
&self,
instance_name: &str,
) -> Result<Arc<PlatformPropertyManager>, Error> {
if let Some(platform_property_manager) =
self.platform_property_managers.lock().get(instance_name)
{
return Ok(platform_property_manager.clone());
}
self.perform_request(instance_name, |instance_name| async move {
// Not in the cache, lookup the capabilities with the upstream.
let channel = self.connection_manager.connection().await?;
let capabilities_result = CapabilitiesClient::new(channel)
.get_capabilities(GetCapabilitiesRequest {
instance_name: instance_name.to_string(),
})
.await
.err_tip(|| "Retrieving upstream GrpcScheduler capabilities");
let capabilities = capabilities_result?.into_inner();
let platform_property_manager = Arc::new(PlatformPropertyManager::new(
capabilities
.execution_capabilities
.err_tip(|| "Unable to get execution properties in GrpcScheduler")?
.supported_node_properties
.iter()
.map(|property| {
(
property.clone(),
nativelink_config::schedulers::PropertyType::exact,
)
})
.collect(),
));
self.platform_property_managers
.lock()
.insert(instance_name.to_string(), platform_property_manager.clone());
Ok(platform_property_manager)
})
.await
}
async fn add_action(
&self,
action_info: ActionInfo,
) -> Result<watch::Receiver<Arc<ActionState>>, Error> {
let execution_policy = if action_info.priority == DEFAULT_EXECUTION_PRIORITY {
None
} else {
Some(ExecutionPolicy {
priority: action_info.priority,
})
};
let request = ExecuteRequest {
instance_name: action_info.instance_name().clone(),
skip_cache_lookup: action_info.skip_cache_lookup,
action_digest: Some(action_info.digest().into()),
execution_policy,
// TODO: Get me from the original request, not very important as we ignore it.
results_cache_policy: None,
digest_function: digest_function::Value::Sha256.into(),
};
let result_stream = self
.perform_request(request, |request| async move {
let channel = self.connection_manager.connection().await?;
ExecutionClient::new(channel)
.execute(Request::new(request))
.await
.err_tip(|| "Sending action to upstream scheduler")
})
.await?
.into_inner();
Self::stream_state(result_stream).await
}
async fn find_existing_action(
&self,
unique_qualifier: &ActionInfoHashKey,
) -> Option<watch::Receiver<Arc<ActionState>>> {
let request = WaitExecutionRequest {
name: unique_qualifier.action_name(),
};
let result_stream = self
.perform_request(request, |request| async move {
let channel = self.connection_manager.connection().await?;
ExecutionClient::new(channel)
.wait_execution(Request::new(request))
.await
.err_tip(|| "While getting wait_execution stream")
})
.and_then(|result_stream| Self::stream_state(result_stream.into_inner()))
.await;
match result_stream {
Ok(result_stream) => Some(result_stream),
Err(err) => {
warn!(
"Error response looking up action with upstream scheduler: {}",
err
);
None
}
}
}
async fn clean_recently_completed_actions(&self) {}
}