ora_client/executor/
run.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
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
517
518
519
520
521
522
use std::{panic::AssertUnwindSafe, sync::Arc, time::UNIX_EPOCH};

use eyre::{Context, OptionExt};
use futures::FutureExt;
use ora_proto::{
    common::v1::JobType,
    server::v1::{
        executor_message::ExecutorMessageKind, server_message::ServerMessageKind,
        ExecutorCapabilities, ExecutorConnectionRequest, ExecutorConnectionResponse,
        ExecutorHeartbeat, ExecutorMessage,
    },
};
use parking_lot::Mutex;
use tokio_util::sync::CancellationToken;
use tracing::Instrument;
use uuid::Uuid;
use wgroup::WaitGroup;

use crate::{executor::ExecutionContext, IndexMap};

use super::{ExecutionHandlerRaw, Executor, ExecutorOptions};

impl Executor {
    /// Run the executor until an error occurs.
    ///
    /// The error includes any errors that would prevent
    /// the executor from running, e.g. network errors.
    ///
    /// Individual task errors are not included as they are
    /// part of the job execution.
    #[tracing::instrument(skip_all, name = "executor_loop", fields(executor_id, executor_name))]
    pub async fn run(&mut self) -> eyre::Result<()> {
        let executor_span = tracing::Span::current();

        executor_span.record("executor_name", &self.options.name);

        let (executor_requests, recv) = flume::bounded(0);

        let mut state = ExecutorState {
            executor_id: None,
            options: &self.options,
            handlers: &self.handlers,
            executor_requests,
            // Start with a pessimistic heartbeat interval.
            heartbeat_interval: std::time::Duration::from_secs(1),
            in_progress_executions: Arc::new(Mutex::new(IndexMap::default())),
            wg: WaitGroup::new(),
        };

        let send_chan_guard = state.wg.add_with("send-channel");

        let mut server_messages = self
            .client
            // `Receiver::into_stream` exists, however it will cause
            // this future to be `!Send` and the compiler goes absolutely
            // bonkers about it.
            .executor_connection(tonic::Request::new(async_stream::stream!({
                loop {
                    tokio::select! {
                        _ = send_chan_guard.waiting() => {
                            tracing::debug!("send channel closed, stopping stream");
                            return;
                        }
                        msg = recv.recv_async() => {
                            if let Ok(msg) = msg {
                                yield msg;
                            } else {
                                tracing::debug!("send channel closed, stopping stream");
                                return;
                            }
                        }
                    }
                }
            })))
            .await?
            .into_inner();

        // Initial setup messages.
        state
            .executor_requests
            .send_async(ExecutorConnectionRequest {
                message: Some(ExecutorMessage {
                    executor_message_kind: Some(ExecutorMessageKind::Capabilities(
                        ExecutorCapabilities {
                            max_concurrent_executions: state
                                .options
                                .max_concurrent_executions
                                .get(),
                            name: state.options.name.clone(),
                            supported_job_types: self
                                .handlers
                                .iter()
                                .map(|h| {
                                    let handler_meta = h.job_type_metadata();

                                    JobType {
                                        id: handler_meta.id.to_string(),
                                        name: handler_meta.name.clone(),
                                        description: handler_meta.description.clone(),
                                        input_schema_json: handler_meta.input_schema_json.clone(),
                                        output_schema_json: handler_meta.output_schema_json.clone(),
                                    }
                                })
                                .collect(),
                        },
                    )),
                }),
            })
            .await?;

        loop {
            tokio::select! {
                _ = tokio::time::sleep(state.heartbeat_interval) => {
                    if state.executor_requests.send(ExecutorConnectionRequest {
                        message: Some(ExecutorMessage {
                            executor_message_kind: Some(ExecutorMessageKind::Heartbeat(
                                ExecutorHeartbeat {},
                            )),
                        }),
                    }).is_err() {
                        return Ok(());
                    }
                }
                server_msg = server_messages.message() => {
                    match server_msg {
                        Ok(Some(server_msg)) => {
                            handle_server_response(&mut state, &executor_span, server_msg).await?;
                        }
                        Ok(None) => {
                            tracing::info!("incoming stream closed by the server");

                            if !state.in_progress_executions.lock().is_empty() {
                                tracing::warn!("cancelling executions in progress");

                                loop {
                                    let execution_state = {
                                        let mut in_progress_executions = state.in_progress_executions.lock();

                                        if in_progress_executions.is_empty() {
                                            break;
                                        }

                                        let execution_id = in_progress_executions.keys().copied().next();

                                        if let Some(execution_id) = execution_id {
                                            in_progress_executions.swap_remove(&execution_id)
                                        } else {
                                            None
                                        }
                                    };

                                    if let Some(mut execution_state) = execution_state {
                                        execution_state.cancellation_token.cancel();

                                        tokio::select! {
                                            _ = &mut execution_state.handle => {}
                                            _ = tokio::time::sleep(state.options.cancellation_grace_period) => {
                                                execution_state.handle.abort();
                                            }
                                        }
                                    } else {
                                        break;
                                    }
                                }
                            }

                            return Ok(());
                        }
                        Err(error) => {
                            tracing::warn!(?error, "received error from the server");
                        }
                    }
                }
            }
        }
    }
}

#[tracing::instrument(name = "handle_server_message", skip_all)]
async fn handle_server_response(
    state: &mut ExecutorState<'_>,
    executor_span: &tracing::Span,
    response: ExecutorConnectionResponse,
) -> eyre::Result<()> {
    let Some(message) = response.message else {
        tracing::warn!("received empty message from the server");
        return Ok(());
    };

    let Some(message) = message.server_message_kind else {
        tracing::warn!("received unknown or missing message kind from the server");
        return Ok(());
    };

    match message {
        ServerMessageKind::Properties(executor_properties) => {
            executor_span.record("executor_id", &executor_properties.executor_id);
            state.executor_id = Some(executor_properties.executor_id);

            if let Some(max_hb_interval) = executor_properties.max_heartbeat_interval {
                if let Ok(max_hb_interval) = std::time::Duration::try_from(max_hb_interval) {
                    state.heartbeat_interval = max_hb_interval / 2;
                    tracing::debug!(
                        heartbeat_interval = ?state.heartbeat_interval,
                        "using heartbeat interval"
                    );
                }
            }

            tracing::info!("received executor properties");
        }
        ServerMessageKind::ExecutionReady(execution_ready) => {
            spawn_execution(state, execution_ready).await?;
        }
        ServerMessageKind::ExecutionCancelled(execution_cancelled) => {
            let execution_id: Uuid = execution_cancelled
                .execution_id
                .parse()
                .wrap_err("expected execution ID to be UUID")?;

            let execution_state = state
                .in_progress_executions
                .lock()
                .swap_remove(&execution_id);

            if let Some(execution_state) = execution_state {
                tokio::spawn(
                    cancel_execution(execution_state, state.options.cancellation_grace_period)
                        .instrument(tracing::Span::current()),
                );
            } else {
                tracing::warn!("received cancellation for unknown execution");
            }
        }
    }

    Ok(())
}

#[tracing::instrument(skip_all, fields(
    execution_id = %execution_state.execution_id,
))]
async fn cancel_execution(mut execution_state: ExecutionState, grace_period: std::time::Duration) {
    tracing::debug!("cancelling execution");
    execution_state.cancellation_token.cancel();

    tokio::select! {
        _ = &mut execution_state.handle => {
            tracing::debug!("execution cancelled");
        }
        _ = tokio::time::sleep(grace_period) => {
            if !execution_state.handle.is_finished() {
                tracing::warn!("execution did not cancel in time, aborting forcefully");
                execution_state.handle.abort();
            }
        }
    }

    tracing::debug!("cancelled execution");
}

#[tracing::instrument(skip_all,
    fields(
        execution_id = %execution_ready.execution_id,
        job_id = %execution_ready.job_id,
    )
)]
async fn spawn_execution(
    state: &ExecutorState<'_>,
    execution_ready: ora_proto::server::v1::ExecutionReady,
) -> eyre::Result<()> {
    let execution_span = tracing::Span::current();

    let executor_requests = state.executor_requests.clone();

    tracing::debug!("received new execution");

    let execution_id: Uuid = execution_ready
        .execution_id
        .parse()
        .wrap_err("expected execution ID to be UUID")?;

    let job_id: Uuid = execution_ready
        .job_id
        .parse()
        .wrap_err("expected job ID to be UUID")?;

    let cancellation_token = CancellationToken::new();

    let ctx = ExecutionContext {
        execution_id,
        job_id,
        target_execution_time: execution_ready
            .target_execution_time
            .and_then(|t| t.try_into().ok())
            .unwrap_or(UNIX_EPOCH),
        attempt_number: execution_ready.attempt_number,
        job_type_id: execution_ready.job_type_id,
        cancellation_token: cancellation_token.clone(),
    };

    let handler = state
        .handlers
        .iter()
        .find(|h| h.can_execute(&ctx))
        .ok_or_eyre("no handler found for the execution")?
        .clone();

    tracing::trace!("found handler for the execution");

    let now = std::time::SystemTime::now();

    if executor_requests
        .send_async(ExecutorConnectionRequest {
            message: Some(ExecutorMessage {
                executor_message_kind: Some(ExecutorMessageKind::ExecutionStarted(
                    ora_proto::server::v1::ExecutionStarted {
                        timestamp: Some(now.into()),
                        execution_id: execution_ready.execution_id,
                    },
                )),
            }),
        })
        .await
        .is_err()
    {
        tracing::debug!("not sending execution started message, executor is shutting down");
        return Ok(());
    }
    tracing::trace!("sent execution started message");

    let execution_guard = state.wg.add_with(&format!("execution-{execution_id}"));

    let cancellation_grace_period = state.options.cancellation_grace_period;

    let handle = tokio::spawn({
        let in_progress_executions = state.in_progress_executions.clone();
        let in_progress_executions2 = state.in_progress_executions.clone();
        tracing::debug!("executing handler");

        let execution_id = ctx.execution_id;

        async move {
            let mut warn_bomb = ExecutionDropWarnBomb::new(tracing::Span::current());

            let handler_fut = async move {
                match AssertUnwindSafe(handler.execute(ctx, &execution_ready.input_payload_json))
                    .catch_unwind()
                    .await
                {
                    Ok(task_result) => match task_result {
                        Ok(output_json) => {
                            tracing::debug!("execution succeeded");
                            let now = std::time::SystemTime::now();

                            if let Err(error) = executor_requests
                                .send_async(ExecutorConnectionRequest {
                                    message: Some(ExecutorMessage {
                                        executor_message_kind: Some(
                                            ExecutorMessageKind::ExecutionSucceeded(
                                                ora_proto::server::v1::ExecutionSucceeded {
                                                    timestamp: Some(now.into()),
                                                    execution_id: execution_id.to_string(),
                                                    output_payload_json: output_json,
                                                },
                                            ),
                                        ),
                                    }),
                                })
                                .await
                            {
                                tracing::warn!(?error, "failed to send execution result");
                            }
                        }
                        Err(error) => {
                            tracing::debug!(error, "execution failed");
                            let now = std::time::SystemTime::now();

                            if let Err(error) = executor_requests
                                .send_async(ExecutorConnectionRequest {
                                    message: Some(ExecutorMessage {
                                        executor_message_kind: Some(
                                            ExecutorMessageKind::ExecutionFailed(
                                                ora_proto::server::v1::ExecutionFailed {
                                                    timestamp: Some(now.into()),
                                                    execution_id: execution_id.to_string(),
                                                    error_message: error,
                                                },
                                            ),
                                        ),
                                    }),
                                })
                                .await
                            {
                                tracing::warn!(?error, "failed to send execution result");
                            }
                        }
                    },
                    Err(panic_out) => {
                        tracing::warn!("handler panicked");
                        let now = std::time::SystemTime::now();

                        let error_message = if let Some(error) = panic_out.downcast_ref::<&str>() {
                            (*error).to_string()
                        } else if let Some(error) = panic_out.downcast_ref::<String>() {
                            error.clone()
                        } else {
                            "handler panicked".to_string()
                        };

                        if let Err(error) = executor_requests
                            .send_async(ExecutorConnectionRequest {
                                message: Some(ExecutorMessage {
                                    executor_message_kind: Some(
                                        ExecutorMessageKind::ExecutionFailed(
                                            ora_proto::server::v1::ExecutionFailed {
                                                timestamp: Some(now.into()),
                                                execution_id: execution_id.to_string(),
                                                error_message,
                                            },
                                        ),
                                    ),
                                }),
                            })
                            .await
                        {
                            tracing::warn!(?error, "failed to send execution result");
                        }
                    }
                }

                if in_progress_executions
                    .lock()
                    .swap_remove(&execution_id)
                    .is_none()
                {
                    tracing::debug!(
                        "execution was not found in the in-progress list, it must have been cancelled"
                    );
                }
            };

            let mut handler_fut = std::pin::pin!(handler_fut);

            loop {
                tokio::select! {
                    _ = execution_guard.waiting() => {
                        let execution_state = in_progress_executions2.lock().swap_remove(&execution_id);

                        if let Some(execution_state) = execution_state {
                            tokio::spawn(
                                cancel_execution(execution_state, cancellation_grace_period)
                                .instrument(tracing::Span::current()));
                        }

                        (&mut handler_fut).await;
                    }
                    _ = &mut handler_fut => {
                        break;
                    }
                }
            }
            warn_bomb.defuse();
        }
        .instrument(execution_span)
    });

    state.in_progress_executions.lock().insert(
        execution_id,
        ExecutionState {
            execution_id,
            cancellation_token,
            handle,
        },
    );

    Ok(())
}

struct ExecutorState<'s> {
    executor_id: Option<String>,
    options: &'s ExecutorOptions,
    handlers: &'s [Arc<dyn ExecutionHandlerRaw + Send + Sync>],
    executor_requests: flume::Sender<ExecutorConnectionRequest>,
    heartbeat_interval: std::time::Duration,
    in_progress_executions: Arc<Mutex<IndexMap<Uuid, ExecutionState>>>,
    wg: WaitGroup,
}

struct ExecutionState {
    execution_id: Uuid,
    cancellation_token: CancellationToken,
    handle: tokio::task::JoinHandle<()>,
}

struct ExecutionDropWarnBomb {
    span: tracing::Span,
    defused: bool,
}

impl ExecutionDropWarnBomb {
    fn new(span: tracing::Span) -> Self {
        Self {
            span,
            defused: false,
        }
    }

    fn defuse(&mut self) {
        self.defused = true;
    }
}

impl Drop for ExecutionDropWarnBomb {
    fn drop(&mut self) {
        if !self.defused {
            self.span.in_scope(|| {
                tracing::warn!("execution was dropped during execution");
            });
        }
    }
}