From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id 6902D1FF13B for ; Wed, 22 Apr 2026 12:00:58 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 176E2132EF; Wed, 22 Apr 2026 12:00:46 +0200 (CEST) From: Daniel Kral To: pve-devel@lists.proxmox.com Subject: [PATCH ha-manager 6/7] make get_node_affinity return all priority classes sorted in descending order Date: Wed, 22 Apr 2026 12:00:24 +0200 Message-ID: <20260422100035.232716-7-d.kral@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260422100035.232716-1-d.kral@proxmox.com> References: <20260422100035.232716-1-d.kral@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1776851951972 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.079 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment 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: GUQZKHJMD4IJPR2QFCVMPVGKLCMULHS7 X-Message-ID-Hash: GUQZKHJMD4IJPR2QFCVMPVGKLCMULHS7 X-MailFrom: d.kral@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: This is in preparation of the next patch, which needs to iterate through the priority classes from the highest to the lowest priority class. Signed-off-by: Daniel Kral --- src/PVE/HA/Manager.pm | 7 +++++-- src/PVE/HA/Rules/NodeAffinity.pm | 24 ++++++++++++++---------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/PVE/HA/Manager.pm b/src/PVE/HA/Manager.pm index ce5d69a4..0d7a2f59 100644 --- a/src/PVE/HA/Manager.pm +++ b/src/PVE/HA/Manager.pm @@ -194,7 +194,9 @@ sub get_resource_migration_candidates { my $current_leader_node = $ss->{$leader_sid}->{node}; my $online_nodes = { map { $_ => 1 } $online_node_usage->list_nodes() }; - my (undef, $target_nodes) = get_node_affinity($node_affinity, $leader_sid, $online_nodes); + my (undef, $priority_classes) = + get_node_affinity($node_affinity, $leader_sid, $online_nodes); + my $target_nodes = shift @$priority_classes // {}; my ($together, $separate) = get_resource_affinity($resource_affinity, $leader_sid, $ss, $online_nodes); apply_negative_resource_affinity($separate, $target_nodes); @@ -325,7 +327,8 @@ sub select_service_node { $compiled_rules->@{qw(node-affinity resource-affinity)}; my $online_nodes = { map { $_ => 1 } $online_node_usage->list_nodes() }; - my ($allowed_nodes, $pri_nodes) = get_node_affinity($node_affinity, $sid, $online_nodes); + my ($allowed_nodes, $priority_classes) = get_node_affinity($node_affinity, $sid, $online_nodes); + my $pri_nodes = shift @$priority_classes // {}; return undef if !%$pri_nodes; diff --git a/src/PVE/HA/Rules/NodeAffinity.pm b/src/PVE/HA/Rules/NodeAffinity.pm index 3fa1fdb4..9b4edbfd 100644 --- a/src/PVE/HA/Rules/NodeAffinity.pm +++ b/src/PVE/HA/Rules/NodeAffinity.pm @@ -253,22 +253,26 @@ __PACKAGE__->register_check( =head3 get_node_affinity($node_affinity, $sid, $online_nodes) -Returns a list of two hashes representing the node affinity of C<$sid> -according to the node affinity C<$node_affinity> and the available nodes in -the C<$online_nodes> hash. +Returns a list of a hash and a list representing the available nodes and the +priority classes of the node affinity of C<$sid> according to the node affinity +C<$node_affinity> and the online nodes in the C<$online_nodes> hash. The first hash is a hash set of available nodes, i.e. nodes where the -resource C<$sid> is allowed to be assigned to, and the second hash is a hash set -of preferred nodes, i.e. nodes where the resource C<$sid> should be assigned to. +resource C<$sid> is allowed to be assigned to. -If there are no available nodes at all, returns C. +The second item is a list of hash sets of priority classes sorted from highest +to lowest priority, where each priority class contains the nodes where the +resource C<$sid> can be assigned to. This list does not contain effectively +empty priority classes. + +If there are no available nodes at all, returns C<({}, [])>. =cut sub get_node_affinity { my ($node_affinity, $sid, $online_nodes) = @_; - return ($online_nodes, $online_nodes) if !defined($node_affinity->{$sid}); + return ($online_nodes, [$online_nodes]) if !defined($node_affinity->{$sid}); my $allowed_nodes = {}; my $prioritized_nodes = {}; @@ -283,10 +287,10 @@ sub get_node_affinity { } my $preferred_nodes = {}; - my $highest_priority = (sort { $b <=> $a } keys %$prioritized_nodes)[0]; - $preferred_nodes = $prioritized_nodes->{$highest_priority} if defined($highest_priority); + my @priority_class_numbers = sort { $b <=> $a } keys %$prioritized_nodes; + my $priority_classes = [map { $prioritized_nodes->{$_} } @priority_class_numbers]; - return ($allowed_nodes, $preferred_nodes); + return ($allowed_nodes, $priority_classes); } 1; -- 2.47.3