public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: "Shannon Sterz" <s.sterz@proxmox.com>
To: "Daniel Krambrock" <krambrock@hrz.uni-marburg.de>,
	<pve-devel@lists.proxmox.com>
Subject: Re: [pve-devel] [PATCH manager] close #4369: make VMID suggestion strategy configurable
Date: Wed, 05 Jun 2024 10:57:50 +0200	[thread overview]
Message-ID: <D1RYJGMP1K55.NTQBB6M3R7J@proxmox.com> (raw)
In-Reply-To: <20240522120553.49114-2-krambrock@hrz.uni-marburg.de>

This is a fairly large patch. It could easily be split into a backend
and front-end portion. That would make it easier to review, in my
opinion.

On Wed May 22, 2024 at 2:05 PM CEST, Daniel Krambrock wrote:
> VMID suggestion strategy is set in datacenter config
>
> Signed-off-by: Daniel Krambrock <krambrock@hrz.uni-marburg.de>
> ---
>  PVE/API2/Cluster.pm           | 16 +++++++++--
>  PVE/Makefile                  |  1 +
>  PVE/UsedVmidList.pm           | 53 +++++++++++++++++++++++++++++++++++
>  www/manager6/dc/OptionView.js | 14 +++++++++
>  4 files changed, 82 insertions(+), 2 deletions(-)
>  create mode 100644 PVE/UsedVmidList.pm
>
> diff --git a/PVE/API2/Cluster.pm b/PVE/API2/Cluster.pm
> index 04387ab4..be79ddf0 100644
> --- a/PVE/API2/Cluster.pm
> +++ b/PVE/API2/Cluster.pm
> @@ -20,6 +20,7 @@ use PVE::RPCEnvironment;
>  use PVE::SafeSyslog;
>  use PVE::Storage;
>  use PVE::Tools qw(extract_param);
> +use PVE::UsedVmidList;
>
>  use PVE::API2::ACMEAccount;
>  use PVE::API2::ACMEPlugin;
> @@ -813,12 +814,23 @@ __PACKAGE__->register_method({
>
>  	my $dc_conf = PVE::Cluster::cfs_read_file('datacenter.cfg');
>  	my $next_id = $dc_conf->{'next-id'} // {};
> +	my $strategy = $dc_conf->{'next-id-strategy'} // "next-free";
>
>  	my $lower = $next_id->{lower} // 100;
>  	my $upper = $next_id->{upper} // (1000 * 1000); # note, lower than the schema-maximum
>
> -	for (my $i = $lower; $i < $upper; $i++) {
> -	    return $i if !defined($idlist->{$i});
> +	if ($strategy eq "next-free") {
> +	    for (my $i = $lower; $i < $upper; $i++) {
> +	        return $i if !defined($idlist->{$i});
> +	    }
> +	} elsif ($strategy eq "max+1") {
> +	    return %$idlist ? (List::Util::max(keys %$idlist) + 1) : $lower
> +	} elsif ($strategy eq "list") {
> +	    for (my $i = $lower; $i < $upper; $i++) {
> +	        return $i if (!defined($idlist->{$i}) and !PVE::UsedVmidList::is_on_vmid_list($i)) ;

Nit: Unnecessary space before semicolon.

> +	    }
> +	} else {
> +	    die "unable to get any free VMID with strategy $strategy\n";
>  	}
>
>  	die "unable to get any free VMID in range [$lower, $upper]\n";
> diff --git a/PVE/Makefile b/PVE/Makefile
> index 660de4d0..fe627296 100644
> --- a/PVE/Makefile
> +++ b/PVE/Makefile
> @@ -14,6 +14,7 @@ PERLSOURCE = 			\
>  	Jobs.pm			\
>  	NodeConfig.pm		\
>  	Report.pm		\
> +	UsedVmidList.pm		\
>  	VZDump.pm
>
>  all: pvecfg.pm $(SUBDIRS)
> diff --git a/PVE/UsedVmidList.pm b/PVE/UsedVmidList.pm
> new file mode 100644
> index 00000000..a60c75d6
> --- /dev/null
> +++ b/PVE/UsedVmidList.pm
> @@ -0,0 +1,53 @@
> +package PVE::UsedVmidList;
> +
> +use strict;
> +use warnings;
> +
> +use PVE::Cluster;
> +
> +my $parse_vmid_list = sub {
> +    my ($filename, $raw) = @_;
> +
> +    return [] if !defined($raw);
> +
> +    my @parsed;
> +    my @lines = split(/\n/, $raw);
> +    foreach my $line (@lines) {
> +	next if $line =~ m/^\s*$/;
> +
> +	if ($line =~ m/^(\d+)$/) {
> +	    push(@parsed, $1);
> +	} else {
> +	    warn "Skipping invalid used_vmids.list entry: $line\n";
> +	}
> +    }
> +
> +    return \@parsed;
> +};
> +
> +my $write_vmid_list = sub {
> +    my ($filename, @data) = @_;
> +
> +    return join("\n", sort @data);
> +};
> +
> +PVE::Cluster::cfs_register_file('used_vmids.list', $parse_vmid_list, $write_vmid_list);
> +
> +sub add_vmid {
> +    my ($vmid) = @_;
> +
> +    PVE::Cluster::cfs_lock_file('used_vmids.list', 10, sub {
> +	my $vmid_list = PVE::Cluster::cfs_read_file('used_vmids.list');
> +
> +	push(@$vmid_list, $vmid);
> +	PVE::Cluster::cfs_write_file('used_vmids.list', join("\n", @$vmid_list));
> +    });
> +}
> +
> +sub is_on_vmid_list {
> +    my ($vmid) = @_;
> +    my $vmid_list = PVE::Cluster::cfs_read_file('used_vmids.list');
> +    return scalar(grep { $_ == $vmid } @$vmid_list);
> +}
> +
> +1;
> diff --git a/www/manager6/dc/OptionView.js b/www/manager6/dc/OptionView.js
> index b200fd12..613a2183 100644
> --- a/www/manager6/dc/OptionView.js
> +++ b/www/manager6/dc/OptionView.js
> @@ -339,6 +339,20 @@ Ext.define('PVE.dc.OptionView', {
>  		submitValue: true,
>  	    }],
>  	});
> +	me.add_combobox_row('next-id-strategy', gettext('Next Free VMID Strategy'), {
> +	    renderer: v => {
> +		if (v === '__default__') {return Proxmox.Utils.defaultText;}
> +	        return v;

Nit: The indetation here seems off, both lines should be indented with
two tabs.

Also, please note that our JS style guide says you should avoid single
line if statements in new code.

> +	    },
> +	    comboItems: [
> +		['__default__', Proxmox.Utils.defaultText + ' (next-free)'],
> +		['next-free', 'next-free'],
> +		['max+1', 'max+1'],
> +		['list', 'list'],
> +	    ],
> +	    defaultValue: '__default__',
> +	    deleteEmpty: true,
> +	});
>  	me.rows['tag-style'] = {
>  	    required: true,
>  	    renderer: (value) => {



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


  parent reply	other threads:[~2024-06-05  8:57 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20240522120553.49114-1-krambrock@hrz.uni-marburg.de>
2024-05-22 12:05 ` Daniel Krambrock via pve-devel
2024-05-22 12:05 ` [pve-devel] [PATCH cluster] " Daniel Krambrock via pve-devel
2024-05-22 12:05 ` [pve-devel] [PATCH container] " Daniel Krambrock via pve-devel
2024-05-22 12:05 ` [pve-devel] [PATCH qemu] " Daniel Krambrock via pve-devel
2024-06-05  8:56 ` [pve-devel] (no subject) Shannon Sterz
     [not found] ` <20240522120553.49114-2-krambrock@hrz.uni-marburg.de>
2024-06-05  8:57   ` Shannon Sterz [this message]
     [not found] ` <20240522120553.49114-3-krambrock@hrz.uni-marburg.de>
2024-06-05  8:57   ` [pve-devel] [PATCH cluster] close #4369: make VMID suggestion strategy configurable Shannon Sterz

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=D1RYJGMP1K55.NTQBB6M3R7J@proxmox.com \
    --to=s.sterz@proxmox.com \
    --cc=krambrock@hrz.uni-marburg.de \
    --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