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

Fix paginator bug where None was returned immediately #1083

Merged
merged 4 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 12 additions & 0 deletions CHANGELOG.next.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,15 @@ message = "Silence profile credential warnings in Lambda environment"
references = ["smithy-rs#1065", "aws-sdk-rust#398"]
meta = { "breaking" = false, "tada" = true, "bug" = true }
author = "nmoutschen"

[[aws-sdk-rust]]
message = "Fixed paginator bug impacting EC2 describe VPCs (and others)"
references = ["aws-sdk-rust#405", "smithy-rs#1083"]
meta = { "breaking" = false, "tada" = false, "bug" = true }
author = "rcoh"

[[smithy-rs]]
message = "Fixed paginator bug impacting EC2 describe VPCs (and others)"
references = ["aws-sdk-rust#405", "smithy-rs#1083"]
meta = { "breaking" = false, "tada" = false, "bug" = true }
author = "rcoh"
36 changes: 36 additions & 0 deletions aws/sdk/integration-tests/ec2/tests/paginators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,39 @@ async fn paginators_handle_empty_tokens() {
assert_eq!(first_item, None);
conn.assert_requests_match(&[]);
}

/// See https://github.com/awslabs/aws-sdk-rust/issues/405
///
/// EC2 can also reply with the token truly unset which will be interpreted as `None`
Copy link
Contributor

Choose a reason for hiding this comment

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

What exactly does this mean? How many "kinds" of unset are there? I can think of
a. field returned but empty or set to a value signifying empty
b. no field returned

Were we only covering case a previously and now we're covering case b too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yeah it can either be <nextToken></nextToken>, or as is the case here, <nextToken> is not present in the response at all. In one case, we get Some(""), in the other, we get None. None used to be handled properly, but regressed when I added the failsafe to avoid infinite loops

#[tokio::test]
async fn paginators_handle_unset_tokens() {
let request= "Action=DescribeSpotPriceHistory&Version=2016-11-15&AvailabilityZone=eu-north-1a&InstanceType.1=g5.48xlarge&ProductDescription.1=Linux%2FUNIX";
let response = r#"<?xml version="1.0" encoding="UTF-8"?>
<DescribeSpotPriceHistoryResponse xmlns="http://ec2.amazonaws.com/doc/2016-11-15/">
<requestId>edf3e86c-4baf-47c1-9228-9a5ea09542e8</requestId>
<spotPriceHistorySet/>
</DescribeSpotPriceHistoryResponse>"#;
let conn = TestConnection::<&str>::new(vec![(
http::Request::builder()
.uri("https://ec2.us-east-1.amazonaws.com/")
.body(request.into())
.unwrap(),
http::Response::builder()
.status(200)
.body(response)
.unwrap(),
)]);
let client = Client::from_conf_conn(stub_config(), conn.clone());
let instance_type = InstanceType::from("g5.48xlarge");
let mut paginator = client
.describe_spot_price_history()
.instance_types(instance_type)
.product_descriptions("Linux/UNIX")
.availability_zone("eu-north-1a")
.into_paginator()
.items()
.send();
let first_item = paginator.try_next().await.expect("success");
assert_eq!(first_item, None);
conn.assert_requests_match(&[]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import software.amazon.smithy.model.Model
import software.amazon.smithy.model.knowledge.PaginatedIndex
import software.amazon.smithy.model.shapes.OperationShape
import software.amazon.smithy.model.shapes.ServiceShape
import software.amazon.smithy.model.shapes.StringShape
import software.amazon.smithy.model.traits.IdempotencyTokenTrait
import software.amazon.smithy.model.traits.PaginatedTrait
import software.amazon.smithy.rust.codegen.rustlang.CargoDependency
Expand Down Expand Up @@ -175,12 +174,13 @@ class PaginatorGenerator private constructor(
let done = match resp {
Ok(ref resp) => {
let new_token = #{output_token}(resp);
if new_token == input.$inputTokenMember.as_ref() {
let is_empty = ${nextTokenEmpty("new_token")};
if !is_empty && new_token == input.$inputTokenMember.as_ref() {
let _ = tx.send(Err(#{SdkError}::ConstructionFailure("next token did not change, aborting paginator. This indicates an SDK or AWS service bug.".into()))).await;
return;
}
input.$inputTokenMember = new_token.cloned();
${nextTokenEmpty("input.$inputTokenMember")}
is_empty
},
Err(_) => true,
};
Expand All @@ -192,7 +192,6 @@ class PaginatorGenerator private constructor(
return
}
}
}))
}
}
Expand Down Expand Up @@ -276,12 +275,7 @@ class PaginatorGenerator private constructor(
}

private fun nextTokenEmpty(token: String): String {
val tokenType = model.expectShape(paginationInfo.outputTokenMemberPath.last().target)
if (tokenType is StringShape) {
return "$token.as_deref().unwrap_or_default().is_empty()"
} else {
return "$token.is_none()"
}
return "$token.map(|token|token.is_empty()).unwrap_or(true)"
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should prefer function pointers in place of closures where possible:

Suggested change
return "$token.map(|token|token.is_empty()).unwrap_or(true)"
return "$token.map(String::is_empty).unwrap_or(true)"

It might not be possible depending on how type inference interprets this though

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

this can be either a string or a hashmap

}

private fun pageSizeSetter() = writable {
Expand Down
11 changes: 10 additions & 1 deletion tools/ci-cdk/canary-lambda/src/paginator_canary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ pub async fn paginator_canary(client: ec2::Client, page_size: usize) -> anyhow::
bail!("should be ~60 of pages of results")
rcoh marked this conversation as resolved.
Show resolved Hide resolved
}

// https://github.com/awslabs/aws-sdk-rust/issues/405
let _ = client
.describe_vpcs()
.into_paginator()
.items()
.send()
.collect::<Result<Vec<_>, _>>()
.await?;

Ok(())
}

Expand All @@ -54,6 +63,6 @@ mod test {
async fn test_paginator() {
let conf = aws_config::load_from_env().await;
let client = aws_sdk_ec2::Client::new(&conf);
paginator_canary(client).await.unwrap()
paginator_canary(client, 20).await.unwrap()
}
}