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 v5 01/14] introduce PVE::HA::HashTools module
Date: Wed, 30 Jul 2025 20:14:05 +0200	[thread overview]
Message-ID: <20250730181428.392906-2-d.kral@proxmox.com> (raw)
In-Reply-To: <20250730181428.392906-1-d.kral@proxmox.com>

Add a new package PVE::HA::HashTools to provide helpers for the common
operations done on hashes.

These helper subroutines implement basic set operations done on hash
sets, which will be used in an upcoming patch for the resource affinity
rules.

Signed-off-by: Daniel Kral <d.kral@proxmox.com>
---
 debian/pve-ha-manager.install |  1 +
 src/PVE/HA/HashTools.pm       | 90 +++++++++++++++++++++++++++++++++++
 src/PVE/HA/Makefile           |  4 +-
 3 files changed, 93 insertions(+), 2 deletions(-)
 create mode 100644 src/PVE/HA/HashTools.pm

diff --git a/debian/pve-ha-manager.install b/debian/pve-ha-manager.install
index b4eff279..79f86d24 100644
--- a/debian/pve-ha-manager.install
+++ b/debian/pve-ha-manager.install
@@ -27,6 +27,7 @@
 /usr/share/perl5/PVE/HA/Fence.pm
 /usr/share/perl5/PVE/HA/FenceConfig.pm
 /usr/share/perl5/PVE/HA/Groups.pm
+/usr/share/perl5/PVE/HA/HashTools.pm
 /usr/share/perl5/PVE/HA/LRM.pm
 /usr/share/perl5/PVE/HA/Manager.pm
 /usr/share/perl5/PVE/HA/NodeStatus.pm
diff --git a/src/PVE/HA/HashTools.pm b/src/PVE/HA/HashTools.pm
new file mode 100644
index 00000000..cf5c7a20
--- /dev/null
+++ b/src/PVE/HA/HashTools.pm
@@ -0,0 +1,90 @@
+package PVE::HA::HashTools;
+
+use strict;
+use warnings;
+
+use base qw(Exporter);
+
+our @EXPORT_OK = qw(
+    set_intersect
+    set_union
+    sets_are_disjoint
+);
+
+=head1 NAME
+
+PVE::HA::HashTools - Helpers for Hashes
+
+=head1 DESCRIPTION
+
+This packages provides helpers for common operations on hashes.
+
+Even though these operations' implementation are often one-liners, they are
+meant to improve code readability by stating a operation name instead of the
+more verbose implementation.
+
+=cut
+
+=head1 FUNCTIONS
+
+=cut
+
+=head3 set_intersect($hash1, $hash2)
+
+Returns a hash set of the intersection of the hash sets C<$hash1> and
+C<$hash2>, i.e. the elements that are both in C<$hash1> and C<$hash2>.
+
+The hashes C<$hash1> and C<$hash2> are expected to be hash sets, i.e.
+key-value pairs are always set to C<1> or another truthy value.
+
+=cut
+
+sub set_intersect : prototype($$) {
+    my ($hash1, $hash2) = @_;
+
+    my $result = { map { $hash1->{$_} && $hash2->{$_} ? ($_ => 1) : () } keys %$hash1 };
+
+    return $result;
+}
+
+=head3 set_union($hash1, $hash2)
+
+Returns a hash set of the union of the hash sets C<$hash1> and C<$hash2>, i.e.
+the elements that are in either C<$hash1> or C<$hash2>.
+
+The hashes C<$hash1> and C<$hash2> are expected to be hash sets, i.e.
+key-value pairs are always set to C<1> or another truthy value.
+
+=cut
+
+sub set_union : prototype($$) {
+    my ($hash1, $hash2) = @_;
+
+    my $result = { map { $_ => 1 } keys %$hash1, keys %$hash2 };
+
+    return $result;
+}
+
+=head3 sets_are_disjoint($hash1, $hash2)
+
+Checks whether the two given hash sets C<$hash1> and C<$hash2> are disjoint,
+i.e. have no common element in both of them.
+
+The hashes C<$hash1> and C<$hash2> are expected to be hash sets, i.e.
+key-value pairs are always set to C<1> or another truthy value.
+
+Returns C<1> if they are disjoint, C<0> otherwise.
+
+=cut
+
+sub sets_are_disjoint : prototype($$) {
+    my ($hash1, $hash2) = @_;
+
+    for my $key (keys %$hash1) {
+        return 0 if $hash2->{$key};
+    }
+
+    return 1;
+}
+
+1;
diff --git a/src/PVE/HA/Makefile b/src/PVE/HA/Makefile
index e386cbfc..88629074 100644
--- a/src/PVE/HA/Makefile
+++ b/src/PVE/HA/Makefile
@@ -1,5 +1,5 @@
-SIM_SOURCES=CRM.pm Env.pm Groups.pm Rules.pm Resources.pm LRM.pm Manager.pm \
-	NodeStatus.pm Tools.pm FenceConfig.pm Fence.pm Usage.pm
+SIM_SOURCES=CRM.pm Env.pm Groups.pm HashTools.pm Rules.pm Resources.pm LRM.pm \
+	Manager.pm NodeStatus.pm Tools.pm FenceConfig.pm Fence.pm Usage.pm
 
 SOURCES=${SIM_SOURCES} Config.pm
 
-- 
2.47.2



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


  reply	other threads:[~2025-07-30 18:13 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-30 18:14 [pve-devel] [PATCH container/docs/ha-manager/manager/qemu-server v5 00/20] HA resource affinity rules Daniel Kral
2025-07-30 18:14 ` Daniel Kral [this message]
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 02/14] rules: introduce plugin-specific canonicalize routines Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 03/14] rules: add haenv node list to the rules' canonicalization stage Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 04/14] rules: introduce resource affinity rule plugin Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 05/14] rules: add global checks between node and resource affinity rules Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 06/14] usage: add information about a service's assigned nodes Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 07/14] manager: apply resource affinity rules when selecting service nodes Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 08/14] manager: handle resource affinity rules in manual migrations Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 09/14] sim: resources: add option to limit start and migrate tries to node Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 10/14] test: ha tester: add test cases for negative resource affinity rules Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 11/14] test: ha tester: add test cases for positive " Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 12/14] test: ha tester: add test cases for static scheduler resource affinity Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 13/14] test: rules: add test cases for resource affinity rules Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH ha-manager v5 14/14] api: resources: add check for resource affinity in resource migrations Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH docs v5 1/1] ha: add documentation about ha resource affinity rules Daniel Kral
2025-08-04 13:00   ` Hannes Duerr
2025-07-30 18:14 ` [pve-devel] [PATCH manager v5 1/3] ui: ha: rules: add " Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH manager v5 2/3] ui: migrate: lxc: display precondition messages for ha resource affinity Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH manager v5 3/3] ui: migrate: vm: " Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH container v5 1/1] api: introduce migration preconditions api endpoint Daniel Kral
2025-07-30 18:14 ` [pve-devel] [PATCH qemu-server v5 1/1] api: migration preconditions: add checks for ha resource affinity rules Daniel Kral
2025-08-04  9:50 ` [pve-devel] applied-series: [PATCH container/docs/ha-manager/manager/qemu-server v5 00/20] HA " Fiona Ebner

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=20250730181428.392906-2-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