From: Thomas Lamprecht <t.lamprecht@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH ha-manager 02/15] helpers: block both locations of a migrating negative affinity partner
Date: Wed, 18 Nov 2026 12:05:09 +0100 [thread overview]
Message-ID: <20261118110705.595989-3-t.lamprecht@proxmox.com> (raw)
In-Reply-To: <20261118110705.595989-1-t.lamprecht@proxmox.com>
The motion info of a resource lists, per node, the resources in
negative affinity with it that block the node. A partner was skipped
if its current node did not match, and also if its migration target
did not match, so it only blocked a node that matched both. A
migrating partner, whose source and target differ, thus blocked
neither of them.
As a result, the manager queued a migration to the node a negative
affinity partner was migrating to or away from instead of rejecting
it, the HA migrate and relocate API reported no blocker for it, and
the migrate preconditions of qemu-server and pve-container listed
such a node as allowed. The HA placement itself already treats a
migrating resource as occupying both nodes.
Block a node if it is either the current node or the migration target
of the partner. Add a test for the motion info, which also covers a
migrating partner.
Fixes: b926020 ("manager: handle resource affinity rules in manual migrations")
Fixes: 47340b1 ("api: resources: add check for resource affinity in resource migrations")
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
---
src/PVE/HA/Helpers.pm | 4 +-
src/test/Makefile | 1 +
src/test/test_resource_motion_info.pl | 185 ++++++++++++++++++++++++++
3 files changed, 188 insertions(+), 2 deletions(-)
create mode 100755 src/test/test_resource_motion_info.pl
diff --git a/src/PVE/HA/Helpers.pm b/src/PVE/HA/Helpers.pm
index a58b1e1..cdc66a1 100644
--- a/src/PVE/HA/Helpers.pm
+++ b/src/PVE/HA/Helpers.pm
@@ -46,8 +46,8 @@ sub get_resource_motion_info($ss, $sid, $cd, $online_nodes, $compiled_rules) {
for my $csid (sort keys %$separate) {
next if !defined($ss->{$csid});
next if $ss->{$csid}->{state} eq 'ignored';
- next if $ss->{$csid}->{node} && $ss->{$csid}->{node} ne $node;
- next if $ss->{$csid}->{target} && $ss->{$csid}->{target} ne $node;
+ # a migrating resource occupies both its source and its target node
+ next if !grep { defined($_) && $_ eq $node } $ss->{$csid}->@{qw(node target)};
push $blocking_resources_by_node->{$node}->@*,
{
diff --git a/src/test/Makefile b/src/test/Makefile
index f72b755..a26c34e 100644
--- a/src/test/Makefile
+++ b/src/test/Makefile
@@ -7,6 +7,7 @@ test:
./test_failover1.pl
./test_rules_config.pl
./test_resource_bundles.pl
+ ./test_resource_motion_info.pl
./ha-tester.pl
./test_fence_config.pl
@echo "-- end regression tests (success) --"
diff --git a/src/test/test_resource_motion_info.pl b/src/test/test_resource_motion_info.pl
new file mode 100755
index 0000000..2f7c3cd
--- /dev/null
+++ b/src/test/test_resource_motion_info.pl
@@ -0,0 +1,185 @@
+#!/usr/bin/perl
+
+use v5.36;
+
+use lib qw(..);
+
+use Test::More;
+use Test::MockModule;
+
+use PVE::HA::Config;
+use PVE::HA::Groups;
+use PVE::HA::Resources;
+use PVE::HA::Rules;
+use PVE::HA::Rules::NodeAffinity;
+use PVE::HA::Rules::ResourceAffinity;
+use PVE::HA::Sim::Resources::VirtCT;
+use PVE::HA::Sim::Resources::VirtVM;
+
+PVE::HA::Sim::Resources::VirtVM->register();
+PVE::HA::Sim::Resources::VirtCT->register();
+
+PVE::HA::Resources->init();
+
+PVE::HA::Rules::NodeAffinity->register();
+PVE::HA::Rules::ResourceAffinity->register();
+
+PVE::HA::Rules->init(property_isolation => 1);
+
+my $resources_cfg = <<EOF;
+vm: 101
+ state started
+
+vm: 102
+ state started
+
+vm: 103
+ state ignored
+
+ct: 104
+ state started
+EOF
+
+my $rules_cfg = <<EOF;
+node-affinity: vm101-strict
+ resources vm:101
+ nodes node1,node2
+ strict 1
+
+resource-affinity: vm101-vm102-apart
+ resources vm:101,vm:102
+ affinity negative
+
+resource-affinity: vm101-ct104-together
+ resources vm:101,ct:104
+ affinity positive
+EOF
+
+my $manager_status = {
+ node_status => { node1 => 'online', node2 => 'online', node3 => 'online' },
+ service_status => {
+ 'vm:101' => { state => 'started', node => 'node1' },
+ 'vm:102' => { state => 'started', node => 'node3' },
+ 'vm:103' => { state => 'ignored', node => 'node2' },
+ 'ct:104' => { state => 'started', node => 'node1' },
+ },
+};
+
+my $vmlist = {
+ ids => {
+ 101 => { node => 'node1', type => 'qemu' },
+ 102 => { node => 'node3', type => 'qemu' },
+ 103 => { node => 'node2', type => 'qemu' },
+ 104 => { node => 'node1', type => 'lxc' },
+ },
+};
+
+my $config_files = {
+ 'ha/groups.cfg' => sub { PVE::HA::Groups->parse_config($_[0], undef) },
+ 'ha/manager_status' => sub { $manager_status },
+ 'ha/resources.cfg' => sub { PVE::HA::Resources->parse_config($_[0], $resources_cfg) },
+ 'ha/rules.cfg' => sub { PVE::HA::Rules->parse_config($_[0], $rules_cfg) },
+};
+
+my $config_module = Test::MockModule->new('PVE::HA::Config');
+$config_module->redefine(
+ cfs_read_file => sub($filename) {
+ my $parser = $config_files->{$filename} or die "unexpected config read '$filename'\n";
+ return $parser->($filename);
+ },
+);
+
+my $cluster_module = Test::MockModule->new('PVE::Cluster');
+$cluster_module->redefine(get_vmlist => sub { return $vmlist });
+
+my $expected = {
+ 'vm:101' => {
+ 'dependent-resources' => ['ct:104'],
+ 'blocking-resources-by-node' => {
+ node3 => [
+ { sid => 'vm:101', cause => 'node-affinity' },
+ { sid => 'vm:102', cause => 'resource-affinity' },
+ ],
+ },
+ },
+ 'vm:102' => {
+ 'dependent-resources' => [],
+ 'blocking-resources-by-node' => {
+ node1 => [
+ { sid => 'ct:104', cause => 'resource-affinity' },
+ { sid => 'vm:101', cause => 'resource-affinity' },
+ ],
+ },
+ },
+ 'ct:104' => {
+ 'dependent-resources' => ['vm:101'],
+ 'blocking-resources-by-node' => {
+ node3 => [
+ { sid => 'ct:104', cause => 'node-affinity' },
+ { sid => 'vm:102', cause => 'resource-affinity' },
+ ],
+ },
+ },
+};
+
+my $test_single_motion_info = sub($expected, $description) {
+ for my $sid (sort keys %$expected) {
+ my ($dependent_resources, $blocking_resources_by_node) =
+ PVE::HA::Config::get_resource_motion_info($sid);
+
+ is_deeply(
+ {
+ 'dependent-resources' => $dependent_resources,
+ 'blocking-resources-by-node' => $blocking_resources_by_node,
+ },
+ $expected->{$sid},
+ "$description: single motion info for $sid",
+ );
+ }
+};
+
+$test_single_motion_info->($expected, 'stationary partners');
+
+for my $sid (qw(vm:103 vm:999)) {
+ is_deeply(
+ [PVE::HA::Config::get_resource_motion_info($sid)],
+ [[], {}],
+ "no single motion info for ignored or unmanaged $sid",
+ );
+}
+
+# a migrating negative affinity partner occupies both its source and its target node
+$manager_status->{service_status}->{'vm:102'} =
+ { state => 'migrate', node => 'node3', target => 'node2' };
+
+my $expected_migrating = {
+ 'vm:101' => {
+ 'dependent-resources' => ['ct:104'],
+ 'blocking-resources-by-node' => {
+ node2 => [
+ { sid => 'vm:102', cause => 'resource-affinity' },
+ ],
+ node3 => [
+ { sid => 'vm:101', cause => 'node-affinity' },
+ { sid => 'vm:102', cause => 'resource-affinity' },
+ ],
+ },
+ },
+ 'vm:102' => $expected->{'vm:102'},
+ 'ct:104' => {
+ 'dependent-resources' => ['vm:101'],
+ 'blocking-resources-by-node' => {
+ node2 => [
+ { sid => 'vm:102', cause => 'resource-affinity' },
+ ],
+ node3 => [
+ { sid => 'ct:104', cause => 'node-affinity' },
+ { sid => 'vm:102', cause => 'resource-affinity' },
+ ],
+ },
+ },
+};
+
+$test_single_motion_info->($expected_migrating, 'migrating partner');
+
+done_testing();
--
2.47.3
next prev parent reply other threads:[~2026-11-18 11:07 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 11:05 [PATCH guest-common,ha-manager,manager,docs 0/15] cluster resource scheduling for all guests Thomas Lamprecht
2026-11-18 11:05 ` [PATCH guest-common 01/15] scheduling: add cluster resource scheduling library Thomas Lamprecht
2026-11-18 11:05 ` Thomas Lamprecht [this message]
2026-11-18 11:05 ` [PATCH ha-manager 03/15] usage: static: count services per node again for scoring fallback Thomas Lamprecht
2026-11-18 11:05 ` [PATCH ha-manager 04/15] config: allow getting motion info of many resources at once Thomas Lamprecht
2026-11-18 11:05 ` [PATCH ha-manager 05/15] env: pve2: delegate cluster stat gathering to scheduling library Thomas Lamprecht
2026-11-18 11:05 ` [PATCH ha-manager 06/15] usage: share one scheduler base between static and dynamic Thomas Lamprecht
2026-11-18 11:05 ` [PATCH manager 07/15] ui: bulk action: translate error title and CT restart warning Thomas Lamprecht
2026-11-18 11:05 ` [PATCH manager 08/15] ui: bulk migrate: show CT restart warning for cluster-wide migration Thomas Lamprecht
2026-11-18 11:05 ` [PATCH manager 09/15] ui: migrate: recheck preconditions if the target changed meanwhile Thomas Lamprecht
2026-11-18 11:05 ` [PATCH manager 10/15] api: cluster: add resource scheduling endpoints Thomas Lamprecht
2026-11-18 11:05 ` [PATCH manager 11/15] ui: migrate: sort target nodes by scheduler recommendation Thomas Lamprecht
2026-11-18 11:05 ` [PATCH manager 12/15] api: bulk migrate: allow a per-guest target node mapping Thomas Lamprecht
2026-11-18 11:05 ` [PATCH manager 13/15] ui: bulk migrate: support per-guest target nodes Thomas Lamprecht
2026-11-18 11:05 ` [PATCH manager 14/15] ui: dc: add a Rebalance Guests bulk action Thomas Lamprecht
2026-11-18 11:05 ` [PATCH docs 15/15] pvecm: document balancing the cluster load with migrations Thomas Lamprecht
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=20261118110705.595989-3-t.lamprecht@proxmox.com \
--to=t.lamprecht@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