[PATCH proxmox v3 06/40] resource-scheduling: topsis: handle empty criteria without panics

Daniel Kral
2026-03-30T14:42:19Z

Iterator::min_by(...) and Iterator::max_by(...) do only return `None` if
there are no entries in the `Matrix` column at all. This can only happen
if the `Matrix` doesn't have any row entries.

This will make any call to score_alternatives(...), the only current
user of IdealAlternatives::compute(...), panic if there are no given
alternatives. Therefore use reasonable default values.

This has not happened yet, because the only non-test caller of
score_alternatives(...) is score_nodes_to_start_resource(...), which
always has nodes present in production.

Signed-off-by: Daniel Kral <d.kral@proxmox.com>
Reviewed-by: Dominik Rusovac <d.rusovac@proxmox.com>
---
changes v2 -> v3:
- none

proxmox-resource-scheduling/src/topsis.rs | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/proxmox-resource-scheduling/src/topsis.rs b/proxmox-resource-scheduling/src/topsis.rs
index 6d078aa6..ed5a9bd1 100644
--- a/proxmox-resource-scheduling/src/topsis.rs
+++ b/proxmox-resource-scheduling/src/topsis.rs
@@ -145,8 +145,10 @@ impl<const N: usize> IdealAlternatives<N> {
let min = fixed_criterion
.clone()
.min_by(|a, b| a.total_cmp(b))
- .unwrap();
- let max = fixed_criterion.max_by(|a, b| a.total_cmp(b)).unwrap();
+ .unwrap_or(f64::NEG_INFINITY);
+ let max = fixed_criterion
+ .max_by(|a, b| a.total_cmp(b))
+ .unwrap_or(f64::INFINITY);

(best[n], worst[n]) = match criteria[n].maximize {
true => (max, min),
--
2.47.3