From: Daniel Kral <d.kral@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH ha-manager v3 07/13] manager: handle resource affinity rules in manual migrations
Date: Fri, 4 Jul 2025 20:20:50 +0200 [thread overview]
Message-ID: <20250704182102.467624-8-d.kral@proxmox.com> (raw)
In-Reply-To: <20250704182102.467624-1-d.kral@proxmox.com>
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@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
_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel
next prev parent reply other threads:[~2025-07-04 18:22 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-04 18:20 [pve-devel] [PATCH container/docs/ha-manager/manager/qemu-server v3 00/19] HA resource affinity rules Daniel Kral
2025-07-04 18:20 ` [pve-devel] [PATCH ha-manager v3 01/13] rules: introduce plugin-specific canonicalize routines Daniel Kral
2025-07-04 18:20 ` [pve-devel] [PATCH ha-manager v3 02/13] rules: add haenv node list to the rules' canonicalization stage Daniel Kral
2025-07-04 18:20 ` [pve-devel] [PATCH ha-manager v3 03/13] rules: introduce resource affinity rule plugin Daniel Kral
2025-07-04 18:20 ` [pve-devel] [PATCH ha-manager v3 04/13] rules: add global checks between node and resource affinity rules Daniel Kral
2025-07-29 11:44 ` Michael Köppl
2025-07-04 18:20 ` [pve-devel] [PATCH ha-manager v3 05/13] usage: add information about a service's assigned nodes Daniel Kral
2025-07-04 18:20 ` [pve-devel] [PATCH ha-manager v3 06/13] manager: apply resource affinity rules when selecting service nodes Daniel Kral
2025-07-04 18:20 ` Daniel Kral [this message]
2025-07-04 18:20 ` [pve-devel] [PATCH ha-manager v3 08/13] sim: resources: add option to limit start and migrate tries to node Daniel Kral
2025-07-04 18:20 ` [pve-devel] [PATCH ha-manager v3 09/13] test: ha tester: add test cases for negative resource affinity rules Daniel Kral
2025-07-04 18:20 ` [pve-devel] [PATCH ha-manager v3 10/13] test: ha tester: add test cases for positive " Daniel Kral
2025-07-04 18:20 ` [pve-devel] [PATCH ha-manager v3 11/13] test: ha tester: add test cases for static scheduler resource affinity Daniel Kral
2025-07-04 18:20 ` [pve-devel] [PATCH ha-manager v3 12/13] test: rules: add test cases for resource affinity rules Daniel Kral
2025-07-04 18:20 ` [pve-devel] [PATCH ha-manager v3 13/13] api: resources: add check for resource affinity in resource migrations Daniel Kral
2025-07-04 18:20 ` [pve-devel] [PATCH docs v3 1/1] ha: add documentation about ha resource affinity rules Daniel Kral
2025-07-08 16:08 ` Shannon Sterz
2025-07-09 6:19 ` Friedrich Weber
2025-07-30 10:05 ` Daniel Kral
2025-07-04 18:20 ` [pve-devel] [PATCH manager v3 1/3] ui: ha: rules: add " Daniel Kral
2025-07-04 18:20 ` [pve-devel] [PATCH manager v3 2/3] ui: migrate: lxc: display precondition messages for ha resource affinity Daniel Kral
2025-07-04 18:21 ` [pve-devel] [PATCH manager v3 3/3] ui: migrate: vm: " Daniel Kral
2025-07-04 18:21 ` [pve-devel] [PATCH container v3 1/1] api: introduce migration preconditions api endpoint Daniel Kral
2025-07-04 18:21 ` [pve-devel] [PATCH qemu-server v3 1/1] api: migration preconditions: add checks for ha resource affinity rules 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=20250704182102.467624-8-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