Skip to content
Draft
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 @@ -170,11 +170,11 @@ public void http2MaxConcurrentStreams() {

@Test(groups = { "emulator" })
public void thinClientEnabledTest() {
assertThat(isThinClientEnabled()).isFalse();
assertThat(isThinClientEnabled()).isTrue();
System.clearProperty("COSMOS.THINCLIENT_ENABLED");
System.setProperty("COSMOS.THINCLIENT_ENABLED", "true");
System.setProperty("COSMOS.THINCLIENT_ENABLED", "false");
try {
assertThat(isThinClientEnabled()).isTrue();
assertThat(isThinClientEnabled()).isFalse();
} finally {
System.clearProperty("COSMOS.THINCLIENT_ENABLED");
}
Expand Down
1 change: 1 addition & 0 deletions sdk/cosmos/azure-cosmos/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#### Features Added
* Added support for N-Region synchronous commit feature - See [PR 47757](https://github.com/Azure/azure-sdk-for-java/pull/47757)
* Auto-enable thin client mode under gateway mode when the account's gateway metadata response indicates a thin client endpoint is available. Use system property `COSMOS.THINCLIENT_ENABLED=false` (or env var `COSMOS_THINCLIENT_ENABLED=false`) to opt out. No new public API is introduced.

#### Breaking Changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class Configs {
private static final String DEFAULT_THINCLIENT_ENDPOINT = "";
private static final String THINCLIENT_ENDPOINT = "COSMOS.THINCLIENT_ENDPOINT";
private static final String THINCLIENT_ENDPOINT_VARIABLE = "COSMOS_THINCLIENT_ENDPOINT";
private static final boolean DEFAULT_THINCLIENT_ENABLED = false;
private static final boolean DEFAULT_THINCLIENT_ENABLED = true;
private static final String THINCLIENT_ENABLED = "COSMOS.THINCLIENT_ENABLED";
private static final String THINCLIENT_ENABLED_VARIABLE = "COSMOS_THINCLIENT_ENABLED";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ public class RxDocumentClientImpl implements AsyncDocumentClient, IAuthorization
private final GlobalPartitionEndpointManagerForPerPartitionAutomaticFailover globalPartitionEndpointManagerForPerPartitionAutomaticFailover;
private final RetryPolicy retryPolicy;
private HttpClient reactorHttpClient;
private HttpClient thinClientReactorHttpClient;
private Function<HttpClient, HttpClient> httpClientInterceptor;
private volatile boolean useMultipleWriteLocations;

Expand Down Expand Up @@ -683,14 +684,7 @@ private RxDocumentClientImpl(URI serviceEndpoint,
this.apiType = apiType;
this.clientTelemetryConfig = clientTelemetryConfig;
this.useThinClient = Configs.isThinClientEnabled()
&& this.connectionPolicy.getConnectionMode() == ConnectionMode.GATEWAY
&& this.connectionPolicy.getHttp2ConnectionConfig() != null
&& ImplementationBridgeHelpers
.Http2ConnectionConfigHelper
.getHttp2ConnectionConfigAccessor()
.isEffectivelyEnabled(
this.connectionPolicy.getHttp2ConnectionConfig()
);
&& this.connectionPolicy.getConnectionMode() == ConnectionMode.GATEWAY;
} catch (RuntimeException e) {
logger.error("unexpected failure in initializing client.", e);
close();
Expand Down Expand Up @@ -809,11 +803,19 @@ public void init(CosmosClientMetadataCachesSnapshot metadataCachesSnapshot, Func
this.reactorHttpClient,
this.apiType);

if (this.useThinClient) {
HttpClient thinClientHttp = thinClientHttpClient();
if (httpClientInterceptor != null) {
thinClientHttp = httpClientInterceptor.apply(thinClientHttp);
}
this.thinClientReactorHttpClient = thinClientHttp;
}

this.thinProxy = createThinProxy(this.sessionContainer,
this.consistencyLevel,
this.userAgentContainer,
this.globalEndpointManager,
this.reactorHttpClient);
this.useThinClient ? this.thinClientReactorHttpClient : this.reactorHttpClient);

this.perPartitionFailoverConfigModifier
= (databaseAccount -> {
Expand Down Expand Up @@ -1000,6 +1002,36 @@ private HttpClient httpClient() {
}
}

private HttpClient thinClientHttpClient() {
// The thin client proxy communicates over HTTP/2; create a dedicated HTTP client with HTTP/2 enabled.
// If the user has already supplied an Http2ConnectionConfig with enabled=true, use it directly.
// Otherwise create a new Http2ConnectionConfig with HTTP/2 forced on, preserving any pool-size
// and stream-concurrency settings the user may have configured.
Http2ConnectionConfig userHttp2Config = this.connectionPolicy.getHttp2ConnectionConfig();
Http2ConnectionConfig http2Config;
if (userHttp2Config != null && Boolean.TRUE.equals(userHttp2Config.isEnabled())) {
http2Config = userHttp2Config;
} else {
http2Config = new Http2ConnectionConfig().setEnabled(true);
if (userHttp2Config != null) {
http2Config
.setMaxConnectionPoolSize(userHttp2Config.getMaxConnectionPoolSize())
.setMinConnectionPoolSize(userHttp2Config.getMinConnectionPoolSize())
.setMaxConcurrentStreams(userHttp2Config.getMaxConcurrentStreams());
}
}

HttpClientConfig httpClientConfig = new HttpClientConfig(this.configs)
.withMaxIdleConnectionTimeout(this.connectionPolicy.getIdleHttpConnectionTimeout())
.withPoolSize(this.connectionPolicy.getMaxConnectionPoolSize())
.withProxy(this.connectionPolicy.getProxy())
.withNetworkRequestTimeout(this.connectionPolicy.getHttpNetworkRequestTimeout())
.withServerCertValidationDisabled(this.connectionPolicy.isServerCertValidationDisabled())
.withHttp2ConnectionConfig(http2Config);

return HttpClient.createFixed(httpClientConfig);
}

private void createStoreModel(boolean subscribeRntbdStatus) {
// EnableReadRequestsFallback, if not explicitly set on the connection policy,
// is false if the account's consistency is bounded staleness,
Expand Down Expand Up @@ -1530,7 +1562,7 @@ private void addUserAgentSuffix(UserAgentContainer userAgentContainer, Set<UserA
userAgentFeatureFlags.remove(UserAgentFeatureFlags.PerPartitionCircuitBreaker);
}

if (!Configs.isThinClientEnabled()) {
if (!this.useThinClient) {
userAgentFeatureFlags.remove(UserAgentFeatureFlags.ThinClient);
}

Expand Down Expand Up @@ -6587,6 +6619,10 @@ public void close() {
LifeCycleUtils.closeQuietly(this.storeClientFactory);
logger.info("Shutting down reactorHttpClient ...");
LifeCycleUtils.closeQuietly(this.reactorHttpClient);
if (this.thinClientReactorHttpClient != null) {
logger.info("Shutting down thinClientReactorHttpClient ...");
LifeCycleUtils.closeQuietly(this.thinClientReactorHttpClient);
}
logger.info("Shutting down CpuMonitor ...");
CpuMemoryMonitor.unregister(this);

Expand Down
Loading