-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathexecution_server.rs
More file actions
501 lines (446 loc) · 16.3 KB
/
execution_server.rs
File metadata and controls
501 lines (446 loc) · 16.3 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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
// Copyright 2024 The NativeLink Authors. All rights reserved.
//
// Licensed under the Functional Source License, Version 1.1, Apache 2.0 Future License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// See LICENSE file for details
//
// 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 core::convert::Into;
use core::pin::Pin;
use core::time::Duration;
use std::collections::HashMap;
use std::fmt;
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use futures::stream::unfold;
use futures::{Stream, StreamExt};
use nativelink_config::cas_server::{ExecutionConfig, InstanceName, WithInstanceName};
use nativelink_error::{Error, ResultExt, make_input_err};
use nativelink_proto::build::bazel::remote::execution::v2::execution_server::{
Execution, ExecutionServer as Server,
};
use nativelink_proto::build::bazel::remote::execution::v2::{
Action, Command, ExecuteRequest, WaitExecutionRequest,
};
use nativelink_proto::google::longrunning::operations_server::{Operations, OperationsServer};
use nativelink_proto::google::longrunning::{
CancelOperationRequest, DeleteOperationRequest, GetOperationRequest, ListOperationsRequest,
ListOperationsResponse, Operation, WaitOperationRequest,
};
use nativelink_store::ac_utils::get_and_decode_digest;
use nativelink_store::store_manager::StoreManager;
use nativelink_util::action_messages::{
ActionInfo, ActionUniqueKey, ActionUniqueQualifier, DEFAULT_EXECUTION_PRIORITY, OperationId,
};
use nativelink_util::common::DigestInfo;
use nativelink_util::digest_hasher::{DigestHasherFunc, make_ctx_for_hash_func};
use nativelink_util::operation_state_manager::{
ActionStateResult, ClientStateManager, OperationFilter,
};
use nativelink_util::store_trait::Store;
use opentelemetry::context::FutureExt;
use tonic::{Request, Response, Status};
use tracing::{Instrument, Level, debug, error, error_span, instrument};
type InstanceInfoName = String;
struct NativelinkOperationId {
instance_name: InstanceInfoName,
client_operation_id: OperationId,
}
impl NativelinkOperationId {
const fn new(instance_name: InstanceInfoName, client_operation_id: OperationId) -> Self {
Self {
instance_name,
client_operation_id,
}
}
fn from_name(name: &str) -> Result<Self, Error> {
let (instance_name, name) = name
.rsplit_once('/')
.err_tip(|| "Expected instance_name and name to be separated by '/'")?;
Ok(Self::new(
instance_name.to_string(),
OperationId::from(name),
))
}
}
impl fmt::Display for NativelinkOperationId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}/{}", self.instance_name, self.client_operation_id)
}
}
#[derive(Clone)]
struct InstanceInfo {
scheduler: Arc<dyn ClientStateManager>,
cas_store: Store,
}
impl fmt::Debug for InstanceInfo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("InstanceInfo")
.field("cas_store", &self.cas_store)
.finish_non_exhaustive()
}
}
impl InstanceInfo {
async fn build_action_info(
&self,
instance_name: String,
action_digest: DigestInfo,
action: Action,
priority: i32,
skip_cache_lookup: bool,
digest_function: DigestHasherFunc,
) -> Result<ActionInfo, Error> {
let command_digest = DigestInfo::try_from(
action
.command_digest
.clone()
.err_tip(|| "Expected command_digest to exist")?,
)
.err_tip(|| "Could not decode command digest")?;
let input_root_digest = DigestInfo::try_from(
action
.clone()
.input_root_digest
.err_tip(|| "Expected input_digest_root")?,
)?;
let timeout = action.timeout.map_or(Duration::MAX, |v| {
Duration::new(v.seconds as u64, v.nanos as u32)
});
let mut platform_properties = HashMap::new();
if let Some(platform) = action.platform {
for property in platform.properties {
platform_properties.insert(property.name, property.value);
}
}
// Goma puts the properties in the Command.
if platform_properties.is_empty() {
let command =
get_and_decode_digest::<Command>(&self.cas_store, command_digest.into()).await?;
if let Some(platform) = command.platform {
for property in platform.properties {
platform_properties.insert(property.name, property.value);
}
}
}
let action_key = ActionUniqueKey {
instance_name,
digest_function,
digest: action_digest,
};
let unique_qualifier = if skip_cache_lookup {
ActionUniqueQualifier::Uncacheable(action_key)
} else {
ActionUniqueQualifier::Cacheable(action_key)
};
Ok(ActionInfo {
command_digest,
input_root_digest,
timeout,
platform_properties,
priority,
load_timestamp: UNIX_EPOCH,
insert_timestamp: SystemTime::now(),
unique_qualifier,
})
}
}
#[derive(Debug, Clone)]
pub struct ExecutionServer {
instance_infos: HashMap<InstanceName, InstanceInfo>,
}
type ExecuteStream = Pin<Box<dyn Stream<Item = Result<Operation, Status>> + Send>>;
impl ExecutionServer {
pub fn new(
configs: &[WithInstanceName<ExecutionConfig>],
scheduler_map: &HashMap<String, Arc<dyn ClientStateManager>>,
store_manager: &StoreManager,
) -> Result<Self, Error> {
let mut instance_infos = HashMap::with_capacity(configs.len());
for config in configs {
let cas_store = store_manager.get_store(&config.cas_store).ok_or_else(|| {
make_input_err!("'cas_store': '{}' does not exist", config.cas_store)
})?;
let scheduler = scheduler_map
.get(&config.scheduler)
.err_tip(|| {
format!(
"Scheduler needs config for '{}' because it exists in execution",
config.scheduler
)
})?
.clone();
instance_infos.insert(
config.instance_name.clone(),
InstanceInfo {
scheduler,
cas_store,
},
);
}
Ok(Self { instance_infos })
}
pub fn into_service(self) -> Server<Self> {
Server::new(self)
}
pub fn into_operations_service(self) -> OperationsServer<Self> {
OperationsServer::new(self)
}
fn to_execute_stream(
nl_client_operation_id: &NativelinkOperationId,
action_listener: Box<dyn ActionStateResult>,
) -> impl Stream<Item = Result<Operation, Status>> + Send + use<> {
let client_operation_id = OperationId::from(nl_client_operation_id.to_string());
unfold(Some(action_listener), move |maybe_action_listener| {
let client_operation_id = client_operation_id.clone();
async move {
let mut action_listener = maybe_action_listener?;
match action_listener.changed().await {
Ok((action_update, _maybe_origin_metadata)) => {
debug!(?action_update, "Execute Resp Stream");
Some((
Ok(action_update.as_operation(client_operation_id)),
(!action_update.stage.is_finished()).then_some(action_listener),
))
}
Err(err) => {
error!(?err, "Error in action_listener stream");
Some((Err(err.into()), None))
}
}
}
})
}
async fn inner_execute(
&self,
request: ExecuteRequest,
) -> Result<impl Stream<Item = Result<Operation, Status>> + Send + use<>, Error> {
let instance_name = request.instance_name;
let instance_info = self
.instance_infos
.get(&instance_name)
.err_tip(|| format!("'instance_name' not configured for '{instance_name}'"))?;
let digest = DigestInfo::try_from(
request
.action_digest
.err_tip(|| "Expected action_digest to exist")?,
)
.err_tip(|| "Failed to unwrap action cache")?;
let priority = request
.execution_policy
.map_or(DEFAULT_EXECUTION_PRIORITY, |p| p.priority);
let action =
get_and_decode_digest::<Action>(&instance_info.cas_store, digest.into()).await?;
let action_info = instance_info
.build_action_info(
instance_name.clone(),
digest,
action,
priority,
request.skip_cache_lookup,
request
.digest_function
.try_into()
.err_tip(|| "Could not convert digest function in inner_execute()")?,
)
.await?;
debug!(?action_info, "Scheduling action");
let action_listener = instance_info
.scheduler
.add_action(OperationId::default(), Arc::new(action_info))
.await
.err_tip(|| "Failed to schedule task")?;
Ok(Box::pin(Self::to_execute_stream(
&NativelinkOperationId::new(
instance_name,
action_listener
.as_state()
.await
.err_tip(|| "In ExecutionServer::inner_execute")?
.0
.client_operation_id
.clone(),
),
action_listener,
)))
}
async fn inner_wait_execution(
&self,
request: WaitExecutionRequest,
) -> Result<impl Stream<Item = Result<Operation, Status>> + Send + use<>, Status> {
let nl_operation_id = NativelinkOperationId::from_name(&request.name)
.err_tip(|| "Failed to parse operation_id in ExecutionServer::wait_execution")?;
let Some(instance_info) = self.instance_infos.get(&nl_operation_id.instance_name) else {
return Err(Status::not_found(format!(
"No scheduler with the instance name {}",
nl_operation_id.instance_name,
)));
};
let Some(rx) = instance_info
.scheduler
.filter_operations(OperationFilter {
client_operation_id: Some(nl_operation_id.client_operation_id.clone()),
..Default::default()
})
.await
.err_tip(|| "Error running find_existing_action in ExecutionServer::wait_execution")?
.next()
.await
else {
return Err(Status::not_found("Failed to find existing task"));
};
Ok(Self::to_execute_stream(&nl_operation_id, rx))
}
}
#[tonic::async_trait]
impl Execution for ExecutionServer {
type ExecuteStream = ExecuteStream;
type WaitExecutionStream = ExecuteStream;
#[instrument(
err,
level = Level::ERROR,
skip_all,
fields(request = ?grpc_request.get_ref())
)]
async fn execute(
&self,
grpc_request: Request<ExecuteRequest>,
) -> Result<Response<ExecuteStream>, Status> {
let request = grpc_request.into_inner();
let digest_function = request.digest_function;
let result = self
.inner_execute(request)
.instrument(error_span!("execution_server_execute"))
.with_context(
make_ctx_for_hash_func(digest_function)
.err_tip(|| "In ExecutionServer::execute")?,
)
.await
.err_tip(|| "Failed on execute() command")?;
Ok(Response::new(Box::pin(result)))
}
#[instrument(
err,
level = Level::ERROR,
skip_all,
fields(request = ?grpc_request.get_ref())
)]
async fn wait_execution(
&self,
grpc_request: Request<WaitExecutionRequest>,
) -> Result<Response<ExecuteStream>, Status> {
let request = grpc_request.into_inner();
let stream_result = self
.inner_wait_execution(request)
.await
.err_tip(|| "Failed on wait_execution() command")
.map_err(Into::into);
let stream = match stream_result {
Ok(stream) => stream,
Err(e) => return Err(e),
};
debug!(return = "Ok(<stream>)");
Ok(Response::new(Box::pin(stream)))
}
}
#[tonic::async_trait]
impl Operations for ExecutionServer {
async fn list_operations(
&self,
_request: Request<ListOperationsRequest>,
) -> Result<Response<ListOperationsResponse>, Status> {
Err(Status::unimplemented("list_operations not implemented"))
}
async fn delete_operation(
&self,
_request: Request<DeleteOperationRequest>,
) -> Result<Response<()>, Status> {
Err(Status::unimplemented("delete_operation not implemented"))
}
async fn cancel_operation(
&self,
_request: Request<CancelOperationRequest>,
) -> Result<Response<()>, Status> {
Err(Status::unimplemented("cancel_operation not implemented"))
}
async fn get_operation(
&self,
request: Request<GetOperationRequest>,
) -> Result<Response<Operation>, Status> {
let inner_request = request.into_inner();
let mut stream = Box::pin(
self.inner_wait_execution(WaitExecutionRequest {
name: inner_request.name,
})
.await?,
);
let operation = stream
.next()
.await
.ok_or_else(|| Status::not_found("Operation not found"))??;
Ok(Response::new(operation))
}
async fn wait_operation(
&self,
request: Request<WaitOperationRequest>,
) -> Result<Response<Operation>, Status> {
let inner_request = request.into_inner();
let timeout_opt = inner_request.timeout.map(|d| {
let secs = u64::try_from(d.seconds).unwrap_or(0);
let nanos = u32::try_from(d.nanos).unwrap_or(0);
Duration::new(secs, nanos)
});
let mut stream = Box::pin(
self.inner_wait_execution(WaitExecutionRequest {
name: inner_request.name,
})
.await?,
);
let mut last_operation = stream
.next()
.await
.ok_or_else(|| Status::not_found("Operation not found"))??;
if last_operation.done {
return Ok(Response::new(last_operation));
}
let end_time = timeout_opt.map(|t| tokio::time::Instant::now() + t);
loop {
let next_fut = stream.next();
let next_res = if let Some(end) = end_time {
match tokio::time::timeout_at(end, next_fut).await {
Ok(res) => res,
Err(_) => break,
}
} else {
next_fut.await
};
match next_res {
Some(Ok(operation)) => {
let is_done = operation.done;
last_operation = operation;
if is_done {
break;
}
}
Some(Err(e)) => return Err(e),
None => break,
}
}
Ok(Response::new(last_operation))
}
}
#[cfg(test)]
#[test]
fn test_nl_op_id_from_name() -> Result<(), Box<dyn core::error::Error>> {
let examples = [("foo/bar", "foo"), ("a/b/c/d", "a/b/c")];
for (input, expected) in examples {
let id = NativelinkOperationId::from_name(input)?;
assert_eq!(id.instance_name, expected);
}
Ok(())
}