rustvello 0.8.0

Distributed task queue and workflow runtime for Rust and Python: typed tasks, retries, priorities, triggers and pluggable backends
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
//! Executor that runs task code in a pool of external worker processes.
//!
//! Built for Python, where one interpreter means one GIL: N worker processes
//! give N independent interpreters, so CPU-bound Python tasks run in parallel
//! while the control plane (polling, admission, heartbeats, recovery) stays in
//! this process. The protocol is language-agnostic JSON lines over the worker's
//! stdin/stdout:
//!
//! - worker start: the worker prints `{"ready": true, ...}` once it can execute;
//! - request: `{"protocol": 1, "invocation_id", "task_id", "language", "module",
//!   "name", "args": {key: json}, "num_retries", "parent_invocation_id",
//!   "traceparent", "tracestate", "workflow": {...} | null,
//!   "is_workflow_defining"}`;
//! - response: `{"ok": true, "result": "<json>"}` or
//!   `{"ok": false, "error_type", "message", "traceback"}`.
//!
//! A worker that exits or breaks the protocol is dropped and replaced on the
//! next request; the in-flight invocation fails with error type
//! `WorkerProcessCrashed` so the task's retry policy decides what happens.

use std::sync::Arc;

use async_trait::async_trait;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::process::{Child, ChildStdin, ChildStdout, Command};
use tokio::sync::{Mutex, Semaphore};

use rustvello_core::context::{InvocationContext, RunnerContext};
use rustvello_core::error::{RustvelloError, RustvelloResult};
use rustvello_core::task::DynTask;
use rustvello_proto::call::SerializedArguments;
use rustvello_proto::identifiers::ExecutorKind;

use super::TaskExecutor;

/// How worker processes are launched.
#[derive(Clone, Debug)]
pub struct SubprocessSpec {
    /// Program and arguments of one worker process.
    pub command: Vec<String>,
    /// Extra environment variables for the worker processes.
    pub env: Vec<(String, String)>,
    /// Executor kind reported in runner contexts and monitoring.
    pub kind: ExecutorKind,
}

struct WorkerProcess {
    child: Child,
    stdin: ChildStdin,
    stdout: BufReader<ChildStdout>,
}

#[derive(Clone)]
pub(crate) struct SubprocessExecutor {
    spec: Arc<SubprocessSpec>,
    idle: Arc<Mutex<Vec<WorkerProcess>>>,
    permits: Arc<Semaphore>,
    size: usize,
}

impl std::fmt::Debug for SubprocessExecutor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SubprocessExecutor")
            .field("command", &self.spec.command)
            .field("size", &self.size)
            .finish_non_exhaustive()
    }
}

fn worker_crash(message: impl Into<String>) -> RustvelloError {
    RustvelloError::TaskExecution {
        error_type: "WorkerProcessCrashed".to_owned(),
        message: message.into(),
        traceback: None,
    }
}

impl SubprocessExecutor {
    pub(crate) fn new(spec: SubprocessSpec, size: usize) -> Self {
        let size = size.max(1);
        Self {
            spec: Arc::new(spec),
            idle: Arc::new(Mutex::new(Vec::with_capacity(size))),
            permits: Arc::new(Semaphore::new(size)),
            size,
        }
    }

