torc 0.21.0

Workflow management system
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
// This binary is only supported on Unix systems (Slurm is Linux-only)
#[cfg(not(unix))]
fn main() {
    eprintln!("torc-slurm-job-runner is only supported on Unix systems (Linux/macOS).");
    eprintln!("Slurm is not available on Windows.");
    std::process::exit(1);
}

#[cfg(unix)]
mod unix_main {
    use chrono::Local;
    use clap::{Parser, builder::styling};
    use env_logger::Builder;
    use log::{LevelFilter, debug, error, info, warn};
    use signal_hook::consts::SIGTERM;
    use signal_hook::iterator::Signals;
    use std::fs::File;
    use std::path::PathBuf;
    use std::sync::atomic::Ordering;
    use std::thread;
    use torc::client::apis::configuration::{Configuration, TlsConfig};
    use torc::client::apis::default_api;
    use torc::client::commands::slurm::{create_compute_node, create_node_resources};
    use torc::client::config::TorcConfig;
    use torc::client::hpc::hpc_interface::HpcInterface;
    use torc::client::hpc::slurm_interface::SlurmInterface;
    use torc::client::job_runner::{JobRunner, PerNodeTracker};
    use torc::client::log_paths::{
        get_slurm_dmesg_log_file, get_slurm_env_log_file, get_slurm_job_runner_log_file,
    };
    use torc::client::utils;

    const STYLES: styling::Styles = styling::Styles::styled()
        .header(styling::AnsiColor::Green.on_default().bold())
        .usage(styling::AnsiColor::Green.on_default().bold())
        .literal(styling::AnsiColor::Cyan.on_default().bold())
        .placeholder(styling::AnsiColor::Cyan.on_default());

    #[derive(Parser, Debug)]
    #[command(name = "torc-slurm-job-runner")]
    #[command(version)]
    #[command(about = "Slurm job runner for Torc workflows", long_about = None)]
    #[command(styles = STYLES)]
    struct Args {
        /// Server URL
        #[arg()]
        url: String,

        /// Workflow ID
        #[arg()]
        workflow_id: i64,

        /// Output directory for compute nodes
        #[arg()]
        output_dir: PathBuf,

        /// Maximum number of parallel jobs to run concurrently.
        /// When NOT set: Uses resource-based job allocation (considers CPU, memory, GPU requirements).
        /// When set: Uses simple queue-based allocation with this parallel limit (ignores resource requirements).
        #[arg(long)]
        max_parallel_jobs: Option<i32>,

        /// Poll interval for job completions (seconds)
        #[arg(short, long)]
        poll_interval: Option<i64>,

        /// Set to true if this is a subtask and multiple workers are running on one Slurm allocation
        #[arg(long, default_value = "false")]
        is_subtask: bool,

        /// Wait this number of minutes if the database is offline
        #[arg(long, default_value = "20")]
        wait_for_healthy_database_minutes: u64,

        /// Path to a PEM-encoded CA certificate to trust for TLS connections
        #[arg(long, env = "TORC_TLS_CA_CERT")]
        tls_ca_cert: Option<String>,

        /// Skip TLS certificate verification (for testing only)
        #[arg(long, env = "TORC_TLS_INSECURE")]
        tls_insecure: bool,

        /// Password for authentication (can also use TORC_PASSWORD env var)
        #[arg(long, env = "TORC_PASSWORD", hide_env_values = true)]
        password: Option<String>,

        /// Log level: error, warn, info, debug, trace
        #[arg(long)]
        log_level: Option<String>,

        /// Maximum startup delay in seconds for thundering herd mitigation.
        /// Each runner sleeps a deterministic jitter in [0, N) seconds before
        /// contacting the server, spreading load when many nodes start at once.
        #[arg(long, default_value = "0")]
        startup_delay_seconds: u64,
    }

    fn workflow_has_multi_node_jobs(
        config: &Configuration,
        workflow_id: i64,
        wait_for_healthy_database_minutes: u64,
    ) -> bool {
        let mut offset = 0i64;
        loop {
            let response = match utils::send_with_retries(
                config,
                || {
                    default_api::list_resource_requirements(
                        config,
                        workflow_id,
                        None,
                        Some(offset),
                        Some(100),
                        None,
                        None,
                        None,
                        None,
                        None,
                        None,
                        None,
                        None,
                    )
                },
                wait_for_healthy_database_minutes,
            ) {
                Ok(response) => response,
                Err(e) => {
                    warn!(
                        "Could not inspect workflow resource requirements for multi-node jobs: {}. \
                         Disabling per-node placement to avoid over-allocation.",
                        e
                    );
                    return true;
                }
            };

            let items = response.items.unwrap_or_default();

            if items.iter().any(|rr| rr.num_nodes > 1) {
                return true;
            }

            if !response.has_more || items.is_empty() {
                return false;
            }
            offset += items.len() as i64;
        }
    }

