-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(spanner): ensure failure of column_metadata table update doesn't …
…cause an apply failure
- Loading branch information
1 parent
90a23ad
commit 4ead0f3
Showing
4 changed files
with
111 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package utils | ||
|
||
import ( | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
// Retry is a utility function to retry a function a number of times with exponential backoff | ||
// and jitter. It will return the result of the function if it succeeds, or the last error if | ||
// it fails. | ||
// | ||
// If the error is a Stop, it will return the original error for later checking. | ||
func Retry[R interface{}](attempts int, initialSleep time.Duration, f func() (R, error)) (R, error) { | ||
if res, err := f(); err != nil { | ||
if s, ok := err.(Stop); ok { | ||
// Return the original error for later checking | ||
return res, s.error | ||
} | ||
|
||
if attempts--; attempts > 0 { | ||
// Calculate exponential backoff | ||
sleep := initialSleep * (1 << uint(attempts)) | ||
|
||
// Add some randomness to prevent creating a Thundering Herd | ||
jitter := time.Duration(rand.Int63n(int64(sleep))) | ||
sleep = sleep + jitter/2 | ||
|
||
time.Sleep(sleep) | ||
return Retry[R](attempts, initialSleep, f) | ||
} | ||
return res, err | ||
} else { | ||
return res, nil | ||
} | ||
} | ||
|
||
type Stop struct { | ||
error | ||
} | ||
|
||
// NonRetryableError is a utility function to return an error that will not be retried | ||
func NonRetryableError(err error) Stop { | ||
return Stop{err} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestRetry(t *testing.T) { | ||
type args[R interface{}] struct { | ||
attempts int | ||
initialSleep time.Duration | ||
f func() (R, error) | ||
} | ||
type testCase[R interface{}] struct { | ||
name string | ||
args args[R] | ||
want R | ||
wantErr bool | ||
} | ||
tests := []testCase[interface{}]{ | ||
{ | ||
name: "Test Retry", | ||
args: args[interface{}]{ | ||
attempts: 2, | ||
initialSleep: 5 * time.Second, | ||
f: func() (interface{}, error) { | ||
return "test", fmt.Errorf("error") | ||
}, | ||
}, | ||
want: "test", | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tStart := time.Now() | ||
got, err := Retry(tt.args.attempts, tt.args.initialSleep, tt.args.f) | ||
tEnd := time.Now() | ||
fmt.Println("Time taken: ", tEnd.Sub(tStart)) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Retry() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("Retry() got = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |