Skip to content

Implemented Retries functionality #3307

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
/**
* Interface for specifying a retry policy to use when evaluating whether or not a request should be retried , and the gap
* between each retry. The {@link #builder()} can be used to construct a retry policy with numRetries and backoffStrategy.
* <p>
* When using the {@link #builder()} the SDK will use default values for fields that are not provided.A custom BackoffStrategy
* can be used to construct a policy or a default {@link BackoffStrategy} is used .
* <p></p>
* can be used to construct a policy or a default {@link BackoffStrategy} is used.
*
* @see BackoffStrategy for a list of SDK provided backoff strategies
*/
@SdkPublicApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@
import java.time.Duration;
import java.util.Objects;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.annotations.Immutable;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.annotations.ThreadSafe;
import software.amazon.awssdk.core.SdkStandardLogger;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.retry.RetryPolicyContext;
import software.amazon.awssdk.http.AbortableInputStream;
Expand All @@ -38,6 +35,7 @@
import software.amazon.awssdk.imds.Ec2MetadataRetryPolicy;
import software.amazon.awssdk.imds.MetadataResponse;
import software.amazon.awssdk.utils.IoUtils;
import software.amazon.awssdk.utils.Logger;

/**
* An Implementation of the Ec2Metadata Interface.
Expand All @@ -49,7 +47,7 @@ public final class DefaultEc2Metadata implements Ec2Metadata {

private static final String TOKEN_RESOURCE_PATH = "/latest/api/token";

private static final Logger log = LoggerFactory.getLogger(DefaultEc2Metadata.class);
private static final Logger log = Logger.loggerFor(DefaultEc2Metadata.class);

private static final RequestMarshaller REQUEST_MARSHALLER = new RequestMarshaller();

Expand Down Expand Up @@ -177,17 +175,13 @@ public MetadataResponse get(String path) {
}
handleException(statusCode, path);
}
pauseBeforeRetryIfNeeded(tries);
//TODO Create IMDS Custom Exception
} catch (SdkClientException sd) {
throw SdkClientException.builder().message(sd.getMessage()).cause(sd).build();
} catch (IOException io) {
SdkStandardLogger.REQUEST_LOGGER.warn(() -> "Received an IOException {0} ", io);
pauseBeforeRetryIfNeeded(tries);

log.warn(() -> "Received an IOException {0} ", io);
} finally {
IoUtils.closeQuietly(abortableInputStream, log);
IoUtils.closeQuietly(abortableInputStream, log.logger());
}
pauseBeforeRetryIfNeeded(tries);
}
return metadataResponse;
}
Expand Down Expand Up @@ -253,10 +247,10 @@ private Optional<String> getToken() throws IOException {
}
handleErrorResponse(statusCode);
} catch (IOException e) {
SdkStandardLogger.REQUEST_LOGGER.warn(() -> "Received an IOException {0} ", e);
log.warn(() -> "Received an IOException {0} ", e);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: {0} should be removed, same as the other log statement

throw e;
} finally {
IoUtils.closeQuietly(abortableInputStream, log);
IoUtils.closeQuietly(abortableInputStream, log.logger());
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void get_failedThriceWith404() throws IOException {
Ec2Metadata ec2Metadata = Ec2Metadata.builder().endpoint(URI.create("http://localhost:8080")).build();
MetadataResponse metadataResponse = ec2Metadata.get("/latest/meta-data/ami-id");
}).hasMessageContaining("metadata")
.hasCauseInstanceOf(SdkClientException.class);
.isInstanceOf(SdkClientException.class);
}

@Test
Expand All @@ -125,7 +125,7 @@ public void get_failedThriceWith401() throws IOException {
Ec2Metadata ec2Metadata = Ec2Metadata.builder().endpoint(URI.create("http://localhost:8080")).build();
MetadataResponse metadataResponse = ec2Metadata.get("/latest/meta-data/ami-id");
}).hasMessageContaining("Exceeded maximum number of retries.")
.hasCauseInstanceOf(SdkClientException.class);
.isInstanceOf(SdkClientException.class);
}

@Test
Expand Down Expand Up @@ -154,7 +154,7 @@ public void getToken_failedThriceWith403() throws IOException {
Ec2Metadata ec2Metadata = Ec2Metadata.builder().endpoint(URI.create("http://localhost:8080")).build();
MetadataResponse metadataResponse = ec2Metadata.get("/latest/meta-data/ami-id");
}).hasMessageContaining("token")
.hasCauseInstanceOf(SdkClientException.class);
.isInstanceOf(SdkClientException.class);
}

@Test
Expand All @@ -167,7 +167,7 @@ public void getToken_failedThriceWith401() throws IOException {
Ec2Metadata ec2Metadata = Ec2Metadata.builder().endpoint(URI.create("http://localhost:8080")).build();
MetadataResponse metadataResponse = ec2Metadata.get("/latest/meta-data/ami-id");
}).hasMessageContaining("Exceeded maximum number of retries.")
.hasCauseInstanceOf(SdkClientException.class);
.isInstanceOf(SdkClientException.class);
WireMock.verify(putRequestedFor(urlPathEqualTo(TOKEN_RESOURCE_PATH)).withHeader(EC2_METADATA_TOKEN_TTL_HEADER, equalTo("21600")));

}
Expand Down Expand Up @@ -369,7 +369,7 @@ public void get_failedTwiceWith401_shouldFailOnThirdAttempt() throws IOException
Ec2Metadata ec2Metadata = Ec2Metadata.builder().endpoint(URI.create("http://localhost:8080")).build();
MetadataResponse metadataResponse = ec2Metadata.get("/latest/meta-data/ami-id");
}).hasMessageContaining("Exceeded maximum number of retries.")
.hasCauseInstanceOf(SdkClientException.class);
.isInstanceOf(SdkClientException.class);

WireMock.verify(putRequestedFor(urlPathEqualTo(TOKEN_RESOURCE_PATH)).withHeader(EC2_METADATA_TOKEN_TTL_HEADER, equalTo("21600")));

Expand Down