    pub fn main() {
        let args = Args::parse();

        // Record start time for dmesg filtering (with 60-minute buffer)
        let dmesg_cutoff = Local::now() - chrono::Duration::minutes(60);

        // Create Slurm interface to get environment info
        let slurm_interface = match SlurmInterface::new() {
            Ok(interface) => interface,
            Err(e) => {
                eprintln!("Error creating Slurm interface: {}", e);
                std::process::exit(1);
            }
        };

        let job_id = slurm_interface.get_current_job_id();
        let node_id = slurm_interface.get_node_id();
        let task_pid = slurm_interface.get_task_pid();

        // Now we can configure the logger with the specific log file path
        let log_file_path = get_slurm_job_runner_log_file(
            args.output_dir.clone(),
            args.workflow_id,
            &job_id,
            &node_id,
            task_pid,
        );

        let log_file = match File::create(&log_file_path) {
            Ok(file) => file,
            Err(e) => {
                eprintln!("Error creating log file {}: {}", log_file_path, e);
                std::process::exit(1);
            }
        };

        // Resolve log level: CLI arg > config file > default ("info")
        let file_config = TorcConfig::load().unwrap_or_default();
        let log_level_str = args
            .log_level
            .clone()
            .unwrap_or_else(|| file_config.client.log_level.clone());

        let level_filter = match log_level_str.to_lowercase().as_str() {
            "error" => LevelFilter::Error,
            "warn" => LevelFilter::Warn,
            "info" => LevelFilter::Info,
            "debug" => LevelFilter::Debug,
            "trace" => LevelFilter::Trace,
            _ => {
                eprintln!(
                    "Warning: unknown log level '{}', defaulting to 'info'",
                    log_level_str
                );
                LevelFilter::Info
            }
        };

        // Initialize logger now that we have the log file
        let mut builder = Builder::from_default_env();
        builder
            .target(env_logger::Target::Pipe(Box::new(log_file)))
            .filter_level(level_filter)
            .init();

        let hostname = hostname::get()
            .expect("Failed to get hostname")
            .into_string()
            .expect("Hostname is not valid UTF-8");

        info!("Starting Slurm job runner (log_level={})", log_level_str);
        info!("Job ID: {}", job_id);
        info!("Node ID: {}", node_id);
        info!("Task PID: {}", task_pid);
        info!("Hostname: {}", hostname);
        info!("Output directory: {}", args.output_dir.display());
        info!("Log file: {}", log_file_path);

        // Capture SLURM environment variables for debugging
        let slurm_env_path = get_slurm_env_log_file(
            args.output_dir.clone(),
            args.workflow_id,
            &job_id,
            &node_id,
            task_pid,
        );
        utils::capture_env_vars(std::path::Path::new(&slurm_env_path), "SLURM");

        // Set up configuration with TLS
        let tls = TlsConfig {
            ca_cert_path: args.tls_ca_cert.as_ref().map(std::path::PathBuf::from),
            insecure: args.tls_insecure,
        };
        let mut config = Configuration::with_tls(tls);
        config.base_path = args.url.clone();

        // Set up authentication if password is provided
        if let Some(ref password) = args.password {
            let username =
                std::env::var("USER").unwrap_or_else(|_| std::env::var("USERNAME").unwrap());
            config.basic_auth = Some((username, Some(password.clone())));
        }

        // Stagger startup to avoid thundering herd when many compute nodes start
        // simultaneously. The delay window is set by the caller (sbatch script)
        // based on the number of concurrent allocations.
        if args.startup_delay_seconds > 0 {
            let jitter = {
                use std::collections::hash_map::DefaultHasher;
                use std::hash::{Hash, Hasher};
                let mut hasher = DefaultHasher::new();
                hostname.hash(&mut hasher);
                job_id.hash(&mut hasher);
                node_id.hash(&mut hasher);
                task_pid.hash(&mut hasher);
                hasher.finish() % args.startup_delay_seconds
            };
            info!(
                "Startup jitter: sleeping {} seconds (window={})",
                jitter, args.startup_delay_seconds
            );
            thread::sleep(std::time::Duration::from_secs(jitter));
        }

        // First, ping the server to ensure we can connect
        match utils::send_with_retries(
            &config,
            || default_api::ping(&config),
            args.wait_for_healthy_database_minutes,
        ) {
            Ok(_) => {
                info!("Successfully connected to server");
            }
            Err(e) => {
                error!("Error pinging server: {}", e);
                std::process::exit(1);
            }
        }

        let workflow = match utils::send_with_retries(
            &config,
            || default_api::get_workflow(&config, args.workflow_id),
            args.wait_for_healthy_database_minutes,
        ) {
            Ok(wf) => wf,
            Err(e) => {
                error!("Error getting workflow: {}", e);
                std::process::exit(1);
            }
        };

        if workflow.compute_node_expiration_buffer_seconds.is_some() {
            warn!(
                "compute_node_expiration_buffer_seconds is deprecated and will be ignored. \
                 Slurm now manages job termination signals via srun --time. \
                 Configure Slurm's KillWait parameter instead."
            );
        }

        let job_end_time = match slurm_interface.get_job_end_time() {
            Ok(end_time) => end_time,
            Err(e) => {
                error!("Error getting job end time: {}", e);
                std::process::exit(1);
            }
        };
        info!("Slurm job end time: {}", job_end_time);

        // All compute nodes get the scheduled compute node
        let scheduled_compute_node =
            get_scheduled_compute_node(&config, args.workflow_id, &slurm_interface);

        let scheduler_id = scheduled_compute_node.as_ref().map(|node| node.id);
        let scheduler_config_id = scheduled_compute_node
            .as_ref()
            .map(|node| node.scheduler_config_id);

        let per_node_resources =
            create_node_resources(&slurm_interface, scheduler_config_id, args.is_subtask);

        // Multiply per-node values by num_nodes to get total allocation capacity.
        // The job runner uses total capacity for its resource pool tracking
        // (decrement on job start, increment on completion). When claiming jobs
        // from the server, it converts back to per-node for correct comparison.
        let num_nodes = per_node_resources.num_nodes;
        let mut resources = torc::models::ComputeNodesResources::new(
            per_node_resources.num_cpus * num_nodes,
            per_node_resources.memory_gb * num_nodes as f64,
            per_node_resources.num_gpus * num_nodes,
            num_nodes,
        );
        resources.scheduler_config_id = per_node_resources.scheduler_config_id;
        resources.time_limit = per_node_resources.time_limit;

        // Initialize per-node resource tracker for multi-node allocations.
        // This tracks which node each job lands on so resources_per_node()
        // reports accurate per-node availability instead of dividing
        // remaining total by num_nodes (which gives incorrect values when
        // jobs are unevenly distributed across nodes).
        let has_multi_node_jobs = num_nodes > 1
            && workflow_has_multi_node_jobs(
                &config,
                args.workflow_id,
                args.wait_for_healthy_database_minutes,
            );

        if slurm_interface.is_head_node()
            && let Some(ref node) = scheduled_compute_node
        {
            set_scheduled_compute_node_status(&config, node, "active");
        }

        let node_tracker = if num_nodes > 1 && !has_multi_node_jobs {
            match slurm_interface.list_active_nodes(&job_id) {
                Ok(node_names) => {
                    info!(
                        "Multi-node allocation: {} nodes {:?}, initializing per-node resource tracker",
                        num_nodes, node_names
                    );
                    Some(PerNodeTracker::new(
                        node_names,
                        per_node_resources.num_cpus,
                        per_node_resources.memory_gb,
                        per_node_resources.num_gpus,
                    ))
                }
                Err(e) => {
                    warn!(
                        "Could not enumerate nodes for multi-node allocation: {}. \
                         Falling back to aggregate resource tracking.",
                        e
                    );
                    None
                }
            }
        } else if has_multi_node_jobs {
            info!(
                "Workflow has multi-node jobs; using aggregate resource tracking. \
                 Multi-node jobs reserve whole nodes exclusively."
            );
            None
        } else {
            None
        };

        let job_id_int: i64 = job_id.parse().unwrap_or(0);
        let scheduler = serde_json::json!({
            "scheduler_id": scheduler_id,
            "type": "slurm",
            "slurm_job_id": job_id_int,
        });
        let compute_node =
            create_compute_node(&config, args.workflow_id, &resources, &hostname, scheduler);
        let run_id = match utils::send_with_retries(
            &config,
            || default_api::get_workflow_status(&config, args.workflow_id),
            args.wait_for_healthy_database_minutes,
        ) {
            Ok(status) => status.run_id,
            Err(e) => {
                error!("Error getting workflow status: {}", e);
                std::process::exit(1);
            }
        };

        let unique_label = format!(
            "wf{}_sl{}_n{}_p{}",
            args.workflow_id, job_id, node_id, task_pid
        );

        let mut job_runner = JobRunner::new(
            config.clone(),
            workflow,
            run_id,
            compute_node.id.expect("Compute node ID is required"),
            args.output_dir.clone(),
            args.poll_interval
                .unwrap_or(file_config.client.slurm.poll_interval as i64) as f64,
            args.max_parallel_jobs.map(|x| x as i64),
            None, // time_limit
            Some(job_end_time),
            resources,
            scheduler_config_id,
            None, // log_prefix
            None, // cpu_affinity_cpus_per_job
            args.is_subtask,
            unique_label,
            node_tracker,
        );

        // Register SIGTERM signal handler
        // When Slurm is about to reach walltime, it sends SIGTERM to this process.
        // The handler sets a flag that the job runner checks in its main loop.
        let termination_flag = job_runner.get_termination_flag();
        let mut signals = match Signals::new([SIGTERM]) {
            Ok(s) => s,
            Err(e) => {
                error!("Failed to register SIGTERM handler: {}", e);
                std::process::exit(1);
            }
        };

        // Spawn a thread to handle signals
        thread::spawn(move || {
            for sig in signals.forever() {
                if sig == SIGTERM {
                    info!("Received SIGTERM signal from Slurm. Initiating graceful shutdown.");
                    termination_flag.store(true, Ordering::SeqCst);
                    // Exit the signal handler thread after setting the flag
                    break;
                }
            }
        });

        let job_runner_result = job_runner.run_worker();

        match &job_runner_result {
            Ok(result) => {
                info!(
                    "JobRunner completed successfully (had_failures={}, had_terminations={})",
                    result.had_failures, result.had_terminations
                );

                // Only capture dmesg output if there were failures or terminations
                if result.had_failures || result.had_terminations {
                    info!("Capturing dmesg output due to job failures or terminations");
                    let dmesg_path = get_slurm_dmesg_log_file(
                        args.output_dir.clone(),
                        args.workflow_id,
                        &job_id,
                        &node_id,
                        task_pid,
                    );
                    utils::capture_dmesg(std::path::Path::new(&dmesg_path), Some(dmesg_cutoff));
                }
            }
            Err(e) => {
                error!("JobRunner::run_worker failed: {}", e);
                // Capture dmesg on error as well
                let dmesg_path = get_slurm_dmesg_log_file(
                    args.output_dir.clone(),
                    args.workflow_id,
                    &job_id,
                    &node_id,
                    task_pid,
                );
                utils::capture_dmesg(std::path::Path::new(&dmesg_path), Some(dmesg_cutoff));
                std::process::exit(1);
            }
        }

        if slurm_interface.is_head_node()
            && let Some(ref node) = scheduled_compute_node
        {
            set_scheduled_compute_node_status(&config, node, "complete");
        }
    }

