]> git.proxmox.com Git - pve-guest-common.git/commitdiff
replication: refactor finding most recent common replication snapshot
authorFabian Ebner <f.ebner@proxmox.com>
Tue, 19 Oct 2021 07:54:53 +0000 (09:54 +0200)
committerFabian Grünbichler <f.gruenbichler@proxmox.com>
Mon, 8 Nov 2021 09:35:34 +0000 (10:35 +0100)
By using a single loop instead. Should make the code more readable,
but also more efficient.

Suggested-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
src/PVE/Replication.pm

index 098ac00771a5929f34dcd3ebbbc0603e3fedd3e6..32395ed861a335e991fa2e7f56ef0d5bf2aaedb0 100644 (file)
@@ -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).