Skip to content

Commit 3fbd40c

Browse files
committed
internal: Adding refresh-exclude-older flag implementation
Signed-off-by: Parm Gill <[email protected]>
1 parent d5ba373 commit 3fbd40c

File tree

8 files changed

+20
-11
lines changed

8 files changed

+20
-11
lines changed

internal/server/instance/drivers/driver_lxc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6206,7 +6206,7 @@ func (d *lxc) MigrateReceive(args instance.MigrateReceiveArgs) error {
62066206
}
62076207

62086208
// Compare the two sets.
6209-
syncSourceSnapshotIndexes, deleteTargetSnapshotIndexes := storagePools.CompareSnapshots(sourceSnapshotComparable, targetSnapshotsComparable)
6209+
syncSourceSnapshotIndexes, deleteTargetSnapshotIndexes := storagePools.CompareSnapshots(sourceSnapshotComparable, targetSnapshotsComparable, args.RefreshExcludeOlder)
62106210

62116211
// Delete the extra local snapshots first.
62126212
for _, deleteTargetSnapshotIndex := range deleteTargetSnapshotIndexes {

internal/server/instance/drivers/driver_qemu.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7315,7 +7315,7 @@ func (d *qemu) MigrateReceive(args instance.MigrateReceiveArgs) error {
73157315
}
73167316

73177317
// Compare the two sets.
7318-
syncSourceSnapshotIndexes, deleteTargetSnapshotIndexes := storagePools.CompareSnapshots(sourceSnapshotComparable, targetSnapshotsComparable)
7318+
syncSourceSnapshotIndexes, deleteTargetSnapshotIndexes := storagePools.CompareSnapshots(sourceSnapshotComparable, targetSnapshotsComparable, args.RefreshExcludeOlder)
73197319

73207320
// Delete the extra local snapshots first.
73217321
for _, deleteTargetSnapshotIndex := range deleteTargetSnapshotIndexes {

internal/server/instance/instance_interface.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ type MigrateSendArgs struct {
238238
type MigrateReceiveArgs struct {
239239
MigrateArgs
240240

241-
InstanceOperation *operationlock.InstanceOperation
242-
Refresh bool
241+
InstanceOperation *operationlock.InstanceOperation
242+
Refresh bool
243+
RefreshExcludeOlder bool
243244
}

internal/server/migration/migration_volumes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ type VolumeTargetArgs struct {
7373
MigrationType Type
7474
TrackProgress bool
7575
Refresh bool
76+
RefreshExcludeOlder bool
7677
Live bool
7778
VolumeSize int64
7879
ContentType string

internal/server/storage/backend.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,7 +1227,7 @@ func (b *backend) CreateInstanceFromCopy(inst instance.Instance, src instance.In
12271227
// RefreshCustomVolume refreshes custom volumes (and optionally snapshots) during the custom volume copy operations.
12281228
// Snapshots that are not present in the source but are in the destination are removed from the
12291229
// destination if snapshots are included in the synchronization.
1230-
func (b *backend) RefreshCustomVolume(projectName string, srcProjectName string, volName string, desc string, config map[string]string, srcPoolName, srcVolName string, snapshots bool, op *operations.Operation) error {
1230+
func (b *backend) RefreshCustomVolume(projectName string, srcProjectName string, volName string, desc string, config map[string]string, srcPoolName, srcVolName string, snapshots bool, excludeOlder bool, op *operations.Operation) error {
12311231
l := b.logger.AddContext(logger.Ctx{"project": projectName, "srcProjectName": srcProjectName, "volName": volName, "desc": desc, "config": config, "srcPoolName": srcPoolName, "srcVolName": srcVolName, "snapshots": snapshots})
12321232
l.Debug("RefreshCustomVolume started")
12331233
defer l.Debug("RefreshCustomVolume finished")
@@ -1327,7 +1327,7 @@ func (b *backend) RefreshCustomVolume(projectName string, srcProjectName string,
13271327
})
13281328
}
13291329

1330-
syncSourceSnapshotIndexes, deleteTargetSnapshotIndexes := CompareSnapshots(sourceSnapshotComparable, targetSnapshotsComparable)
1330+
syncSourceSnapshotIndexes, deleteTargetSnapshotIndexes := CompareSnapshots(sourceSnapshotComparable, targetSnapshotsComparable, excludeOlder)
13311331

13321332
// Delete extra snapshots first.
13331333
for _, deleteTargetSnapIndex := range deleteTargetSnapshotIndexes {

internal/server/storage/backend_mock.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ func (b *mockBackend) CleanupInstancePaths(inst instance.Instance, op *operation
172172
return nil
173173
}
174174

175-
func (b *mockBackend) RefreshCustomVolume(projectName string, srcProjectName string, volName string, desc string, config map[string]string, srcPoolName, srcVolName string, srcVolOnly bool, op *operations.Operation) error {
175+
// RefreshCustomVolume refresh a custom volume.
176+
func (b *mockBackend) RefreshCustomVolume(projectName string, srcProjectName string, volName string, desc string, config map[string]string, srcPoolName, srcVolName string, srcVolOnly bool, excludeOlder bool, op *operations.Operation) error {
176177
return nil
177178
}
178179

internal/server/storage/pool_interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ type Pool interface {
126126
MountCustomVolume(projectName string, volName string, op *operations.Operation) (*MountInfo, error)
127127
UnmountCustomVolume(projectName string, volName string, op *operations.Operation) (bool, error)
128128
ImportCustomVolume(projectName string, poolVol *backupConfig.Config, op *operations.Operation) (revert.Hook, error)
129-
RefreshCustomVolume(projectName string, srcProjectName string, volName, desc string, config map[string]string, srcPoolName, srcVolName string, snapshots bool, op *operations.Operation) error
129+
RefreshCustomVolume(projectName string, srcProjectName string, volName, desc string, config map[string]string, srcPoolName, srcVolName string, snapshots bool, excludeOlder bool, op *operations.Operation) error
130130
GenerateCustomVolumeBackupConfig(projectName string, volName string, snapshots bool, op *operations.Operation) (*backupConfig.Config, error)
131131
CreateCustomVolumeFromISO(projectName string, volName string, srcData io.ReadSeeker, size int64, op *operations.Operation) error
132132

internal/server/storage/utils.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,10 +1133,11 @@ type ComparableSnapshot struct {
11331133
// CompareSnapshots returns a list of snapshot indexes (from the associated input slices) to sync from the source
11341134
// and to delete from the target respectively.
11351135
// A snapshot will be added to "to sync from source" slice if it either doesn't exist in the target or its ID or
1136-
// creation date is different to the source.
1136+
// creation date is different to the source. When excludeOlder is true, source snapshots earlier than
1137+
// latest target snapshot are excluded.
11371138
// A snapshot will be added to the "to delete from target" slice if it doesn't exist in the source or its ID or
11381139
// creation date is different to the source.
1139-
func CompareSnapshots(sourceSnapshots []ComparableSnapshot, targetSnapshots []ComparableSnapshot) ([]int, []int) {
1140+
func CompareSnapshots(sourceSnapshots []ComparableSnapshot, targetSnapshots []ComparableSnapshot, excludeOlder bool) ([]int, []int) {
11401141
// Compare source and target.
11411142
sourceSnapshotsByName := make(map[string]*ComparableSnapshot, len(sourceSnapshots))
11421143
targetSnapshotsByName := make(map[string]*ComparableSnapshot, len(targetSnapshots))
@@ -1148,6 +1149,9 @@ func CompareSnapshots(sourceSnapshots []ComparableSnapshot, targetSnapshots []Co
11481149
sourceSnapshotsByName[sourceSnapshots[sourceSnapIndex].Name] = &sourceSnapshots[sourceSnapIndex]
11491150
}
11501151

1152+
// Find the latest creation date among target snapshots.
1153+
var latestTargetSnapshotTime time.Time
1154+
11511155
// If target snapshot doesn't exist in source, or its creation date or ID differ,
11521156
// then mark it for deletion on target.
11531157
for targetSnapIndex := range targetSnapshots {
@@ -1157,14 +1161,16 @@ func CompareSnapshots(sourceSnapshots []ComparableSnapshot, targetSnapshots []Co
11571161
sourceSnap, sourceSnapExists := sourceSnapshotsByName[targetSnapshots[targetSnapIndex].Name]
11581162
if !sourceSnapExists || !sourceSnap.CreationDate.Equal(targetSnapshots[targetSnapIndex].CreationDate) || sourceSnap.ID != targetSnapshots[targetSnapIndex].ID {
11591163
deleteFromTarget = append(deleteFromTarget, targetSnapIndex)
1164+
} else if targetSnapshots[targetSnapIndex].CreationDate.After(latestTargetSnapshotTime) {
1165+
latestTargetSnapshotTime = targetSnapshots[targetSnapIndex].CreationDate
11601166
}
11611167
}
11621168

11631169
// If source snapshot doesn't exist in target, or its creation date or ID differ,
11641170
// then mark it for syncing to target.
11651171
for sourceSnapIndex := range sourceSnapshots {
11661172
targetSnap, targetSnapExists := targetSnapshotsByName[sourceSnapshots[sourceSnapIndex].Name]
1167-
if !targetSnapExists || !targetSnap.CreationDate.Equal(sourceSnapshots[sourceSnapIndex].CreationDate) || targetSnap.ID != sourceSnapshots[sourceSnapIndex].ID {
1173+
if (!targetSnapExists && (!excludeOlder || sourceSnapshots[sourceSnapIndex].CreationDate.After(latestTargetSnapshotTime))) || !targetSnap.CreationDate.Equal(sourceSnapshots[sourceSnapIndex].CreationDate) || targetSnap.ID != sourceSnapshots[sourceSnapIndex].ID {
11681174
syncFromSource = append(syncFromSource, sourceSnapIndex)
11691175
}
11701176
}

0 commit comments

Comments
 (0)