-
Notifications
You must be signed in to change notification settings - Fork 270
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
Fix experimental_retry #6338
Open
bnjjj
wants to merge
5
commits into
dev
Choose a base branch
from
bnjjj/fix_retry_metric
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+0
−0
Open
Fix experimental_retry #6338
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1ce68d0
fix(traffic_shaping): fix experimental_retry and add test
bnjjj c88d0a0
fix(traffic_shaping): fix experimental_retry and add test
bnjjj 0ccae42
changelog
bnjjj a243068
fix description for retry metrics
bnjjj 19350ad
Merge branch 'dev' of github.com:apollographql/router into bnjjj/fix_…
bnjjj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
### Fix and test experimental_retry ([PR #6338](https://github.com/apollographql/router/pull/6338)) | ||
|
||
Fix the behavior of `experimental_retry` and make sure both the feature and metrics are working. | ||
An entry in the context was also added, which would be useful later to implement a new standard attribute and selector for advanced telemetry. | ||
|
||
By [@bnjjj](https://github.com/bnjjj) in https://github.com/apollographql/router/pull/6338 |
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 |
---|---|---|
|
@@ -5,14 +5,14 @@ use std::time::Duration; | |
use tower::retry::budget::Budget; | ||
use tower::retry::Policy; | ||
|
||
use crate::plugins::telemetry::config_new::attributes::SubgraphRequestResendCountKey; | ||
use crate::query_planner::OperationKind; | ||
use crate::services::subgraph; | ||
|
||
#[derive(Clone, Default)] | ||
pub(crate) struct RetryPolicy { | ||
budget: Arc<Budget>, | ||
retry_mutations: bool, | ||
subgraph_name: String, | ||
} | ||
|
||
impl RetryPolicy { | ||
|
@@ -21,7 +21,6 @@ impl RetryPolicy { | |
min_per_sec: Option<u32>, | ||
retry_percent: Option<f32>, | ||
retry_mutations: Option<bool>, | ||
subgraph_name: String, | ||
) -> Self { | ||
Self { | ||
budget: Arc::new(Budget::new( | ||
|
@@ -30,21 +29,57 @@ impl RetryPolicy { | |
retry_percent.unwrap_or(0.2), | ||
)), | ||
retry_mutations: retry_mutations.unwrap_or(false), | ||
subgraph_name, | ||
} | ||
} | ||
} | ||
|
||
impl<Res, E> Policy<subgraph::Request, Res, E> for RetryPolicy { | ||
impl<E> Policy<subgraph::Request, subgraph::Response, E> for RetryPolicy { | ||
type Future = future::Ready<Self>; | ||
|
||
fn retry(&self, req: &subgraph::Request, result: Result<&Res, &E>) -> Option<Self::Future> { | ||
fn retry( | ||
&self, | ||
req: &subgraph::Request, | ||
result: Result<&subgraph::Response, &E>, | ||
) -> Option<Self::Future> { | ||
let subgraph_name = req.subgraph_name.clone().unwrap_or_default(); | ||
match result { | ||
Ok(_) => { | ||
// Treat all `Response`s as success, | ||
// so deposit budget and don't retry... | ||
self.budget.deposit(); | ||
None | ||
Ok(resp) => { | ||
if resp.response.status() >= http::StatusCode::BAD_REQUEST { | ||
if req.operation_kind == OperationKind::Mutation && !self.retry_mutations { | ||
return None; | ||
} | ||
|
||
let withdrew = self.budget.withdraw(); | ||
if withdrew.is_err() { | ||
u64_counter!( | ||
"apollo_router_http_request_retry_total", | ||
"Number of retry for an http request to a subgraph", | ||
1u64, | ||
status = "aborted", | ||
subgraph = subgraph_name | ||
); | ||
|
||
return None; | ||
} | ||
|
||
let _ = req | ||
.context | ||
.upsert::<_, usize>(SubgraphRequestResendCountKey::new(req), |val| val + 1); | ||
|
||
u64_counter!( | ||
"apollo_router_http_request_retry_total", | ||
"Number of retry for an http request to a subgraph", | ||
1u64, | ||
subgraph = subgraph_name | ||
); | ||
|
||
Some(future::ready(self.clone())) | ||
} else { | ||
// Treat all `Response`s as success, | ||
// so deposit budget and don't retry... | ||
self.budget.deposit(); | ||
None | ||
} | ||
} | ||
Err(_e) => { | ||
if req.operation_kind == OperationKind::Mutation && !self.retry_mutations { | ||
|
@@ -53,20 +88,27 @@ impl<Res, E> Policy<subgraph::Request, Res, E> for RetryPolicy { | |
|
||
let withdrew = self.budget.withdraw(); | ||
if withdrew.is_err() { | ||
tracing::info!( | ||
monotonic_counter.apollo_router_http_request_retry_total = 1u64, | ||
u64_counter!( | ||
"apollo_router_http_request_retry_total", | ||
"Number of retry for an http request to a subgraph", | ||
bnjjj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
1u64, | ||
status = "aborted", | ||
subgraph = %self.subgraph_name, | ||
subgraph = subgraph_name | ||
); | ||
|
||
return None; | ||
} | ||
|
||
tracing::info!( | ||
monotonic_counter.apollo_router_http_request_retry_total = 1u64, | ||
subgraph = %self.subgraph_name, | ||
u64_counter!( | ||
"apollo_router_http_request_retry_total", | ||
"Number of retry for an http request to a subgraph", | ||
1u64, | ||
subgraph = subgraph_name | ||
); | ||
|
||
let _ = req | ||
.context | ||
.upsert::<_, usize>(SubgraphRequestResendCountKey::new(req), |val| val + 1); | ||
|
||
Some(future::ready(self.clone())) | ||
} | ||
} | ||
|
@@ -76,3 +118,129 @@ impl<Res, E> Policy<subgraph::Request, Res, E> for RetryPolicy { | |
Some(req.clone()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use http::StatusCode; | ||
use tower::BoxError; | ||
|
||
use super::*; | ||
use crate::error::FetchError; | ||
use crate::graphql; | ||
use crate::http_ext; | ||
use crate::metrics::FutureMetricsExt; | ||
|
||
#[tokio::test] | ||
async fn test_retry_with_error() { | ||
async { | ||
let retry = RetryPolicy::new( | ||
Some(Duration::from_secs(10)), | ||
Some(10), | ||
Some(0.2), | ||
Some(false), | ||
); | ||
|
||
let subgraph_req = subgraph::Request::fake_builder() | ||
.subgraph_name("my_subgraph_name_error") | ||
.subgraph_request( | ||
http_ext::Request::fake_builder() | ||
.header("test", "my_value_set") | ||
.body( | ||
graphql::Request::fake_builder() | ||
.query(String::from("query { test }")) | ||
.build(), | ||
) | ||
.build() | ||
.unwrap(), | ||
) | ||
.build(); | ||
|
||
assert!(retry | ||
.retry( | ||
&subgraph_req, | ||
Err(&Box::new(FetchError::SubrequestHttpError { | ||
status_code: None, | ||
service: String::from("my_subgraph_name_error"), | ||
reason: String::from("cannot contact the subgraph"), | ||
})) | ||
) | ||
.is_some()); | ||
|
||
assert!(retry | ||
.retry( | ||
&subgraph_req, | ||
Err(&Box::new(FetchError::SubrequestHttpError { | ||
status_code: None, | ||
service: String::from("my_subgraph_name_error"), | ||
reason: String::from("cannot contact the subgraph"), | ||
})) | ||
) | ||
.is_some()); | ||
|
||
assert_counter!( | ||
"apollo_router_http_request_retry_total", | ||
2, | ||
"subgraph" = "my_subgraph_name_error" | ||
); | ||
} | ||
.with_metrics() | ||
.await; | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_retry_with_http_status_code() { | ||
async { | ||
let retry = RetryPolicy::new( | ||
Some(Duration::from_secs(10)), | ||
Some(10), | ||
Some(0.2), | ||
Some(false), | ||
); | ||
|
||
let subgraph_req = subgraph::Request::fake_builder() | ||
.subgraph_name("my_subgraph_name_error") | ||
.subgraph_request( | ||
http_ext::Request::fake_builder() | ||
.header("test", "my_value_set") | ||
.body( | ||
graphql::Request::fake_builder() | ||
.query(String::from("query { test }")) | ||
.build(), | ||
) | ||
.build() | ||
.unwrap(), | ||
) | ||
.build(); | ||
|
||
assert!(retry | ||
.retry( | ||
&subgraph_req, | ||
Ok::<&subgraph::Response, &BoxError>( | ||
&subgraph::Response::fake_builder() | ||
.status_code(StatusCode::BAD_REQUEST) | ||
.build() | ||
) | ||
) | ||
.is_some()); | ||
|
||
assert!(retry | ||
.retry( | ||
&subgraph_req, | ||
Ok::<&subgraph::Response, &BoxError>( | ||
&subgraph::Response::fake_builder() | ||
.status_code(StatusCode::BAD_REQUEST) | ||
.build() | ||
) | ||
) | ||
.is_some()); | ||
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. Add a test for a redirect: MOVED_PERMANENTLY |
||
|
||
assert_counter!( | ||
"apollo_router_http_request_retry_total", | ||
2, | ||
"subgraph" = "my_subgraph_name_error" | ||
); | ||
} | ||
.with_metrics() | ||
.await; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Is this correct? I mean, what if the subgraph does something "weird" like returning a redirect?
i.e.: should this be:
?
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 recently saw users using 302 status code as a correct status code and they were making some stuffs in rhai about this. So I prefer to be conservative and use 400 status code as a threshold
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.
It seems weird to me. If we go with this, then it means that we are encoding "weird" behaviour into the router and that might not be what some customers want.
My preference is to do the expected thing, rather than produce something which fits into a "weird" behaviour of a customer. See what other people think, since I may really be in the minority here.
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.
If a subgraph returns a redirect, I would not expect it to be retried
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 agree, but I also agree with what @goto-bus-stop why would we retry on 3XX ?
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.
To be honest: I find the concept of the retry module a bit weird. Maybe the best thing to do is to define exactly which codes we will retry on and document that?
@goto-bus-stop Says definitely on a 429. I'd probably also want to retry a 503. Apart for those two, I probably wouldn't want to retry.
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 like starting with a small selection of codes.
I'm second-guessing 429 because we don't support
Retry-After
in this implementation and maybe that's something that clients should be doing, rather than us? If we just retry instantly then there is a good chance that we get a 429 again.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.
https://www.rfc-editor.org/rfc/rfc9110.html#name-retry-after mentions 3xx is legit for retry.
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 don't think it says that. I read that the user agent should wait
Retry-After
seconds before following the redirect, not that it should retry the original request: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.
Would we want to do that? Retry to the redirected request?