The /cluster/nextid call is not thread safe, when making calls in
(quasi) parallel the callee may get overlapping VMIDs and then only
the first one actually writing the config to the pmxcfs "wins" and
may use it.

Use the new 'next_unused_vmid' from the cluster package to improve
this. It not only avoids the race condition in the method itself but
also reserves the VMID temporary (60s) so that the callee may take
some time between calling nextid and actually write the config.

Signed-off-by: Thomas Lamprecht <t.lampre...@proxmox.com>
---
 PVE/API2/Cluster.pm | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/PVE/API2/Cluster.pm b/PVE/API2/Cluster.pm
index 331e2f7..5172b93 100644
--- a/PVE/API2/Cluster.pm
+++ b/PVE/API2/Cluster.pm
@@ -450,6 +450,12 @@ __PACKAGE__->register_method({
        additionalProperties => 0,
        properties => {
            vmid => get_standard_option('pve-vmid', {optional => 1}),
+           reserve => {
+               type => 'boolean',
+               optional => 1,
+               default => 0,
+               description => "Reserve the returned VMID temporary (only 
reserved in nextid calls). Useful when creating VMs in parallel.",
+           }
        },
     },
     returns => {
@@ -467,11 +473,7 @@ __PACKAGE__->register_method({
            raise_param_exc({ vmid => "VM $vmid already exists" });
        }
 
-       for (my $i = 100; $i < 10000; $i++) {
-           return $i if !defined($idlist->{$i});
-       }
-
-       die "unable to get any free VMID\n";
+       return PVE::Cluster::next_unused_vmid(100, 100000, $param->{reserve});
     }});
 
 1;
-- 
2.1.4


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

Reply via email to