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 v5 14/14] api: resources: add check for resource affinity in resource migrations
Date: Wed, 30 Jul 2025 20:14:18 +0200	[thread overview]
Message-ID: <20250730181428.392906-15-d.kral@proxmox.com> (raw)
In-Reply-To: <20250730181428.392906-1-d.kral@proxmox.com>

The HA Manager already handles positive and negative resource affinity
rules for individual resource migrations, but the information about
these is only redirected to the HA environment's logger, i.e., for
production usage these messages are redirected to the HA Manager node's
syslog.

Therefore, add checks when migrating/relocating resources through their
respective API endpoints to give users information about comigrated
resources, i.e., resources which are migrated together to the requested
target node because of positive resource affinity rules, and blocking
resources, i.e., resources which are blocking a resource to be migrated
to a requested target node, because of a negative resource affinity
rule.

get_resource_motion_info(...) is also callable from other packages, to
get a listing of all allowed and disallowed nodes with respect to the
Resource Affinity rules, e.g., a migration precondition check.

Signed-off-by: Daniel Kral <d.kral@proxmox.com>
---
 src/PVE/API2/HA/Resources.pm | 128 +++++++++++++++++++++++++++++++++--
 src/PVE/CLI/ha_manager.pm    |  52 +++++++++++++-
 src/PVE/HA/Config.pm         |  56 +++++++++++++++
 3 files changed, 228 insertions(+), 8 deletions(-)

diff --git a/src/PVE/API2/HA/Resources.pm b/src/PVE/API2/HA/Resources.pm
index 05848f51..894fe905 100644
--- a/src/PVE/API2/HA/Resources.pm
+++ b/src/PVE/API2/HA/Resources.pm
@@ -327,19 +327,75 @@ __PACKAGE__->register_method({
             ),
         },
     },
-    returns => { type => 'null' },
+    returns => {
+        type => 'object',
+        properties => {
+            sid => {
+                description => "HA resource, which is requested to be migrated.",
+                type => 'string',
+                optional => 0,
+            },
+            'requested-node' => {
+                description => "Node, which was requested to be migrated to.",
+                type => 'string',
+                optional => 0,
+            },
+            'comigrated-resources' => {
+                description => "HA resources, which are migrated to the same"
+                    . " requested target node as the given HA resource, because"
+                    . " these are in positive affinity with the HA resource.",
+                type => 'array',
+                optional => 1,
+            },
+            'blocking-resources' => {
+                description => "HA resources, which are blocking the given"
+                    . " HA resource from being migrated to the requested"
+                    . " target node.",
+                type => 'array',
+                optional => 1,
+                items => {
+                    description => "A blocking HA resource",
+                    type => 'object',
+                    properties => {
+                        sid => {
+                            type => 'string',
+                            description => "The blocking HA resource id",
+                        },
+                        cause => {
+                            type => 'string',
+                            description => "The reason why the HA resource is"
+                                . " blocking the migration.",
+                            enum => ['resource-affinity'],
+                        },
+                    },
+                },
+            },
+        },
+    },
     code => sub {
         my ($param) = @_;
 
+        my $result = {};
+
         my ($sid, $type, $name) = PVE::HA::Config::parse_sid(extract_param($param, 'sid'));
+        my $req_node = extract_param($param, 'node');
 
         PVE::HA::Config::service_is_ha_managed($sid);
 
         check_service_state($sid);
 
-        PVE::HA::Config::queue_crm_commands("migrate $sid $param->{node}");
+        PVE::HA::Config::queue_crm_commands("migrate $sid $req_node");
+        $result->{sid} = $sid;
+        $result->{'requested-node'} = $req_node;
 
-        return undef;
+        my ($comigrated_resources, $blocking_resources_by_node) =
+            PVE::HA::Config::get_resource_motion_info($sid);
+        my $blocking_resources = $blocking_resources_by_node->{$req_node};
+
+        $result->{'comigrated-resources'} = $comigrated_resources if @$comigrated_resources;
+        $result->{'blocking-resources'} = $blocking_resources if $blocking_resources;
+
+        return $result;
     },
 });
 
@@ -369,19 +425,79 @@ __PACKAGE__->register_method({
             ),
         },
     },
-    returns => { type => 'null' },
+    returns => {
+        type => 'object',
+        properties => {
+            sid => {
+                description => "HA resource, which is requested to be relocated.",
+                type => 'string',
+                optional => 0,
+            },
+            'requested-node' => {
+                description => "Node, which was requested to be relocated to.",
+                type => 'string',
+                optional => 0,
+            },
+            'comigrated-resources' => {
+                description => "HA resources, which are relocated to the same"
+                    . " requested target node as the given HA resource, because"
+                    . " these are in positive affinity with the HA resource.",
+                type => 'array',
+                optional => 1,
+                items => {
+                    type => 'string',
+                    description => "A comigrated HA resource",
+                },
+            },
+            'blocking-resources' => {
+                description => "HA resources, which are blocking the given"
+                    . " HA resource from being relocated to the requested"
+                    . " target node.",
+                type => 'array',
+                optional => 1,
+                items => {
+                    description => "A blocking HA resource",
+                    type => 'object',
+                    properties => {
+                        sid => {
+                            type => 'string',
+                            description => "The blocking HA resource id",
+                        },
+                        cause => {
+                            type => 'string',
+                            description => "The reason why the HA resource is"
+                                . " blocking the relocation.",
+                            enum => ['resource-affinity'],
+                        },
+                    },
+                },
+            },
+        },
+    },
     code => sub {
         my ($param) = @_;
 
+        my $result = {};
+
         my ($sid, $type, $name) = PVE::HA::Config::parse_sid(extract_param($param, 'sid'));
+        my $req_node = extract_param($param, 'node');
 
         PVE::HA::Config::service_is_ha_managed($sid);
 
         check_service_state($sid);
 
-        PVE::HA::Config::queue_crm_commands("relocate $sid $param->{node}");
+        PVE::HA::Config::queue_crm_commands("relocate $sid $req_node");
+        $result->{sid} = $sid;
+        $result->{'requested-node'} = $req_node;
 
-        return undef;
+        my ($comigrated_resources, $blocking_resources_by_node) =
+            PVE::HA::Config::get_resource_motion_info($sid);
+        my $blocking_resources = $blocking_resources_by_node->{$req_node};
+
+        $result->{'comigrated-resources'} = $comigrated_resources if @$comigrated_resources;
+        $result->{'blocking-resources'} = $blocking_resources if @$blocking_resources;
+
+        return $result;
     },
 });
 
diff --git a/src/PVE/CLI/ha_manager.pm b/src/PVE/CLI/ha_manager.pm
index ab74fe4e..bccb4438 100644
--- a/src/PVE/CLI/ha_manager.pm
+++ b/src/PVE/CLI/ha_manager.pm
@@ -147,6 +147,42 @@ __PACKAGE__->register_method({
     },
 });
 
