[pve-devel] [PATCH ha-manager v2 16/18] api: status: sync active service counting with lrm's helper

Daniel Kral d.kral at proxmox.com
Tue Sep 9 10:33:53 CEST 2025


There are some inconsistencies between the status API endpoint's and the
LRM's active_service_count(...) helper's counting of active services.
Sync these by:

- Counting a migrating service as active on both the source and target
  node's LRM as has been introduced in commit a94a38f9 ("LRM: count
  incoming migrations towards a nodes active resources").

- Not counting services as active if these are in error or request_start
  state as has been introduced in commit 38545741 ("LRM: do not count
  erroneous service as active") and commit 4931b586 ("manager: add new
  intermediate state for stop->start transitions") respectively.

- Removing the `sort` as sorting the services is not required for
  counting active services.

While at it, move the counting code in the node loop, where it will be
moved in an upcoming patch, to make it easier to see that the logic is
the same as the LRM's helper.

Signed-off-by: Daniel Kral <d.kral at proxmox.com>
---
changes since v1:
  - move active service counting in node loop as changing the code
    directly is more error-prone ans makes it less clear whether the
    code is equal to the LRM's active service counting code
  - remove sort too as it's not needed

 src/PVE/API2/HA/Status.pm | 30 ++++++++++++++++--------------
 1 file changed, 16 insertions(+), 14 deletions(-)

diff --git a/src/PVE/API2/HA/Status.pm b/src/PVE/API2/HA/Status.pm
index 6e13c2c8..3966a4b3 100644
--- a/src/PVE/API2/HA/Status.pm
+++ b/src/PVE/API2/HA/Status.pm
@@ -193,20 +193,22 @@ __PACKAGE__->register_method({
                 };
         }
 
-        # compute active services for all nodes
-        my $active_count = {};
-        foreach my $sid (sort keys %{ $status->{service_status} }) {
-            my $sd = $status->{service_status}->{$sid};
-            next if !$sd->{node};
-            $active_count->{ $sd->{node} } = 0 if !defined($active_count->{ $sd->{node} });
-            my $req_state = $sd->{state};
-            next if !defined($req_state);
-            next if $req_state eq 'stopped';
-            next if $req_state eq 'freeze';
-            $active_count->{ $sd->{node} }++;
-        }
-
         foreach my $node (sort keys %{ $status->{node_status} }) {
+            my $active_count = 0;
+            for my $sid (keys $status->{service_status}->%*) {
+                my $sd = $status->{service_status}->{$sid};
+                my $target = $sd->{target};
+                next
+                    if (!$sd->{node} || $sd->{node} ne $nodename)
+                    && (!$target || $target ne $nodename);
+                my $req_state = $sd->{state};
+                next if !defined($req_state);
+                next if $req_state eq 'stopped';
+                next if $req_state eq 'freeze';
+                next if $req_state eq 'error';
+                next if $req_state eq 'request_start';
+                $active_count++;
+            }
             my $lrm_status = PVE::HA::Config::read_lrm_status($node);
             my $id = "lrm:$node";
             if (!$lrm_status->{timestamp}) {
@@ -227,7 +229,7 @@ __PACKAGE__->register_method({
                     if ($lrm_mode ne 'active') {
                         $status_str = "$lrm_mode mode";
                     } else {
-                        if ($lrm_state eq 'wait_for_agent_lock' && !$active_count->{$node}) {
+                        if ($lrm_state eq 'wait_for_agent_lock' && !$active_count) {
                             $status_str = 'idle';
                         } else {
                             $status_str = $lrm_state;
-- 
2.47.3





More information about the pve-devel mailing list