gflow 0.4.15

A lightweight, single-node job scheduler written in Rust.
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
use super::*;

impl SchedulerRuntime {
    /// Save scheduler state to disk asynchronously
    pub async fn save_state(&mut self) {
        if !self.state_writable {
            self.append_journal_snapshot().await;
            return;
        }

        let state_dir = self
            .scheduler
            .state_path()
            .parent()
            .unwrap_or_else(|| std::path::Path::new("."));

        match serialization::save_state(
            &self.scheduler,
            state_dir,
            serialization::SerializationFormat::MessagePack,
        ) {
            Ok(_) => {
                if self.journal_applied {
                    if let Err(e) = tokio::fs::OpenOptions::new()
                        .write(true)
                        .truncate(true)
                        .open(&self.journal_path)
                        .await
                    {
                        tracing::warn!(
                            "Failed to truncate journal file {}: {}",
                            self.journal_path.display(),
                            e
                        );
                    } else {
                        self.journal_applied = false;
                    }
                }
            }
            Err(e) => {
                tracing::error!("Failed to save scheduler state: {}", e);
            }
        }
    }

    /// Mark state as dirty without saving immediately
    pub(super) fn mark_dirty(&mut self) {
        if !(self.state_writable || self.journal_writable) {
            return;
        }
        self.dirty = true;
        if let Some(ref saver) = self.state_saver {
            saver.mark_dirty();
        }
    }

    /// Save state only if dirty flag is set, then clear flag
    pub async fn save_state_if_dirty(&mut self) {
        if self.dirty {
            self.save_state().await;
            if self.state_writable || self.journal_writable {
                self.dirty = false;
            }
        }
    }

    /// Set the state saver handle for async background persistence
    pub fn set_state_saver(&mut self, saver: StateSaverHandle) {
        let should_kick = self.dirty;
        self.state_saver = Some(saver);
        if should_kick {
            if let Some(ref saver) = self.state_saver {
                saver.mark_dirty();
            }
        }
    }

    /// Load scheduler state from disk
    pub fn load_state(&mut self) {
        self.state_writable = true;
        self.state_load_error = None;
        self.state_backup_path = None;
        self.journal_applied = false;

        let state_dir = self
            .scheduler
            .state_path()
            .parent()
            .unwrap_or_else(|| std::path::Path::new("."))
            .to_path_buf();
        let mut loaded: Option<Scheduler> = None;

        match serialization::load_state_auto(&state_dir) {
            Ok(Some(loaded_scheduler)) => {
                match gflow::core::migrations::migrate_state(loaded_scheduler) {
                    Ok(migrated) => {
                        loaded = Some(migrated);
                    }
                    Err(e) => {
                        let json_path = state_dir.join("state.json");
                        let msgpack_path = state_dir.join("state.msgpack");
                        let backup_path = if msgpack_path.exists() {
                            &msgpack_path
                        } else {
                            &json_path
                        };

                        let (backup_result, backup_err) = backup_state_file(backup_path, "backup");
                        if let Some(err) = backup_err {
                            tracing::error!("Failed to backup state file: {}", err);
                        }

                        self.state_writable = false;
                        self.state_load_error = Some(format!(
                        "State migration failed: {e}. gflowd entered recovery mode (journal) to avoid overwriting your state file."
                    ));
                        self.state_backup_path = backup_result;
                        tracing::error!("{}", self.state_load_error.as_deref().unwrap());

                        if let Ok(Some(scheduler)) = serialization::load_state_auto(&state_dir) {
                            loaded = Some(scheduler);
                        }
                    }
                }
            }
            Ok(None) => {
                tracing::info!(
                    "No existing state file found in {}, starting fresh",
                    state_dir.display()
                );
            }
            Err(e) => {
                let json_path = state_dir.join("state.json");
                let msgpack_path = state_dir.join("state.msgpack");
                let failed_path = if msgpack_path.exists() {
                    &msgpack_path
                } else {
                    &json_path
                };

                let (backup_result, backup_err) = backup_state_file(failed_path, "corrupt");
                if let Some(err) = backup_err {
                    tracing::error!("Failed to backup corrupted state file: {}", err);
                }

                self.state_writable = false;
                self.state_load_error = Some(format!(
                    "Failed to load state file from {}: {e}. gflowd entered recovery mode (journal) to avoid overwriting your state file.",
                    state_dir.display()
                ));
                self.state_backup_path = backup_result;
                tracing::error!("{}", self.state_load_error.as_deref().unwrap());

                self.scheduler.set_next_job_id(2_000_000_000);
            }
        }

        let legacy_json_path = state_dir.join("state.json");
        if should_apply_journal(&legacy_json_path, &self.journal_path) {
            if let Some((snapshot, ts)) = load_last_journal_snapshot(&self.journal_path) {
                tracing::warn!(
                    "Loading scheduler state from journal snapshot (ts={}) at {}",
                    ts,
                    self.journal_path.display()
                );
                loaded = Some(snapshot);
                self.journal_applied = true;
                if self.state_writable {
                    self.dirty = true;
                }
            }
        }

        if let Some(scheduler) = loaded {
            self.apply_loaded_scheduler(scheduler);
        }

        self.reinitialize_runtime_resources();
    }

    pub(super) fn init_journal(&mut self) {
        self.journal_writable = false;
        self.journal_error = None;

        if let Some(parent) = self.journal_path.parent() {
            if let Err(e) = std::fs::create_dir_all(parent) {
                self.journal_error = Some(format!("Failed to create journal dir: {e}"));
                tracing::error!("{}", self.journal_error.as_deref().unwrap());
                return;
            }
        }

        match std::fs::OpenOptions::new()
            .create(true)
            .write(true)
            .truncate(false)
            .open(&self.journal_path)
        {
            Ok(_) => {
                self.journal_writable = true;
            }
            Err(e) => {
                self.journal_error = Some(format!(
                    "Failed to open journal file {}: {e}",
                    self.journal_path.display()
                ));
                tracing::error!("{}", self.journal_error.as_deref().unwrap());
            }
        }
    }

    fn apply_loaded_scheduler(&mut self, loaded: Scheduler) {
        self.scheduler.apply_persisted_state(loaded);
        self.scheduler.rebuild_user_jobs_index();
    }

    fn reinitialize_runtime_resources(&mut self) {
        match Nvml::init() {
            Ok(nvml) => {
                self.scheduler.update_gpu_slots(Self::get_gpus(&nvml));
                self.nvml = Some(nvml);
            }
            Err(e) => {
                tracing::warn!(
                    "Failed to initialize NVML during state load: {}. Running without GPU support.",
                    e
                );
                self.scheduler.update_gpu_slots(HashMap::new());
                self.nvml = None;
            }
        }

        let total_memory_mb = Self::get_total_system_memory_mb();
        self.scheduler.update_memory(total_memory_mb);
        self.scheduler.refresh_available_memory();
    }

    async fn append_journal_snapshot(&mut self) {
        if !self.journal_writable {
            tracing::error!(
                "Refusing to persist state: state.json is not writable and journal is not writable"
            );
            return;
        }

        if let Some(parent) = self.journal_path.parent() {
            if let Err(e) = tokio::fs::create_dir_all(parent).await {
                tracing::error!(
                    "Failed to create journal directory {}: {}",
                    parent.display(),
                    e
                );
                self.journal_writable = false;
                self.journal_error = Some(format!("Failed to create journal dir: {e}"));
                return;
            }
        }

        #[derive(serde::Serialize)]
        struct SchedulerSnapshot<'a> {
            version: u32,
            jobs: Vec<Job>,
            state_path: &'a std::path::PathBuf,
            next_job_id: u32,
            allowed_gpu_indices: Option<&'a Vec<u32>>,
            reservations: &'a Vec<gflow::core::reservation::GpuReservation>,
            next_reservation_id: u32,
        }

