public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Daniel Kral <d.kral@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH ha-manager v2 18/18] factor out counting of active services into helper
Date: Tue,  9 Sep 2025 10:33:55 +0200	[thread overview]
Message-ID: <20250909083539.39675-19-d.kral@proxmox.com> (raw)
In-Reply-To: <20250909083539.39675-1-d.kral@proxmox.com>

Make the counting its own helper to be used throughout the code base to
have a single source of truth of what constitutes a active services.

Signed-off-by: Daniel Kral <d.kral@proxmox.com>
---
changes since v1:
  - change new code to use `for` instead of `foreach`
  - changes from the previous two patches

 src/PVE/API2/HA/Status.pm | 17 ++---------------
 src/PVE/HA/LRM.pm         | 20 +-------------------
 src/PVE/HA/Manager.pm     | 14 +-------------
 src/PVE/HA/Tools.pm       | 25 +++++++++++++++++++++++++
 4 files changed, 29 insertions(+), 47 deletions(-)

diff --git a/src/PVE/API2/HA/Status.pm b/src/PVE/API2/HA/Status.pm
index 3966a4b3..282577cc 100644
--- a/src/PVE/API2/HA/Status.pm
+++ b/src/PVE/API2/HA/Status.pm
@@ -194,21 +194,8 @@ __PACKAGE__->register_method({
         }
 
         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 $active_count =
+                PVE::HA::Tools::count_active_services($status->{service_status}, $node);
             my $lrm_status = PVE::HA::Config::read_lrm_status($node);
             my $id = "lrm:$node";
             if (!$lrm_status->{timestamp}) {
diff --git a/src/PVE/HA/LRM.pm b/src/PVE/HA/LRM.pm
index b645e5b0..1342c8eb 100644
--- a/src/PVE/HA/LRM.pm
+++ b/src/PVE/HA/LRM.pm
@@ -297,25 +297,7 @@ sub active_service_count {
 
     my $ss = $self->{service_status};
 
-    my $count = 0;
-    foreach my $sid (keys %$ss) {
-        my $sd = $ss->{$sid};
-        my $target = $sd->{target}; # count as active if we are the 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';
-        # NOTE: 'ignored' ones are already dropped by the manager from service_status
-        next if $req_state eq 'freeze';
-        # erroneous services are not managed by HA, don't count them as active
-        next if $req_state eq 'error';
-        # request_start is for (optional) better node selection for stop -> started transition
-        next if $req_state eq 'request_start';
-
-        $count++;
-    }
-
-    return $count;
+    return PVE::HA::Tools::count_active_services($ss, $nodename);
 }
 
 my $wrote_lrm_status_at_startup = 0;
diff --git a/src/PVE/HA/Manager.pm b/src/PVE/HA/Manager.pm
index 4cdddb55..e6f8dc25 100644
--- a/src/PVE/HA/Manager.pm
+++ b/src/PVE/HA/Manager.pm
@@ -541,19 +541,7 @@ my $have_groups_been_migrated = sub {
 my $is_lrm_active_or_idle = sub {
     my ($ss, $node, $lrm_state) = @_;
 
-    my $active_count = 0;
-    for my $sid (keys %$ss) {
-        my $sd = $ss->{$sid};
-        my $target = $sd->{target}; # count as active if we are the target.
-        next if (!$sd->{node} || $sd->{node} ne $node) && (!$target || $target ne $node);
-        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 $active_count = PVE::HA::Tools::count_active_services($ss, $node);
 
     return 1 if $lrm_state eq 'active';
     return 1 if $lrm_state eq 'wait_for_agent_lock' && !$active_count;
diff --git a/src/PVE/HA/Tools.pm b/src/PVE/HA/Tools.pm
index 47b4aa03..1fa53df8 100644
--- a/src/PVE/HA/Tools.pm
+++ b/src/PVE/HA/Tools.pm
@@ -188,6 +188,31 @@ sub count_fenced_services {
     return $count;
 }
 
+sub count_active_services {
+    my ($ss, $node) = @_;
+
+    my $active_count = 0;
+
+    for my $sid (keys %$ss) {
+        my $sd = $ss->{$sid};
+        my $target = $sd->{target}; # count as active if we are the target.
+        next if (!$sd->{node} || $sd->{node} ne $node) && (!$target || $target ne $node);
+        my $req_state = $sd->{state};
+        next if !defined($req_state);
+        next if $req_state eq 'stopped';
+        # NOTE: 'ignored' ones are already dropped by the manager from service_status
+        next if $req_state eq 'freeze';
+        # erroneous services are not managed by HA, don't count them as active
+        next if $req_state eq 'error';
+        # request_start is for (optional) better node selection for stop -> started transition
+        next if $req_state eq 'request_start';
+
+        $active_count++;
+    }
+
+    return $active_count;
+}
+
 sub get_verbose_service_state {
     my ($service_state, $service_conf) = @_;
 
-- 
2.47.3



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


      parent reply	other threads:[~2025-09-09  8:37 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-09  8:33 [pve-devel] [PATCH ha-manager v2 00/18] HA rules fixes + cleanup + performance improvements Daniel Kral
2025-09-09  8:33 ` [pve-devel] [PATCH ha-manager v2 01/18] config: do not add ignored resources to dependent resources Daniel Kral
2025-09-09  8:33 ` [pve-devel] [PATCH ha-manager v2 02/18] manager: retranslate rules if nodes are added or removed Daniel Kral
2025-09-09  8:33 ` [pve-devel] [PATCH ha-manager v2 03/18] rules: factor out disjoint rules' resource set helper Daniel Kral
2025-09-09  8:33 ` [pve-devel] [PATCH ha-manager v2 04/18] rules: resource affinity: inter-consistency check with merged positive rules Daniel Kral
2025-09-09  8:33 ` [pve-devel] [PATCH ha-manager v2 05/18] rules: add merged positive resource affinity info in global checks Daniel Kral
2025-09-09  8:33 ` [pve-devel] [PATCH ha-manager v2 06/18] rules: make rules sorting optional in foreach_rule helper Daniel Kral
2025-09-09  8:33 ` [pve-devel] [PATCH ha-manager v2 07/18] rename rule's canonicalize stage to transform stage Daniel Kral
2025-09-09  8:33 ` [pve-devel] [PATCH ha-manager v2 08/18] rules: make plugins register transformers instead of plugin_transform Daniel Kral
2025-09-09  8:33 ` [pve-devel] [PATCH ha-manager v2 09/18] rules: node affinity: decouple get_node_affinity helper from Usage class Daniel Kral
2025-09-09  8:33 ` [pve-devel] [PATCH ha-manager v2 10/18] compile ha rules to a more compact representation Daniel Kral
2025-09-09  8:33 ` [pve-devel] [PATCH ha-manager v2 11/18] test: rules: use to_json instead of Data::Dumper for config output Daniel Kral
2025-09-09  8:33 ` [pve-devel] [PATCH ha-manager v2 12/18] test: rules: add compiled config output to rules config test cases Daniel Kral
2025-09-09  8:33 ` [pve-devel] [PATCH ha-manager v2 13/18] rules: node affinity: define node priority outside hash access Daniel Kral
2025-09-09  8:33 ` [pve-devel] [PATCH ha-manager v2 14/18] move minimum version check helper to ha tools Daniel Kral
2025-09-09  8:33 ` [pve-devel] [PATCH ha-manager v2 15/18] manager: move group migration cooldown variable into helper Daniel Kral
2025-09-09  8:33 ` [pve-devel] [PATCH ha-manager v2 16/18] api: status: sync active service counting with lrm's helper Daniel Kral
2025-09-09  8:33 ` [pve-devel] [PATCH ha-manager v2 17/18] manager: group migration: " Daniel Kral
2025-09-09  8:33 ` Daniel Kral [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250909083539.39675-19-d.kral@proxmox.com \
    --to=d.kral@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal