razel 0.5.7

a command executor with caching for data processing pipelines
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
use crate::config::RESPONSE_FILE_PREFIX;
use crate::CGroup;
use anyhow::anyhow;
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::process::{ExitStatus, Stdio};
use std::time::Instant;
use tokio::io::AsyncWriteExt;

use crate::executors::{ExecutionResult, ExecutionStatus};

#[derive(Clone, Default)]
pub struct CustomCommandExecutor {
    pub executable: String,
    pub args: Vec<String>,
    pub env: HashMap<String, String>,
    pub stdout_file: Option<PathBuf>,
    pub stderr_file: Option<PathBuf>,
    pub timeout: Option<u16>,
}

impl CustomCommandExecutor {
    pub async fn exec(
        &self,
        sandbox_dir_option: Option<PathBuf>,
        cgroup: Option<CGroup>,
    ) -> ExecutionResult {
        let mut result: ExecutionResult = Default::default();
        let response_file_args = match self.maybe_use_response_file(&sandbox_dir_option).await {
            Ok(Some(x)) => Some(vec![x]),
            Ok(None) => None,
            Err(x) => {
                result.status = ExecutionStatus::FailedToCreateResponseFile;
                result.error = Some(x);
                return result;
            }
        };
        let cwd = sandbox_dir_option.unwrap_or_else(|| ".".into());
        let execution_start = Instant::now();
        let child = match tokio::process::Command::new(&self.executable)
            .env_clear()
            .envs(&self.env)
            .args(response_file_args.as_ref().unwrap_or(&self.args))
            .current_dir(&cwd)
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .kill_on_drop(true)
            .spawn()
        {
            Ok(child) => child,
            Err(e) => {
                result.status = ExecutionStatus::FailedToStart;
                result.error = Some(e.into());
                return result;
            }
        };
        if let Some(cgroup) = cgroup {
            cgroup.add_task("memory", child.id().unwrap()).ok();
        }
        let (exec_result, timed_out) = self.wait_with_timeout(child).await;
        match exec_result {
            Ok(output) => {
                if output.status.success() {
                    result.status = ExecutionStatus::Success;
                } else if timed_out {
                    result.status = ExecutionStatus::Timeout;
                } else {
                    (result.status, result.signal, result.error) =
                        Self::evaluate_status(output.status);
                }
                result.exit_code = output.status.code();
                result.stdout = output.stdout;
                result.stderr = output.stderr;
                if !result.success() {
                    result.improve_error_message();
                }
            }
            Err(e) => {
                result.status = ExecutionStatus::Failed;
                result.error = Some(e.into());
            }
        }
        result.exec_duration = Some(execution_start.elapsed());
        self.write_redirect_files(&cwd, &mut result).await;
        result
    }

    async fn wait_with_timeout(
        &self,
        mut child: tokio::process::Child,
    ) -> (std::io::Result<std::process::Output>, bool) {
        let timed_out = if let Some(timeout_s) = self.timeout {
            let sleep = tokio::time::sleep(std::time::Duration::from_secs(timeout_s.into()));
            tokio::pin!(sleep);
            tokio::select! {
                _ = child.wait() => {
                    false
                }
                _ = &mut sleep => {
                    let _ = child.kill().await;
                    true
                }
            }
        } else {
            false
        };
        (child.wait_with_output().await, timed_out)
    }

    pub fn args_with_executable(&self) -> Vec<String> {
        [self.executable.clone()]
            .iter()
            .chain(self.args.iter())
            .cloned()
            .collect()
    }

    pub fn command_line_with_redirects(&self) -> Vec<String> {
        [self.executable.clone()]
            .iter()
            .chain(self.args.iter())
            .chain(
                self.stdout_file
                    .as_ref()
                    .map(|x| [">".to_string(), x.to_str().unwrap().to_string()])
                    .iter()
                    .flatten(),
            )
            .chain(
                self.stderr_file
                    .as_ref()
                    .map(|x| ["2>".to_string(), x.to_str().unwrap().to_string()])
                    .iter()
                    .flatten(),
            )
            .cloned()
            .collect()
    }

    #[cfg(target_family = "windows")]
    fn evaluate_status(
        exit_status: ExitStatus,
    ) -> (ExecutionStatus, Option<i32>, Option<anyhow::Error>) {
        if exit_status.success() {
            (ExecutionStatus::Success, None, None)
        } else {
            (
                ExecutionStatus::Failed,
                None,
                Some(anyhow!("command failed: {}", exit_status)),
            )
        }
    }

