-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add retry logic to UCP GetAWSResourceWithPost
handler
#8170
Changes from all commits
d537fcf
395ecce
a408ba7
21f5cf9
9931f87
3850ee9
748893e
77fba24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package retry | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/sethvargo/go-retry" | ||
) | ||
|
||
const ( | ||
defaultInterval = 1 * time.Second | ||
defaultMaxRetries = 10 | ||
defaultMaxDuration = 60 * time.Second | ||
) | ||
|
||
// RetryConfig is the configuration for a retry operation. | ||
type RetryConfig struct { | ||
// BackoffStrategy is the backoff strategy to use. | ||
BackoffStrategy retry.Backoff | ||
} | ||
|
||
// Retryer is a utility for retrying functions. | ||
type Retryer struct { | ||
config *RetryConfig | ||
} | ||
|
||
// NewNoOpRetryer creates a new Retryer that does not retry. | ||
// This is useful for testing. | ||
func NewNoOpRetryer() *Retryer { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is useful for testing. using this retryer should be the same functionality as we have today. |
||
b := retry.NewConstant(1 * time.Second) | ||
b = retry.WithMaxRetries(0, b) | ||
|
||
return NewRetryer(&RetryConfig{ | ||
BackoffStrategy: b, | ||
}) | ||
} | ||
|
||
// DefaultBackoffStrategy returns the default backoff strategy. | ||
// The default backoff strategy is an exponential backoff with a maximum duration and maximum retries. | ||
func DefaultBackoffStrategy() retry.Backoff { | ||
b := retry.NewExponential(1 * time.Second) | ||
b = retry.WithMaxDuration(defaultMaxDuration, b) | ||
b = retry.WithMaxRetries(defaultMaxRetries, b) | ||
|
||
return b | ||
} | ||
|
||
// NewDefaultRetryer creates a new Retryer with the default configuration. | ||
// The default configuration is an exponential backoff with a maximum duration and maximum retries. | ||
func NewDefaultRetryer() *Retryer { | ||
return NewRetryer(&RetryConfig{ | ||
BackoffStrategy: DefaultBackoffStrategy(), | ||
}) | ||
} | ||
|
||
// NewRetryer creates a new Retryer with the given configuration. | ||
// If either the config or config.BackoffStrategy are nil, | ||
// the default configuration is used. | ||
// The default configuration is an exponential backoff with a maximum duration and maximum retries. | ||
func NewRetryer(config *RetryConfig) *Retryer { | ||
retryConfig := &RetryConfig{} | ||
|
||
if config != nil && config.BackoffStrategy != nil { | ||
retryConfig.BackoffStrategy = config.BackoffStrategy | ||
} else { | ||
retryConfig.BackoffStrategy = DefaultBackoffStrategy() | ||
} | ||
|
||
return &Retryer{ | ||
config: retryConfig, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can retryConfig ever be just empty? Like config is not nil but config.BackOffStrategy is? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is that an okay case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good find - I changed the code to not allow this case. now the retryer will use the default configuration in this case. I also added a test |
||
} | ||
} | ||
|
||
// RetryFunc retries the given function with the backoff strategy. | ||
func (r *Retryer) RetryFunc(ctx context.Context, f func(ctx context.Context) error) error { | ||
return retry.Do(ctx, r.config.BackoffStrategy, f) | ||
} | ||
|
||
// RetryableError marks an error as retryable. | ||
func RetryableError(err error) error { | ||
return retry.RetryableError(err) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package retry | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
goretry "github.com/sethvargo/go-retry" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestNewNoOpRetryer(t *testing.T) { | ||
retryer := NewNoOpRetryer() | ||
require.NotNil(t, retryer) | ||
require.NotNil(t, retryer.config) | ||
require.NotNil(t, retryer.config.BackoffStrategy) | ||
|
||
expectedBackoffStrategy := goretry.NewConstant(time.Second * 1) | ||
expectedBackoffStrategy = goretry.WithMaxRetries(0, expectedBackoffStrategy) | ||
|
||
require.IsType(t, expectedBackoffStrategy, retryer.config.BackoffStrategy) | ||
} | ||
|
||
func TestDefaultBackoffStrategy(t *testing.T) { | ||
backoff := DefaultBackoffStrategy() | ||
require.NotNil(t, backoff) | ||
} | ||
|
||
func TestNewDefaultRetryer(t *testing.T) { | ||
retryer := NewDefaultRetryer() | ||
require.NotNil(t, retryer) | ||
require.NotNil(t, retryer.config) | ||
require.NotNil(t, retryer.config.BackoffStrategy) | ||
|
||
expectedBackoffStrategy := goretry.NewConstant(time.Second * 1) | ||
expectedBackoffStrategy = goretry.WithMaxRetries(0, expectedBackoffStrategy) | ||
|
||
require.IsType(t, expectedBackoffStrategy, retryer.config.BackoffStrategy) | ||
} | ||
|
||
func TestNewRetryer(t *testing.T) { | ||
config := &RetryConfig{ | ||
BackoffStrategy: goretry.NewConstant(1 * time.Second), | ||
} | ||
Comment on lines
+42
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can test this with a RetryConfig that has a nil BackOffStrategy. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added a test |
||
retryer := NewRetryer(config) | ||
require.NotNil(t, retryer) | ||
require.NotNil(t, retryer.config) | ||
|
||
retryer = NewRetryer(nil) | ||
require.NotNil(t, retryer) | ||
require.NotNil(t, retryer.config) | ||
require.NotNil(t, retryer.config.BackoffStrategy) | ||
|
||
config = &RetryConfig{} | ||
retryer = NewRetryer(config) | ||
require.NotNil(t, retryer) | ||
require.NotNil(t, retryer.config) | ||
require.NotNil(t, retryer.config.BackoffStrategy) | ||
} | ||
|
||
func TestRetryer_RetryFunc(t *testing.T) { | ||
retryer := NewDefaultRetryer() | ||
ctx := context.Background() | ||
|
||
// Test successful function | ||
err := retryer.RetryFunc(ctx, func(ctx context.Context) error { | ||
return nil | ||
}) | ||
require.NoError(t, err) | ||
|
||
// Test retryable error | ||
retryCount := 0 | ||
err = retryer.RetryFunc(ctx, func(ctx context.Context) error { | ||
retryCount++ | ||
if retryCount < 3 { | ||
return RetryableError(errors.New("retryable error")) | ||
} | ||
return nil | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, 3, retryCount) | ||
|
||
// Test non-retryable error | ||
err = retryer.RetryFunc(ctx, func(ctx context.Context) error { | ||
return errors.New("non-retryable error") | ||
}) | ||
require.Error(t, err) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,7 +87,7 @@ func (p *CreateOrUpdateAWSResource) Run(ctx context.Context, w http.ResponseWrit | |
} | ||
|
||
cloudControlOpts := []func(*cloudcontrol.Options){CloudControlRegionOption(region)} | ||
cloudFormationOpts := []func(*cloudformation.Options){CloudFormationWithRegionOption(region)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I renamed this function to match the |
||
cloudFormationOpts := []func(*cloudformation.Options){CloudFormationRegionOption(region)} | ||
|
||
// Create and update work differently for AWS - we need to know if the resource | ||
// we're working on exists already. | ||
|
@@ -125,7 +125,6 @@ func (p *CreateOrUpdateAWSResource) Run(ctx context.Context, w http.ResponseWrit | |
|
||
if existing { | ||
// Get resource type schema | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. extra space |
||
describeTypeOutput, err := p.awsClients.CloudFormation.DescribeType(ctx, &cloudformation.DescribeTypeInput{ | ||
Type: types.RegistryTypeResource, | ||
TypeName: to.Ptr(serviceCtx.ResourceTypeInAWSFormat()), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,6 +36,8 @@ import ( | |
"github.com/aws/aws-sdk-go-v2/service/cloudcontrol" | ||
"github.com/aws/aws-sdk-go-v2/service/cloudformation" | ||
"github.com/aws/aws-sdk-go-v2/service/cloudformation/types" | ||
|
||
"github.com/radius-project/radius/pkg/retry" | ||
) | ||
|
||
var _ armrpc_controller.Controller = (*GetAWSResourceWithPost)(nil) | ||
|
@@ -44,13 +46,15 @@ var _ armrpc_controller.Controller = (*GetAWSResourceWithPost)(nil) | |
type GetAWSResourceWithPost struct { | ||
armrpc_controller.Operation[*datamodel.AWSResource, datamodel.AWSResource] | ||
awsClients ucpaws.Clients | ||
retryer *retry.Retryer | ||
} | ||
|
||
// NewGetAWSResourceWithPost creates a new GetAWSResourceWithPost controller with the given options and AWS clients. | ||
func NewGetAWSResourceWithPost(opts armrpc_controller.Options, awsClients ucpaws.Clients) (armrpc_controller.Controller, error) { | ||
func NewGetAWSResourceWithPost(opts armrpc_controller.Options, awsClients ucpaws.Clients, retryer *retry.Retryer) (armrpc_controller.Controller, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could consider adding the retryer to the other ucp awsproxy routes too, either in the future or this PR. I wanted to get some feedback first |
||
return &GetAWSResourceWithPost{ | ||
Operation: armrpc_controller.NewOperation(opts, armrpc_controller.ResourceOptions[datamodel.AWSResource]{}), | ||
awsClients: awsClients, | ||
retryer: retryer, | ||
}, nil | ||
} | ||
|
||
|
@@ -77,7 +81,7 @@ func (p *GetAWSResourceWithPost) Run(ctx context.Context, w http.ResponseWriter, | |
return armrpc_rest.NewBadRequestARMResponse(e), nil | ||
} | ||
|
||
cloudFormationOpts := []func(*cloudformation.Options){CloudFormationWithRegionOption(region)} | ||
cloudFormationOpts := []func(*cloudformation.Options){CloudFormationRegionOption(region)} | ||
describeTypeOutput, err := p.awsClients.CloudFormation.DescribeType(ctx, &cloudformation.DescribeTypeInput{ | ||
Type: types.RegistryTypeResource, | ||
TypeName: to.Ptr(serviceCtx.ResourceTypeInAWSFormat()), | ||
|
@@ -100,15 +104,27 @@ func (p *GetAWSResourceWithPost) Run(ctx context.Context, w http.ResponseWriter, | |
|
||
cloudcontrolOpts := []func(*cloudcontrol.Options){CloudControlRegionOption(region)} | ||
logger.Info("Fetching resource", "resourceType", serviceCtx.ResourceTypeInAWSFormat(), "resourceID", awsResourceIdentifier) | ||
response, err := p.awsClients.CloudControl.GetResource(ctx, &cloudcontrol.GetResourceInput{ | ||
TypeName: to.Ptr(serviceCtx.ResourceTypeInAWSFormat()), | ||
Identifier: aws.String(awsResourceIdentifier), | ||
}, cloudcontrolOpts...) | ||
|
||
if ucpaws.IsAWSResourceNotFoundError(err) { | ||
return armrpc_rest.NewNotFoundMessageResponse(constructNotFoundResponseMessage(middleware.GetRelativePath(p.Options().PathBase, req.URL.Path), awsResourceIdentifier)), nil | ||
} else if err != nil { | ||
return ucpaws.HandleAWSError(err) | ||
|
||
var response *cloudcontrol.GetResourceOutput | ||
if err := p.retryer.RetryFunc(ctx, func(ctx context.Context) error { | ||
response, err = p.awsClients.CloudControl.GetResource(ctx, &cloudcontrol.GetResourceInput{ | ||
TypeName: to.Ptr(serviceCtx.ResourceTypeInAWSFormat()), | ||
Identifier: aws.String(awsResourceIdentifier), | ||
}, cloudcontrolOpts...) | ||
|
||
// If the resource is not found, retry. | ||
if ucpaws.IsAWSResourceNotFoundError(err) { | ||
return retry.RetryableError(err) | ||
} | ||
|
||
// If any other error occurs, return the error. | ||
return err | ||
}); err != nil { | ||
if ucpaws.IsAWSResourceNotFoundError(err) { | ||
return armrpc_rest.NewNotFoundMessageResponse(constructNotFoundResponseMessage(middleware.GetRelativePath(p.Options().PathBase, req.URL.Path), awsResourceIdentifier)), nil | ||
} else { | ||
return ucpaws.HandleAWSError(err) | ||
} | ||
} | ||
|
||
resourceProperties := map[string]any{} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this package to simplify our retry logic across the project. Looks like it is well tested with no dependencies so I think it is a good choice. Let's discuss in this PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last commit seems to be from 6 months ago. Just wondering if that could be an issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be okay. The code is straightforward and has no dependencies, so hopefully there shouldn't need to be too many new commits.