From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id C54F21FF187 for ; Mon, 3 Nov 2025 11:20:59 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 4BE7917851; Mon, 3 Nov 2025 11:21:29 +0100 (CET) From: Daniel Kral To: pve-devel@lists.proxmox.com Date: Mon, 3 Nov 2025 11:19:47 +0100 Message-ID: <20251103102118.153666-4-d.kral@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20251103102118.153666-1-d.kral@proxmox.com> References: <20251103102118.153666-1-d.kral@proxmox.com> MIME-Version: 1.0 X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1762165265969 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.015 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 v3 03/21] 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 --- changes since v2: - fix minor merge conflict for Makefile changes while rebasing - make new HA rules helper use perl 5.36 + signatures - change `my $name = sub { ... }` to `my sub name { ... }` as the latter has a name in perl debugging utilities debian/pve-ha-manager.install | 1 + src/PVE/HA/Rules/Helpers.pm | 77 ++++++++++++++++++++++++++++ src/PVE/HA/Rules/Makefile | 2 +- src/PVE/HA/Rules/ResourceAffinity.pm | 58 ++------------------- 4 files changed, 83 insertions(+), 55 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..7861332b --- /dev/null +++ b/src/PVE/HA/Rules/Helpers.pm @@ -0,0 +1,77 @@ +package PVE::HA::Rules::Helpers; + +use v5.36; + +use PVE::HA::HashTools qw(sets_are_disjoint); + +my sub sort_by_lowest_resource_id($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; +} + +=head3 find_disjoint_rules_resource_sets($rules) + +Finds the rule subsets in C<$rules>, which reference a disjoint set of resources +with respect to the other rules. The disjoint rule resource sets are returned as +a list of hashes, where each item contains the disjoint resource set and the +ruleids from C<$rules> that were used to create the resource subset. + +For example, if one rule references the resources C<'vm:101'> and C<'vm:102'>, +and another rule references the resources C<'vm:102'> and C<'vm:103'>, these +will result in one disjoint rule resource set, thus the return value: + + ( + { + ruleids = [ 'rule1', 'rule2' ], + resources => { + 'vm:101' => 1, + 'vm:102' => 1, + 'vm:103' => 1 + } + } + ) + +=cut + +sub find_disjoint_rules_resource_sets($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 1cbde5ba..977c882a 100644 --- a/src/PVE/HA/Rules/Makefile +++ b/src/PVE/HA/Rules/Makefile @@ -1,4 +1,4 @@ -SIM_SOURCES=NodeAffinity.pm ResourceAffinity.pm +SIM_SOURCES=Helpers.pm NodeAffinity.pm ResourceAffinity.pm SOURCES=${SIM_SOURCES} .PHONY: install diff --git a/src/PVE/HA/Rules/ResourceAffinity.pm b/src/PVE/HA/Rules/ResourceAffinity.pm index 9a928196..86b94d60 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 PVE::HA::Usage; use base qw(Exporter); @@ -248,58 +249,6 @@ __PACKAGE__->register_check( =cut -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, which contain disjoint resource affinity rules, i.e., -# put resource affinity constraints on disjoint sets of resources -my $find_disjoint_resource_affinity_rules = sub { - 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; -}; - =head3 merge_connected_positive_resource_affinity_rules($rules, $positive_rules) Modifies C<$rules> to contain only disjoint positive resource affinity rules @@ -320,7 +269,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.3 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel