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 v3 10/15] manager: migrate ha groups to node affinity rules in-memory
Date: Fri,  4 Jul 2025 20:16:50 +0200	[thread overview]
Message-ID: <20250704181659.465441-12-d.kral@proxmox.com> (raw)
In-Reply-To: <20250704181659.465441-1-d.kral@proxmox.com>

Migrate the currently configured groups to node affinity rules
in-memory, so that they can be applied as such in the next patches and
therefore replace HA groups internally.

HA node affinity rules in their initial implementation are designed to
be as restrictive as HA groups, i.e. only allow a HA resource to be used
in a single node affinity rule, to ease the migration between them.

HA groups map directly to node affinity rules, except that the
'restricted' property is renamed to 'strict' and that the 'failback'
property is moved to the HA resources config.

The 'nofailback' property is moved to the HA resources config, because
it allows users to set it more granularly for individual HA resources
and allows the node affinity rules to be more extendible in the future,
e.g. multiple node affinity rules for a single HA resource.

Signed-off-by: Daniel Kral <d.kral@proxmox.com>
---
 src/PVE/HA/Config.pm  |  3 ++-
 src/PVE/HA/Groups.pm  | 48 +++++++++++++++++++++++++++++++++++++++++++
 src/PVE/HA/Manager.pm | 18 ++++++++++++++--
 3 files changed, 66 insertions(+), 3 deletions(-)

diff --git a/src/PVE/HA/Config.pm b/src/PVE/HA/Config.pm
index 7d071f3..424a6e1 100644
--- a/src/PVE/HA/Config.pm
+++ b/src/PVE/HA/Config.pm
@@ -131,7 +131,8 @@ sub read_and_check_resources_config {
         }
     }
 
