Skip to content

Commit

Permalink
aws2: fix retry header parsing (#1093)
Browse files Browse the repository at this point in the history
The retry header was changed in [2.15.41]. Update the parsing
logic to work with the newer version.

[2.15.41]: https://github.com/aws/aws-sdk-java-v2/blob/8aaeeb0a001106db4f6add5bf9517dc130188b4b/.changes/2.15.41.json#L39
  • Loading branch information
brharrington authored Nov 15, 2023
1 parent fb69125 commit 92e7033
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,34 @@ private void logRetryAttempt(ExecutionAttributes attrs) {
/**
* Extract the attempt number from the {@code amz-sdk-retry} header.
*/
private IpcAttempt extractAttempt(SdkHttpRequest request) {
private void updateAttempts(IpcLogEntry logEntry, SdkHttpRequest request) {
int attempt = 0;
List<String> vs = request.headers().get("amz-sdk-retry");
int max = 0;
List<String> vs = request.headers().get("amz-sdk-request");
if (vs != null) {
for (String v : vs) {
// Format is: {requestCount - 1}/{lastBackoffDelay}/{availableRetryCapacity}
// See internal RetryHandler for more details.
int pos = v.indexOf('/');
// Format is: attempt={}; max={}
// https://github.com/aws/aws-sdk-java-v2/pull/2179/files
int pos = v.indexOf(';');
if (pos > 0) {
try {
attempt = Integer.parseInt(v.substring(0, pos)) + 1;
} catch (NumberFormatException e) {
// If we cannot parse it, then attempt is unknown
attempt = 0;
}
attempt = parseFieldValue(v.substring(0, pos));
max = parseFieldValue(v.substring(pos + 1));
}
}
}
return IpcAttempt.forAttemptNumber(attempt);
logEntry
.withAttempt(IpcAttempt.forAttemptNumber(attempt))
.withAttemptFinal(attempt == max);
}

private int parseFieldValue(String field) {
int pos = field.indexOf("=");
try {
return pos > 0 ? Integer.parseInt(field.substring(pos + 1)) : 0;
} catch (NumberFormatException e) {
// If we cannot parse it, then attempt is unknown
return 0;
}
}

@Override
Expand All @@ -131,9 +140,8 @@ public void beforeTransmission(Context.BeforeTransmission context, ExecutionAttr
.withProtocol(IpcProtocol.http_1)
.withHttpMethod(request.method().name())
.withUri(request.getUri())
.withEndpoint(endpoint)
.withAttempt(extractAttempt(request))
.withAttemptFinal(false); // Don't know if it is the final attempt
.withEndpoint(endpoint);
updateAttempts(logEntry, request);

request.headers().forEach((k, vs) -> vs.forEach(v -> logEntry.addRequestHeader(k, v)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ private void parseRetryHeaderTest(String expected, String header) {
SdkHttpRequest request = SdkHttpRequest.builder()
.method(SdkHttpMethod.POST)
.uri(URI.create("https://ec2.us-east-1.amazonaws.com"))
.appendHeader("amz-sdk-retry", header)
.appendHeader("amz-sdk-request", header)
.build();
SdkHttpResponse response = SdkHttpResponse.builder()
.statusCode(200)
Expand All @@ -238,27 +238,27 @@ private void parseRetryHeaderTest(String expected, String header) {

@Test
public void parseRetryHeaderInitial() {
parseRetryHeaderTest("initial", "0/NotUsed");
parseRetryHeaderTest("initial", "attempt=1; max=4");
}

@Test
public void parseRetryHeaderSecond() {
parseRetryHeaderTest("second", "1/NotUsed");
parseRetryHeaderTest("second", "attempt=2; max=4");
}

@Test
public void parseRetryHeaderThird() {
parseRetryHeaderTest("third_up", "2/NotUsed");
parseRetryHeaderTest("third_up", "attempt=3; max=4");
}

@Test
public void parseRetryHeader50() {
parseRetryHeaderTest("third_up", "50/NotUsed");
parseRetryHeaderTest("third_up", "attempt=50; max=50");
}

@Test
public void parseRetryHeaderInvalidNumber() {
parseRetryHeaderTest("unknown", "foo/bar");
parseRetryHeaderTest("unknown", "attempt=foo; max=bar");
}

@Test
Expand Down

0 comments on commit 92e7033

Please sign in to comment.