scientific-workflow 0.10.0

Configuration-driven scientific tasks, typed state, and durable recordings
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
517
518
519
//! Validated Workflow-owned study manifest declarations.

use std::collections::HashSet;
use std::num::NonZeroU64;
use std::path::{Path, PathBuf};
use std::time::Duration;

use serde::Deserialize;
use serde_json::{Map, Value};

use super::error::ConfigError;
use super::parameters::ResolvedTask;
use super::python::PythonTaskDeclaration;

/// Effective policy for isolated study replicates.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct ReplicatePolicy {
    count: u64,
    scheduling: ReplicateScheduling,
    failure_policy: FailurePolicy,
}

impl ReplicatePolicy {
    /// Returns the positive number of replicate executions.
    pub const fn count(self) -> u64 {
        self.count
    }

    /// Returns the effective replicate scheduling policy.
    pub const fn scheduling(self) -> ReplicateScheduling {
        self.scheduling
    }

    /// Returns the effective response to a failed replicate.
    pub const fn failure_policy(self) -> FailurePolicy {
        self.failure_policy
    }
}

/// Effective scheduling mode for isolated replicates.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ReplicateScheduling {
    /// Complete one replicate before launching the next.
    Sequential,
    /// Launch eligible replicates concurrently.
    Parallel,
}

/// Effective failure propagation policy.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum FailurePolicy {
    /// Prevent further admission after the first failure.
    FailFast,
    /// Allow already-declared sibling work to finish.
    FinishAll,
}

/// The validated Workflow-owned portion of `study.json`.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct StudyManifest {
    replicates: ReplicatePolicy,
    persistence: PersistenceSpecification,
}

impl StudyManifest {
    /// Returns the complete effective replicate policy.
    pub const fn replicate_policy(self) -> ReplicatePolicy {
        self.replicates
    }

    /// Returns the complete effective local-persistence settings.
    pub const fn persistence(self) -> PersistenceSpecification {
        self.persistence
    }
}

/// Effective persistence settings parsed from the study manifest.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PersistenceSpecification {
    chunk_target_bytes: NonZeroU64,
    queue_capacity_bytes: NonZeroU64,
}

impl PersistenceSpecification {
    /// Returns the positive approximate local chunk rollover target.
    pub const fn chunk_target_bytes(self) -> NonZeroU64 {
        self.chunk_target_bytes
    }

    /// Returns the positive per-stream backpressure capacity.
    pub const fn queue_capacity_bytes(self) -> NonZeroU64 {
        self.queue_capacity_bytes
    }
}

/// One validated phase with resolved generic tasks and effective policy.
#[derive(Clone, Debug)]
pub struct PhaseSpecification {
    pub(crate) name: Box<str>,
    pub(crate) dependencies: Box<[Box<str>]>,
    pub(crate) tasks: Box<[ResolvedTask]>,
    pub(crate) max_concurrency: usize,
    pub(crate) start_interval: Duration,
    pub(crate) timeout: Option<Duration>,
    pub(crate) failure_policy: FailurePolicy,
}

impl PhaseSpecification {
    /// Returns the manifest phase key.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Iterates dependency phase keys in declaration order.
    pub fn dependencies(&self) -> impl ExactSizeIterator<Item = &str> {
        self.dependencies.iter().map(Box::as_ref)
    }

    /// Returns resolved tasks in deterministic execution order.
    pub fn tasks(&self) -> &[ResolvedTask] {
        &self.tasks
    }

    /// Returns the positive effective maximum number of active tasks.
    pub const fn max_concurrency(&self) -> usize {
        self.max_concurrency
    }

    /// Returns the effective interval between task admissions.
    pub const fn start_interval(&self) -> Duration {
        self.start_interval
    }

    /// Returns the optional effective phase timeout.
    pub const fn timeout(&self) -> Option<Duration> {
        self.timeout
    }

    /// Returns the effective sibling-task failure policy.
    pub const fn failure_policy(&self) -> FailurePolicy {
        self.failure_policy
    }
}

pub(crate) struct ParsedManifest {
    pub(crate) manifest: StudyManifest,
    pub(crate) phases: Vec<ParsedPhase>,
}

pub(crate) struct ParsedPhase {
    pub(crate) name: Box<str>,
    pub(crate) dependencies: Box<[Box<str>]>,
    pub(crate) tasks: Vec<ParsedTask>,
    pub(crate) max_concurrency: usize,
    pub(crate) start_interval: Duration,
    pub(crate) timeout: Option<Duration>,
    pub(crate) failure_policy: FailurePolicy,
}

pub(crate) enum ParsedTask {
    Model {
        model: Box<str>,
        timeout: Option<Duration>,
    },
    Program {
        program: PathBuf,
        args: Box<[Box<str>]>,
        timeout: Option<Duration>,
    },
    Python {
        declaration: PythonTaskDeclaration,
        timeout: Option<Duration>,
    },
}