-    return $conf;
+    # TODO PVE 10: Remove digest when HA groups have been fully migrated to rules
+    return wantarray ? ($conf, $res->{digest}) : $conf;
 }
 
 sub update_resources_config {
diff --git a/src/PVE/HA/Groups.pm b/src/PVE/HA/Groups.pm
index 821d969..f065732 100644
--- a/src/PVE/HA/Groups.pm
+++ b/src/PVE/HA/Groups.pm
@@ -6,6 +6,7 @@ use warnings;
 use PVE::JSONSchema qw(get_standard_option);
 use PVE::SectionConfig;
 use PVE::HA::Tools;
+use PVE::HA::Rules;
 
 use base qw(PVE::SectionConfig);
 
@@ -107,4 +108,51 @@ sub parse_section_header {
 __PACKAGE__->register();
 __PACKAGE__->init();
 
+# Migrate nofailback flag from $groups to $resources
+sub migrate_groups_to_resources {
+    my ($groups, $resources) = @_;
+
+    for my $sid (keys %$resources) {
+        my $groupid = $resources->{$sid}->{group}
+            or next; # skip resources without groups
+
+        $resources->{$sid}->{failback} = !$groups->{ids}->{$groupid}->{nofailback};
+    }
+}
+
+# Migrate groups from groups from $groups and $resources to node affinity rules in $rules
+sub migrate_groups_to_rules {
+    my ($rules, $groups, $resources) = @_;
+
+    my $group_resources = {};
+
+    for my $sid (keys %$resources) {
+        my $groupid = $resources->{$sid}->{group}
+            or next; # skip resources without groups
+
+        $group_resources->{$groupid}->{$sid} = 1;
+    }
+
+    while (my ($group, $resources) = each %$group_resources) {
+        next if !$groups->{ids}->{$group}; # skip non-existant groups
+
+        my $new_ruleid = "ha-group-$group";
+        my $nodes = {};
+        for my $entry (keys $groups->{ids}->{$group}->{nodes}->%*) {
+            my ($node, $priority) = PVE::HA::Tools::parse_node_priority($entry);
+
+            $nodes->{$node} = { priority => $priority };
+        }
+
+        $rules->{ids}->{$new_ruleid} = {
+            type => 'node-affinity',
+            resources => $resources,
+            nodes => $nodes,
+            strict => $groups->{ids}->{$group}->{restricted},
+            comment => "Generated from HA group '$group'.",
+        };
+        $rules->{order}->{$new_ruleid} = PVE::HA::Rules::get_next_ordinal($rules);
+    }
+}
+
 1;
diff --git a/src/PVE/HA/Manager.pm b/src/PVE/HA/Manager.pm
index 88ff4a6..148447d 100644
--- a/src/PVE/HA/Manager.pm
+++ b/src/PVE/HA/Manager.pm
@@ -6,6 +6,7 @@ use warnings;
 use Digest::MD5 qw(md5_base64);
 
 use PVE::Tools;
+use PVE::HA::Groups;
 use PVE::HA::Tools ':exit_codes';
 use PVE::HA::NodeStatus;
 use PVE::HA::Rules;
@@ -47,6 +48,8 @@ sub new {
         haenv => $haenv,
         crs => {},
         last_rules_digest => '',
+        last_groups_digest => '',
+        last_services_digest => '',
     }, $class;
 
     my $old_ms = $haenv->read_manager_status();
@@ -529,7 +532,7 @@ sub manage {
 
     $self->update_crs_scheduler_mode();
 
-    my $sc = $haenv->read_service_config();
+    my ($sc, $services_digest) = $haenv->read_service_config();
 
     $self->{groups} = $haenv->read_group_config(); # update
 
@@ -564,7 +567,16 @@ sub manage {
 
     my $new_rules = $haenv->read_rules_config();
 
-    if ($new_rules->{digest} ne $self->{last_rules_digest}) {
+    # TODO PVE 10: Remove group migration when HA groups have been fully migrated to rules
+    PVE::HA::Groups::migrate_groups_to_resources($self->{groups}, $sc);
+
+    if (
+        !$self->{rules}
+        || $new_rules->{digest} ne $self->{last_rules_digest}
+        || $self->{groups}->{digest} ne $self->{last_groups_digest}
+        || $services_digest && $services_digest ne $self->{last_services_digest}
+    ) {
+        PVE::HA::Groups::migrate_groups_to_rules($new_rules, $self->{groups}, $sc);
 
         my $messages = PVE::HA::Rules->canonicalize($new_rules);
         $haenv->log('info', $_) for @$messages;
@@ -572,6 +584,8 @@ sub manage {
         $self->{rules} = $new_rules;
 
         $self->{last_rules_digest} = $self->{rules}->{digest};
+        $self->{last_groups_digest} = $self->{groups}->{digest};
+        $self->{last_services_digest} = $services_digest;
     }
 
     $self->update_crm_commands();
-- 
2.39.5



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


  parent reply	other threads:[~2025-07-04 18:18 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-04 18:16 [pve-devel] [PATCH cluster/docs/ha-manager/manager v3 00/20] HA Rules Daniel Kral
2025-07-04 18:16 ` [pve-devel] [PATCH cluster v3 1/1] cfs: add 'ha/rules.cfg' to observed files Daniel Kral
2025-07-16 14:02   ` [pve-devel] applied: " Thomas Lamprecht
2025-07-04 18:16 ` [pve-devel] [PATCH ha-manager v3 01/15] tree-wide: make arguments for select_service_node explicit Daniel Kral
2025-07-04 18:16 ` [pve-devel] [PATCH ha-manager v3 02/15] manager: improve signature of select_service_node Daniel Kral
2025-07-04 18:16 ` [pve-devel] [PATCH ha-manager v3 03/15] introduce rules base plugin Daniel Kral
2025-07-04 18:16 ` [pve-devel] [PATCH ha-manager v3 04/15] rules: introduce node affinity rule plugin Daniel Kral
2025-07-04 18:16 ` [pve-devel] [PATCH ha-manager v3 05/15] config, env, hw: add rules read and parse methods Daniel Kral
2025-07-04 18:16 ` [pve-devel] [PATCH ha-manager v3 06/15] config: delete services from rules if services are deleted from config Daniel Kral
2025-07-04 18:16 ` [pve-devel] [PATCH ha-manager v3 07/15] manager: read and update rules config Daniel Kral
2025-07-04 18:16 ` [pve-devel] [PATCH ha-manager v3 08/15] test: ha tester: add test cases for future node affinity rules Daniel Kral
2025-07-04 18:16 ` [pve-devel] [PATCH ha-manager v3 09/15] resources: introduce failback property in ha resource config Daniel Kral
2025-07-04 18:16 ` Daniel Kral [this message]
2025-07-22 16:38   ` [pve-devel] [PATCH ha-manager v3 10/15] manager: migrate ha groups to node affinity rules in-memory Michael Köppl
2025-07-04 18:16 ` [pve-devel] [PATCH ha-manager v3 11/15] manager: apply node affinity rules when selecting service nodes Daniel Kral
2025-07-04 18:16 ` [pve-devel] [PATCH ha-manager v3 12/15] test: add test cases for rules config Daniel Kral
2025-07-04 18:16 ` [pve-devel] [PATCH ha-manager v3 13/15] api: introduce ha rules api endpoints Daniel Kral
2025-07-04 18:16 ` [pve-devel] [PATCH ha-manager v3 14/15] cli: expose ha rules api endpoints to ha-manager cli Daniel Kral
2025-07-04 18:16 ` [pve-devel] [RFC ha-manager v3 15/15] manager: persistently migrate ha groups to ha rules Daniel Kral
2025-07-22 16:38   ` Michael Köppl
2025-07-22 16:56     ` Michael Köppl
2025-07-04 18:16 ` [pve-devel] [PATCH docs v3 1/1] ha: add documentation about ha rules and ha node affinity rules Daniel Kral
2025-07-04 18:16 ` [pve-devel] [PATCH manager v3 1/3] api: ha: add ha rules api endpoints Daniel Kral
2025-07-04 18:16 ` [pve-devel] [PATCH manager v3 2/3] ui: ha: remove ha groups from ha resource components Daniel Kral
2025-07-04 18:16 ` [pve-devel] [PATCH manager v3 3/3] ui: ha: show failback flag in resources status view Daniel Kral
2025-07-22 16:38 ` [pve-devel] [PATCH cluster/docs/ha-manager/manager v3 00/20] HA Rules Michael Köppl
2025-07-23 15:36   ` Michael Köppl
2025-07-30 10:13 ` [pve-devel] superseded: " Daniel Kral

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=20250704181659.465441-12-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