aquila_compute_aws 0.7.0-rc.13

AWS Batch compute backend for the aquila server
Documentation
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
use aquila_core::prelude::*;

use aws_sdk_batch::{
    Client as BatchClient,
    error::SdkError,
    types::{
        ContainerOverrides, ContainerProperties, JobDefinitionType, JobStatus as AwsJobStatus,
        KeyValuePair, ResourceRequirement, ResourceType,
    },
};
use aws_sdk_cloudwatchlogs::{Client as LogsClient, operation::get_log_events::GetLogEventsError};

use futures::stream::{self, BoxStream, StreamExt};

use std::{
    collections::{HashMap, VecDeque},
    time::Duration,
};

use uuid::Uuid;

#[derive(Clone, Debug)]
pub struct AwsBatchBackend {
    batch: BatchClient,
    logs: LogsClient,
    default_queue: String,
    profiles: HashMap<String, String>,
}

impl AwsBatchBackend {
    pub fn new(
        config: &aws_config::SdkConfig,
        default_queue: impl Into<String>,
        profiles: HashMap<String, String>,
    ) -> Self {
        Self {
            batch: BatchClient::new(config),
            logs: LogsClient::new(config),
            default_queue: default_queue.into(),
            profiles,
        }
    }

    /// Resolves the base Job Definition ARN from the request profile.
    fn get_base_arn(&self, profile: Option<&str>) -> Result<String, ComputeError> {
        let key = profile.unwrap_or("default");
        self.profiles
            .get(key)
            .cloned()
            .ok_or_else(|| ComputeError::InvalidRequest(format!("Profile '{}' not found", key)))
    }

