[pve-devel] [PATCH ha-manager v3 03/21] rules: factor out disjoint rules' resource set helper

Daniel Kral d.kral at proxmox.com
Mon Nov 3 11:19:47 CET 2025


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 <d.kral at proxmox.com>
---
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





More information about the pve-devel mailing list