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 6312B1FF170 for ; Thu, 21 Aug 2025 16:39:05 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 99B2F2DD5E; Thu, 21 Aug 2025 16:37:48 +0200 (CEST) From: Daniel Kral To: pve-devel@lists.proxmox.com Date: Thu, 21 Aug 2025 16:35:36 +0200 Message-ID: <20250821143705.256562-4-d.kral@proxmox.com> X-Mailer: git-send-email 2.47.2 In-Reply-To: <20250821143705.256562-1-d.kral@proxmox.com> References: <20250821143705.256562-1-d.kral@proxmox.com> MIME-Version: 1.0 X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1755787029015 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.013 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 Subject: [pve-devel] [PATCH ha-manager 03/18] rules: factor out disjoint rules' resource set helper 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" This is done in preparation to the next patches, where the helper will be used in different packages. As the helper is not dependent on resource affinity rules at all, rename it to find_disjoint_rules_resource_sets(...) and adapt the documentation accordingly. Signed-off-by: Daniel Kral --- debian/pve-ha-manager.install | 1 + src/PVE/HA/Rules/Helpers.pm | 61 ++++++++++++++++++++++++++++ src/PVE/HA/Rules/Makefile | 2 +- src/PVE/HA/Rules/ResourceAffinity.pm | 6 ++- 4 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 src/PVE/HA/Rules/Helpers.pm diff --git a/debian/pve-ha-manager.install b/debian/pve-ha-manager.install index 2e6b7d55..38d5d60b 100644 --- a/debian/pve-ha-manager.install +++ b/debian/pve-ha-manager.install @@ -35,6 +35,7 @@ /usr/share/perl5/PVE/HA/Resources/PVECT.pm /usr/share/perl5/PVE/HA/Resources/PVEVM.pm /usr/share/perl5/PVE/HA/Rules.pm +/usr/share/perl5/PVE/HA/Rules/Helpers.pm /usr/share/perl5/PVE/HA/Rules/NodeAffinity.pm /usr/share/perl5/PVE/HA/Rules/ResourceAffinity.pm /usr/share/perl5/PVE/HA/Tools.pm diff --git a/src/PVE/HA/Rules/Helpers.pm b/src/PVE/HA/Rules/Helpers.pm new file mode 100644 index 00000000..2e6ea3f8 --- /dev/null +++ b/src/PVE/HA/Rules/Helpers.pm @@ -0,0 +1,61 @@ +package PVE::HA::Rules::Helpers; + +use strict; +use warnings; + +use PVE::HA::HashTools qw(sets_are_disjoint); + +my $sort_by_lowest_resource_id = sub { + my ($rules) = @_; + + my $lowest_rule_resource_id = {}; + for my $ruleid (keys %$rules) { + my @rule_resources = sort keys $rules->{$ruleid}->{resources}->%*; + $lowest_rule_resource_id->{$ruleid} = $rule_resources[0]; + } + + # sort rules such that rules with the lowest numbered resource come first + my @sorted_ruleids = sort { + $lowest_rule_resource_id->{$a} cmp $lowest_rule_resource_id->{$b} + } sort keys %$rules; + + return @sorted_ruleids; +}; + +# returns a list of hashes, where each hash item contains a resource set +# disjoint from the other items and the ruleids that were used to form that +# resource set. +sub find_disjoint_rules_resource_sets { + my ($rules) = @_; + + my @disjoint_rules = (); + + # order needed so that it is easier to check whether there is an overlap + my @sorted_ruleids = $sort_by_lowest_resource_id->($rules); + + for my $ruleid (@sorted_ruleids) { + my $rule = $rules->{$ruleid}; + + my $found = 0; + for my $entry (@disjoint_rules) { + next if sets_are_disjoint($rule->{resources}, $entry->{resources}); + + $found = 1; + push @{ $entry->{ruleids} }, $ruleid; + $entry->{resources}->{$_} = 1 for keys $rule->{resources}->%*; + + last; + } + if (!$found) { + push @disjoint_rules, + { + ruleids => [$ruleid], + resources => { $rule->{resources}->%* }, + }; + } + } + + return @disjoint_rules; +} + +1; diff --git a/src/PVE/HA/Rules/Makefile b/src/PVE/HA/Rules/Makefile index 64119257..ce030d1f 100644 --- a/src/PVE/HA/Rules/Makefile +++ b/src/PVE/HA/Rules/Makefile @@ -1,4 +1,4 @@ -SOURCES=NodeAffinity.pm ResourceAffinity.pm +SOURCES=Helpers.pm NodeAffinity.pm ResourceAffinity.pm .PHONY: install install: diff --git a/src/PVE/HA/Rules/ResourceAffinity.pm b/src/PVE/HA/Rules/ResourceAffinity.pm index 9bc039ba..edfa4b3d 100644 --- a/src/PVE/HA/Rules/ResourceAffinity.pm +++ b/src/PVE/HA/Rules/ResourceAffinity.pm @@ -3,8 +3,9 @@ package PVE::HA::Rules::ResourceAffinity; use strict; use warnings; -use PVE::HA::HashTools qw(set_intersect sets_are_disjoint); +use PVE::HA::HashTools qw(set_intersect); use PVE::HA::Rules; +use PVE::HA::Rules::Helpers; use base qw(Exporter); use base qw(PVE::HA::Rules); @@ -319,7 +320,8 @@ a resource, in C<$rules> at a later point in time. sub merge_connected_positive_resource_affinity_rules { my ($rules, $positive_rules) = @_; - my @disjoint_positive_rules = $find_disjoint_resource_affinity_rules->($positive_rules); + my @disjoint_positive_rules = + PVE::HA::Rules::Helpers::find_disjoint_rules_resource_sets($positive_rules); for my $entry (@disjoint_positive_rules) { next if @{ $entry->{ruleids} } < 2; -- 2.47.2 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel