Skip to content

Commit

Permalink
fix: remove cross region snapshot copy 5s delay
Browse files Browse the repository at this point in the history
  • Loading branch information
andyzhangx committed Aug 22, 2023
1 parent 2458896 commit f8cfae3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
38 changes: 30 additions & 8 deletions pkg/azuredisk/azuredisk.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,25 +418,47 @@ func (d *DriverCore) getHostUtil() hostUtil {
return d.hostUtil
}

// getSnapshotCopyCompletionPercent returns the completion percent of copy snapshot
func (d *DriverCore) getSnapshotCopyCompletionPercent(ctx context.Context, subsID, resourceGroup, copySnapshotName string) (float64, error) {
copySnapshot, rerr := d.cloud.SnapshotsClient.Get(ctx, subsID, resourceGroup, copySnapshotName)
if rerr != nil {
return 0.0, rerr.Error()
}

if copySnapshot.SnapshotProperties == nil || copySnapshot.SnapshotProperties.CompletionPercent == nil {
return 0.0, fmt.Errorf("copy snapshot(%s) under rg(%s) has no SnapshotProperties or CompletionPercent is nil", copySnapshotName, resourceGroup)
}

return *copySnapshot.SnapshotProperties.CompletionPercent, nil
}

// waitForSnapshotCopy wait for copy incremental snapshot to a new region until completionPercent is 100.0
func (d *DriverCore) waitForSnapshotCopy(ctx context.Context, subsID, resourceGroup, copySnapshotName string, intervel, timeout time.Duration) error {
timeAfter := time.After(timeout)
timeTick := time.Tick(intervel)
completionPercent, err := d.getSnapshotCopyCompletionPercent(ctx, subsID, resourceGroup, copySnapshotName)
if err != nil {
return err
}

if completionPercent >= float64(100.0) {
klog.V(2).Infof("copy snapshot(%s) under rg(%s) complete", copySnapshotName, resourceGroup)
return nil
}

timeTick := time.Tick(intervel)
timeAfter := time.After(timeout)
for {
select {
case <-timeTick:
copySnapshot, rerr := d.cloud.SnapshotsClient.Get(ctx, subsID, resourceGroup, copySnapshotName)
if rerr != nil {
return rerr.Error()
completionPercent, err = d.getSnapshotCopyCompletionPercent(ctx, subsID, resourceGroup, copySnapshotName)
if err != nil {
return err
}

completionPercent := *copySnapshot.SnapshotProperties.CompletionPercent
klog.V(2).Infof("copy snapshot(%s) under rg(%s) region(%s) completionPercent: %f", copySnapshotName, resourceGroup, *copySnapshot.Location, completionPercent)
if completionPercent >= float64(100.0) {
klog.V(2).Infof("copy snapshot(%s) under rg(%s) region(%s) complete", copySnapshotName, resourceGroup, *copySnapshot.Location)
klog.V(2).Infof("copy snapshot(%s) under rg(%s) complete", copySnapshotName, resourceGroup)
return nil
}
klog.V(2).Infof("copy snapshot(%s) under rg(%s) completionPercent: %f", copySnapshotName, resourceGroup, completionPercent)
case <-timeAfter:
return fmt.Errorf("timeout waiting for copy snapshot(%s) under rg(%s)", copySnapshotName, resourceGroup)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/azuredisk/controllerserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -967,8 +967,8 @@ func (d *Driver) CreateSnapshot(ctx context.Context, req *csi.CreateSnapshotRequ

klog.V(2).Infof("begin to delete snapshot(%s) under rg(%s) region(%s)", snapshotName, resourceGroup, d.cloud.Location)
if rerr := d.cloud.SnapshotsClient.Delete(ctx, subsID, resourceGroup, snapshotName); rerr != nil {
azureutils.SleepIfThrottled(rerr.Error(), consts.SnapshotOpThrottlingSleepSec)
klog.Errorf("delete snapshot error: %v", rerr.Error())
azureutils.SleepIfThrottled(rerr.Error(), consts.SnapshotOpThrottlingSleepSec)
} else {
klog.V(2).Infof("delete snapshot(%s) under rg(%s) region(%s) successfully", snapshotName, resourceGroup, d.cloud.Location)
}
Expand Down

0 comments on commit f8cfae3

Please sign in to comment.