+my $print_resource_motion_output = sub {
+    my ($cmd) = @_;
+
+    return sub {
+        my ($data) = @_;
+
+        my $sid = $data->{sid};
+        my $req_node = $data->{'requested-node'};
+
+        if (my $blocking_resources = $data->{'blocking-resources'}) {
+            my $err_msg = "cannot $cmd resource '$sid' to node '$req_node':\n\n";
+
+            for my $blocking_resource (@$blocking_resources) {
+                my ($csid, $cause) = $blocking_resource->@{qw(sid cause)};
+
+                $err_msg .= "- resource '$csid' on target node '$req_node'";
+
+                if ($cause eq 'resource-affinity') {
+                    $err_msg .= " in negative affinity with resource '$sid'";
+                }
+
+                $err_msg .= "\n";
+            }
+
+            die $err_msg;
+        }
+
+        if ($data->{'comigrated-resources'}) {
+            for my $csid ($data->{'comigrated-resources'}->@*) {
+                print "also $cmd resource '$csid' in positive affinity with"
+                    . " resource '$sid' to target node '$req_node'\n";
+            }
+        }
+    };
+};
+
 our $cmddef = {
     status => [__PACKAGE__, 'status'],
     config => [
@@ -243,8 +279,20 @@ our $cmddef = {
     relocate => { alias => 'crm-command relocate' },
 
     'crm-command' => {
-        migrate => ["PVE::API2::HA::Resources", 'migrate', ['sid', 'node']],
-        relocate => ["PVE::API2::HA::Resources", 'relocate', ['sid', 'node']],
+        migrate => [
+            "PVE::API2::HA::Resources",
+            'migrate',
+            ['sid', 'node'],
+            {},
+            $print_resource_motion_output->('migrate'),
+        ],
+        relocate => [
+            "PVE::API2::HA::Resources",
+            'relocate',
+            ['sid', 'node'],
+            {},
+            $print_resource_motion_output->('relocate'),
+        ],
         stop => [__PACKAGE__, 'stop', ['sid', 'timeout']],
         'node-maintenance' => {
             enable => [__PACKAGE__, 'node-maintenance-set', ['node'], { disable => 0 }],
diff --git a/src/PVE/HA/Config.pm b/src/PVE/HA/Config.pm
index 5faa557b..65ab44ba 100644
--- a/src/PVE/HA/Config.pm
+++ b/src/PVE/HA/Config.pm
@@ -8,6 +8,7 @@ use JSON;
 use PVE::HA::Tools;
 use PVE::HA::Groups;
 use PVE::HA::Rules;
+use PVE::HA::Rules::ResourceAffinity qw(get_affinitive_resources);
 use PVE::Cluster qw(cfs_register_file cfs_read_file cfs_write_file cfs_lock_file);
 use PVE::HA::Resources;
 
@@ -223,6 +224,24 @@ sub read_and_check_rules_config {
     return $rules;
 }
 
+sub read_and_check_effective_rules_config {
+
+    my $rules = read_and_check_rules_config();
+
+    my $manager_status = read_manager_status();
+    my $nodes = [keys $manager_status->{node_status}->%*];
+
+    # TODO PVE 10: Remove group migration when HA groups have been fully migrated to location rules
+    my $groups = read_group_config();
+    my $resources = read_and_check_resources_config();
+
+    PVE::HA::Groups::migrate_groups_to_rules($rules, $groups, $resources);
+
+    PVE::HA::Rules->canonicalize($rules, $nodes);
+
+    return $rules;
+}
+
 sub write_rules_config {
     my ($cfg) = @_;
 
@@ -359,6 +378,43 @@ sub service_is_configured {
     return 0;
 }
 
+sub get_resource_motion_info {
+    my ($sid) = @_;
+
+    my $resources = read_resources_config();
+
+    my $comigrated_resources = [];
+    my $blocking_resources_by_node = {};
+
+    if (&$service_check_ha_state($resources, $sid)) {
+        my $manager_status = read_manager_status();
+        my $ss = $manager_status->{service_status};
+        my $ns = $manager_status->{node_status};
+
+        my $rules = read_and_check_effective_rules_config();
+        my ($together, $separate) = get_affinitive_resources($rules, $sid);
+
+        push @$comigrated_resources, $_ for sort keys %$together;
+
+        for my $node (keys %$ns) {
+            next if $ns->{$node} ne 'online';
+
+            for my $csid (sort keys %$separate) {
+                next if $ss->{$csid}->{node} && $ss->{$csid}->{node} ne $node;
+                next if $ss->{$csid}->{target} && $ss->{$csid}->{target} ne $node;
+
+                push $blocking_resources_by_node->{$node}->@*,
+                    {
+                        sid => $csid,
+                        cause => 'resource-affinity',
+                    };
+            }
+        }
+    }
+
+    return ($comigrated_resources, $blocking_resources_by_node);
+}
+
 # graceful, as long as locking + cfs_write works
 sub delete_service_from_config {
     my ($sid) = @_;
-- 
2.47.2



_______________________________________________
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-30 18:15 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-30 18:14 [pve-devel] [PATCH container/docs/ha-manager/manager/qemu-server v5 00/20] HA resource affinity rules Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 01/14] introduce PVE::HA::HashTools module Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 02/14] rules: introduce plugin-specific canonicalize routines Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 03/14] rules: add haenv node list to the rules' canonicalization stage Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 04/14] rules: introduce resource affinity rule plugin Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 05/14] rules: add global checks between node and resource affinity rules Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 06/14] usage: add information about a service's assigned nodes Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 07/14] manager: apply resource affinity rules when selecting service nodes Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 08/14] manager: handle resource affinity rules in manual migrations Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 09/14] sim: resources: add option to limit start and migrate tries to node Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 10/14] test: ha tester: add test cases for negative resource affinity rules Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 11/14] test: ha tester: add test cases for positive " Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 12/14] test: ha tester: add test cases for static scheduler resource affinity Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 13/14] test: rules: add test cases for resource affinity rules Daniel Kral
2025-07-30 18:14 ` Daniel Kral [this message]
2025-07-30 18:14 ` [pve-devel] [PATCH docs v5 1/1] ha: add documentation about ha " Daniel Kral
2025-08-04 13:00   ` Hannes Duerr
2025-07-30 18:14 ` [pve-devel] [PATCH manager v5 1/3] ui: ha: rules: add " Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH manager v5 2/3] ui: migrate: lxc: display precondition messages for ha resource affinity Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH manager v5 3/3] ui: migrate: vm: " Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH container v5 1/1] api: introduce migration preconditions api endpoint Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH qemu-server v5 1/1] api: migration preconditions: add checks for ha resource affinity rules Daniel Kral
2025-08-04  9:50 ` [pve-devel] applied-series: [PATCH container/docs/ha-manager/manager/qemu-server v5 00/20] HA " Fiona Ebner

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=20250730181428.392906-15-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