-
Notifications
You must be signed in to change notification settings - Fork 527
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add retry middleware in query-frontend
Requests from the query-frontend to the querier might fail when a querier is starting or stopping. Simply retrying this request usually solves this.
- Loading branch information
Koenraad Verheyden
committed
Jul 14, 2021
1 parent
addfa70
commit c196775
Showing
8 changed files
with
208 additions
and
6 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
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
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,57 @@ | ||
package frontend | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/go-kit/kit/log" | ||
"github.com/opentracing/opentracing-go" | ||
ot_log "github.com/opentracing/opentracing-go/log" | ||
) | ||
|
||
func RetryWare(maxRetries int, logger log.Logger) Middleware { | ||
return MiddlewareFunc(func(next Handler) Handler { | ||
return retryWare{ | ||
next: next, | ||
logger: logger, | ||
maxRetries: maxRetries, | ||
} | ||
}) | ||
} | ||
|
||
type retryWare struct { | ||
next Handler | ||
logger log.Logger | ||
maxRetries int | ||
} | ||
|
||
// Do implements Handler | ||
func (r retryWare) Do(req *http.Request) (*http.Response, error) { | ||
ctx := req.Context() | ||
span, ctx := opentracing.StartSpanFromContext(ctx, "frontend.Retry") | ||
defer span.Finish() | ||
|
||
// context propagation | ||
req = req.WithContext(ctx) | ||
|
||
triesLeft := r.maxRetries | ||
|
||
for { | ||
if ctx.Err() != nil { | ||
return nil, ctx.Err() | ||
} | ||
|
||
resp, err := r.next.Do(req) | ||
|
||
triesLeft-- | ||
if triesLeft == 0 { | ||
return resp, err | ||
} | ||
|
||
if err == nil && resp.StatusCode/100 != 5 { | ||
return resp, nil | ||
} | ||
|
||
span.LogFields(ot_log.String("msg", "error processing request"), ot_log.Int("try", triesLeft), ot_log.Error(err)) | ||
continue | ||
} | ||
} |
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,130 @@ | ||
package frontend | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/go-kit/kit/log" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/atomic" | ||
) | ||
|
||
type HandlerFunc func(req *http.Request) (*http.Response, error) | ||
|
||
// Wrap implements Handler. | ||
func (q HandlerFunc) Do(req *http.Request) (*http.Response, error) { | ||
return q(req) | ||
} | ||
|
||
func TestRetry(t *testing.T) { | ||
var try atomic.Int32 | ||
|
||
for _, tc := range []struct { | ||
name string | ||
handler Handler | ||
expectedTries int32 | ||
expectedRes *http.Response | ||
expectedErr error | ||
}{ | ||
{ | ||
name: "retry until success", | ||
handler: HandlerFunc(func(req *http.Request) (*http.Response, error) { | ||
if try.Inc() == 5 { | ||
return &http.Response{StatusCode: 200}, nil | ||
} | ||
return nil, errors.New("this request failed") | ||
}), | ||
expectedTries: 5, | ||
expectedRes: &http.Response{StatusCode: 200}, | ||
expectedErr: nil, | ||
}, | ||
{ | ||
name: "don't retry 400's", | ||
handler: HandlerFunc(func(req *http.Request) (*http.Response, error) { | ||
try.Inc() | ||
return &http.Response{StatusCode: 400}, nil | ||
}), | ||
expectedTries: 1, | ||
expectedRes: &http.Response{StatusCode: 400}, | ||
expectedErr: nil, | ||
}, | ||
{ | ||
name: "retry 500s", | ||
handler: HandlerFunc(func(req *http.Request) (*http.Response, error) { | ||
try.Inc() | ||
return &http.Response{StatusCode: 500}, nil | ||
}), | ||
expectedTries: 5, | ||
expectedRes: &http.Response{StatusCode: 500}, | ||
expectedErr: nil, | ||
}, | ||
{ | ||
name: "return last error", | ||
handler: HandlerFunc(func(req *http.Request) (*http.Response, error) { | ||
if try.Inc() == 5 { | ||
return nil, errors.New("request failed") | ||
} | ||
return nil, errors.New("not the last request") | ||
}), | ||
expectedTries: 5, | ||
expectedRes: nil, | ||
expectedErr: errors.New("request failed"), | ||
}, | ||
} { | ||
t.Run(tc.name, func(t *testing.T) { | ||
try.Store(0) | ||
|
||
retryWare := RetryWare(5, log.NewNopLogger()) | ||
handler := retryWare.Wrap(tc.handler) | ||
|
||
req := httptest.NewRequest("GET", "http://example.com", nil) | ||
|
||
res, err := handler.Do(req) | ||
|
||
require.Equal(t, tc.expectedTries, try.Load()) | ||
require.Equal(t, tc.expectedErr, err) | ||
require.Equal(t, tc.expectedRes, res) | ||
}) | ||
} | ||
} | ||
|
||
func TestRetry_CancelledRequest(t *testing.T) { | ||
var try atomic.Int32 | ||
|
||
// request is cancelled before first call | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
cancel() | ||
|
||
req, err := http.NewRequestWithContext(ctx, "GET", "http://example.com", nil) | ||
require.NoError(t, err) | ||
|
||
_, err = RetryWare(5, log.NewNopLogger()). | ||
Wrap(HandlerFunc(func(req *http.Request) (*http.Response, error) { | ||
try.Inc() | ||
return nil, ctx.Err() | ||
})). | ||
Do(req) | ||
|
||
require.Equal(t, int32(0), try.Load()) | ||
require.Equal(t, ctx.Err(), err) | ||
|
||
// request is cancelled after first call | ||
ctx, cancel = context.WithCancel(context.Background()) | ||
|
||
req, err = http.NewRequestWithContext(ctx, "GET", "http://example.com", nil) | ||
require.NoError(t, err) | ||
|
||
_, err = RetryWare(5, log.NewNopLogger()). | ||
Wrap(HandlerFunc(func(req *http.Request) (*http.Response, error) { | ||
try.Inc() | ||
cancel() | ||
return nil, errors.New("this request failed") | ||
})). | ||
Do(req) | ||
|
||
require.Equal(t, int32(1), try.Load()) | ||
require.Equal(t, ctx.Err(), err) | ||
} |