@@ -21,6 +21,7 @@ import (
2121 "context"
2222 "encoding/binary"
2323 "encoding/json"
24+ "errors"
2425 "fmt"
2526 "io"
2627 "math"
@@ -50,7 +51,7 @@ import (
5051)
5152
5253const (
53- maxAttachAttempts = 50
54+ maxAttachAttempts = 500
5455
5556 // hba number used to create tcmu devices in configfs
5657 // all overlaybd devices are configured in /sys/kernel/config/target/core/user_999999999/
@@ -478,6 +479,36 @@ func (o *snapshotter) attachAndMountBlockDevice(ctx context.Context, snID string
478479 return nil
479480 }
480481 }
482+ return attachRetryError {lastErr }
483+ }
484+
485+ type attachRetryError struct {
486+ err error
487+ }
488+
489+ func (e attachRetryError ) Error () string {
490+ return e .err .Error ()
491+ }
492+
493+
494+ func (o * snapshotter ) attachWithRetry (ctx context.Context , snID string , writable string , fsType string , mkfs bool ) error {
495+ maxRetry := 5
496+ var lastErr error
497+ for retry := 0 ; retry < maxRetry ; retry ++ {
498+ lastErr = o .attachAndMountBlockDevice (ctx , snID , writable , fsType , mkfs )
499+ if errors .As (lastErr , & attachRetryError {}) {
500+ log .G (ctx ).Warnf ("attach failed, retrying(%d/%d)... snID: %s, err: %v" , retry + 1 , maxRetry , snID , lastErr )
501+ time .Sleep (1 * time .Second ) // Wait for 1 second before retrying
502+ continue
503+ } else if lastErr != nil {
504+ log .G (ctx ).Errorf ("attach failed, snID: %s, err: %v" , snID , lastErr )
505+ return lastErr
506+ } else {
507+ log .G (ctx ).Infof ("attach success, snID: %s" , snID )
508+ return nil
509+ }
510+ }
511+ log .G (ctx ).Errorf ("attach failed, max retry reached" )
481512 return lastErr
482513}
483514
0 commit comments