From: Thomas Lamprecht <t.lamprecht@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH ha-manager 03/15] usage: static: count services per node again for scoring fallback
Date: Wed, 18 Nov 2026 12:05:10 +0100 [thread overview]
Message-ID: <20261118110705.595989-4-t.lamprecht@proxmox.com> (raw)
In-Reply-To: <20261118110705.595989-1-t.lamprecht@proxmox.com>
If scoring the nodes for a service fails, for example because its
usage stats are missing, the static usage driver falls back to ranking
the nodes by the number of services they hold, so the service still
ends up on a less busy node.
Since adding a service moved to a single add_service call, nothing
recorded which services were on which node anymore, so every node got
a fallback score of zero and the service was placed on the first node
by name. Record the service on its current and target node again, as
before.
Add a test for the fallback, which fails without this fix.
Fixes: 9780600e3539 ("usage: use add_service to add service usage to nodes")
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
---
src/PVE/HA/Usage/Static.pm | 3 ++
src/test/Makefile | 1 +
src/test/test_usage_fallback.pl | 72 +++++++++++++++++++++++++++++++++
3 files changed, 76 insertions(+)
create mode 100755 src/test/test_usage_fallback.pl
diff --git a/src/PVE/HA/Usage/Static.pm b/src/PVE/HA/Usage/Static.pm
index e67d5f5..570a801 100644
--- a/src/PVE/HA/Usage/Static.pm
+++ b/src/PVE/HA/Usage/Static.pm
@@ -87,6 +87,9 @@ sub add_service {
undef $target_node;
}
+ $self->{'node-services'}->{$current_node}->{$sid} = 1;
+ $self->{'node-services'}->{$target_node}->{$sid} = 1 if defined($target_node);
+
eval {
my $service_usage = get_service_usage($self, $sid);
diff --git a/src/test/Makefile b/src/test/Makefile
index a26c34e..4b327ca 100644
--- a/src/test/Makefile
+++ b/src/test/Makefile
@@ -8,6 +8,7 @@ test:
./test_rules_config.pl
./test_resource_bundles.pl
./test_resource_motion_info.pl
+ ./test_usage_fallback.pl
./ha-tester.pl
./test_fence_config.pl
@echo "-- end regression tests (success) --"
diff --git a/src/test/test_usage_fallback.pl b/src/test/test_usage_fallback.pl
new file mode 100755
index 0000000..b9bb6dd
--- /dev/null
+++ b/src/test/test_usage_fallback.pl
@@ -0,0 +1,72 @@
+#!/usr/bin/perl
+
+use v5.36;
+
+use lib qw(..);
+
+use Test::More;
+
+use PVE::HA::Usage::Static;
+
+my $GiB = 1024**3;
+
+package MockEnv {
+
+ sub new($class, $node_stats) {
+ return bless { node_stats => $node_stats, logs => [] }, $class;
+ }
+
+ sub get_static_node_stats($self) {
+ return $self->{node_stats};
+ }
+
+ sub log($self, $level, $msg) {
+ push $self->{logs}->@*, "$level: $msg";
+ }
+}
+
+my $node_usage = { maxcpu => 8, cpu => 0.0, maxmem => 16 * $GiB, mem => 0 };
+my $node_stats = { map { $_ => { $node_usage->%* } } qw(node1 node2 node3) };
+
+my $service_usage = { maxcpu => 1, cpu => 0.0, maxmem => $GiB, mem => 0 };
+my $service_stats = { map { ("vm:$_" => { usage => { $service_usage->%* } }) } 101 .. 105 };
+
+my $services = {
+ 'vm:101' => { state => 'started', node => 'node1', running => 1 },
+ 'vm:102' => { state => 'started', node => 'node1', running => 1 },
+ 'vm:103' => { state => 'migrate', node => 'node2', target => 'node3' },
+ 'vm:104' => { state => 'request_start_balance', node => 'node1', target => 'node2' },
+ 'vm:105' => { state => 'stopped', node => 'node3' },
+};
+
+my $test_fallback = sub($class) {
+ my $haenv = MockEnv->new($node_stats);
+ my $usage = $class->new($haenv, $service_stats);
+ $usage->add_node($_) for sort keys %$node_stats;
+ $usage->add_service_usage($_, $services->{$_}) for sort keys %$services;
+
+ # scoring fails for a service without usage stats, so fall back to the service count
+ is_deeply(
+ $usage->score_nodes_to_start_service('vm:200'),
+ { node1 => 2, node2 => 2, node3 => 1 },
+ "$class: fall back to the service count per node",
+ );
+ like(
+ $haenv->{logs}->[-1],
+ qr/^err: unable to score nodes .* for service 'vm:200'/,
+ "$class: log the scoring failure",
+ );
+
+ $usage->remove_service_usage('vm:101');
+ $usage->remove_service_usage('vm:103');
+
+ is_deeply(
+ $usage->score_nodes_to_start_service('vm:200'),
+ { node1 => 1, node2 => 1, node3 => 0 },
+ "$class: service count fallback drops removed services",
+ );
+};
+
+$test_fallback->('PVE::HA::Usage::Static');
+
+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 ` [PATCH ha-manager 02/15] helpers: block both locations of a migrating negative affinity partner Thomas Lamprecht
2026-11-18 11:05 ` Thomas Lamprecht [this message]
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-4-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.