public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Daniel Kral <d.kral@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH ha-manager v3 03/21] rules: factor out disjoint rules' resource set helper
Date: Mon,  3 Nov 2025 11:19:47 +0100	[thread overview]
Message-ID: <20251103102118.153666-4-d.kral@proxmox.com> (raw)
In-Reply-To: <20251103102118.153666-1-d.kral@proxmox.com>

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@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



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


  parent reply	other threads:[~2025-11-03 10:20 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-03 10:19 [pve-devel] [PATCH ha-manager v3 00/21] HA rules fixes + performance improvements + cleanup Daniel Kral
2025-11-03 10:19 ` [pve-devel] [PATCH ha-manager v3 01/21] config: do not add ignored resources to dependent resources Daniel Kral
2025-11-03 10:19 ` [pve-devel] [PATCH ha-manager v3 02/21] manager: retranslate rules if nodes are added or removed Daniel Kral
2025-11-03 10:19 ` Daniel Kral [this message]
2025-11-03 10:19 ` [pve-devel] [PATCH ha-manager v3 04/21] rules: resource affinity: inter-consistency check with merged positive rules Daniel Kral
2025-11-03 10:19 ` [pve-devel] [PATCH ha-manager v3 05/21] rules: add merged positive resource affinity info in global checks Daniel Kral
2025-11-03 17:24   ` Michael Köppl
2025-11-03 10:19 ` [pve-devel] [PATCH ha-manager v3 06/21] rules: make rules sorting optional in foreach_rule helper Daniel Kral
2025-11-03 10:19 ` [pve-devel] [PATCH ha-manager v3 07/21] rename rule's canonicalize stage to transform stage Daniel Kral
2025-11-03 10:19 ` [pve-devel] [PATCH ha-manager v3 08/21] rules: make plugins register transformers instead of plugin_transform Daniel Kral
2025-11-03 10:19 ` [pve-devel] [PATCH ha-manager v3 09/21] rules: node affinity: decouple get_node_affinity helper from Usage class Daniel Kral
2025-11-03 10:19 ` [pve-devel] [PATCH ha-manager v3 10/21] compile ha rules to a more compact representation Daniel Kral
2025-11-03 10:19 ` [pve-devel] [PATCH ha-manager v3 11/21] test: rules: use to_json instead of Data::Dumper for config output Daniel Kral
2025-11-03 10:19 ` [pve-devel] [PATCH ha-manager v3 12/21] test: rules: add compiled config output to rules config test cases Daniel Kral
2025-11-03 10:19 ` [pve-devel] [PATCH ha-manager v3 13/21] rules: node affinity: define node priority outside hash access Daniel Kral
2025-11-03 10:19 ` [pve-devel] [PATCH ha-manager v3 14/21] move minimum version check helper to ha tools Daniel Kral
2025-11-03 10:19 ` [pve-devel] [PATCH ha-manager v3 15/21] manager: move group migration cooldown variable into helper Daniel Kral
2025-11-03 10:20 ` [pve-devel] [PATCH ha-manager v3 16/21] api: status: sync active service counting with lrm's helper Daniel Kral
2025-11-03 10:20 ` [pve-devel] [PATCH ha-manager v3 17/21] manager: group migration: " Daniel Kral
2025-11-03 10:20 ` [pve-devel] [PATCH ha-manager v3 18/21] factor out counting of active services into helper Daniel Kral
2025-11-03 10:20 ` [pve-devel] [PATCH ha-manager v3 19/21] tree-wide: remove misused function prototype declaractions Daniel Kral
2025-11-03 10:20 ` [pve-devel] [PATCH ha-manager v3 20/21] rules: fix documentation for inter-rules checker subroutines Daniel Kral
2025-11-03 10:20 ` [pve-devel] [PATCH ha-manager v3 21/21] rules: add documentation about current feasibility check implementations Daniel Kral
2025-11-03 17:43 ` [pve-devel] [PATCH ha-manager v3 00/21] HA rules fixes + performance improvements + cleanup Michael Köppl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251103102118.153666-4-d.kral@proxmox.com \
    --to=d.kral@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal