bssh 1.4.1

Parallel SSH command execution tool for cluster management
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
// Copyright 2025 Lablup Inc. and Jeongkyu Shin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Exit code calculation strategies for distributed execution.
//!
//! This module defines how bssh determines the final exit code when executing
//! commands across multiple nodes. Different strategies are appropriate for
//! different use cases:
//!
//! - **MainRank** (default): Returns the main rank's exit code, matching standard
//!   MPI tools (mpirun, srun, mpiexec). Best for MPI workloads and CI/CD integration.
//!
//! - **RequireAllSuccess**: Returns 0 only if all nodes succeeded, 1 otherwise.
//!   This was the default behavior in v1.0-v1.1. Best for health checks and
//!   monitoring where all nodes must be operational.
//!
//! - **MainRankWithFailureCheck**: Returns the main rank's exit code if it's
//!   non-zero, or 1 if the main rank succeeded but other nodes failed. Hybrid
//!   approach that preserves error diagnostics while ensuring failures are noticed.

use crate::executor::result_types::ExecutionResult;

/// Strategy for calculating the final exit code from multiple node results.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExitCodeStrategy {
    /// Return main rank's exit code (DEFAULT in v1.2+).
    ///
    /// Matches standard MPI tools: mpirun, srun, mpiexec.
    /// Preserves actual exit codes (139=SIGSEGV, 137=OOM, etc.) for diagnostics.
    ///
    /// # Use Cases
    /// - MPI workloads and distributed computing
    /// - CI/CD pipelines requiring exit code inspection
    /// - Shell scripts with error handling logic
    /// - When debugging requires specific exit codes
    MainRank,

    /// Return 0 only if ALL nodes succeeded (v1.0-v1.1 behavior).
    ///
    /// Returns exit code 1 if any node failed, regardless of the specific error.
    /// Useful when all nodes must be operational.
    ///
    /// # Use Cases
    /// - Health checks and monitoring
    /// - Cluster validation
    /// - When any failure should be treated equally
    RequireAllSuccess,

    /// Hybrid: Return main rank exit code if non-zero, or 1 if main OK but others failed.
    ///
    /// Combines the diagnostic benefits of MainRank with the safety of RequireAllSuccess.
    ///
    /// # Use Cases
    /// - When you need detailed error codes but also want to catch failures on any node
    /// - Production deployments where both diagnostics and completeness matter
    MainRankWithFailureCheck,
}

impl ExitCodeStrategy {
    /// Calculate the final exit code based on the strategy.
    ///
    /// # Arguments
    /// * `results` - Execution results from all nodes
    /// * `main_idx` - Index of the main rank node (if known)
    ///
    /// # Returns
    /// The exit code to be returned by the bssh process
    ///
    /// # Examples
    /// ```
    /// use bssh::executor::exit_strategy::ExitCodeStrategy;
    /// // See tests for usage examples
    /// ```
    pub fn calculate(&self, results: &[ExecutionResult], main_idx: Option<usize>) -> i32 {
        match self {
            Self::MainRank => {
                // Return main rank's exit code
                main_idx
                    .and_then(|i| results.get(i))
                    .map(|r| r.get_exit_code())
                    .unwrap_or(1) // If no main rank identified, return failure
            }

            Self::RequireAllSuccess => {
                // Old behavior: any failure → 1
                if results.iter().any(|r| !r.is_success()) {
                    1
                } else {
                    0
                }
            }

            Self::MainRankWithFailureCheck => {
                // Get main rank's exit code
                let main_code = main_idx
                    .and_then(|i| results.get(i))
                    .map(|r| r.get_exit_code())
                    .unwrap_or(0);

                // Check if any other node failed
                let other_failed = results
                    .iter()
                    .enumerate()
                    .any(|(i, r)| Some(i) != main_idx && !r.is_success());

                if main_code != 0 {
                    main_code // Main failed → return its code
                } else if other_failed {
                    1 // Main OK but others failed → 1
                } else {
                    0 // All OK
                }
            }
        }
    }
}

