[pve-devel] [PATCH ha-manager 6/9] manager: make recompute_online_node_usage use get_service_nodes helper

Daniel Kral d.kral at proxmox.com
Tue Sep 30 16:19:16 CEST 2025


As the previously introduced PVE::HA::Tools::get_used_service_nodes(...)
helper reflects the same logic as in recompute_online_node_usage(...),
use the helper instead to reduce code duplication.

Signed-off-by: Daniel Kral <d.kral at proxmox.com>
---
 src/PVE/HA/Manager.pm | 53 +++++++++----------------------------------
 1 file changed, 11 insertions(+), 42 deletions(-)

diff --git a/src/PVE/HA/Manager.pm b/src/PVE/HA/Manager.pm
index 0226e427..d0d4d0a5 100644
--- a/src/PVE/HA/Manager.pm
+++ b/src/PVE/HA/Manager.pm
@@ -240,7 +240,7 @@ sub recompute_online_node_usage {
 
     my $haenv = $self->{haenv};
 
-    my $online_nodes = $self->{ns}->list_online_nodes();
+    my $online_nodes = { map { $_ => 1 } $self->{ns}->list_online_nodes()->@* };
 
     my $online_node_usage;
 
@@ -248,7 +248,7 @@ sub recompute_online_node_usage {
         if ($mode eq 'static') {
             $online_node_usage = eval {
                 my $scheduler = PVE::HA::Usage::Static->new($haenv);
-                $scheduler->add_node($_) for $online_nodes->@*;
+                $scheduler->add_node($_) for keys $online_nodes->%*;
                 $haenv->update_static_service_stats();
                 return $scheduler;
             };
@@ -266,49 +266,18 @@ sub recompute_online_node_usage {
     # fallback to the basic algorithm in any case
     if (!$online_node_usage) {
         $online_node_usage = PVE::HA::Usage::Basic->new($haenv);
-        $online_node_usage->add_node($_) for $online_nodes->@*;
+        $online_node_usage->add_node($_) for keys $online_nodes->%*;
     }
 
-    foreach my $sid (sort keys %{ $self->{ss} }) {
+    for my $sid (sort keys $self->{ss}->%*) {
         my $sd = $self->{ss}->{$sid};
-        my $state = $sd->{state};
-        my $target = $sd->{target}; # optional
-        if ($online_node_usage->contains_node($sd->{node})) {
-            if (
-                $state eq 'started'
-                || $state eq 'request_stop'
-                || $state eq 'fence'
-                || $state eq 'freeze'
-                || $state eq 'error'
-                || $state eq 'recovery'
-            ) {
-                $online_node_usage->add_service_usage_to_node($sd->{node}, $sid, $sd->{node});
-            } elsif (
-                $state eq 'migrate'
-                || $state eq 'relocate'
-                || $state eq 'request_start_balance'
-            ) {
-                my $source = $sd->{node};
-                # count it for both, source and target as load is put on both
-                if ($state ne 'request_start_balance') {
-                    $online_node_usage->add_service_usage_to_node($source, $sid, $source, $target);
-                }
-                if ($online_node_usage->contains_node($target)) {
-                    $online_node_usage->add_service_usage_to_node($target, $sid, $source, $target);
-                }
-            } elsif ($state eq 'stopped' || $state eq 'request_start') {
-                # do nothing
-            } else {
-                die "should not be reached (sid = '$sid', state = '$state')";
-            }
-        } elsif (defined($target) && $online_node_usage->contains_node($target)) {
-            if ($state eq 'migrate' || $state eq 'relocate') {
-                # to correctly track maintenance modi and also consider the target as used for the
-                # case a node dies, as we cannot really know if the to-be-aborted incoming migration
-                # has already cleaned up all used resources
-                $online_node_usage->add_service_usage_to_node($target, $sid, $sd->{node}, $target);
-            }
-        }
+        my $used_nodes = PVE::HA::Tools::get_used_service_nodes($sd, $online_nodes);
+        my ($current, $target) = $used_nodes->@{qw(current target)};
+
+        $online_node_usage->add_service_usage_to_node($current, $sid, $sd->{node}, $sd->{target})
+            if $current;
+        $online_node_usage->add_service_usage_to_node($target, $sid, $sd->{node}, $sd->{target})
+            if $target;
     }
 
     $self->{online_node_usage} = $online_node_usage;
-- 
2.47.3





More information about the pve-devel mailing list