[pve-devel] [PATCH guest-common 1/4] replication: refactor finding most recent common replication snapshot

Fabian Ebner f.ebner at proxmox.com
Tue Oct 19 09:54:53 CEST 2021


By using a single loop instead. Should make the code more readable,
but also more efficient.

Suggested-by: Fabian Grünbichler <f.gruenbichler at proxmox.com>
Signed-off-by: Fabian Ebner <f.ebner at proxmox.com>
---
 src/PVE/Replication.pm | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)

diff --git a/src/PVE/Replication.pm b/src/PVE/Replication.pm
index 098ac00..32395ed 100644
--- a/src/PVE/Replication.pm
+++ b/src/PVE/Replication.pm
@@ -67,17 +67,19 @@ sub find_common_replication_snapshot {
 		$base_snapshots->{$volid} = $parent_snapname;
 	    } else {
 		# First, try all replication snapshots.
-		my @desc_sorted_snap =
-		    map { $_->[1] } sort { $b->[0] <=> $a->[0] }
-		    grep { $_->[0] != 0 } # only consider replication snapshots
-		    map { [ ($_ =~ /__replicate_\Q$vmid\E-(?:\d+)_(\d+)_/)[0] || 0, $_ ] }
-		    keys %{$remote_snapshots->{$volid}};
-
-		foreach my $remote_snap (@desc_sorted_snap) {
-		    if (defined($last_snapshots->{$volid}->{$remote_snap})) {
-			$base_snapshots->{$volid} = $remote_snap;
-			last;
-		    }
+		my $most_recent = [0, undef];
+		for my $remote_snap (keys $remote_snapshots->{$volid}->%*) {
+		    next if !defined($last_snapshots->{$volid}->{$remote_snap});
+
+		    my $timestamp = ($remote_snap =~ /__replicate_\Q$vmid\E-(?:\d+)_(\d+)_/)[0];
+		    next if !$timestamp;
+
+		    $most_recent = [$timestamp, $remote_snap] if $timestamp > $most_recent->[0];
+		}
+
+		if ($most_recent->[1]) {
+		    $base_snapshots->{$volid} = $most_recent->[1];
+		    next;
 		}
 
 		# Then, try config snapshots ($parent_snapname was already tested for above).
-- 
2.30.2






More information about the pve-devel mailing list