    /// Get the scheduled compute node for a Slurm job ID.
    /// Returns the node model if successfully found, None otherwise.
    fn get_scheduled_compute_node(
        config: &Configuration,
        workflow_id: i64,
        slurm_interface: &SlurmInterface,
    ) -> Option<torc::models::ScheduledComputeNodesModel> {
        let job_id = slurm_interface.get_current_job_id();
        debug!(
            "Getting scheduled compute node for Slurm job ID: {}",
            job_id
        );

        let scheduled_nodes = match default_api::list_scheduled_compute_nodes(
            config,
            workflow_id,
            None,          // offset
            None,          // limit
            None,          // sort_by
            None,          // reverse_sort
            Some(&job_id), // scheduler_id
            None,          // scheduler_config_id
            None,          // status
        ) {
            Ok(response) => response,
            Err(e) => {
                error!("Error listing scheduled compute nodes: {}", e);
                return None;
            }
        };

        let items = scheduled_nodes.items.unwrap_or_default();
        if items.len() != 1 {
            error!(
                "Expected exactly 1 scheduled compute node for Slurm job ID {}, found {}",
                job_id,
                items.len()
            );
            return None;
        }

        Some(items[0].clone())
    }

    /// Set the status of a scheduled compute node.
    fn set_scheduled_compute_node_status(
        config: &Configuration,
        node: &torc::models::ScheduledComputeNodesModel,
        status: &str,
    ) {
        let mut updated_node = node.clone();
        let node_id = updated_node
            .id
            .expect("Scheduled compute node must have an ID");

        updated_node.status = status.to_string();

        match default_api::update_scheduled_compute_node(config, node_id, updated_node) {
            Ok(result) => {
                info!(
                    "Successfully updated scheduled compute node {} to status: {}",
                    node_id, result.status
                );
            }
            Err(e) => {
                error!(
                    "Error updating scheduled compute node {} to status '{}': {}",
                    node_id, status, e
                );
            }
        }
    }
}

#[cfg(unix)]
fn main() {
    unix_main::main();
}