Skip to content

Remove identity join in async client for endpoint discovery #3947

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 all 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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;
import com.squareup.javapoet.WildcardTypeName;
import java.net.URI;
import java.nio.ByteBuffer;
import java.util.ArrayList;
Expand Down Expand Up @@ -80,6 +81,7 @@
import software.amazon.awssdk.core.endpointdiscovery.EndpointDiscoveryRefreshCache;
import software.amazon.awssdk.core.endpointdiscovery.EndpointDiscoveryRequest;
import software.amazon.awssdk.core.metrics.CoreMetric;
import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity;
import software.amazon.awssdk.metrics.MetricCollector;
import software.amazon.awssdk.metrics.MetricPublisher;
import software.amazon.awssdk.metrics.NoOpMetricCollector;
Expand Down Expand Up @@ -374,24 +376,29 @@ protected MethodSpec.Builder operationBody(MethodSpec.Builder builder, Operation
}
}

builder.addStatement("$T cachedEndpoint = null", URI.class);
builder.addStatement("$T<$T> endpointFuture = $T.completedFuture(null)",
CompletableFuture.class, URI.class, CompletableFuture.class);
builder.beginControlFlow("if (endpointDiscoveryEnabled)");

builder.addCode("$T key = $N.overrideConfiguration()", String.class, opModel.getInput().getVariableName())
ParameterizedTypeName identityFutureTypeName = ParameterizedTypeName.get(ClassName.get(CompletableFuture.class),
WildcardTypeName.subtypeOf(AwsCredentialsIdentity.class));
builder.addCode("$T identityFuture = $N.overrideConfiguration()", identityFutureTypeName,
opModel.getInput().getVariableName())
.addCode(" .flatMap($T::credentialsIdentityProvider)", AwsRequestOverrideConfiguration.class)
.addCode(" .orElseGet(() -> clientConfiguration.option($T.CREDENTIALS_IDENTITY_PROVIDER))",
AwsClientOption.class)
// TODO: avoid join inside async
.addCode(" .resolveIdentity().join().accessKeyId();");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This PR only changes the handling in AsyncClientClass.java. A join will remain in SyncClientClass.java, though it will change slightly for exception handling - that'll be in a separate PR.

.addCode(" .resolveIdentity();");

builder.addCode("$1T endpointDiscoveryRequest = $1T.builder()", EndpointDiscoveryRequest.class)
.addCode(" .required($L)", opModel.getInputShape().getEndpointDiscovery().isRequired())
.addCode(" .defaultEndpoint(clientConfiguration.option($T.ENDPOINT))", SdkClientOption.class)
.addCode(" .overrideConfiguration($N.overrideConfiguration().orElse(null))",
builder.addCode("endpointFuture = identityFuture.thenApply(credentials -> {")
.addCode(" $1T endpointDiscoveryRequest = $1T.builder()", EndpointDiscoveryRequest.class)
.addCode(" .required($L)", opModel.getInputShape().getEndpointDiscovery().isRequired())
.addCode(" .defaultEndpoint(clientConfiguration.option($T.ENDPOINT))", SdkClientOption.class)
.addCode(" .overrideConfiguration($N.overrideConfiguration().orElse(null))",
opModel.getInput().getVariableName())
.addCode(" .build();");
.addCode(" .build();")
.addCode(" return endpointDiscoveryCache.get(credentials.accessKeyId(), endpointDiscoveryRequest);")
.addCode("});");

builder.addStatement("cachedEndpoint = endpointDiscoveryCache.get(key, endpointDiscoveryRequest)");
builder.endControlFlow();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,9 @@ public CodeBlock asyncExecutionHandler(IntermediateModel intermediateModel, Oper
: pojoResponseType;
TypeName executeFutureValueType = executeFutureValueType(opModel, poetExtensions);

builder.add("\n\n$T<$T> executeFuture = clientHandler.execute(new $T<$T, $T>()\n",
CompletableFuture.class, executeFutureValueType, ClientExecutionParams.class, requestType, responseType)
builder.add("\n\n$T<$T> executeFuture = ", CompletableFuture.class, executeFutureValueType)
.add(opModel.getEndpointDiscovery() != null ? "endpointFuture.thenCompose(cachedEndpoint -> " : "")
.add("clientHandler.execute(new $T<$T, $T>()\n", ClientExecutionParams.class, requestType, responseType)
Comment on lines -243 to +245
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Only JsonProtocolSpec changed. No other ProtocolSpec has handling in its asyncExecutionHandler for discoveredEndpoint.

.add(".withOperationName(\"$N\")\n", opModel.getOperationName())
.add(".withMarshaller($L)\n", asyncMarshaller(model, opModel, marshaller, protocolFactory))
.add(asyncRequestBody(opModel))
Expand All @@ -257,8 +258,9 @@ public CodeBlock asyncExecutionHandler(IntermediateModel intermediateModel, Oper
.add(HttpChecksumRequiredTrait.putHttpChecksumAttribute(opModel))
.add(HttpChecksumTrait.create(opModel))
.add(NoneAuthTypeRequestTrait.create(opModel))
.add(".withInput($L)$L);",
opModel.getInput().getVariableName(), asyncResponseTransformerVariable(isStreaming, isRestJson, opModel));
.add(".withInput($L)$L)",
opModel.getInput().getVariableName(), asyncResponseTransformerVariable(isStreaming, isRestJson, opModel))
.add(opModel.getEndpointDiscovery() != null ? ");" : ";");

if (opModel.hasStreamingOutput()) {
builder.addStatement("$T<$T, ReturnT> finalAsyncResponseTransformer = asyncResponseTransformer",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import software.amazon.awssdk.core.endpointdiscovery.EndpointDiscoveryRequest;
import software.amazon.awssdk.core.http.HttpResponseHandler;
import software.amazon.awssdk.core.metrics.CoreMetric;
import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity;
import software.amazon.awssdk.metrics.MetricCollector;
import software.amazon.awssdk.metrics.MetricPublisher;
import software.amazon.awssdk.metrics.NoOpMetricCollector;
Expand Down Expand Up @@ -175,26 +176,30 @@ public CompletableFuture<TestDiscoveryIdentifiersRequiredResponse> testDiscovery
throw new IllegalStateException(
"This operation requires endpoint discovery, but endpoint discovery was disabled on the client.");
}
URI cachedEndpoint = null;
CompletableFuture<URI> endpointFuture = CompletableFuture.completedFuture(null);
if (endpointDiscoveryEnabled) {
String key = testDiscoveryIdentifiersRequiredRequest.overrideConfiguration()
.flatMap(AwsRequestOverrideConfiguration::credentialsIdentityProvider)
.orElseGet(() -> clientConfiguration.option(AwsClientOption.CREDENTIALS_IDENTITY_PROVIDER))
.resolveIdentity().join().accessKeyId();
EndpointDiscoveryRequest endpointDiscoveryRequest = EndpointDiscoveryRequest.builder().required(true)
.defaultEndpoint(clientConfiguration.option(SdkClientOption.ENDPOINT))
.overrideConfiguration(testDiscoveryIdentifiersRequiredRequest.overrideConfiguration().orElse(null))
.build();
cachedEndpoint = endpointDiscoveryCache.get(key, endpointDiscoveryRequest);
CompletableFuture<? extends AwsCredentialsIdentity> identityFuture =
testDiscoveryIdentifiersRequiredRequest.overrideConfiguration()
.flatMap(AwsRequestOverrideConfiguration::credentialsIdentityProvider)
.orElseGet(() -> clientConfiguration.option(AwsClientOption.CREDENTIALS_IDENTITY_PROVIDER))
.resolveIdentity();
endpointFuture = identityFuture.thenApply(credentials -> {
EndpointDiscoveryRequest endpointDiscoveryRequest = EndpointDiscoveryRequest.builder().required(true)
.defaultEndpoint(clientConfiguration.option(SdkClientOption.ENDPOINT))
.overrideConfiguration(testDiscoveryIdentifiersRequiredRequest.overrideConfiguration().orElse(null))
.build();
return endpointDiscoveryCache.get(credentials.accessKeyId(), endpointDiscoveryRequest);
});
}

CompletableFuture<TestDiscoveryIdentifiersRequiredResponse> executeFuture = clientHandler
.execute(new ClientExecutionParams<TestDiscoveryIdentifiersRequiredRequest, TestDiscoveryIdentifiersRequiredResponse>()
CompletableFuture<TestDiscoveryIdentifiersRequiredResponse> executeFuture =
endpointFuture.thenCompose(cachedEndpoint ->
clientHandler.execute(new ClientExecutionParams<TestDiscoveryIdentifiersRequiredRequest, TestDiscoveryIdentifiersRequiredResponse>()
.withOperationName("TestDiscoveryIdentifiersRequired")
.withMarshaller(new TestDiscoveryIdentifiersRequiredRequestMarshaller(protocolFactory))
.withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
.withMetricCollector(apiCallMetricCollector).discoveredEndpoint(cachedEndpoint)
.withInput(testDiscoveryIdentifiersRequiredRequest));
.withInput(testDiscoveryIdentifiersRequiredRequest)));
CompletableFuture<TestDiscoveryIdentifiersRequiredResponse> whenCompleted = executeFuture.whenComplete((r, e) -> {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
});
Expand Down Expand Up @@ -243,25 +248,29 @@ public CompletableFuture<TestDiscoveryOptionalResponse> testDiscoveryOptional(
operationMetadata);
boolean endpointDiscoveryEnabled = clientConfiguration.option(SdkClientOption.ENDPOINT_DISCOVERY_ENABLED);
boolean endpointOverridden = clientConfiguration.option(SdkClientOption.ENDPOINT_OVERRIDDEN) == Boolean.TRUE;
URI cachedEndpoint = null;
CompletableFuture<URI> endpointFuture = CompletableFuture.completedFuture(null);
if (endpointDiscoveryEnabled) {
String key = testDiscoveryOptionalRequest.overrideConfiguration()
.flatMap(AwsRequestOverrideConfiguration::credentialsIdentityProvider)
.orElseGet(() -> clientConfiguration.option(AwsClientOption.CREDENTIALS_IDENTITY_PROVIDER))
.resolveIdentity().join().accessKeyId();
EndpointDiscoveryRequest endpointDiscoveryRequest = EndpointDiscoveryRequest.builder().required(false)
.defaultEndpoint(clientConfiguration.option(SdkClientOption.ENDPOINT))
.overrideConfiguration(testDiscoveryOptionalRequest.overrideConfiguration().orElse(null)).build();
cachedEndpoint = endpointDiscoveryCache.get(key, endpointDiscoveryRequest);
CompletableFuture<? extends AwsCredentialsIdentity> identityFuture =
testDiscoveryOptionalRequest.overrideConfiguration()
.flatMap(AwsRequestOverrideConfiguration::credentialsIdentityProvider)
.orElseGet(() -> clientConfiguration.option(AwsClientOption.CREDENTIALS_IDENTITY_PROVIDER))
.resolveIdentity();
endpointFuture = identityFuture.thenApply(credentials -> {
EndpointDiscoveryRequest endpointDiscoveryRequest = EndpointDiscoveryRequest.builder().required(false)
.defaultEndpoint(clientConfiguration.option(SdkClientOption.ENDPOINT))
.overrideConfiguration(testDiscoveryOptionalRequest.overrideConfiguration().orElse(null)).build();
return endpointDiscoveryCache.get(credentials.accessKeyId(), endpointDiscoveryRequest);
});
}

CompletableFuture<TestDiscoveryOptionalResponse> executeFuture = clientHandler
.execute(new ClientExecutionParams<TestDiscoveryOptionalRequest, TestDiscoveryOptionalResponse>()
CompletableFuture<TestDiscoveryOptionalResponse> executeFuture =
endpointFuture.thenCompose(cachedEndpoint ->
clientHandler.execute(new ClientExecutionParams<TestDiscoveryOptionalRequest, TestDiscoveryOptionalResponse>()
.withOperationName("TestDiscoveryOptional")
.withMarshaller(new TestDiscoveryOptionalRequestMarshaller(protocolFactory))
.withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
.withMetricCollector(apiCallMetricCollector).discoveredEndpoint(cachedEndpoint)
.withInput(testDiscoveryOptionalRequest));
.withInput(testDiscoveryOptionalRequest)));
CompletableFuture<TestDiscoveryOptionalResponse> whenCompleted = executeFuture.whenComplete((r, e) -> {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
});
Expand Down Expand Up @@ -318,25 +327,29 @@ public CompletableFuture<TestDiscoveryRequiredResponse> testDiscoveryRequired(
throw new IllegalStateException(
"This operation requires endpoint discovery, but endpoint discovery was disabled on the client.");
}
URI cachedEndpoint = null;
CompletableFuture<URI> endpointFuture = CompletableFuture.completedFuture(null);
if (endpointDiscoveryEnabled) {
String key = testDiscoveryRequiredRequest.overrideConfiguration()
.flatMap(AwsRequestOverrideConfiguration::credentialsIdentityProvider)
.orElseGet(() -> clientConfiguration.option(AwsClientOption.CREDENTIALS_IDENTITY_PROVIDER))
.resolveIdentity().join().accessKeyId();
EndpointDiscoveryRequest endpointDiscoveryRequest = EndpointDiscoveryRequest.builder().required(true)
.defaultEndpoint(clientConfiguration.option(SdkClientOption.ENDPOINT))
.overrideConfiguration(testDiscoveryRequiredRequest.overrideConfiguration().orElse(null)).build();
cachedEndpoint = endpointDiscoveryCache.get(key, endpointDiscoveryRequest);
CompletableFuture<? extends AwsCredentialsIdentity> identityFuture =
testDiscoveryRequiredRequest.overrideConfiguration()
.flatMap(AwsRequestOverrideConfiguration::credentialsIdentityProvider)
.orElseGet(() -> clientConfiguration.option(AwsClientOption.CREDENTIALS_IDENTITY_PROVIDER))
.resolveIdentity();
endpointFuture = identityFuture.thenApply(credentials -> {
EndpointDiscoveryRequest endpointDiscoveryRequest = EndpointDiscoveryRequest.builder().required(true)
.defaultEndpoint(clientConfiguration.option(SdkClientOption.ENDPOINT))
.overrideConfiguration(testDiscoveryRequiredRequest.overrideConfiguration().orElse(null)).build();
return endpointDiscoveryCache.get(credentials.accessKeyId(), endpointDiscoveryRequest);
});
}

CompletableFuture<TestDiscoveryRequiredResponse> executeFuture = clientHandler
.execute(new ClientExecutionParams<TestDiscoveryRequiredRequest, TestDiscoveryRequiredResponse>()
CompletableFuture<TestDiscoveryRequiredResponse> executeFuture =
endpointFuture.thenCompose(cachedEndpoint ->
clientHandler.execute(new ClientExecutionParams<TestDiscoveryRequiredRequest, TestDiscoveryRequiredResponse>()
.withOperationName("TestDiscoveryRequired")
.withMarshaller(new TestDiscoveryRequiredRequestMarshaller(protocolFactory))
.withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
.withMetricCollector(apiCallMetricCollector).discoveredEndpoint(cachedEndpoint)
.withInput(testDiscoveryRequiredRequest));
.withInput(testDiscoveryRequiredRequest)));
CompletableFuture<TestDiscoveryRequiredResponse> whenCompleted = executeFuture.whenComplete((r, e) -> {
metricPublishers.forEach(p -> p.publish(apiCallMetricCollector.collect()));
});
Expand Down