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 1F8A71FF0C0 for ; Wed, 18 Nov 2026 12:07:39 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 940B121860; Wed, 18 Nov 2026 12:07:16 +0100 (CET) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Subject: [PATCH ha-manager 04/15] config: allow getting motion info of many resources at once Date: Wed, 18 Nov 2026 12:05:11 +0100 Message-ID: <20261118110705.595989-5-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: 1795000030475 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.175 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: GZT7FX2UWID55FIGXZM5CZBXZ7EXBPHJ X-Message-ID-Hash: GZT7FX2UWID55FIGXZM5CZBXZ7EXBPHJ 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: The cluster rebalance proposal of the resource scheduling API needs to know, for possibly every guest in the cluster, which nodes its HA rules allow and which HA resources move together with it. Getting that through get_resource_motion_info reads and compiles the whole rule set again for each resource, which scales poorly with the number of resources in a request. Add get_resources_motion_info, which reads the configs and compiles the rules once and returns the motion info for each HA-managed resource of the given list. Turn get_resource_motion_info into a thin wrapper around it, so both share one implementation and its existing callers, such as the migrate preconditions of qemu-server and pve-container, see no change. Each read of the manager status returns a fresh copy, which gets costly for many resources in a large cluster. So pass the manager status that was already read to the HA state check instead of reading it again for every resource. For the same reason, add get_resources_requested_state, which tells the HA-managed resources of a list and their requested state with one read of the resources config, while vm_is_ha_managed copies the whole config for each guest. Extend the motion info test to cover the new helpers. Signed-off-by: Thomas Lamprecht --- src/PVE/HA/Config.pm | 83 ++++++++++++++++++++------- src/test/test_resource_motion_info.pl | 43 ++++++++++++++ 2 files changed, 106 insertions(+), 20 deletions(-) diff --git a/src/PVE/HA/Config.pm b/src/PVE/HA/Config.pm index bee1ac7..4e4e8b2 100644 --- a/src/PVE/HA/Config.pm +++ b/src/PVE/HA/Config.pm @@ -353,14 +353,14 @@ sub read_crm_commands { } my $service_check_ha_state = sub { - my ($conf, $sid, $has_state) = @_; + my ($conf, $sid, $has_state, $manager_status) = @_; if (my $d = $conf->{ids}->{$sid}) { if (!defined($has_state)) { # ignored service behave as if they were not managed by HA return 0 if defined($d->{state}) && $d->{state} eq 'ignored'; # cluster-wide disarm with ignore mode - resources can be managed directly - my $ms = cfs_read_file($manager_status_filename); + my $ms = $manager_status // cfs_read_file($manager_status_filename); if (my $disarm = $ms->{disarm}) { return 0 if $disarm->{mode} eq 'ignore'; } @@ -391,36 +391,79 @@ sub service_is_configured { return 0; } -sub get_resource_motion_info { - my ($sid) = @_; +# Returns the motion info of each HA-managed resource in $sids as a hash of +# $sid => { 'dependent-resources' => [...], 'blocking-resources-by-node' => { $node => [...] } } +# as described for PVE::HA::Helpers::get_resource_motion_info(). Resources that are not +# HA-managed are omitted. The rules are read and compiled once, independent of the number of +# resources. +sub get_resources_motion_info { + my ($sids) = @_; my $cfg = read_resources_config(); + # read once, as each copy of a large manager status is costly + my $manager_status = read_manager_status(); - my $dependent_resources = []; - my $blocking_resources_by_node = {}; + my $managed_sids = + [grep { $service_check_ha_state->($cfg, $_, undef, $manager_status) } @$sids]; + return {} if !@$managed_sids; - if (&$service_check_ha_state($cfg, $sid)) { - my $manager_status = read_manager_status(); - my $ss = $manager_status->{service_status}; - my $ns = $manager_status->{node_status}; - # get_resource_motion_info expects a hashset of all nodes with status 'online' - my $online_nodes = { map { $ns->{$_} eq 'online' ? ($_ => 1) : () } keys %$ns }; - # get_resource_motion_info expects a resource config with defaults set - my $resources = checked_resources_config($cfg); + my $ss = $manager_status->{service_status}; + my $ns = $manager_status->{node_status}; + # get_resource_motion_info expects a hashset of all nodes with status 'online' + my $online_nodes = { map { $ns->{$_} eq 'online' ? ($_ => 1) : () } keys %$ns }; + # get_resource_motion_info expects a resource config with defaults set + my $resources = checked_resources_config($cfg); - my $rules = read_and_check_rules_config(); - # TODO PVE 10: Remove group migration when HA groups have been fully migrated to rules - my $groups = read_group_config(); - my $compiled_rules = compile_rules_config($rules, $groups, $resources, $manager_status); + my $rules = read_and_check_rules_config(); + # TODO PVE 10: Remove group migration when HA groups have been fully migrated to rules + my $groups = read_group_config(); + my $compiled_rules = compile_rules_config($rules, $groups, $resources, $manager_status); + my $motion_info = {}; + for my $sid (@$managed_sids) { my $cd = $resources->{$sid} // {}; - ($dependent_resources, $blocking_resources_by_node) = + my ($dependent_resources, $blocking_resources_by_node) = PVE::HA::Helpers::get_resource_motion_info( $ss, $sid, $cd, $online_nodes, $compiled_rules, ); + + $motion_info->{$sid} = { + 'dependent-resources' => $dependent_resources, + 'blocking-resources-by-node' => $blocking_resources_by_node, + }; } - return ($dependent_resources, $blocking_resources_by_node); + return $motion_info; +} + +# Returns the requested state of each HA-managed resource in $sids as a hash of $sid => $state, +# reading the resources config only once. Resources that are not HA-managed are omitted, as for +# service_is_ha_managed(). Pass the manager status if it was already read. +sub get_resources_requested_state { + my ($sids, $manager_status) = @_; + + my $cfg = read_resources_config(); + $manager_status //= read_manager_status(); + + my $states = {}; + for my $sid (@$sids) { + next if !$service_check_ha_state->($cfg, $sid, undef, $manager_status); + my $state = $cfg->{ids}->{$sid}->{state} // 'started'; + $states->{$sid} = $state eq 'enabled' ? 'started' : $state; + } + + return $states; +} + +sub get_resource_motion_info { + my ($sid) = @_; + + my $motion_info = get_resources_motion_info([$sid])->{$sid} // {}; + + return ( + $motion_info->{'dependent-resources'} // [], + $motion_info->{'blocking-resources-by-node'} // {}, + ); } # graceful, as long as locking + cfs_write works diff --git a/src/test/test_resource_motion_info.pl b/src/test/test_resource_motion_info.pl index 2f7c3cd..9729ca6 100755 --- a/src/test/test_resource_motion_info.pl +++ b/src/test/test_resource_motion_info.pl @@ -148,6 +148,30 @@ for my $sid (qw(vm:103 vm:999)) { ); } +my $compile_count = 0; +my $rules_module = Test::MockModule->new('PVE::HA::Rules'); +$rules_module->redefine( + compile => sub { + $compile_count++; + return $rules_module->original('compile')->(@_); + }, +); + +is_deeply( + PVE::HA::Config::get_resources_motion_info([sort(keys %$expected), 'vm:103', 'vm:999']), + $expected, + "motion info for multiple resources omits ignored and unmanaged ones", +); +is($compile_count, 1, "rules are compiled once for multiple resources"); + +is_deeply(PVE::HA::Config::get_resources_motion_info([]), {}, "no motion info for no resources"); + +is_deeply( + PVE::HA::Config::get_resources_requested_state([qw(vm:101 vm:103 ct:104 vm:999)]), + { 'vm:101' => 'started', 'ct:104' => 'started' }, + "requested state of the HA-managed resources, without ignored and unmanaged ones", +); + # 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' }; @@ -182,4 +206,23 @@ my $expected_migrating = { $test_single_motion_info->($expected_migrating, 'migrating partner'); +is_deeply( + PVE::HA::Config::get_resources_motion_info([sort keys %$expected_migrating]), + $expected_migrating, + "migrating partner: motion info for multiple resources", +); + +$manager_status->{disarm} = { mode => 'ignore' }; + +is_deeply( + PVE::HA::Config::get_resources_motion_info([sort keys %$expected]), + {}, + "no motion info while HA is disarmed with ignore mode", +); +is_deeply( + [PVE::HA::Config::get_resource_motion_info('vm:101')], + [[], {}], + "no single motion info while HA is disarmed with ignore mode", +); + done_testing(); -- 2.47.3