-
Notifications
You must be signed in to change notification settings - Fork 803
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generic retry in query frontend (#5561)
* Add generic retry in query frontend Signed-off-by: 🌲 Harry 🌊 John 🏔 <johrry@amazon.com> * Remove retry middleware Signed-off-by: 🌲 Harry 🌊 John 🏔 <johrry@amazon.com> * Update Changelog Signed-off-by: 🌲 Harry 🌊 John 🏔 <johrry@amazon.com> * Fail fast if context errors out Signed-off-by: 🌲 Harry 🌊 John 🏔 <johrry@amazon.com> * Fix test Signed-off-by: 🌲 Harry 🌊 John 🏔 <johrry@amazon.com> --------- Signed-off-by: 🌲 Harry 🌊 John 🏔 <johrry@amazon.com>
- Loading branch information
1 parent
f560115
commit a897070
Showing
14 changed files
with
223 additions
and
280 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package transport | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/promauto" | ||
"github.com/weaveworks/common/httpgrpc" | ||
) | ||
|
||
type Retry struct { | ||
maxRetries int | ||
retriesCount prometheus.Histogram | ||
} | ||
|
||
func NewRetry(maxRetries int, reg prometheus.Registerer) *Retry { | ||
return &Retry{ | ||
maxRetries: maxRetries, | ||
retriesCount: promauto.With(reg).NewHistogram(prometheus.HistogramOpts{ | ||
Namespace: "cortex", | ||
Name: "query_frontend_retries", | ||
Help: "Number of times a request is retried.", | ||
Buckets: []float64{0, 1, 2, 3, 4, 5}, | ||
}), | ||
} | ||
} | ||
|
||
func (r *Retry) Do(ctx context.Context, f func() (*httpgrpc.HTTPResponse, error)) (*httpgrpc.HTTPResponse, error) { | ||
if r.maxRetries == 0 { | ||
// Retries are disabled. Try only once. | ||
return f() | ||
} | ||
|
||
tries := 0 | ||
defer func() { r.retriesCount.Observe(float64(tries)) }() | ||
|
||
var ( | ||
resp *httpgrpc.HTTPResponse | ||
err error | ||
) | ||
for ; tries < r.maxRetries; tries++ { | ||
if ctx.Err() != nil { | ||
return nil, ctx.Err() | ||
} | ||
|
||
resp, err = f() | ||
if err != nil && err != context.Canceled { | ||
continue // Retryable | ||
} else if resp != nil && resp.Code/100 == 5 { | ||
continue // Retryable | ||
} else { | ||
break | ||
} | ||
} | ||
return resp, 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,31 @@ | ||
package transport | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/weaveworks/common/httpgrpc" | ||
"go.uber.org/atomic" | ||
) | ||
|
||
func TestRetry(t *testing.T) { | ||
tries := atomic.NewInt64(3) | ||
r := NewRetry(3, nil) | ||
ctx := context.Background() | ||
res, err := r.Do(ctx, func() (*httpgrpc.HTTPResponse, error) { | ||
try := tries.Dec() | ||
if try > 1 { | ||
return &httpgrpc.HTTPResponse{ | ||
Code: 500, | ||
}, nil | ||
} | ||
return &httpgrpc.HTTPResponse{ | ||
Code: 200, | ||
}, nil | ||
|
||
}) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, int32(200), res.Code) | ||
} |
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
Oops, something went wrong.