Skip to content

Commit

Permalink
internal: Adding refresh-exclude-older flag implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Parm Gill <[email protected]>
  • Loading branch information
ps-gill committed Nov 10, 2024
1 parent d5ba373 commit 3fbd40c
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 11 deletions.
2 changes: 1 addition & 1 deletion internal/server/instance/drivers/driver_lxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6206,7 +6206,7 @@ func (d *lxc) MigrateReceive(args instance.MigrateReceiveArgs) error {
}

// Compare the two sets.
syncSourceSnapshotIndexes, deleteTargetSnapshotIndexes := storagePools.CompareSnapshots(sourceSnapshotComparable, targetSnapshotsComparable)
syncSourceSnapshotIndexes, deleteTargetSnapshotIndexes := storagePools.CompareSnapshots(sourceSnapshotComparable, targetSnapshotsComparable, args.RefreshExcludeOlder)

// Delete the extra local snapshots first.
for _, deleteTargetSnapshotIndex := range deleteTargetSnapshotIndexes {
Expand Down
2 changes: 1 addition & 1 deletion internal/server/instance/drivers/driver_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -7315,7 +7315,7 @@ func (d *qemu) MigrateReceive(args instance.MigrateReceiveArgs) error {
}

// Compare the two sets.
syncSourceSnapshotIndexes, deleteTargetSnapshotIndexes := storagePools.CompareSnapshots(sourceSnapshotComparable, targetSnapshotsComparable)
syncSourceSnapshotIndexes, deleteTargetSnapshotIndexes := storagePools.CompareSnapshots(sourceSnapshotComparable, targetSnapshotsComparable, args.RefreshExcludeOlder)

// Delete the extra local snapshots first.
for _, deleteTargetSnapshotIndex := range deleteTargetSnapshotIndexes {
Expand Down
5 changes: 3 additions & 2 deletions internal/server/instance/instance_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ type MigrateSendArgs struct {
type MigrateReceiveArgs struct {
MigrateArgs

InstanceOperation *operationlock.InstanceOperation
Refresh bool
InstanceOperation *operationlock.InstanceOperation
Refresh bool
RefreshExcludeOlder bool
}
1 change: 1 addition & 0 deletions internal/server/migration/migration_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type VolumeTargetArgs struct {
MigrationType Type
TrackProgress bool
Refresh bool
RefreshExcludeOlder bool
Live bool
VolumeSize int64
ContentType string
Expand Down
4 changes: 2 additions & 2 deletions internal/server/storage/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -1227,7 +1227,7 @@ func (b *backend) CreateInstanceFromCopy(inst instance.Instance, src instance.In
// RefreshCustomVolume refreshes custom volumes (and optionally snapshots) during the custom volume copy operations.
// Snapshots that are not present in the source but are in the destination are removed from the
// destination if snapshots are included in the synchronization.
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 {
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 {
l := b.logger.AddContext(logger.Ctx{"project": projectName, "srcProjectName": srcProjectName, "volName": volName, "desc": desc, "config": config, "srcPoolName": srcPoolName, "srcVolName": srcVolName, "snapshots": snapshots})
l.Debug("RefreshCustomVolume started")
defer l.Debug("RefreshCustomVolume finished")
Expand Down Expand Up @@ -1327,7 +1327,7 @@ func (b *backend) RefreshCustomVolume(projectName string, srcProjectName string,
})
}

syncSourceSnapshotIndexes, deleteTargetSnapshotIndexes := CompareSnapshots(sourceSnapshotComparable, targetSnapshotsComparable)
syncSourceSnapshotIndexes, deleteTargetSnapshotIndexes := CompareSnapshots(sourceSnapshotComparable, targetSnapshotsComparable, excludeOlder)

// Delete extra snapshots first.
for _, deleteTargetSnapIndex := range deleteTargetSnapshotIndexes {
Expand Down
3 changes: 2 additions & 1 deletion internal/server/storage/backend_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ func (b *mockBackend) CleanupInstancePaths(inst instance.Instance, op *operation
return nil
}

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 {
// RefreshCustomVolume refresh a custom volume.
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 {
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/server/storage/pool_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ type Pool interface {
MountCustomVolume(projectName string, volName string, op *operations.Operation) (*MountInfo, error)
UnmountCustomVolume(projectName string, volName string, op *operations.Operation) (bool, error)
ImportCustomVolume(projectName string, poolVol *backupConfig.Config, op *operations.Operation) (revert.Hook, error)
RefreshCustomVolume(projectName string, srcProjectName string, volName, desc string, config map[string]string, srcPoolName, srcVolName string, snapshots bool, op *operations.Operation) error
RefreshCustomVolume(projectName string, srcProjectName string, volName, desc string, config map[string]string, srcPoolName, srcVolName string, snapshots bool, excludeOlder bool, op *operations.Operation) error
GenerateCustomVolumeBackupConfig(projectName string, volName string, snapshots bool, op *operations.Operation) (*backupConfig.Config, error)
CreateCustomVolumeFromISO(projectName string, volName string, srcData io.ReadSeeker, size int64, op *operations.Operation) error

Expand Down
12 changes: 9 additions & 3 deletions internal/server/storage/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1133,10 +1133,11 @@ type ComparableSnapshot struct {
// CompareSnapshots returns a list of snapshot indexes (from the associated input slices) to sync from the source
// and to delete from the target respectively.
// A snapshot will be added to "to sync from source" slice if it either doesn't exist in the target or its ID or
// creation date is different to the source.
// creation date is different to the source. When excludeOlder is true, source snapshots earlier than
// latest target snapshot are excluded.
// A snapshot will be added to the "to delete from target" slice if it doesn't exist in the source or its ID or
// creation date is different to the source.
func CompareSnapshots(sourceSnapshots []ComparableSnapshot, targetSnapshots []ComparableSnapshot) ([]int, []int) {
func CompareSnapshots(sourceSnapshots []ComparableSnapshot, targetSnapshots []ComparableSnapshot, excludeOlder bool) ([]int, []int) {
// Compare source and target.
sourceSnapshotsByName := make(map[string]*ComparableSnapshot, len(sourceSnapshots))
targetSnapshotsByName := make(map[string]*ComparableSnapshot, len(targetSnapshots))
Expand All @@ -1148,6 +1149,9 @@ func CompareSnapshots(sourceSnapshots []ComparableSnapshot, targetSnapshots []Co
sourceSnapshotsByName[sourceSnapshots[sourceSnapIndex].Name] = &sourceSnapshots[sourceSnapIndex]
}

// Find the latest creation date among target snapshots.
var latestTargetSnapshotTime time.Time

// If target snapshot doesn't exist in source, or its creation date or ID differ,
// then mark it for deletion on target.
for targetSnapIndex := range targetSnapshots {
Expand All @@ -1157,14 +1161,16 @@ func CompareSnapshots(sourceSnapshots []ComparableSnapshot, targetSnapshots []Co
sourceSnap, sourceSnapExists := sourceSnapshotsByName[targetSnapshots[targetSnapIndex].Name]
if !sourceSnapExists || !sourceSnap.CreationDate.Equal(targetSnapshots[targetSnapIndex].CreationDate) || sourceSnap.ID != targetSnapshots[targetSnapIndex].ID {
deleteFromTarget = append(deleteFromTarget, targetSnapIndex)
} else if targetSnapshots[targetSnapIndex].CreationDate.After(latestTargetSnapshotTime) {
latestTargetSnapshotTime = targetSnapshots[targetSnapIndex].CreationDate
}
}

// If source snapshot doesn't exist in target, or its creation date or ID differ,
// then mark it for syncing to target.
for sourceSnapIndex := range sourceSnapshots {
targetSnap, targetSnapExists := targetSnapshotsByName[sourceSnapshots[sourceSnapIndex].Name]
if !targetSnapExists || !targetSnap.CreationDate.Equal(sourceSnapshots[sourceSnapIndex].CreationDate) || targetSnap.ID != sourceSnapshots[sourceSnapIndex].ID {
if (!targetSnapExists && (!excludeOlder || sourceSnapshots[sourceSnapIndex].CreationDate.After(latestTargetSnapshotTime))) || !targetSnap.CreationDate.Equal(sourceSnapshots[sourceSnapIndex].CreationDate) || targetSnap.ID != sourceSnapshots[sourceSnapIndex].ID {
syncFromSource = append(syncFromSource, sourceSnapIndex)
}
}
Expand Down

0 comments on commit 3fbd40c

Please sign in to comment.