pub(crate) fn parse(path: &Path, value: Value) -> Result<ParsedManifest, ConfigError> {
    let raw: RawStudy = serde_json::from_value(value)
        .map_err(|error| ConfigError::invalid(path, "/", error.to_string()))?;
    if raw.phases.is_empty() {
        return Err(ConfigError::invalid(
            path,
            "/phases",
            "at least one phase must be declared",
        ));
    }
    if raw.replicates.count == 0 {
        return Err(ConfigError::invalid(
            path,
            "/replicates/count",
            "replicate count must be positive",
        ));
    }
    let chunk_target_bytes = megabytes_to_bytes(
        path,
        "/persistence/chunk_target_mb",
        raw.persistence.chunk_target_mb,
    )?;
    let queue_capacity_bytes = megabytes_to_bytes(
        path,
        "/persistence/queue_capacity_mb",
        raw.persistence.queue_capacity_mb,
    )?;

    let manifest = StudyManifest {
        replicates: ReplicatePolicy {
            count: raw.replicates.count,
            scheduling: raw.replicates.scheduling.into(),
            failure_policy: raw.replicates.failure_policy.into(),
        },
        persistence: PersistenceSpecification {
            chunk_target_bytes,
            queue_capacity_bytes,
        },
    };

    let declared_names = raw.phases.keys().cloned().collect::<HashSet<_>>();
    let mut phases = Vec::with_capacity(raw.phases.len());
    for (name, value) in raw.phases {
        validate_identifier(path, &format!("/phases/{name}"), &name, "phase")?;
        let raw: RawPhase = serde_json::from_value(value).map_err(|error| {
            ConfigError::invalid(path, format!("/phases/{name}"), error.to_string())
        })?;
        if raw.tasks.is_empty() {
            return Err(ConfigError::invalid(
                path,
                format!("/phases/{name}/tasks"),
                "a phase must declare at least one task",
            ));
        }
        if raw.max_concurrency == 0 {
            return Err(ConfigError::invalid(
                path,
                format!("/phases/{name}/max_concurrency"),
                "maximum concurrency must be positive",
            ));
        }

        let mut dependencies = Vec::with_capacity(raw.after.len());
        let mut seen_dependencies = HashSet::with_capacity(raw.after.len());
        for dependency in raw.after {
            validate_identifier(
                path,
                &format!("/phases/{name}/after"),
                &dependency,
                "phase dependency",
            )?;
            if dependency == name {
                return Err(ConfigError::invalid(
                    path,
                    format!("/phases/{name}/after"),
                    "a phase cannot depend on itself",
                ));
            }
            if !declared_names.contains(&dependency) {
                return Err(ConfigError::UnknownDependency {
                    phase: name.clone(),
                    dependency,
                });
            }
            if !seen_dependencies.insert(dependency.clone()) {
                return Err(ConfigError::invalid(
                    path,
                    format!("/phases/{name}/after"),
                    format!("dependency `{dependency}` is repeated"),
                ));
            }
            dependencies.push(dependency.into_boxed_str());
        }

        let mut tasks = Vec::with_capacity(raw.tasks.len());
        for (index, task) in raw.tasks.into_iter().enumerate() {
            let pointer = format!("/phases/{name}/tasks/{index}");
            let timeout = task.timeout_ms.map(Duration::from_millis);
            match (task.model, task.program, task.python) {
                (Some(model), None, None) if task.args.is_empty() => {
                    validate_identifier(path, &format!("{pointer}/model"), &model, "model")?;
                    tasks.push(ParsedTask::Model {
                        model: model.into_boxed_str(),
                        timeout,
                    });
                }
                (None, Some(program), None) => {
                    if program.as_os_str().is_empty() {
                        return Err(ConfigError::invalid(
                            path,
                            format!("{pointer}/program"),
                            "program path must be nonempty",
                        ));
                    }
                    tasks.push(ParsedTask::Program {
                        program,
                        args: task.args.into_iter().map(String::into_boxed_str).collect(),
                        timeout,
                    });
                }
                (None, None, Some(declaration)) if task.args.is_empty() => {
                    tasks.push(ParsedTask::Python {
                        declaration,
                        timeout,
                    });
                }
                _ => {
                    return Err(ConfigError::invalid(
                        path,
                        pointer,
                        "a task must declare exactly `model`, `program`, or `python`; top-level `args` are valid only for a program",
                    ));
                }
            }
        }

        phases.push(ParsedPhase {
            name: name.into_boxed_str(),
            dependencies: dependencies.into_boxed_slice(),
            tasks,
            max_concurrency: raw.max_concurrency,
            start_interval: Duration::from_millis(raw.start_interval_ms),
            timeout: raw.timeout_ms.map(Duration::from_millis),
            failure_policy: raw.failure_policy.into(),
        });
    }

    validate_acyclic(path, &phases)?;
    Ok(ParsedManifest { manifest, phases })
}

