[pve-devel] [PATCH manager] improve simultaneous free VMID api calls (#889)

Thomas Lamprecht t.lamprecht at proxmox.com
Mon Oct 17 17:44:33 CEST 2016


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.lamprecht at 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





More information about the pve-devel mailing list