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 C408B1FF165 for ; Thu, 31 Jul 2025 10:20:03 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id D1B4D3582B; Thu, 31 Jul 2025 10:21:27 +0200 (CEST) Mime-Version: 1.0 Date: Thu, 31 Jul 2025 10:21:24 +0200 Message-Id: From: "Daniel Kral" To: "Thomas Lamprecht" , "Proxmox VE development discussion" X-Mailer: aerc 0.20.0 References: <20250730175957.386674-1-d.kral@proxmox.com> <20250730175957.386674-12-d.kral@proxmox.com> <9e5c0a1b-8012-4f01-a259-29b3cb1f823b@proxmox.com> In-Reply-To: <9e5c0a1b-8012-4f01-a259-29b3cb1f823b@proxmox.com> X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1753950072922 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.014 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 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: Re: [pve-devel] [PATCH ha-manager v5 11/23] manager: apply node affinity rules when selecting service nodes X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox VE development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" On Thu Jul 31, 2025 at 7:26 AM CEST, Thomas Lamprecht wrote: > Am 30.07.25 um 20:01 schrieb Daniel Kral: >> @@ -210,4 +215,82 @@ __PACKAGE__->register_check( >> }, >> ); >> >> +=head1 NODE AFFINITY RULE HELPERS >> + >> +=cut >> + >> +my $get_resource_node_affinity_rule = sub { >> + my ($rules, $sid) = @_; >> + >> + # with the current restriction a resource can only be in one node affinity rule >> + my $node_affinity_rule; >> + PVE::HA::Rules::foreach_rule( >> + $rules, >> + sub { >> + my ($rule) = @_; >> + >> + $node_affinity_rule = dclone($rule) if !$node_affinity_rule; >> + }, >> + { >> + sid => $sid, >> + type => 'node-affinity', >> + exclude_disabled_rules => 1, >> + }, > > meh. this a bit hard to read, passing the opts as hash value not hash > ref could be nicer? > > foreach_rule : prototype($$;%) { > my ($rules, $func, %opts) = @_; > > # here avoid the useless intermediate variables > > ....-+ > } > > > PVE::HA::Rules::foreach_rule( > $rules, > sub { > ... > }, > sid => $sid, > type => 'node-affinity', > .... > } ACK, that look much cleaner, I'll send it as a follow-up patch (after the more important follow-ups of course). > >> + ); >> + >> + return $node_affinity_rule; >> +}; >> + >> +=head3 get_node_affinity($rules, $sid, $online_node_usage) >> + >> +Returns a list of two hashes representing the node affinity of C<$sid> >> +according to the node affinity rules in C<$rules> and the available nodes in >> +C<$online_node_usage>. >> + >> +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. >> + >> +If there are no available nodes at all, returns C. >> + >> +=cut >> + >> +sub get_node_affinity : prototype($$$) { >> + my ($rules, $sid, $online_node_usage) = @_; >> + >> + my $node_affinity_rule = $get_resource_node_affinity_rule->($rules, $sid); >> + >> + # default to a node affinity rule with all available nodes >> + if (!$node_affinity_rule) { > > This seems not so nice, uses auto-vifivication to get the hash going again, > that's almost always a code smell, let's rather explicitly assign and check, > e.g. something like: > > if (!defined($node_affinity_rule) || !scalar($node_affinity_rule->%*)) { > $node_affinity_rule //= {}; > ACK, will also send a follow-up patch cleaning that up! > > >> + for my $node ($online_node_usage->list_nodes()) { >> + $node_affinity_rule->{nodes}->{$node} = { priority => 0 }; >> + } >> + } >> + >> + # add remaining nodes with low priority for non-strict node affinity rules >> + if (!$node_affinity_rule->{strict}) { >> + for my $node ($online_node_usage->list_nodes()) { >> + next if defined($node_affinity_rule->{nodes}->{$node}); >> + >> + $node_affinity_rule->{nodes}->{$node} = { priority => -1 }; >> + } >> + } >> + >> + my $allowed_nodes = {}; >> + my $prioritized_nodes = {}; >> + >> + while (my ($node, $props) = each %{ $node_affinity_rule->{nodes} }) { >> + next if !$online_node_usage->contains_node($node); # node is offline >> + >> + $allowed_nodes->{$node} = 1; >> + $prioritized_nodes->{ $props->{priority} }->{$node} = 1; > > style nit: please avoid using hash for hash key access. ACK, here too. > >> + } >> + >> + my $preferred_nodes = {}; >> + my $highest_priority = (sort { $b <=> $a } keys %$prioritized_nodes)[0]; >> + $preferred_nodes = $prioritized_nodes->{$highest_priority} if defined($highest_priority); >> + >> + return ($allowed_nodes, $preferred_nodes); >> +} >> + >> 1; _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel