Skip to content

Commit

Permalink
fix: Read "404" as object not exist (#13901)
Browse files Browse the repository at this point in the history
**What this PR does / why we need it**:
Modify the s3 client to recognize "NotFound" errors when checking if an object error is "NotFound".
I did some tests locally and `Head` calls to objects that doesn't exist gives a "NotFound" instead of "NoSuchKey".
  • Loading branch information
DylanGuedes authored Aug 15, 2024
1 parent 811f5f0 commit 3c9c647
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pkg/storage/chunk/client/aws/s3_storage_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,13 @@ func (a *S3ObjectClient) List(ctx context.Context, prefix, delimiter string) ([]

// IsObjectNotFoundErr returns true if error means that object is not found. Relevant to GetObject and DeleteObject operations.
func (a *S3ObjectClient) IsObjectNotFoundErr(err error) bool {
if aerr, ok := errors.Cause(err).(awserr.Error); ok && aerr.Code() == s3.ErrCodeNoSuchKey {
aerr, ok := errors.Cause(err).(awserr.Error)
if !ok {
return false
}

code := aerr.Code()
if code == s3.ErrCodeNoSuchKey || code == "NotFound" {
return true
}

Expand Down
39 changes: 39 additions & 0 deletions pkg/storage/chunk/client/aws/s3_storage_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/grafana/loki/v3/pkg/storage/chunk/client/hedging"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
Expand All @@ -34,6 +35,44 @@ func (f RoundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req)
}

func TestIsObjectNotFoundErr(t *testing.T) {
tests := []struct {
err error
expected bool
name string
}{
{
name: "no such key error is recognized as object not found",
err: awserr.New(s3.ErrCodeNoSuchKey, "NoSuchKey", nil),
expected: true,
},
{
name: "NotFound code is recognized as object not found",
err: awserr.New("NotFound", "NotFound", nil),
expected: true,
},
{
name: "Nil error isnt recognized as object not found",
err: nil,
expected: false,
},
{
name: "Other error isnt recognized as object not found",
err: awserr.New(s3.ErrCodeNoSuchBucket, "NoSuchBucket", nil),
expected: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client, err := NewS3ObjectClient(S3Config{BucketNames: "mybucket"}, hedging.Config{})
require.NoError(t, err)

require.Equal(t, tt.expected, client.IsObjectNotFoundErr(tt.err))
})
}
}

func TestRequestMiddleware(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, r.Header.Get("echo-me"))
Expand Down

0 comments on commit 3c9c647

Please sign in to comment.