From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 129881FF0C0 for ; Wed, 18 Nov 2026 12:07:18 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 15C8F217C4; Wed, 18 Nov 2026 12:07:15 +0100 (CET) From: Thomas Lamprecht 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 Message-ID: <20261118110705.595989-4-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261118110705.595989-1-t.lamprecht@proxmox.com> References: <20261118110705.595989-1-t.lamprecht@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1795000030417 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.176 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: WG45TZNPKZI4CPPV4UUQPPG43QNVK4A3 X-Message-ID-Hash: WG45TZNPKZI4CPPV4UUQPPG43QNVK4A3 X-MailFrom: t.lamprecht@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- 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