From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <s.reiter@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id AA49A66E96;
 Mon, 11 Jan 2021 12:14:46 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id A70C01922B;
 Mon, 11 Jan 2021 12:14:45 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [212.186.127.180])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id A125319106;
 Mon, 11 Jan 2021 12:14:40 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 67D0145F29;
 Mon, 11 Jan 2021 12:14:40 +0100 (CET)
From: Stefan Reiter <s.reiter@proxmox.com>
To: pve-devel@lists.proxmox.com,
	pbs-devel@lists.proxmox.com
Date: Mon, 11 Jan 2021 12:14:04 +0100
Message-Id: <20210111111409.32385-7-s.reiter@proxmox.com>
X-Mailer: git-send-email 2.20.1
In-Reply-To: <20210111111409.32385-1-s.reiter@proxmox.com>
References: <20210111111409.32385-1-s.reiter@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.040 Adjusted score from AWL reputation of From: address
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 RCVD_IN_DNSWL_MED        -2.3 Sender listed at https://www.dnswl.org/,
 medium trust
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pve-devel] [PATCH qemu-server 06/11] make
 qemu_drive_mirror_monitor more generic
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Mon, 11 Jan 2021 11:14:46 -0000

...so it works with other block jobs as well. Intended use case is
block-stream, which also requires a new "auto" (wait only) completion
mode, since it finishes automatically anyway.

Signed-off-by: Stefan Reiter <s.reiter@proxmox.com>
---
 PVE/QemuServer.pm | 41 +++++++++++++++++++++++------------------
 1 file changed, 23 insertions(+), 18 deletions(-)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index bca5669..d517dae 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -6777,15 +6777,16 @@ sub qemu_drive_mirror {
 	die "mirroring error: $err\n";
     }
 
-    qemu_drive_mirror_monitor ($vmid, $vmiddst, $jobs, $completion, $qga);
+    qemu_drive_job_monitor("mirror", $vmid, $vmiddst, $jobs, $completion, $qga);
 }
 
 # $completion can be either
 # 'complete': wait until all jobs are ready, block-job-complete them (default)
 # 'cancel': wait until all jobs are ready, block-job-cancel them
 # 'skip': wait until all jobs are ready, return with block jobs in ready state
-sub qemu_drive_mirror_monitor {
-    my ($vmid, $vmiddst, $jobs, $completion, $qga) = @_;
+# 'auto': wait until all jobs disappear, only use for jobs which complete automatically
+sub qemu_drive_job_monitor {
+    my ($op, $vmid, $vmiddst, $jobs, $completion, $qga) = @_;
 
     $completion //= 'complete';
 
@@ -6793,46 +6794,50 @@ sub qemu_drive_mirror_monitor {
 	my $err_complete = 0;
 
 	while (1) {
-	    die "storage migration timed out\n" if $err_complete > 300;
+	    die "block job ('$op') timed out\n" if $err_complete > 300;
 
 	    my $stats = mon_cmd($vmid, "query-block-jobs");
 
-	    my $running_mirror_jobs = {};
+	    my $running_jobs = {};
 	    foreach my $stat (@$stats) {
-		next if $stat->{type} ne 'mirror';
-		$running_mirror_jobs->{$stat->{device}} = $stat;
+		next if $stat->{type} ne $op;
+		$running_jobs->{$stat->{device}} = $stat;
 	    }
 
 	    my $readycounter = 0;
 
 	    foreach my $job (keys %$jobs) {
 
-	        if(defined($jobs->{$job}->{complete}) && !defined($running_mirror_jobs->{$job})) {
-		    print "$job : finished\n";
+		my $vanished = !defined($running_jobs->{$job});
+		my $complete = defined($jobs->{$job}->{complete}) && $vanished;
+	        if($complete || ($vanished && $completion eq 'auto')) {
+		    print "$job: finished\n";
 		    delete $jobs->{$job};
 		    next;
 		}
 
-		die "$job: mirroring has been cancelled\n" if !defined($running_mirror_jobs->{$job});
+		die "$job: '$op' has been cancelled\n" if !defined($running_jobs->{$job});
 
-		my $busy = $running_mirror_jobs->{$job}->{busy};
-		my $ready = $running_mirror_jobs->{$job}->{ready};
-		if (my $total = $running_mirror_jobs->{$job}->{len}) {
-		    my $transferred = $running_mirror_jobs->{$job}->{offset} || 0;
+		my $busy = $running_jobs->{$job}->{busy};
+		my $ready = $running_jobs->{$job}->{ready};
+		if (my $total = $running_jobs->{$job}->{len}) {
+		    my $transferred = $running_jobs->{$job}->{offset} || 0;
 		    my $remaining = $total - $transferred;
 		    my $percent = sprintf "%.2f", ($transferred * 100 / $total);
 
 		    print "$job: transferred: $transferred bytes remaining: $remaining bytes total: $total bytes progression: $percent % busy: $busy ready: $ready \n";
 		}
 
-		$readycounter++ if $running_mirror_jobs->{$job}->{ready};
+		$readycounter++ if $running_jobs->{$job}->{ready};
 	    }
 
 	    last if scalar(keys %$jobs) == 0;
 
 	    if ($readycounter == scalar(keys %$jobs)) {
-		print "all mirroring jobs are ready \n";
-		last if $completion eq 'skip'; #do the complete later
+		print "all '$op' jobs are ready\n";
+
+		# do the complete later (or has already been done)
+		last if $completion eq 'skip' || $completion eq 'auto';
 
 		if ($vmiddst && $vmiddst != $vmid) {
 		    my $agent_running = $qga && qga_check_running($vmid);
@@ -6888,7 +6893,7 @@ sub qemu_drive_mirror_monitor {
 
     if ($err) {
 	eval { PVE::QemuServer::qemu_blockjobs_cancel($vmid, $jobs) };
-	die "mirroring error: $err";
+	die "block job ('$op') error: $err";
     }
 
 }
-- 
2.20.1