    async fn spawn_worker(&self) -> RustvelloResult<WorkerProcess> {
        let (program, args) =
            self.spec
                .command
                .split_first()
                .ok_or_else(|| RustvelloError::Configuration {
                    message: "subprocess executor needs a worker command".to_owned(),
                })?;
        let mut child = Command::new(program)
            .args(args)
            .envs(self.spec.env.iter().map(|(k, v)| (k.as_str(), v.as_str())))
            .stdin(std::process::Stdio::piped())
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::inherit())
            .kill_on_drop(true)
            .spawn()
            .map_err(|error| RustvelloError::Configuration {
                message: format!("cannot start worker process {program:?}: {error}"),
            })?;
        let stdin = child.stdin.take().ok_or_else(|| RustvelloError::Internal {
            message: "worker stdin not piped".to_owned(),
        })?;
        let stdout = child
            .stdout
            .take()
            .ok_or_else(|| RustvelloError::Internal {
                message: "worker stdout not piped".to_owned(),
            })?;
        let mut worker = WorkerProcess {
            child,
            stdin,
            stdout: BufReader::new(stdout),
        };
        // The worker announces readiness after importing the application, so an
        // import failure surfaces here instead of failing every invocation.
        let mut ready = String::new();
        let read = worker.stdout.read_line(&mut ready).await;
        let announced = matches!(read, Ok(n) if n > 0)
            && serde_json::from_str::<serde_json::Value>(ready.trim())
                .ok()
                .and_then(|value| value.get("ready").and_then(serde_json::Value::as_bool))
                .unwrap_or(false);
        if !announced {
            let _ = worker.child.kill().await;
            return Err(RustvelloError::Configuration {
                message: format!(
                    "worker process {program:?} did not announce readiness (got {ready:?})"
                ),
            });
        }
        tracing::info!(pid = ?worker.child.id(), "worker process ready");
        Ok(worker)
    }

    async fn checkout(&self) -> RustvelloResult<WorkerProcess> {
        if let Some(worker) = self.idle.lock().await.pop() {
            return Ok(worker);
        }
        self.spawn_worker().await
    }

    fn request_json(
        task: &dyn DynTask,
        args: &SerializedArguments,
        invocation: &InvocationContext,
    ) -> String {
        let task_id = task.task_id();
        let args: serde_json::Map<String, serde_json::Value> = args
            .0
            .iter()
            .map(|(k, v)| (k.clone(), serde_json::Value::String(v.clone())))
            .collect();
        let workflow = invocation.workflow.as_ref().map(|workflow| {
            serde_json::json!({
                "workflow_id": workflow.workflow_id.to_string(),
                "workflow_type": workflow.workflow_type.to_string(),
                "parent_id": workflow.parent_id.as_ref().map(ToString::to_string),
            })
        });
        serde_json::json!({
            "protocol": 1,
            "invocation_id": invocation.invocation_id.to_string(),
            "task_id": task_id.to_string(),
            "language": task_id.language().as_str(),
            "module": task_id.module(),
            "name": task_id.name(),
            "args": args,
            "num_retries": invocation.num_retries,
            "parent_invocation_id": invocation.parent_invocation_id.as_ref().map(ToString::to_string),
            "traceparent": invocation.trace_context.traceparent,
            "tracestate": invocation.trace_context.tracestate,
            "workflow": workflow,
            "is_workflow_defining": invocation.is_workflow_defining,
        })
        .to_string()
    }

    async fn round_trip(worker: &mut WorkerProcess, request: &str) -> RustvelloResult<String> {
        worker
            .stdin
            .write_all(request.as_bytes())
            .await
            .map_err(|error| worker_crash(format!("cannot write to worker: {error}")))?;
        worker
            .stdin
            .write_all(b"\n")
            .await
            .map_err(|error| worker_crash(format!("cannot write to worker: {error}")))?;
        worker
            .stdin
            .flush()
            .await
            .map_err(|error| worker_crash(format!("cannot flush worker stdin: {error}")))?;
        let mut line = String::new();
        let read = worker
            .stdout
            .read_line(&mut line)
            .await
            .map_err(|error| worker_crash(format!("cannot read from worker: {error}")))?;
        if read == 0 {
            return Err(worker_crash(
                "worker process exited while executing the task",
            ));
        }
        let response: serde_json::Value = serde_json::from_str(line.trim()).map_err(|error| {
            worker_crash(format!("worker returned invalid JSON ({error}): {line:?}"))
        })?;
        if response.get("ok").and_then(serde_json::Value::as_bool) == Some(true) {
            return Ok(response
                .get("result")
                .and_then(serde_json::Value::as_str)
                .unwrap_or("null")
                .to_owned());
        }
        let field = |key: &str| {
            response
                .get(key)
                .and_then(serde_json::Value::as_str)
                .map(ToOwned::to_owned)
        };
        Err(RustvelloError::TaskExecution {
            error_type: field("error_type").unwrap_or_else(|| "TaskExecutionError".to_owned()),
            message: field("message").unwrap_or_default(),
            traceback: field("traceback"),
        })
    }
}

#[async_trait]
impl TaskExecutor for SubprocessExecutor {
    fn kind(&self) -> ExecutorKind {
        self.spec.kind
    }

