[pve-devel] [PATCH ha-manager 02/15] tools: add hash set helper subroutines

Thomas Lamprecht t.lamprecht at proxmox.com
Tue Mar 25 18:53:37 CET 2025


Am 25.03.25 um 16:12 schrieb Daniel Kral:
> Implement helper subroutines, which implement basic set operations done
> on hash sets, i.e. hashes with elements set to a true value, e.g. 1.
> 
> These will be used for various tasks in the HA Manager colocation rules,
> e.g. for verifying the satisfiability of the rules or applying the
> colocation rules on the allowed set of nodes.
> 
> Signed-off-by: Daniel Kral <d.kral at proxmox.com>
> ---
> If they're useful somewhere else, I can move them to PVE::Tools
> post-RFC, but it'd be probably useful to prefix them with `hash_` there.

meh, not a big fan of growing the overly generic PVE::Tools more, if, this
should go into a dedicated module for hash/data structure helpers ...

> AFAICS there weren't any other helpers for this with a quick grep over
> all projects and `PVE::Tools::array_intersect()` wasn't what I needed.

... which those existing one should then also move into, but out of scope
of this series.

> 
>  src/PVE/HA/Tools.pm | 42 ++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 42 insertions(+)
> 
> diff --git a/src/PVE/HA/Tools.pm b/src/PVE/HA/Tools.pm
> index 0f9e9a5..fc3282c 100644
> --- a/src/PVE/HA/Tools.pm
> +++ b/src/PVE/HA/Tools.pm
> @@ -115,6 +115,48 @@ sub write_json_to_file {
>      PVE::Tools::file_set_contents($filename, $raw);
>  }
>  
> +sub is_disjoint {

IMO a bit too generic name for being in a Tools named module, maybe
prefix them all with hash_ or hashes_ ?

> +    my ($hash1, $hash2) = @_;
> +
> +    for my $key (keys %$hash1) {
> +	return 0 if exists($hash2->{$key});
> +    }
> +
> +    return 1;
> +};
> +
> +sub intersect {
> +    my ($hash1, $hash2) = @_;
> +
> +    my $result = { map { $_ => $hash2->{$_} } keys %$hash1 };
> +
> +    for my $key (keys %$result) {
> +	delete $result->{$key} if !defined($result->{$key});
> +    }
> +
> +    return $result;
> +};
> +
> +sub set_difference {
> +    my ($hash1, $hash2) = @_;
> +
> +    my $result = { map { $_ => 1 } keys %$hash1 };
> +
> +    for my $key (keys %$result) {
> +	delete $result->{$key} if defined($hash2->{$key});
> +    }
> +
> +    return $result;
> +};
> +
> +sub union {
> +    my ($hash1, $hash2) = @_;
> +
> +    my $result = { map { $_ => 1 } keys %$hash1, keys %$hash2 };
> +
> +    return $result;
> +};
> +
>  sub count_fenced_services {
>      my ($ss, $node) = @_;
>  





More information about the pve-devel mailing list