    #[cfg(target_family = "unix")]
    fn evaluate_status(
        exit_status: ExitStatus,
    ) -> (ExecutionStatus, Option<i32>, Option<anyhow::Error>) {
        use std::os::unix::process::ExitStatusExt;
        if exit_status.success() {
            (ExecutionStatus::Success, None, None)
        } else if exit_status.core_dumped() {
            let signal = exit_status.signal().unwrap();
            (
                ExecutionStatus::Crashed,
                Some(signal),
                Some(anyhow!("command core dumped with signal {signal}")),
            )
        } else if let Some(signal) = exit_status.stopped_signal() {
            (
                ExecutionStatus::Crashed,
                Some(signal),
                Some(anyhow!("command stopped by signal {signal}")),
            )
        } else if let Some(signal) = exit_status.signal() {
            (
                ExecutionStatus::Crashed,
                Some(signal),
                Some(anyhow!("command terminated by signal {signal}")),
            )
        } else if let Some(exit_code) = exit_status.code() {
            (
                ExecutionStatus::Failed,
                None,
                Some(anyhow!("command failed with exit code {exit_code}")),
            )
        } else {
            (
                ExecutionStatus::Failed,
                None,
                Some(anyhow!("command failed: {}", exit_status)),
            )
        }
    }

    async fn maybe_use_response_file(
        &self,
        sandbox_dir: &Option<PathBuf>,
    ) -> Result<Option<String>, anyhow::Error> {
        if !self.is_response_file_needed() {
            return Ok(None);
        }
        let file_name = "params";
        let path = sandbox_dir
            .as_ref()
            .ok_or_else(|| anyhow!("Sandbox is required for response file!"))?
            .join(file_name);
        let mut file = tokio::fs::File::create(path).await?;
        file.write_all(self.args.join("\n").as_bytes()).await?;
        file.sync_all().await?;
        Ok(Some(RESPONSE_FILE_PREFIX.to_string() + file_name))
    }

    fn is_response_file_needed(&self) -> bool {
        /* those limits are taken from test_arg_max()
         * TODO replace hardcoded limits with running that check before executing commands */
        let (max_len, terminator_len) = if cfg!(windows) {
            (32_760, 1)
        } else if cfg!(target_os = "macos") {
            (1_048_512, 1 + std::mem::size_of::<usize>())
        } else {
            (2_097_088, 1 + std::mem::size_of::<usize>())
        };
        let mut args_len_sum = 0;
        for x in &self.args {
            args_len_sum += x.len() + terminator_len;
            if args_len_sum >= max_len {
                return true;
            }
        }
        false
    }

    async fn write_redirect_files(&self, cwd: &Path, result: &mut ExecutionResult) {
        if let Err(e) = Self::maybe_write_redirect_file(
            &self.stdout_file.as_ref().map(|x| cwd.join(x)),
            &mut result.stdout,
        )
        .await
        {
            result.status = ExecutionStatus::FailedToWriteStdoutFile;
            result.error = Some(e);
            return;
        }
        if let Err(e) = Self::maybe_write_redirect_file(
            &self.stderr_file.as_ref().map(|x| cwd.join(x)),
            &mut result.stderr,
        )
        .await
        {
            result.status = ExecutionStatus::FailedToWriteStderrFile;
            result.error = Some(e);
        }
    }