    /// Creates a new, dynamic Job Definition based on a template ARN + Request Overrides.
    async fn register_dynamic_definition(
        &self,
        base_arn: &str,
        req: &JobRequest,
    ) -> Result<String, ComputeError> {
        let desc = self
            .batch
            .describe_job_definitions()
            .job_definitions(base_arn)
            .send()
            .await
            .map_err(|e| {
                ComputeError::System(format!("Failed to describe base definition: {:?}", e))
            })?;

        let base_def = desc.job_definitions().first().ok_or_else(|| {
            ComputeError::NotFound(format!("Base definition {} not found", base_arn))
        })?;

        let base_props = base_def.container_properties().ok_or_else(|| {
            ComputeError::System("Base definition missing container properties".into())
        })?;

        let mut requirements = base_props.resource_requirements.clone().unwrap_or_default();
        if let Some(cpu) = &req.cpu {
            requirements.retain(|r| r.r#type != Some(ResourceType::Vcpu));
            requirements.push(
                ResourceRequirement::builder()
                    .r#type(ResourceType::Vcpu)
                    .value(cpu)
                    .build(),
            );
        }

        if let Some(mem) = &req.memory {
            requirements.retain(|r| r.r#type != Some(ResourceType::Memory));
            requirements.push(
                ResourceRequirement::builder()
                    .r#type(ResourceType::Memory)
                    .value(mem)
                    .build(),
            );
        }

        if let Some(_gpu) = &req.gpu {
            requirements.retain(|r| r.r#type != Some(ResourceType::Gpu));
            requirements.push(
                ResourceRequirement::builder()
                    .r#type(ResourceType::Gpu)
                    // 1 GPU for now
                    .value("1")
                    .build(),
            );
        }

        let image = req
            .img
            .clone()
            .or_else(|| base_props.image.clone())
            .ok_or_else(|| {
                ComputeError::InvalidRequest(
                    "No image specified in request or base definition".into(),
                )
            })?;

        let mut props_builder = ContainerProperties::builder()
            .image(image)
            .set_resource_requirements(Some(requirements))
            .set_environment(base_props.environment.clone())
            .set_secrets(base_props.secrets.clone())
            .set_volumes(base_props.volumes.clone())
            .set_mount_points(base_props.mount_points.clone())
            .set_ulimits(base_props.ulimits.clone())
            .set_network_configuration(base_props.network_configuration.clone())
            .set_log_configuration(base_props.log_configuration.clone());

        if let Some(role) = base_props.job_role_arn() {
            props_builder = props_builder.job_role_arn(role);
        }
        if let Some(role) = base_props.execution_role_arn() {
            props_builder = props_builder.execution_role_arn(role);
        }
        if let Some(fargate) = base_props.fargate_platform_configuration() {
            props_builder = props_builder.fargate_platform_configuration(fargate.clone());
        }
        if let Some(linux) = base_props.linux_parameters() {
            props_builder = props_builder.linux_parameters(linux.clone());
        }

        let name = format!("aquila-dynamic-{}", Uuid::new_v4());
        self.batch
            .register_job_definition()
            .job_definition_name(name)
            .r#type(JobDefinitionType::Container)
            .container_properties(props_builder.build())
            .set_retry_strategy(base_def.retry_strategy.clone())
            .set_timeout(base_def.timeout.clone())
            .set_platform_capabilities(base_def.platform_capabilities.clone())
            .send()
            .await
            .map(|r| r.job_definition_arn)
            .map_err(|e| ComputeError::System(format!("Failed to register definition: {:?}", e)))?
            .map(Ok)
            .unwrap_or(Err(ComputeError::System(
                "Failed to register definition".to_string(),
            )))
    }
}

impl ComputeBackend for AwsBatchBackend {
    async fn init(&self) -> Result<(), ComputeError> {
        self.batch
            .describe_job_queues()
            .job_queues(&self.default_queue)
            .send()
            .await
            .map(|_| ())
            .map_err(|e| ComputeError::System(format!("AWS Batch error: {}", e)))
    }

    async fn run(&self, req: JobRequest) -> Result<JobResult, ComputeError> {
        let base_arn = self.get_base_arn(req.profile.as_deref())?;
        let job = self.register_dynamic_definition(&base_arn, &req).await?;
        let env: Vec<KeyValuePair> = req
            .env
            .iter()
            .map(|(k, v)| KeyValuePair::builder().name(k).value(v).build())
            .collect();

        let mut builder = ContainerOverrides::builder().set_environment(Some(env));

        if !req.cmd.is_empty() {
            builder = builder.set_command(Some(req.cmd));
        }

        let name = format!("aquila-{}", Uuid::new_v4());
        let queue = req.queue.as_deref().unwrap_or(&self.default_queue);

        self.batch
            .submit_job()
            .job_name(name)
            .job_queue(queue)
            .job_definition(job)
            .container_overrides(builder.build())
            .send()
            .await
            .map(|output| JobResult {
                id: output.job_id.unwrap_or_default(),
                status: JobStatus::pending(),
            })
            .map_err(|e| ComputeError::System(e.to_string()))
    }

    // TODO refactor this into sensible pieces/reduce nesting
    async fn attach(
        &self,
        job_id: &str,
    ) -> Result<BoxStream<'static, Result<LogOutput, ComputeError>>, ComputeError> {
        let state = LogStreamState {
            batch: self.batch.clone(),
            logs: self.logs.clone(),
            job_id: job_id.to_string(),
            log_stream_name: None,
            next_token: None,
            buffer: VecDeque::new(),
            job_finished: false,
            terminated: false,
            error_count: 0,
            grace_checks: 0,
        };

        let stream = stream::unfold(state, |mut state| async move {
            if let Some(log) = state.buffer.pop_front() {
                return Some((Ok(log), state));
            }

            if state.terminated {
                return None;
            }

            if state.error_count > 15 {
                state.terminated = true;
                return Some((
                    Err(ComputeError::System("Too many transient errors".into())),
                    state,
                ));
            }

            loop {
                if !state.buffer.is_empty() {
                    let log = state.buffer.pop_front().unwrap();
                    return Some((Ok(log), state));
                }

                if !state.job_finished || state.log_stream_name.is_none() {
                    match state.refresh_job_status().await {
                        Ok(_) => {
                            if state.job_finished && state.log_stream_name.is_none() {
                                return None;
                            }
                        }
                        Err(e) => {
                            if state.handle_error(e) {
                                state.terminated = true;
                                return Some((
                                    Err(ComputeError::System("Batch API Error".into())),
                                    state,
                                ));
                            }
                            tokio::time::sleep(Duration::from_secs(2)).await;
                            continue;
                        }
                    }
                }

                if let Some(name) = &state.log_stream_name.clone() {
                    match state.fetch_log_events(name).await {
                        Ok(has_new_events) => {
                            state.error_count = 0;

                            if has_new_events {
                                state.grace_checks = 0;
                                continue;
                            }

                            if state.job_finished {
                                state.grace_checks += 1;
                                if state.grace_checks > 3 {
                                    return None;
                                }
                            }

                            tokio::time::sleep(Duration::from_secs(2)).await;
                            continue;
                        }
                        Err(e) => {
                            if should_retry(&e) {
                                state.handle_error(e.to_string());
                                tokio::time::sleep(Duration::from_secs(2)).await;
                                continue;
                            } else {
                                state.terminated = true;
                                return Some((Err(ComputeError::System(e.to_string())), state));
                            }
                        }
                    }
                } else {
                    tokio::time::sleep(Duration::from_secs(2)).await;
                }
            }
        });

        Ok(stream.boxed())
    }

    async fn stop(&self, id: &str) -> Result<(), ComputeError> {
        self.batch
            .terminate_job()
            .job_id(id)
            .reason("Stopped by user via Aquila API")
            .send()
            .await
            .map(|_| ())
            .map_err(|e| ComputeError::System(format!("Failed to stop job: {}", e)))
    }

    async fn get_logs(&self, id: &str) -> Result<String, ComputeError> {
        let jobs = self
            .batch
            .describe_jobs()
            .jobs(id)
            .send()
            .await
            .map_err(|e| ComputeError::System(e.to_string()))?;

        let job = jobs
            .jobs()
            .first()
            .ok_or(ComputeError::NotFound("Job not found".into()))?;

        let stream_name = job
            .container()
            .and_then(|c| c.log_stream_name())
            .ok_or(ComputeError::System("Log stream not yet available".into()))?;

        let mut events = Vec::new();
        let mut next_token = None;

        loop {
            let mut req = self
                .logs
                .get_log_events()
                .log_group_name("/aws/batch/job")
                .log_stream_name(stream_name)
                .start_from_head(true);

            if let Some(token) = &next_token {
                req = req.next_token(token);
            }

            let res = req
                .send()
                .await
                .map_err(|e| ComputeError::System(e.to_string()))?;

            if let Some(chunk) = res.events {
                for event in chunk {
                    if let Some(msg) = event.message {
                        events.push(msg);
                    }
                }
            }

            if res.next_forward_token == next_token {
                break;
            }
            next_token = res.next_forward_token;
        }

        Ok(events.join("\n"))
    }

    async fn get_status(&self, id: &str) -> Result<JobStatus, ComputeError> {
        let jobs = self
            .batch
            .describe_jobs()
            .jobs(id)
            .send()
            .await
            .map_err(|e| ComputeError::System(e.to_string()))?;

        let job = jobs
            .jobs()
            .first()
            .ok_or(ComputeError::NotFound("Job not found".into()))?;

        let state = match job.status() {
            Some(AwsJobStatus::Submitted)
            | Some(AwsJobStatus::Pending)
            | Some(AwsJobStatus::Runnable) => JobState::Pending,
            Some(AwsJobStatus::Starting) | Some(AwsJobStatus::Running) => JobState::Running,
            Some(AwsJobStatus::Succeeded) => JobState::Succeeded,
            Some(AwsJobStatus::Failed) => JobState::Failed,
            _ => JobState::Pending,
        };

        let message = job.status_reason().map(|s| s.to_string());
        let exit_code = job.container().and_then(|c| c.exit_code());

        Ok(JobStatus {
            state,
            message,
            exit_code,
            ..Default::default()
        })
    }
}

struct LogStreamState {
    batch: BatchClient,
    logs: LogsClient,
    job_id: String,
    log_stream_name: Option<String>,
    next_token: Option<String>,
    buffer: VecDeque<LogOutput>,
    job_finished: bool,
    terminated: bool,
    error_count: u32,
    grace_checks: u32,
}

impl LogStreamState {
    async fn refresh_job_status(&mut self) -> Result<(), String> {
        if self.job_finished && self.log_stream_name.is_some() {
            return Ok(());
        }

        let resp = self
            .batch
            .describe_jobs()
            .jobs(&self.job_id)
            .send()
            .await
            .map_err(|e| e.to_string())?;

        let job = resp.jobs().first().ok_or("Job not found")?;

        if matches!(
            job.status(),
            Some(AwsJobStatus::Succeeded | AwsJobStatus::Failed)
        ) {
            self.job_finished = true;
        }

        if self.log_stream_name.is_none()
            && let Some(container) = job.container()
            && let Some(ls) = container.log_stream_name()
        {
            self.log_stream_name = Some(ls.to_string());
        }

        Ok(())
    }

    /// Returns `Ok(true)` if new logs were added to the buffer, `Ok(false)` if empty.
    async fn fetch_log_events(
        &mut self,
        stream_name: &str,
    ) -> Result<bool, SdkError<GetLogEventsError>> {
        let mut req = self
            .logs
            .get_log_events()
            .log_group_name("/aws/batch/job")
            .log_stream_name(stream_name)
            .start_from_head(true);

        if let Some(token) = &self.next_token {
            req = req.next_token(token);
        }

        let out = req.send().await?;

        let next = out.next_forward_token;
        let events = out.events.unwrap_or_default();
        let any = !events.is_empty();
        if any {
            self.next_token = next;
        }

        self.buffer.extend(events.iter().map(|e| {
            let timestamp = e.timestamp().map(|ts| {
                use chrono::TimeZone;
                chrono::Utc.timestamp_millis_opt(ts).unwrap().to_rfc3339()
            });

            LogOutput {
                source: LogSource::Stdout,
                timestamp,
                message: format!("{}\n", e.message().unwrap_or_default()),
            }
        }));

        Ok(any)
    }

    fn handle_error<T: std::fmt::Debug>(&mut self, _err: T) -> bool {
        self.error_count += 1;
        self.error_count > 15
    }
}

fn should_retry(err: &SdkError<GetLogEventsError>) -> bool {
    match err {
        SdkError::DispatchFailure(_) | SdkError::TimeoutError(_) => true,
        SdkError::ServiceError(context) => match context.err() {
            GetLogEventsError::ServiceUnavailableException(_) => true,
            GetLogEventsError::ResourceNotFoundException(_) => true,
            GetLogEventsError::InvalidParameterException(_) => false,
            _ => context.raw().status().is_server_error(),
        },
        _ => false,
    }
}