        #[derive(serde::Serialize)]
        struct JournalEntry<'a> {
            ts: u64,
            kind: &'static str,
            scheduler: SchedulerSnapshot<'a>,
        }

        let ts = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        let entry = JournalEntry {
            ts,
            kind: "snapshot",
            scheduler: SchedulerSnapshot {
                version: self.scheduler.version,
                jobs: self.scheduler.jobs_as_vec(),
                state_path: self.scheduler.state_path(),
                next_job_id: self.scheduler.next_job_id(),
                allowed_gpu_indices: self.scheduler.allowed_gpu_indices(),
                reservations: &self.scheduler.reservations,
                next_reservation_id: self.scheduler.next_reservation_id,
            },
        };

        let line = match serde_json::to_string(&entry) {
            Ok(s) => s,
            Err(e) => {
                tracing::error!("Failed to serialize journal entry: {}", e);
                return;
            }
        };

        let tmp_path = self.journal_path.with_extension("jsonl.tmp");
        match tokio::fs::File::create(&tmp_path).await {
            Ok(mut file) => {
                if let Err(e) =
                    tokio::io::AsyncWriteExt::write_all(&mut file, format!("{line}\n").as_bytes())
                        .await
                {
                    tracing::error!(
                        "Failed to write journal snapshot to {}: {}",
                        tmp_path.display(),
                        e
                    );
                    return;
                }

                if let Err(e) = file.sync_all().await {
                    tracing::warn!(
                        "Failed to fsync journal temp file {}: {}",
                        tmp_path.display(),
                        e
                    );
                }

                if let Err(e) = tokio::fs::rename(&tmp_path, &self.journal_path).await {
                    let _ = tokio::fs::remove_file(&self.journal_path).await;
                    if let Err(e2) = tokio::fs::rename(&tmp_path, &self.journal_path).await {
                        tracing::error!(
                            "Failed to move journal snapshot from {} to {}: {} (retry error: {})",
                            tmp_path.display(),
                            self.journal_path.display(),
                            e,
                            e2
                        );
                        self.journal_writable = false;
                        self.journal_error =
                            Some(format!("Failed to finalize journal snapshot: {e2}"));
                    }
                }
            }
            Err(e) => {
                tracing::error!(
                    "Failed to create journal temp file {}: {}",
                    tmp_path.display(),
                    e
                );
                self.journal_writable = false;
                self.journal_error = Some(format!("Failed to create journal temp file: {e}"));
            }
        };
    }
}

fn should_apply_journal(state_path: &std::path::Path, journal_path: &std::path::Path) -> bool {
    let Ok(j_meta) = std::fs::metadata(journal_path) else {
        return false;
    };
    if j_meta.len() == 0 {
        return false;
    }
    let Ok(j_mtime) = j_meta.modified() else {
        return true;
    };

    let Ok(s_meta) = std::fs::metadata(state_path) else {
        return true;
    };
    let Ok(s_mtime) = s_meta.modified() else {
        return true;
    };

    j_mtime >= s_mtime
}

fn load_last_journal_snapshot(journal_path: &std::path::Path) -> Option<(Scheduler, u64)> {
    #[derive(serde::Deserialize)]
    struct Entry {
        ts: u64,
        kind: String,
        scheduler: Scheduler,
    }

    let content = std::fs::read_to_string(journal_path).ok()?;
    let line = content.lines().next()?.trim();
    if line.is_empty() {
        return None;
    }
    let entry = serde_json::from_str::<Entry>(line).ok()?;
    if entry.kind != "snapshot" {
        return None;
    }
    Some((entry.scheduler, entry.ts))
}

fn backup_state_file(
    path: &std::path::Path,
    kind: &str,
) -> (Option<PathBuf>, Option<anyhow::Error>) {
    use std::time::{SystemTime, UNIX_EPOCH};

    let ts = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();

    let backup_name = match path.file_name().and_then(|n| n.to_str()) {
        Some(name) => format!("{name}.{kind}.{ts}"),
        None => format!("state.{kind}.{ts}"),
    };
    let backup_path = path.with_file_name(backup_name);
    match std::fs::copy(path, &backup_path) {
        Ok(_) => (Some(backup_path), None),
        Err(e) => (None, Some(anyhow::anyhow!(e))),
    }
}