Skip to content

Commit 1be835d

Browse files
xinlian12annie-mac
andauthored
expose networkRequestTimeout public API (Azure#25094)
* expose networkRequestTimeout Co-authored-by: annie-mac <annie-mac@SHRISU-YOGA.redmond.corp.microsoft.com>
1 parent f48ebe8 commit 1be835d

19 files changed

+175
-97
lines changed

sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/BridgeInternal.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -701,13 +701,8 @@ public static CosmosException createServiceUnavailableException(Exception innerE
701701
}
702702

703703
@Warning(value = INTERNAL_USE_ONLY_WARNING)
704-
public static Duration getRequestTimeoutFromDirectConnectionConfig(DirectConnectionConfig directConnectionConfig) {
705-
return directConnectionConfig.getRequestTimeout();
706-
}
707-
708-
@Warning(value = INTERNAL_USE_ONLY_WARNING)
709-
public static Duration getRequestTimeoutFromGatewayConnectionConfig(GatewayConnectionConfig gatewayConnectionConfig) {
710-
return gatewayConnectionConfig.getRequestTimeout();
704+
public static Duration getNetworkRequestTimeoutFromGatewayConnectionConfig(GatewayConnectionConfig gatewayConnectionConfig) {
705+
return gatewayConnectionConfig.getNetworkRequestTimeout();
711706
}
712707

713708
@Warning(value = INTERNAL_USE_ONLY_WARNING)

sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/CosmosClientBuilder.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -760,9 +760,7 @@ private void buildConnectionPolicy() {
760760
// Check if the user passed additional gateway connection configuration
761761
if (this.gatewayConnectionConfig != null) {
762762
this.connectionPolicy.setMaxConnectionPoolSize(this.gatewayConnectionConfig.getMaxConnectionPoolSize());
763-
// TODO(kuthapar): potential bug - when we expose requestTimeout from direct and gateway connection config,
764-
// as gateway connection config will overwrite direct connection config settings
765-
this.connectionPolicy.setRequestTimeout(this.gatewayConnectionConfig.getRequestTimeout());
763+
this.connectionPolicy.setHttpNetworkRequestTimeout(this.gatewayConnectionConfig.getNetworkRequestTimeout());
766764
this.connectionPolicy.setIdleHttpConnectionTimeout(this.gatewayConnectionConfig.getIdleConnectionTimeout());
767765
this.connectionPolicy.setProxy(this.gatewayConnectionConfig.getProxy());
768766
}

sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/DirectConnectionConfig.java

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.time.Duration;
99

1010
import static com.azure.cosmos.implementation.guava25.base.Preconditions.checkArgument;
11+
import static com.azure.cosmos.implementation.guava25.base.Preconditions.checkNotNull;
1112

1213
/**
1314
* Represents the connection config with {@link ConnectionMode#DIRECT} associated with Cosmos Client in the Azure Cosmos DB database service.
@@ -20,15 +21,17 @@ public final class DirectConnectionConfig {
2021
private static final Boolean DEFAULT_CONNECTION_ENDPOINT_REDISCOVERY_ENABLED = false;
2122
private static final Duration DEFAULT_IDLE_ENDPOINT_TIMEOUT = Duration.ofHours(1l);
2223
private static final Duration DEFAULT_CONNECT_TIMEOUT = Duration.ofSeconds(5L);
23-
private static final Duration DEFAULT_REQUEST_TIMEOUT = Duration.ofSeconds(5L);
24+
private static final Duration DEFAULT_NETWORK_REQUEST_TIMEOUT = Duration.ofSeconds(5L);
25+
private static final Duration MIN_NETWORK_REQUEST_TIMEOUT = Duration.ofSeconds(5L);
26+
private static final Duration MAX_NETWORK_REQUEST_TIMEOUT = Duration.ofSeconds(10L);
2427
private static final int DEFAULT_MAX_CONNECTIONS_PER_ENDPOINT = 130;
2528
private static final int DEFAULT_MAX_REQUESTS_PER_CONNECTION = 30;
2629

2730
private boolean connectionEndpointRediscoveryEnabled;
2831
private Duration connectTimeout;
2932
private Duration idleConnectionTimeout;
3033
private Duration idleEndpointTimeout;
31-
private Duration requestTimeout;
34+
private Duration networkRequestTimeout;
3235
private int maxConnectionsPerEndpoint;
3336
private int maxRequestsPerConnection;
3437

@@ -42,7 +45,7 @@ public DirectConnectionConfig() {
4245
this.idleEndpointTimeout = DEFAULT_IDLE_ENDPOINT_TIMEOUT;
4346
this.maxConnectionsPerEndpoint = DEFAULT_MAX_CONNECTIONS_PER_ENDPOINT;
4447
this.maxRequestsPerConnection = DEFAULT_MAX_REQUESTS_PER_CONNECTION;
45-
this.requestTimeout = DEFAULT_REQUEST_TIMEOUT;
48+
this.networkRequestTimeout = DEFAULT_NETWORK_REQUEST_TIMEOUT;
4649
}
4750

4851
/**
@@ -234,28 +237,39 @@ public DirectConnectionConfig setMaxRequestsPerConnection(int maxRequestsPerConn
234237
}
235238

236239
/**
237-
* Gets the request timeout interval
238-
* This represents the timeout interval for requests
240+
* Gets the network request timeout interval (time to wait for response from network peer).
239241
*
240-
* Default value is 60 seconds
242+
* Default value is 5 seconds
241243
*
242-
* @return the request timeout interval
244+
* @return the network request timeout interval
243245
*/
244-
Duration getRequestTimeout() {
245-
return requestTimeout;
246+
public Duration getNetworkRequestTimeout() {
247+
return networkRequestTimeout;
246248
}
247249

248250
/**
249-
* Sets the request timeout interval
250-
* This represents the timeout interval for requests
251+
* Sets the network request timeout interval (time to wait for response from network peer).
251252
*
252-
* Default value is 5 seconds
253+
* Default value is 5 seconds.
254+
* It only allows values &ge;5s and &le;10s. (backend allows requests to take up-to 5 seconds processing time - 5 seconds
255+
* buffer so 10 seconds in total for transport is more than sufficient).
253256
*
254-
* @param requestTimeout the request timeout interval
257+
* Attention! Please adjust this value with caution.
258+
* This config represents the max time allowed to wait for and consume a service response after the request has been written to the network connection.
259+
* Setting a value too low can result in having not enough time to wait for the service response - which could cause too aggressive retries and degrade performance.
260+
* Setting a value too high can result in fewer retries and reduce chances of success by retries.
261+
*
262+
* @param networkRequestTimeout the network request timeout interval.
255263
* @return the {@link DirectConnectionConfig}
256264
*/
257-
DirectConnectionConfig setRequestTimeout(Duration requestTimeout) {
258-
this.requestTimeout = requestTimeout;
265+
public DirectConnectionConfig setNetworkRequestTimeout(Duration networkRequestTimeout) {
266+
checkNotNull(networkRequestTimeout, "NetworkRequestTimeout can not be null");
267+
checkArgument(networkRequestTimeout.toMillis() >= MIN_NETWORK_REQUEST_TIMEOUT.toMillis(),
268+
"NetworkRequestTimeout can not be less than %s Millis", MIN_NETWORK_REQUEST_TIMEOUT.toMillis());
269+
checkArgument(networkRequestTimeout.toMillis() <= MAX_NETWORK_REQUEST_TIMEOUT.toMillis(),
270+
"NetworkRequestTimeout can not be larger than %s Millis", MAX_NETWORK_REQUEST_TIMEOUT.toMillis());
271+
272+
this.networkRequestTimeout = networkRequestTimeout;
259273
return this;
260274
}
261275

@@ -267,6 +281,7 @@ public String toString() {
267281
", idleEndpointTimeout=" + idleEndpointTimeout +
268282
", maxConnectionsPerEndpoint=" + maxConnectionsPerEndpoint +
269283
", maxRequestsPerConnection=" + maxRequestsPerConnection +
284+
", networkRequestTimeout=" + networkRequestTimeout +
270285
'}';
271286
}
272287
}

sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/GatewayConnectionConfig.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@
77

88
import java.time.Duration;
99

10+
import static com.azure.cosmos.implementation.guava25.base.Preconditions.checkArgument;
11+
import static com.azure.cosmos.implementation.guava25.base.Preconditions.checkNotNull;
12+
1013
/**
1114
* Represents the connection config with {@link ConnectionMode#GATEWAY} associated with Cosmos Client in the Azure Cosmos DB database service.
1215
*/
1316
public final class GatewayConnectionConfig {
1417
// Constants
15-
private static final Duration DEFAULT_REQUEST_TIMEOUT = Duration.ofSeconds(5);
18+
private static final Duration MIN_NETWORK_REQUEST_TIMEOUT = Duration.ofSeconds(60);
19+
private static final Duration DEFAULT_NETWORK_REQUEST_TIMEOUT = Duration.ofSeconds(60);
1620
private static final Duration DEFAULT_IDLE_CONNECTION_TIMEOUT = Duration.ofSeconds(60);
1721
private static final int DEFAULT_MAX_CONNECTION_POOL_SIZE = 1000;
1822

19-
private Duration requestTimeout;
23+
private Duration networkRequestTimeout;
2024
private int maxConnectionPoolSize;
2125
private Duration idleConnectionTimeout;
2226
private ProxyOptions proxy;
@@ -27,7 +31,7 @@ public final class GatewayConnectionConfig {
2731
public GatewayConnectionConfig() {
2832
this.idleConnectionTimeout = DEFAULT_IDLE_CONNECTION_TIMEOUT;
2933
this.maxConnectionPoolSize = DEFAULT_MAX_CONNECTION_POOL_SIZE;
30-
this.requestTimeout = DEFAULT_REQUEST_TIMEOUT;
34+
this.networkRequestTimeout = DEFAULT_NETWORK_REQUEST_TIMEOUT;
3135
}
3236

3337
/**
@@ -40,23 +44,27 @@ public static GatewayConnectionConfig getDefaultConfig() {
4044
}
4145

4246
/**
43-
* Gets the request timeout (time to wait for response from network peer).
47+
* Gets the network request timeout interval (time to wait for response from network peer).
48+
* The default is 60 seconds.
4449
*
45-
* @return the request timeout duration.
50+
* @return the network request timeout duration.
4651
*/
47-
Duration getRequestTimeout() {
48-
return this.requestTimeout;
52+
Duration getNetworkRequestTimeout() {
53+
return this.networkRequestTimeout;
4954
}
5055

5156
/**
52-
* Sets the request timeout (time to wait for response from network peer).
53-
* The default is 5 seconds.
57+
* Sets the network request timeout interval (time to wait for response from network peer).
58+
* The default is 60 seconds.
5459
*
55-
* @param requestTimeout the request timeout duration.
60+
* @param networkRequestTimeout the network request timeout duration.
5661
* @return the {@link GatewayConnectionConfig}.
5762
*/
58-
GatewayConnectionConfig setRequestTimeout(Duration requestTimeout) {
59-
this.requestTimeout = requestTimeout;
63+
GatewayConnectionConfig setNetworkRequestTimeout(Duration networkRequestTimeout) {
64+
checkNotNull(networkRequestTimeout, "NetworkRequestTimeout can not be null");
65+
checkArgument(networkRequestTimeout.toMillis() >= MIN_NETWORK_REQUEST_TIMEOUT.toMillis(),
66+
"NetworkRequestTimeout can not be less than %s millis", MIN_NETWORK_REQUEST_TIMEOUT.toMillis());
67+
this.networkRequestTimeout = networkRequestTimeout;
6068
return this;
6169
}
6270

@@ -138,6 +146,7 @@ public String toString() {
138146
return "GatewayConnectionConfig{" +
139147
", maxConnectionPoolSize=" + maxConnectionPoolSize +
140148
", idleConnectionTimeout=" + idleConnectionTimeout +
149+
", networkRequestTimeout=" + networkRequestTimeout +
141150
", proxyType=" + proxyType +
142151
", inetSocketProxyAddress=" + proxyAddress +
143152
'}';

sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/ConnectionPolicy.java

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public final class ConnectionPolicy {
3232

3333
// Gateway connection config properties
3434
private int maxConnectionPoolSize;
35-
private Duration requestTimeout;
35+
private Duration httpNetworkRequestTimeout;
3636
private ProxyOptions proxy;
3737
private Duration idleHttpConnectionTimeout;
3838

@@ -42,7 +42,7 @@ public final class ConnectionPolicy {
4242
private Duration idleTcpEndpointTimeout;
4343
private int maxConnectionsPerEndpoint;
4444
private int maxRequestsPerConnection;
45-
45+
private Duration tcpNetworkRequestTimeout;
4646
private boolean tcpConnectionEndpointRediscoveryEnabled;
4747

4848

@@ -55,7 +55,7 @@ public ConnectionPolicy(GatewayConnectionConfig gatewayConnectionConfig) {
5555
this(ConnectionMode.GATEWAY);
5656
this.idleHttpConnectionTimeout = gatewayConnectionConfig.getIdleConnectionTimeout();
5757
this.maxConnectionPoolSize = gatewayConnectionConfig.getMaxConnectionPoolSize();
58-
this.requestTimeout = BridgeInternal.getRequestTimeoutFromGatewayConnectionConfig(gatewayConnectionConfig);
58+
this.httpNetworkRequestTimeout = BridgeInternal.getNetworkRequestTimeoutFromGatewayConnectionConfig(gatewayConnectionConfig);
5959
this.proxy = gatewayConnectionConfig.getProxy();
6060
this.tcpConnectionEndpointRediscoveryEnabled = false;
6161
}
@@ -67,7 +67,7 @@ public ConnectionPolicy(DirectConnectionConfig directConnectionConfig) {
6767
this.idleTcpEndpointTimeout = directConnectionConfig.getIdleEndpointTimeout();
6868
this.maxConnectionsPerEndpoint = directConnectionConfig.getMaxConnectionsPerEndpoint();
6969
this.maxRequestsPerConnection = directConnectionConfig.getMaxRequestsPerConnection();
70-
this.requestTimeout = BridgeInternal.getRequestTimeoutFromDirectConnectionConfig(directConnectionConfig);
70+
this.tcpNetworkRequestTimeout = directConnectionConfig.getNetworkRequestTimeout();
7171
this.tcpConnectionEndpointRediscoveryEnabled = directConnectionConfig.isConnectionEndpointRediscoveryEnabled();
7272
}
7373

@@ -112,23 +112,55 @@ public static ConnectionPolicy getDefaultPolicy() {
112112
}
113113

114114
/**
115-
* Gets the request timeout (time to wait for response from network peer).
115+
* Gets the http network request timeout interval (time to wait for response from network peer).
116+
* The default is 60 seconds.
116117
*
117-
* @return the request timeout duration.
118+
* @return the http request timeout duration.
118119
*/
119-
public Duration getRequestTimeout() {
120-
return this.requestTimeout;
120+
public Duration getHttpNetworkRequestTimeout() {
121+
return this.httpNetworkRequestTimeout;
121122
}
122123

123124
/**
124-
* Sets the request timeout (time to wait for response from network peer).
125+
* Sets the http network request timeout interval (time to wait for response from network peer).
125126
* The default is 60 seconds.
126127
*
127-
* @param requestTimeout the request timeout duration.
128+
* @param httpNetworkRequestTimeout the http request timeout duration.
128129
* @return the ConnectionPolicy.
129130
*/
130-
public ConnectionPolicy setRequestTimeout(Duration requestTimeout) {
131-
this.requestTimeout = requestTimeout;
131+
public ConnectionPolicy setHttpNetworkRequestTimeout(Duration httpNetworkRequestTimeout) {
132+
this.httpNetworkRequestTimeout = httpNetworkRequestTimeout;
133+
return this;
134+
}
135+
136+
/**
137+
* Gets the tcp network request timeout interval (time to wait for response from network peer).
138+
*
139+
* Default value is 5 seconds
140+
*
141+
* @return the network request timeout interval
142+
*/
143+
public Duration getTcpNetworkRequestTimeout() {
144+
return this.tcpNetworkRequestTimeout;
145+
}
146+
147+
/**
148+
* Sets the tcp network request timeout interval (time to wait for response from network peer).
149+
*
150+
* Default value is 5 seconds.
151+
* It only allows values &ge;5s and &le;10s. (backend allows requests to take up-to 5 seconds processing time - 5 seconds
152+
* buffer so 10 seconds in total for transport is more than sufficient).
153+
*
154+
* Attention! Please adjust this value with caution.
155+
* This config represents the max time allowed to wait for and consume a service response after the request has been written to the network connection.
156+
* Setting a value too low can result in having not enough time to wait for the service response - which could cause too aggressive retries and degrade performance.
157+
* Setting a value too high can result in fewer retries and reduce chances of success by retries.
158+
*
159+
* @param tcpNetworkRequestTimeout the network request timeout interval.
160+
* @return the {@link ConnectionPolicy}
161+
*/
162+
public ConnectionPolicy setTcpNetworkRequestTimeout(Duration tcpNetworkRequestTimeout) {
163+
this.tcpNetworkRequestTimeout = tcpNetworkRequestTimeout;
132164
return this;
133165
}
134166

@@ -511,7 +543,8 @@ public void setClientTelemetryEnabled(boolean clientTelemetryEnabled) {
511543
@Override
512544
public String toString() {
513545
return "ConnectionPolicy{" +
514-
"requestTimeout=" + requestTimeout +
546+
"httpNetworkRequestTimeout=" + httpNetworkRequestTimeout +
547+
", tcpNetworkRequestTimeout=" + tcpNetworkRequestTimeout +
515548
", connectionMode=" + connectionMode +
516549
", maxConnectionPoolSize=" + maxConnectionPoolSize +
517550
", idleHttpConnectionTimeout=" + idleHttpConnectionTimeout +

sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/DiagnosticsClientContext.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ private String gwConfigInternal() {
186186
if (this.httpClientConfig == null) {
187187
return null;
188188
}
189-
return Strings.lenientFormat("(cps:%s, rto:%s, icto:%s, p:%s)",
189+
return Strings.lenientFormat("(cps:%s, nrto:%s, icto:%s, p:%s)",
190190
this.httpClientConfig.getMaxPoolSize(),
191-
this.httpClientConfig.getRequestTimeout(),
191+
this.httpClientConfig.getNetworkRequestTimeout(),
192192
this.httpClientConfig.getMaxIdleConnectionTimeout(),
193193
this.httpClientConfig.getProxy() != null);
194194
}
@@ -197,9 +197,9 @@ private String rntbdConfigInternal(RntbdTransportClient.Options rntbdOptions) {
197197
if (rntbdOptions == null) {
198198
return null;
199199
}
200-
return Strings.lenientFormat("(cto:%s, rto:%s, icto:%s, ieto:%s, mcpe:%s, mrpc:%s, cer:%s)",
200+
return Strings.lenientFormat("(cto:%s, nrto:%s, icto:%s, ieto:%s, mcpe:%s, mrpc:%s, cer:%s)",
201201
rntbdOptions.connectTimeout(),
202-
rntbdOptions.requestTimeout(),
202+
rntbdOptions.tcpNetworkRequestTimeout(),
203203
rntbdOptions.idleChannelTimeout(),
204204
rntbdOptions.idleEndpointTimeout(),
205205
rntbdOptions.maxChannelsPerEndpoint(),

sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/RxDocumentClientImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ private HttpClient httpClient() {
532532
.withMaxIdleConnectionTimeout(this.connectionPolicy.getIdleHttpConnectionTimeout())
533533
.withPoolSize(this.connectionPolicy.getMaxConnectionPoolSize())
534534
.withProxy(this.connectionPolicy.getProxy())
535-
.withRequestTimeout(this.connectionPolicy.getRequestTimeout());
535+
.withNetworkRequestTimeout(this.connectionPolicy.getHttpNetworkRequestTimeout());
536536

537537
if (connectionSharingAcrossClientsEnabled) {
538538
return SharedGatewayHttpClient.getOrCreateInstance(httpClientConfig, diagnosticsClientConfig);

sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/implementation/directconnectivity/HttpTransportClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public class HttpTransportClient extends TransportClient {
7171
HttpClient createHttpClient(ConnectionPolicy connectionPolicy) {
7272
// TODO: use one instance of SSL context everywhere
7373
HttpClientConfig httpClientConfig = new HttpClientConfig(this.configs);
74-
httpClientConfig.withRequestTimeout(connectionPolicy.getRequestTimeout());
74+
httpClientConfig.withNetworkRequestTimeout(connectionPolicy.getHttpNetworkRequestTimeout());
7575
httpClientConfig.withPoolSize(configs.getDirectHttpsMaxConnectionLimit());
7676

7777
return HttpClient.createFixed(httpClientConfig);

0 commit comments

Comments
 (0)