    async fn execute(
        &self,
        task: Arc<dyn DynTask>,
        args: SerializedArguments,
        invocation_context: InvocationContext,
        _runner_context: RunnerContext,
    ) -> RustvelloResult<String> {
        let _permit = Arc::clone(&self.permits)
            .acquire_owned()
            .await
            .map_err(|error| RustvelloError::Internal {
                message: format!("subprocess executor closed: {error}"),
            })?;
        let mut worker = self.checkout().await?;
        let request = Self::request_json(task.as_ref(), &args, &invocation_context);
        let outcome = Self::round_trip(&mut worker, &request).await;
        match &outcome {
            // Protocol or process failures leave the worker unusable: drop it (kill_on_drop).
            Err(RustvelloError::TaskExecution { error_type, .. })
                if error_type == "WorkerProcessCrashed" =>
            {
                let _ = worker.child.kill().await;
            }
            _ => self.idle.lock().await.push(worker),
        }
        outcome
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use rustvello_core::task::{TaskDefinition, TaskRegistry};
    use rustvello_proto::config::TaskConfig;
    use rustvello_proto::identifiers::{InvocationId, RunnerId, TaskId, TaskLanguage};

    use super::*;

    /// A tiny protocol-speaking worker written in Python; skips when python3 is absent.
    fn python_worker(body: &str) -> Option<Vec<String>> {
        std::process::Command::new("python3")
            .arg("--version")
            .output()
            .ok()?;
        let script =
            format!("import sys, json\nprint(json.dumps({{'ready': True}}), flush=True)\n{body}\n");
        Some(vec!["python3".to_owned(), "-c".to_owned(), script])
    }

    fn echo_worker() -> Option<Vec<String>> {
        python_worker(
            "for line in sys.stdin:\n    req = json.loads(line)\n    out = {'ok': True, 'result': json.dumps({'echo': req['args'], 'task': req['task_id'], 'retries': req['num_retries']})}\n    print(json.dumps(out), flush=True)",
        )
    }

    fn task() -> Arc<dyn DynTask> {
        let task_id = TaskId::for_language(TaskLanguage::Python, "mod", "echo");
        let mut registry = TaskRegistry::new();
        registry
            .register(TaskDefinition::new(
                task_id.clone(),
                TaskConfig::default(),
                Arc::new(|_| {
                    panic!("in-process function must not run under the subprocess executor")
                }),
            ))
            .unwrap();
        registry.get_dyn(&task_id).unwrap()
    }

    fn invocation(num_retries: u32) -> InvocationContext {
        InvocationContext {
            invocation_id: InvocationId::new(),
            task_id: TaskId::for_language(TaskLanguage::Python, "mod", "echo"),
            workflow: None,
            is_workflow_defining: false,
            state_backend: None,
            parent_invocation_id: None,
            num_retries,
            trace_context: Default::default(),
        }
    }

    fn runner() -> RunnerContext {
        RunnerContext::new(
            RunnerId::new(),
            Arc::from("subprocess-test"),
            "SubprocessTest",
        )
    }

    fn spec(command: Vec<String>) -> SubprocessSpec {
        SubprocessSpec {
            command,
            env: vec![],
            kind: ExecutorKind::Python,
        }
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn round_trips_arguments_and_context() {
        let Some(command) = echo_worker() else { return };
        let executor = SubprocessExecutor::new(spec(command), 2);
        let mut args = SerializedArguments::new();
        args.insert("x".to_owned(), "1".to_owned());
        let result = executor
            .execute(task(), args, invocation(3), runner())
            .await
            .unwrap();
        let value: serde_json::Value = serde_json::from_str(&result).unwrap();
        assert_eq!(value["echo"]["x"], "1");
        assert_eq!(value["task"], "python::mod.echo");
        assert_eq!(value["retries"], 3);
        assert_eq!(executor.kind(), ExecutorKind::Python);
        // the worker went back to the pool
        assert_eq!(executor.idle.lock().await.len(), 1);
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn task_errors_keep_type_message_and_worker() {
        let Some(command) = python_worker(
            "for line in sys.stdin:\n    print(json.dumps({'ok': False, 'error_type': 'ValueError', 'message': 'bad input', 'traceback': 'tb'}), flush=True)",
        ) else {
            return;
        };
        let executor = SubprocessExecutor::new(spec(command), 1);
        let error = executor
            .execute(task(), SerializedArguments::new(), invocation(0), runner())
            .await
            .unwrap_err();
        match error {
            RustvelloError::TaskExecution {
                error_type,
                message,
                traceback,
            } => {
                assert_eq!(error_type, "ValueError");
                assert_eq!(message, "bad input");
                assert_eq!(traceback.as_deref(), Some("tb"));
            }
            other => panic!("unexpected error: {other:?}"),
        }
        assert_eq!(executor.idle.lock().await.len(), 1);
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn crashed_worker_is_reported_and_replaced() {
        let Some(command) = python_worker("sys.stdin.readline()\nsys.exit(3)") else {
            return;
        };
        let executor = SubprocessExecutor::new(spec(command), 1);
        let error = executor
            .execute(task(), SerializedArguments::new(), invocation(0), runner())
            .await
            .unwrap_err();
        assert!(
            matches!(error, RustvelloError::TaskExecution { ref error_type, .. } if error_type == "WorkerProcessCrashed"),
            "{error:?}"
        );
        assert!(executor.idle.lock().await.is_empty());
        // the next call spawns a fresh worker instead of failing permanently
        let error = executor
            .execute(task(), SerializedArguments::new(), invocation(0), runner())
            .await
            .unwrap_err();
        assert!(matches!(error, RustvelloError::TaskExecution { .. }));
    }

    #[tokio::test]
    async fn unready_worker_is_a_configuration_error() {
        if std::process::Command::new("python3")
            .arg("--version")
            .output()
            .is_err()
        {
            return;
        }
        let command = vec![
            "python3".to_owned(),
            "-c".to_owned(),
            "import sys; sys.exit(1)".to_owned(),
        ];
        let executor = SubprocessExecutor::new(spec(command), 1);
        let error = executor
            .execute(task(), SerializedArguments::new(), invocation(0), runner())
            .await
            .unwrap_err();
        assert!(
            matches!(error, RustvelloError::Configuration { .. }),
            "{error:?}"
        );
    }
}