    async fn maybe_write_redirect_file(
        path: &Option<PathBuf>,
        buf: &mut Vec<u8>,
    ) -> Result<(), anyhow::Error> {
        if let Some(path) = path {
            let mut file = tokio::fs::File::create(path).await?;
            file.write_all(buf).await?;
            file.sync_all().await?;
            buf.clear();
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use crate::executors::{CustomCommandExecutor, ExecutionStatus};
    use crate::metadata::Tag;
    use crate::Razel;
    use std::path::Path;

    #[tokio::test]
    async fn exec_ok() {
        let mut razel = Razel::new();
        let command = razel
            .push_custom_command(
                "test".into(),
                "cmake".into(),
                vec!["-E".into(), "true".into()],
                Default::default(),
                vec![],
                vec![],
                None,
                None,
                vec![],
                vec![],
            )
            .map(|id| razel.get_command(id).unwrap())
            .unwrap();
        let result = command.executor.exec(Path::new("."), None, None).await;
        assert!(result.success());
        assert_eq!(result.status, ExecutionStatus::Success);
        assert_eq!(result.exit_code, Some(0));
        assert!(result.error.is_none());
    }

    #[tokio::test]
    async fn exec_fail_to_start() {
        let mut razel = Razel::new();
        let command = razel
            .push_custom_command(
                "test".into(),
                "./examples/data/a.csv".into(), // file exists but is not executable
                vec![],
                Default::default(),
                vec![],
                vec![],
                None,
                None,
                vec![],
                vec![],
            )
            .map(|id| razel.get_command(id).unwrap())
            .unwrap();
        let result = command.executor.exec(Path::new("."), None, None).await;
        assert!(!result.success());
        assert_eq!(result.status, ExecutionStatus::FailedToStart);
        assert_eq!(result.exit_code, None);
        assert!(result.error.is_some());
    }

    #[tokio::test]
    async fn exec_failed_to_run() {
        let mut razel = Razel::new();
        let command = razel
            .push_custom_command(
                "test".into(),
                "cmake".into(),
                vec!["-E".into(), "false".into()],
                Default::default(),
                vec![],
                vec![],
                None,
                None,
                vec![],
                vec![],
            )
            .map(|id| razel.get_command(id).unwrap())
            .unwrap();
        let result = command.executor.exec(Path::new("."), None, None).await;
        assert!(!result.success());
        assert_eq!(result.status, ExecutionStatus::Failed);
        assert_eq!(result.exit_code, Some(1));
        assert!(result.error.is_some());
    }

    #[tokio::test]
    async fn exec_stdout() {
        let mut razel = Razel::new();
        let command = razel
            .push_custom_command(
                "test".into(),
                "cmake".into(),
                vec!["-h".into()],
                Default::default(),
                vec![],
                vec![],
                None,
                None,
                vec![],
                vec![],
            )
            .map(|id| razel.get_command(id).unwrap())
            .unwrap();
        let result = command.executor.exec(Path::new("."), None, None).await;
        assert!(result.success());
        assert_eq!(result.status, ExecutionStatus::Success);
        assert_eq!(result.exit_code, Some(0));
        assert!(result.error.is_none());
        assert!(!result.stdout.is_empty());
        assert!(result.stderr.is_empty());
    }

    #[tokio::test]
    async fn exec_stderr() {
        let mut razel = Razel::new();
        let command = razel
            .push_custom_command(
                "test".into(),
                "cmake".into(),
                vec!["-E".into(), "hopefully-not-existing-command".into()],
                Default::default(),
                vec![],
                vec![],
                None,
                None,
                vec![],
                vec![],
            )
            .map(|id| razel.get_command(id).unwrap())
            .unwrap();
        let result = command.executor.exec(Path::new("."), None, None).await;
        assert!(!result.success());
        assert_eq!(result.status, ExecutionStatus::Failed);
        assert_eq!(result.exit_code, Some(1));
        assert!(result.error.is_some());
        assert!(result.stdout.is_empty());
        assert!(!result.stderr.is_empty());
    }

    #[tokio::test]
    async fn exec_timeout() {
        let mut razel = Razel::new();
        let command = razel
            .push_custom_command(
                "test".into(),
                "cmake".into(),
                vec!["-E".into(), "sleep".into(), "3".into()],
                Default::default(),
                vec![],
                vec![],
                None,
                None,
                vec![],
                vec![Tag::Timeout(1)],
            )
            .map(|id| razel.get_command(id).unwrap())
            .unwrap();
        let result = command.executor.exec(Path::new("."), None, None).await;
        assert!(!result.success());
        assert_eq!(result.status, ExecutionStatus::Timeout);
        assert_ne!(result.exit_code, Some(0));
    }

    /* TODO
    #[tokio::test]
    async fn exec_kill() {
        let mut razel = Scheduler::new();
        let command = razel
            .push_custom_command(
                "test".into(),
                "cmake".into(),
                vec!["-E".into(), "sleep".into(), "10".into()],
                Default::default(),
                vec![],
                vec![],
                None,
                None,
            )
            .map(|id| razel.get_command(id).unwrap())
            .unwrap();
        let result = command.executor.exec().await;
        assert!(!result.success());
        assert_eq!(result.status, ExecutionStatus::Failed);
        assert_eq!(result.exit_code, Some(-1));
        assert!(result.error.is_some());
    }
     */

    #[tokio::test]
    async fn test_arg_max() {
        let mut executor = CustomCommandExecutor {
            executable: "echo".to_string(),
            ..Default::default()
        };
        for arg in &["a", "ab", "abcdefabcdef"] {
            executor.args.clear();
            let mut lower: usize = 0;
            let mut upper: Option<usize> = None;
            let mut current = 2048;
            loop {
                executor.args.resize(current, arg.to_string().clone());
                let result = executor.exec(None, None).await;
                if result.success() {
                    lower = current;
                } else {
                    upper = Some(current);
                }
                let new_len = upper.map(|x| (lower + x) / 2).unwrap_or_else(|| lower * 2);
                if new_len == current {
                    break;
                }
                current = new_len;
            }
            let max = if cfg!(windows) {
                // add terminator for all args
                (lower - 1) * (arg.len() + 1)
            } else {
                // add terminator and pointer for all args
                (lower - 1) * (arg.len() + 1 + std::mem::size_of::<usize>())
            };
            println!("{arg:>13}: {lower:>7} {max:>7}");
        }
    }
}