public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: "Fabian Grünbichler" <f.gruenbichler@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH qemu-server 2/2] api: template: invert lock and fork
Date: Fri, 10 Sep 2021 09:48:20 +0200	[thread overview]
Message-ID: <20210910074820.1477562-2-f.gruenbichler@proxmox.com> (raw)
In-Reply-To: <20210910074820.1477562-1-f.gruenbichler@proxmox.com>

like for other API calls, repeat the cheap checks done for early abort
before forking and without locks after forking and obtaining the lock,
and only hold the flock in the forked worker instead of across the fork.

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
---
not sure whether we want to switch to the "progress bar -> task viewer"
thing in the GUI for this endpoint? it's usually instant, except when
the storage is very slow/overloaded/..

 PVE/API2/Qemu.pm | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index 0a23525..a8fbd9d 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -4353,8 +4353,7 @@ __PACKAGE__->register_method({
 
 	my $disk = extract_param($param, 'disk');
 
-	my $updatefn =  sub {
-
+	my $load_and_check = sub {
 	    my $conf = PVE::QemuConfig->load_config($vmid);
 
 	    PVE::QemuConfig->check_lock($conf);
@@ -4368,17 +4367,23 @@ __PACKAGE__->register_method({
 	    die "you can't convert a VM to template if VM is running\n"
 		if PVE::QemuServer::check_running($vmid);
 
-	    my $realcmd = sub {
-		PVE::QemuServer::template_create($vmid, $conf, $disk);
-	    };
+	    return $conf;
+	};
 
-	    $conf->{template} = 1;
-	    PVE::QemuConfig->write_config($vmid, $conf);
+	$load_and_check->();
 
-	    return $rpcenv->fork_worker('qmtemplate', $vmid, $authuser, $realcmd);
+	my $realcmd = sub {
+	    PVE::QemuConfig->lock_config($vmid, sub {
+		my $conf = $load_and_check->();
+
+		$conf->{template} = 1;
+		PVE::QemuConfig->write_config($vmid, $conf);
+
+		PVE::QemuServer::template_create($vmid, $conf, $disk);
+	    });
 	};
 
-	return PVE::QemuConfig->lock_config($vmid, $updatefn);
+	return $rpcenv->fork_worker('qmtemplate', $vmid, $authuser, $realcmd);
     }});
 
 __PACKAGE__->register_method({
-- 
2.30.2





  reply	other threads:[~2021-09-10  7:48 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-10  7:48 [pve-devel] [PATCH qemu-server 1/2] api: return UPID in template call Fabian Grünbichler
2021-09-10  7:48 ` Fabian Grünbichler [this message]
2021-10-04  7:46   ` [pve-devel] applied: [PATCH qemu-server 2/2] api: template: invert lock and fork Thomas Lamprecht
2021-09-10  8:32 ` [pve-devel] [PATCH qemu-server 1/2] api: return UPID in template call Fabian Ebner
2021-09-10 10:03   ` Fabian Grünbichler
2021-09-10  9:26 ` Dominik Csapak
2021-09-10 10:00   ` Thomas Lamprecht
2021-09-10 10:03   ` Fabian Grünbichler
2021-10-04  7:46 ` [pve-devel] applied: " Thomas Lamprecht

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=20210910074820.1477562-2-f.gruenbichler@proxmox.com \
    --to=f.gruenbichler@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