Skip to content
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 sqs client retryer #1099

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions cmd/node-termination-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package main
import (
"context"
"fmt"
"github.com/aws/aws-node-termination-handler/pkg/monitor/asglifecycle"
"os"
"os/signal"
"strings"
Expand All @@ -31,6 +30,7 @@ import (
"github.com/aws/aws-node-termination-handler/pkg/interruptioneventstore"
"github.com/aws/aws-node-termination-handler/pkg/logging"
"github.com/aws/aws-node-termination-handler/pkg/monitor"
"github.com/aws/aws-node-termination-handler/pkg/monitor/asglifecycle"
"github.com/aws/aws-node-termination-handler/pkg/monitor/rebalancerecommendation"
"github.com/aws/aws-node-termination-handler/pkg/monitor/scheduledevent"
"github.com/aws/aws-node-termination-handler/pkg/monitor/spotitn"
Expand All @@ -43,7 +43,6 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -223,7 +222,7 @@ func main() {
QueueURL: nthConfig.QueueURL,
InterruptionChan: interruptionChan,
CancelChan: cancelChan,
SQS: sqs.New(sess),
SQS: sqsevent.GetSqsClient(sess),
ASG: autoscaling.New(sess),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also add retry for asg client?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could keep use default retryer for asg client.

EC2: ec2.New(sess),
BeforeCompleteLifecycleAction: func() { <-time.After(completeLifecycleActionDelay) },
Expand Down
49 changes: 49 additions & 0 deletions pkg/monitor/sqsevent/sqs-retryer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.

package sqsevent

import (
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/client"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
)

type SqsRetryer struct {
client.DefaultRetryer
}

func (r SqsRetryer) ShouldRetry(req *request.Request) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: personally I would prefer more descriptive variable names (perhaps retryer instead or r), just so I don't have to search for the type.

return r.DefaultRetryer.ShouldRetry(req) ||
(req.Error != nil && strings.Contains(req.Error.Error(), "connection reset"))
}

func GetSqsClient(sess *session.Session) *sqs.SQS {
return sqs.New(sess, &aws.Config{
Retryer: SqsRetryer{
DefaultRetryer: client.DefaultRetryer{
// Monitor continuously monitors SQS for events every 2 seconds
NumMaxRetries: client.DefaultRetryerMaxNumRetries,
MinRetryDelay: client.DefaultRetryerMinRetryDelay,
MaxRetryDelay: 1200 * time.Millisecond,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any particular reason why we don't set MaxRetryDelay to be DefaultRetryerMaxRetryDelay and same for DefaultRetryerMinThrottleDelay? https://github.com/aws/aws-sdk-go/blob/7112c0a0c2d01713a9db2d57f0e5722225baf5b5/aws/client/default_retryer.go#L49C2-L49C29

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed by adding doc comment.

MinThrottleDelay: client.DefaultRetryerMinThrottleDelay,
MaxThrottleDelay: 1200 * time.Millisecond,
},
},
})
}
116 changes: 116 additions & 0 deletions pkg/monitor/sqsevent/sqs-retryer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.

package sqsevent_test

import (
"fmt"
"net"
"testing"
"time"

"github.com/aws/aws-node-termination-handler/pkg/monitor/sqsevent"
h "github.com/aws/aws-node-termination-handler/pkg/test"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/client"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
)

type temporaryError struct {
error
temp bool
}

func TestGetSqsClient(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test case seems unnecessary because it's testing an implementation detail rather than behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would keep test case to test custom retryer config is consumed or not.

retryer := getSqsRetryer(t)

h.Equals(t, client.DefaultRetryerMaxNumRetries, retryer.NumMaxRetries)
h.Equals(t, time.Duration(1200*time.Millisecond), retryer.MaxRetryDelay)
}

func TestShouldRetry(t *testing.T) {
retryer := getSqsRetryer(t)

testCases := []struct {
name string
req *request.Request
shouldRetry bool
}{
{
name: "AWS throttling error",
req: &request.Request{
Error: awserr.New("ThrottlingException", "Rate exceeded", nil),
},
shouldRetry: true,
},
{
name: "AWS validation error",
req: &request.Request{
Error: awserr.New("ValidationError", "Invalid parameter", nil),
},
shouldRetry: false,
},
{
name: "read connection reset by peer error",
req: &request.Request{
Error: &temporaryError{
error: &net.OpError{
Op: "read",
Err: fmt.Errorf("read: connection reset by peer"),
},
temp: false,
}},
shouldRetry: true,
},
{
name: "read unknown error",
req: &request.Request{
Error: &temporaryError{
error: &net.OpError{
Op: "read",
Err: fmt.Errorf("read unknown error"),
},
temp: false,
}},
shouldRetry: false,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := retryer.ShouldRetry(tc.req)
h.Equals(t, tc.shouldRetry, result)
})
}
}

func getSqsRetryer(t *testing.T) sqsevent.SqsRetryer {
sess, err := session.NewSession(&aws.Config{
Region: aws.String("us-east-1"),
})
h.Ok(t, err)

sqsClient := sqsevent.GetSqsClient(sess)
h.Assert(t, sqsClient.Client.Config.Region != nil, "Region should not be nil")
h.Equals(t, "us-east-1", *sqsClient.Client.Config.Region)

retryer, ok := sqsClient.Client.Config.Retryer.(sqsevent.SqsRetryer)
h.Assert(t, ok, "Retryer should be of type SqsRetryer")
return retryer
}

func (e *temporaryError) Temporary() bool {
return e.temp
}
Loading