[pve-devel] [PATCH ha-manager v3 07/13] manager: handle resource affinity rules in manual migrations

Daniel Kral d.kral at proxmox.com
Fri Jul 4 20:20:50 CEST 2025


Make any manual user migration of a resource follow the resource
affinity rules it is part of. That is:

- prevent a resource to be manually migrated to a node, which contains a
  resource, that the resource must be kept separate from (negative
  resource affinity).

- make resources, which must be kept together (positive resource
  affinity), migrate to the same target node, and

The log information here is only redirected to the HA Manager node's
syslog, so user-facing endpoints need to implement this logic as well to
give users adequate feedback about these actions.

Signed-off-by: Daniel Kral <d.kral at proxmox.com>
---
 src/PVE/HA/Manager.pm                | 46 ++++++++++++++++++++++--
 src/PVE/HA/Rules/ResourceAffinity.pm | 53 ++++++++++++++++++++++++++++
 2 files changed, 96 insertions(+), 3 deletions(-)

diff --git a/src/PVE/HA/Manager.pm b/src/PVE/HA/Manager.pm
index 06d83cd..fc0c116 100644
--- a/src/PVE/HA/Manager.pm
+++ b/src/PVE/HA/Manager.pm
@@ -12,7 +12,7 @@ use PVE::HA::NodeStatus;
 use PVE::HA::Rules;
 use PVE::HA::Rules::NodeAffinity qw(get_node_affinity);
 use PVE::HA::Rules::ResourceAffinity
-    qw(get_resource_affinity apply_positive_resource_affinity apply_negative_resource_affinity);
+    qw(get_affinitive_resources get_resource_affinity apply_positive_resource_affinity apply_negative_resource_affinity);
 use PVE::HA::Usage::Basic;
 use PVE::HA::Usage::Static;
 
@@ -409,6 +409,47 @@ sub read_lrm_status {
     return ($results, $modes);
 }
 
+sub execute_migration {
+    my ($self, $cmd, $task, $sid, $target) = @_;
+
+    my ($haenv, $ss) = $self->@{qw(haenv ss)};
+
+    my ($together, $separate) = get_affinitive_resources($self->{rules}, $sid);
+
+    for my $csid (sort keys %$separate) {
+        next if $ss->{$csid}->{node} && $ss->{$csid}->{node} ne $target;
+        next if $ss->{$csid}->{target} && $ss->{$csid}->{target} ne $target;
+
+        $haenv->log(
+            'err',
+            "crm command '$cmd' error - service '$csid' on node '$target' in"
+                . " negative affinity with service '$sid'",
+        );
+
+        return; # one negative resource affinity is enough to not execute migration
+    }
+
+    $haenv->log('info', "got crm command: $cmd");
+    $ss->{$sid}->{cmd} = [$task, $target];
+
+    my $resources_to_migrate = [];
+    for my $csid (sort keys %$together) {
+        next if $ss->{$csid}->{node} && $ss->{$csid}->{node} eq $target;
+        next if $ss->{$csid}->{target} && $ss->{$csid}->{target} eq $target;
+
+        push @$resources_to_migrate, $csid;
+    }
+
+    for my $csid (@$resources_to_migrate) {
+        $haenv->log(
+            'info',
+            "crm command '$cmd' - $task service '$csid' to node '$target'"
+                . " (service '$csid' in positive affinity with service '$sid')",
+        );
+        $ss->{$csid}->{cmd} = [$task, $target];
+    }
+}
+
 # read new crm commands and save them into crm master status
 sub update_crm_commands {
     my ($self) = @_;
@@ -432,8 +473,7 @@ sub update_crm_commands {
                             "ignore crm command - service already on target node: $cmd",
                         );
                     } else {
-                        $haenv->log('info', "got crm command: $cmd");
-                        $ss->{$sid}->{cmd} = [$task, $node];
+                        $self->execute_migration($cmd, $task, $sid, $node);
                     }
                 }
             } else {
diff --git a/src/PVE/HA/Rules/ResourceAffinity.pm b/src/PVE/HA/Rules/ResourceAffinity.pm
index 965b9a1..e5a858e 100644
--- a/src/PVE/HA/Rules/ResourceAffinity.pm
+++ b/src/PVE/HA/Rules/ResourceAffinity.pm
@@ -10,6 +10,7 @@ use base qw(Exporter);
 use base qw(PVE::HA::Rules);
 
 our @EXPORT_OK = qw(
+    get_affinitive_resources
     get_resource_affinity
     apply_positive_resource_affinity
     apply_negative_resource_affinity
@@ -447,6 +448,58 @@ sub plugin_canonicalize {
 
 =cut
 
+=head3 get_affinitive_resources($rules, $sid)
+
+Returns a list of two hash sets, where the first hash set contains the
+resources, which C<$sid> is positively affinitive to, and the second hash
+contains the resources, which C<$sid> is negatively affinitive to, acording to
+the resource affinity rules in C<$rules>.
+
+Note that a resource C<$sid> becomes part of any negative affinity relation
+of its positively affinitive resources.
+
+For example, if a resource is negatively affinitive to C<'vm:101'> and positively
+affinitive to C<'ct:200'> and C<'ct:201'>, the returned value will be:
+
+    {
+        together => {
+            'vm:101' => 1
+        },
+        separate => {
+            'ct:200' => 1,
+            'ct:201' => 1
+        }
+    }
+
+=cut
+
+sub get_affinitive_resources : prototype($$) {
+    my ($rules, $sid) = @_;
+
+    my $together = {};
+    my $separate = {};
+
+    PVE::HA::Rules::foreach_rule(
+        $rules,
+        sub {
+            my ($rule, $ruleid) = @_;
+
+            my $affinity_set = $rule->{affinity} eq 'positive' ? $together : $separate;
+
+            for my $csid (sort keys %{ $rule->{resources} }) {
+                $affinity_set->{$csid} = 1 if $csid ne $sid;
+            }
+        },
+        {
+            sid => $sid,
+            type => 'resource-affinity',
+            exclude_disabled_rules => 1,
+        },
+    );
+
+    return ($together, $separate);
+}
+
 =head3 get_resource_affinity($rules, $sid, $online_node_usage)
 
 Returns a list of two hashes, where the first describes the positive resource
-- 
2.39.5





More information about the pve-devel mailing list