fn megabytes_to_bytes(
    path: &Path,
    pointer: &'static str,
    megabytes: NonZeroU64,
) -> Result<NonZeroU64, ConfigError> {
    megabytes
        .get()
        .checked_mul(BYTES_PER_MEGABYTE)
        .and_then(NonZeroU64::new)
        .ok_or_else(|| {
            ConfigError::invalid(
                path,
                pointer,
                "persistence size is too large to represent as a byte count",
            )
        })
}

fn validate_identifier(
    path: &Path,
    pointer: &str,
    value: &str,
    kind: &str,
) -> Result<(), ConfigError> {
    if value.is_empty() || value.trim() != value {
        return Err(ConfigError::invalid(
            path,
            pointer,
            format!("{kind} must be nonempty and contain no surrounding whitespace"),
        ));
    }
    Ok(())
}

fn validate_acyclic(path: &Path, phases: &[ParsedPhase]) -> Result<(), ConfigError> {
    fn visit(
        index: usize,
        phases: &[ParsedPhase],
        by_name: &std::collections::HashMap<&str, usize>,
        states: &mut [u8],
    ) -> Option<String> {
        if states[index] == 1 {
            return Some(phases[index].name.to_string());
        }
        if states[index] == 2 {
            return None;
        }
        states[index] = 1;
        for dependency in &phases[index].dependencies {
            let dependency = by_name[dependency.as_ref()];
            if let Some(phase) = visit(dependency, phases, by_name, states) {
                return Some(phase);
            }
        }
        states[index] = 2;
        None
    }

    let by_name = phases
        .iter()
        .enumerate()
        .map(|(index, phase)| (phase.name.as_ref(), index))
        .collect::<std::collections::HashMap<_, _>>();
    let mut states = vec![0_u8; phases.len()];
    for index in 0..phases.len() {
        if let Some(phase) = visit(index, phases, &by_name, &mut states) {
            return Err(ConfigError::invalid(
                path,
                "/phases",
                format!("phase dependency cycle includes `{phase}`"),
            ));
        }
    }
    Ok(())
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RawStudy {
    #[serde(default)]
    replicates: RawReplicatePolicy,
    #[serde(default)]
    persistence: RawPersistence,
    phases: Map<String, Value>,
}

const BYTES_PER_MEGABYTE: u64 = 1_000_000;
const DEFAULT_PERSISTENCE_MB: u64 = 64;

#[derive(Deserialize)]
#[serde(default, deny_unknown_fields)]
struct RawPersistence {
    chunk_target_mb: NonZeroU64,
    queue_capacity_mb: NonZeroU64,
}

impl Default for RawPersistence {
    fn default() -> Self {
        Self {
            chunk_target_mb: NonZeroU64::new(DEFAULT_PERSISTENCE_MB)
                .expect("the built-in persistence megabyte setting is positive"),
            queue_capacity_mb: NonZeroU64::new(DEFAULT_PERSISTENCE_MB)
                .expect("the built-in persistence megabyte setting is positive"),
        }
    }
}

#[derive(Deserialize)]
#[serde(default, deny_unknown_fields)]
struct RawReplicatePolicy {
    count: u64,
    scheduling: RawReplicateScheduling,
    failure_policy: RawFailurePolicy,
}

impl Default for RawReplicatePolicy {
    fn default() -> Self {
        Self {
            count: 1,
            scheduling: RawReplicateScheduling::default(),
            failure_policy: RawFailurePolicy::default(),
        }
    }
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RawPhase {
    #[serde(default)]
    after: Vec<String>,
    tasks: Vec<RawTask>,
    #[serde(default = "default_max_concurrency")]
    max_concurrency: usize,
    #[serde(default)]
    start_interval_ms: u64,
    #[serde(default)]
    timeout_ms: Option<u64>,
    #[serde(default)]
    failure_policy: RawFailurePolicy,
}

const fn default_max_concurrency() -> usize {
    1
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RawTask {
    #[serde(default)]
    model: Option<String>,
    #[serde(default)]
    program: Option<PathBuf>,
    #[serde(default)]
    python: Option<PythonTaskDeclaration>,
    #[serde(default)]
    args: Vec<String>,
    #[serde(default)]
    timeout_ms: Option<u64>,
}

#[derive(Default, Deserialize)]
#[serde(rename_all = "snake_case")]
enum RawReplicateScheduling {
    #[default]
    Sequential,
    Parallel,
}

impl From<RawReplicateScheduling> for ReplicateScheduling {
    fn from(value: RawReplicateScheduling) -> Self {
        match value {
            RawReplicateScheduling::Sequential => Self::Sequential,
            RawReplicateScheduling::Parallel => Self::Parallel,
        }
    }
}

#[derive(Default, Deserialize)]
#[serde(rename_all = "snake_case")]
enum RawFailurePolicy {
    #[default]
    FailFast,
    FinishAll,
}

impl From<RawFailurePolicy> for FailurePolicy {
    fn from(value: RawFailurePolicy) -> Self {
        match value {
            RawFailurePolicy::FailFast => Self::FailFast,
            RawFailurePolicy::FinishAll => Self::FinishAll,
        }
    }
}