impl Default for ExitCodeStrategy {
    /// Default strategy is MainRank (v1.2.0+).
    ///
    /// This matches standard MPI tool behavior and provides better diagnostics.
    fn default() -> Self {
        Self::MainRank
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::node::Node;
    use crate::ssh::client::CommandResult;
    use anyhow::anyhow;

    fn create_success_result(host: &str) -> ExecutionResult {
        ExecutionResult {
            node: Node::new(host.to_string(), 22, "user".to_string()),
            result: Ok(CommandResult {
                host: host.to_string(),
                output: Vec::new(),
                stderr: Vec::new(),
                exit_status: 0,
            }),
            is_main_rank: false, // Will be set by caller if needed
        }
    }

    fn create_failure_result(host: &str, exit_code: i32) -> ExecutionResult {
        ExecutionResult {
            node: Node::new(host.to_string(), 22, "user".to_string()),
            result: Ok(CommandResult {
                host: host.to_string(),
                output: Vec::new(),
                stderr: Vec::new(),
                exit_status: exit_code as u32,
            }),
            is_main_rank: false, // Will be set by caller if needed
        }
    }

    fn create_error_result(host: &str) -> ExecutionResult {
        ExecutionResult {
            node: Node::new(host.to_string(), 22, "user".to_string()),
            result: Err(anyhow!("Connection failed")),
            is_main_rank: false, // Will be set by caller if needed
        }
    }

    #[test]
    fn test_default_strategy_is_main_rank() {
        assert_eq!(ExitCodeStrategy::default(), ExitCodeStrategy::MainRank);
    }

    #[test]
    fn test_main_rank_all_success() {
        let results = vec![
            create_success_result("host1"),
            create_success_result("host2"),
            create_success_result("host3"),
        ];

        let exit_code = ExitCodeStrategy::MainRank.calculate(&results, Some(0));
        assert_eq!(exit_code, 0);
    }

    #[test]
    fn test_main_rank_main_failed_with_segfault() {
        let results = vec![
            create_failure_result("host1", 139), // SIGSEGV
            create_success_result("host2"),
            create_success_result("host3"),
        ];

        let exit_code = ExitCodeStrategy::MainRank.calculate(&results, Some(0));
        assert_eq!(exit_code, 139); // Preserve actual exit code
    }

    #[test]
    fn test_main_rank_main_ok_other_failed() {
        let results = vec![
            create_success_result("host1"),
            create_failure_result("host2", 1),
            create_success_result("host3"),
        ];

        let exit_code = ExitCodeStrategy::MainRank.calculate(&results, Some(0));
        assert_eq!(exit_code, 0); // Main rank succeeded, that's what matters
    }

    #[test]
    fn test_main_rank_all_failed() {
        let results = vec![
            create_failure_result("host1", 137), // OOM
            create_failure_result("host2", 1),
            create_failure_result("host3", 1),
        ];

        let exit_code = ExitCodeStrategy::MainRank.calculate(&results, Some(0));
        assert_eq!(exit_code, 137); // Return main rank's specific exit code
    }

    #[test]
    fn test_main_rank_no_main_identified() {
        let results = vec![
            create_success_result("host1"),
            create_success_result("host2"),
        ];

        let exit_code = ExitCodeStrategy::MainRank.calculate(&results, None);
        assert_eq!(exit_code, 1); // No main rank → failure
    }

    #[test]
    fn test_main_rank_with_connection_error() {
        let results = vec![create_error_result("host1"), create_success_result("host2")];

        let exit_code = ExitCodeStrategy::MainRank.calculate(&results, Some(0));
        assert_eq!(exit_code, 1); // Connection error treated as exit code 1
    }

    #[test]
    fn test_require_all_success_all_ok() {
        let results = vec![
            create_success_result("host1"),
            create_success_result("host2"),
            create_success_result("host3"),
        ];

        let exit_code = ExitCodeStrategy::RequireAllSuccess.calculate(&results, Some(0));
        assert_eq!(exit_code, 0);
    }

    #[test]
    fn test_require_all_success_one_failed() {
        let results = vec![
            create_success_result("host1"),
            create_failure_result("host2", 139), // Specific exit code doesn't matter
            create_success_result("host3"),
        ];

        let exit_code = ExitCodeStrategy::RequireAllSuccess.calculate(&results, Some(0));
        assert_eq!(exit_code, 1); // Any failure → 1
    }

    #[test]
    fn test_require_all_success_all_failed() {
        let results = vec![
            create_failure_result("host1", 139),
            create_failure_result("host2", 137),
            create_failure_result("host3", 1),
        ];

        let exit_code = ExitCodeStrategy::RequireAllSuccess.calculate(&results, Some(0));
        assert_eq!(exit_code, 1);
    }

    #[test]
    fn test_require_all_success_with_error() {
        let results = vec![create_success_result("host1"), create_error_result("host2")];

        let exit_code = ExitCodeStrategy::RequireAllSuccess.calculate(&results, Some(0));
        assert_eq!(exit_code, 1);
    }

    #[test]
    fn test_hybrid_all_success() {
        let results = vec![
            create_success_result("host1"),
            create_success_result("host2"),
            create_success_result("host3"),
        ];

        let exit_code = ExitCodeStrategy::MainRankWithFailureCheck.calculate(&results, Some(0));
        assert_eq!(exit_code, 0);
    }

    #[test]
    fn test_hybrid_main_failed() {
        let results = vec![
            create_failure_result("host1", 139),
            create_success_result("host2"),
            create_success_result("host3"),
        ];

        let exit_code = ExitCodeStrategy::MainRankWithFailureCheck.calculate(&results, Some(0));
        assert_eq!(exit_code, 139); // Return main's specific exit code
    }

    #[test]
    fn test_hybrid_main_ok_other_failed() {
        let results = vec![
            create_success_result("host1"),
            create_failure_result("host2", 137),
            create_success_result("host3"),
        ];

        let exit_code = ExitCodeStrategy::MainRankWithFailureCheck.calculate(&results, Some(0));
        assert_eq!(exit_code, 1); // Main OK but others failed → 1
    }

    #[test]
    fn test_hybrid_all_failed() {
        let results = vec![
            create_failure_result("host1", 139),
            create_failure_result("host2", 137),
            create_failure_result("host3", 1),
        ];

        let exit_code = ExitCodeStrategy::MainRankWithFailureCheck.calculate(&results, Some(0));
        assert_eq!(exit_code, 139); // Main's exit code takes precedence
    }

    #[test]
    fn test_hybrid_no_main_all_ok() {
        let results = vec![
            create_success_result("host1"),
            create_success_result("host2"),
        ];

        let exit_code = ExitCodeStrategy::MainRankWithFailureCheck.calculate(&results, None);
        assert_eq!(exit_code, 0); // No main rank but all succeeded
    }

    #[test]
    fn test_hybrid_no_main_with_failures() {
        let results = vec![
            create_success_result("host1"),
            create_failure_result("host2", 1),
        ];

        let exit_code = ExitCodeStrategy::MainRankWithFailureCheck.calculate(&results, None);
        assert_eq!(exit_code, 1); // No main rank + failures → 1
    }

    #[test]
    fn test_main_rank_non_zero_index() {
        let results = vec![
            create_success_result("host1"),
            create_failure_result("host2", 124), // Timeout
            create_success_result("host3"),
        ];

        // host2 (index 1) is main rank
        let exit_code = ExitCodeStrategy::MainRank.calculate(&results, Some(1));
        assert_eq!(exit_code, 124);
    }

    #[test]
    fn test_empty_results() {
        let results: Vec<ExecutionResult> = vec![];

        // MainRank with no results
        let exit_code = ExitCodeStrategy::MainRank.calculate(&results, None);
        assert_eq!(exit_code, 1);

        // RequireAllSuccess with no results (vacuous truth)
        let exit_code = ExitCodeStrategy::RequireAllSuccess.calculate(&results, None);
        assert_eq!(exit_code, 0);

        // Hybrid with no results
        let exit_code = ExitCodeStrategy::MainRankWithFailureCheck.calculate(&results, None);
        assert_eq!(exit_code, 0);
    }

    #[test]
    fn test_large_exit_code() {
        // Test with exit code that would overflow i32 if not handled properly
        let large_exit_code = u32::MAX; // 4294967295
        let results = vec![ExecutionResult {
            node: Node::new("host1".to_string(), 22, "user".to_string()),
            result: Ok(CommandResult {
                host: "host1".to_string(),
                output: Vec::new(),
                stderr: Vec::new(),
                exit_status: large_exit_code,
            }),
            is_main_rank: true,
        }];

        // Should handle large exit codes without panic
        let exit_code = ExitCodeStrategy::MainRank.calculate(&results, Some(0));
        assert_eq!(exit_code, i32::MAX); // Should be clamped to i32::MAX
    }
}