Skip to content

Commit ffe5242

Browse files
authored
Add maxAdaptiveRetries API (#1944)
JAVA-6141
1 parent fa82369 commit ffe5242

16 files changed

Lines changed: 433 additions & 73 deletions

File tree

driver-core/src/main/com/mongodb/ConnectionString.java

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.mongodb;
1818

1919
import com.mongodb.annotations.Alpha;
20+
import com.mongodb.annotations.Beta;
2021
import com.mongodb.annotations.Reason;
2122
import com.mongodb.connection.ClusterSettings;
2223
import com.mongodb.connection.ConnectionPoolSettings;
@@ -264,14 +265,17 @@
264265
* <p>SRV configuration:</p>
265266
* <ul>
266267
* <li>{@code srvServiceName=string}: The SRV service name. See {@link ClusterSettings#getSrvServiceName()} for details.</li>
267-
* <li>{@code srvMaxHosts=number}: The maximum number of hosts from the SRV record to connect to.</li>
268+
* <li>{@code srvMaxHosts=n}: The maximum number of hosts from the SRV record to connect to.</li>
268269
* </ul>
269270
* <p>General configuration:</p>
270271
* <ul>
271-
* <li>{@code retryWrites=true|false}. If true the driver will retry supported write operations if they fail due to a network error.
272-
* Defaults to true.</li>
273-
* <li>{@code retryReads=true|false}. If true the driver will retry supported read operations if they fail due to a network error.
274-
* Defaults to true.</li>
272+
* <li>{@code retryWrites=true|false}: Whether attempts to execute write commands should be retried if they fail due to a retryable error.
273+
* Defaults to true. See also {@code maxAdaptiveRetries}.</li>
274+
* <li>{@code retryReads=true|false}: Whether attempts to execute read commands should be retried if they fail due to a retryable error.
275+
* Defaults to true. See also {@code maxAdaptiveRetries}.</li>
276+
* <li>{@code maxAdaptiveRetries=n}: This is {@linkplain Beta Beta API}.
277+
* The maximum number of retry attempts when encountering a retryable overload error.
278+
* See {@link MongoClientSettings.Builder#maxAdaptiveRetries(Integer)} for more information.</li>
275279
* <li>{@code uuidRepresentation=unspecified|standard|javaLegacy|csharpLegacy|pythonLegacy}. See
276280
* {@link MongoClientSettings#getUuidRepresentation()} for documentation of semantics of this parameter. Defaults to "javaLegacy", but
277281
* will change to "unspecified" in the next major release.</li>
@@ -308,6 +312,7 @@ public class ConnectionString {
308312
private WriteConcern writeConcern;
309313
private Boolean retryWrites;
310314
private Boolean retryReads;
315+
private Integer maxAdaptiveRetries;
311316
private ReadConcern readConcern;
312317

313318
private Integer minConnectionPoolSize;
@@ -558,6 +563,7 @@ public ConnectionString(final String connectionString, @Nullable final DnsClient
558563
GENERAL_OPTIONS_KEYS.add("servermonitoringmode");
559564
GENERAL_OPTIONS_KEYS.add("retrywrites");
560565
GENERAL_OPTIONS_KEYS.add("retryreads");
566+
GENERAL_OPTIONS_KEYS.add("maxadaptiveretries");
561567

562568
GENERAL_OPTIONS_KEYS.add("appname");
563569

@@ -706,6 +712,12 @@ private void translateOptions(final Map<String, List<String>> optionsMap) {
706712
case "retryreads":
707713
retryReads = parseBoolean(value, "retryreads");
708714
break;
715+
case "maxadaptiveretries":
716+
maxAdaptiveRetries = parseInteger(value, "maxadaptiveretries");
717+
if (maxAdaptiveRetries < 0) {
718+
throw new IllegalArgumentException("maxAdaptiveRetries must be >= 0");
719+
}
720+
break;
709721
case "uuidrepresentation":
710722
uuidRepresentation = createUuidRepresentation(value);
711723
break;
@@ -1455,13 +1467,15 @@ public WriteConcern getWriteConcern() {
14551467
}
14561468

14571469
/**
1458-
* <p>Gets whether writes should be retried if they fail due to a network error</p>
1459-
*
1470+
* Gets whether attempts to execute write commands should be retried if they fail due to a retryable error.
1471+
* See {@link MongoClientSettings.Builder#retryWrites(boolean)} for more information.
1472+
* <p>
14601473
* The name of this method differs from others in this class so as not to conflict with the now removed
14611474
* getRetryWrites() method, which returned a primitive {@code boolean} value, and didn't allow callers to differentiate
14621475
* between a false value and an unset value.
14631476
*
1464-
* @return the retryWrites value, or null if unset
1477+
* @return the {@code retryWrites} value, or {@code null} if unset
1478+
* @see #getMaxAdaptiveRetries()
14651479
* @since 3.9
14661480
* @mongodb.server.release 3.6
14671481
*/
@@ -1471,9 +1485,11 @@ public Boolean getRetryWritesValue() {
14711485
}
14721486

14731487
/**
1474-
* <p>Gets whether reads should be retried if they fail due to a network error</p>
1488+
* Gets whether attempts to execute read commands should be retried if they fail due to a retryable error.
1489+
* See {@link MongoClientSettings.Builder#retryReads(boolean)} for more information.
14751490
*
1476-
* @return the retryWrites value
1491+
* @return the {@code retryReads} value, or {@code null} if unset
1492+
* @see #getMaxAdaptiveRetries()
14771493
* @since 3.11
14781494
* @mongodb.server.release 3.6
14791495
*/
@@ -1482,6 +1498,19 @@ public Boolean getRetryReads() {
14821498
return retryReads;
14831499
}
14841500

1501+
/**
1502+
* Gets the maximum number of retry attempts when encountering a retryable overload error.
1503+
* See {@link MongoClientSettings.Builder#maxAdaptiveRetries(Integer)} for more information.
1504+
*
1505+
* @return The {@code maxAdaptiveRetries} value, or {@code null} if unset.
1506+
* @since 5.7
1507+
*/
1508+
@Beta(Reason.CLIENT)
1509+
@Nullable
1510+
public Integer getMaxAdaptiveRetries() {
1511+
return maxAdaptiveRetries;
1512+
}
1513+
14851514
/**
14861515
* Gets the minimum connection pool size specified in the connection string.
14871516
* @return the minimum connection pool size
@@ -1795,6 +1824,7 @@ public boolean equals(final Object o) {
17951824
&& Objects.equals(writeConcern, that.writeConcern)
17961825
&& Objects.equals(retryWrites, that.retryWrites)
17971826
&& Objects.equals(retryReads, that.retryReads)
1827+
&& Objects.equals(maxAdaptiveRetries, that.maxAdaptiveRetries)
17981828
&& Objects.equals(readConcern, that.readConcern)
17991829
&& Objects.equals(minConnectionPoolSize, that.minConnectionPoolSize)
18001830
&& Objects.equals(maxConnectionPoolSize, that.maxConnectionPoolSize)
@@ -1826,7 +1856,7 @@ public boolean equals(final Object o) {
18261856
@Override
18271857
public int hashCode() {
18281858
return Objects.hash(credential, isSrvProtocol, hosts, database, collection, directConnection, readPreference,
1829-
writeConcern, retryWrites, retryReads, readConcern, minConnectionPoolSize, maxConnectionPoolSize, maxWaitTime,
1859+
writeConcern, retryWrites, retryReads, maxAdaptiveRetries, readConcern, minConnectionPoolSize, maxConnectionPoolSize, maxWaitTime,
18301860
maxConnectionIdleTime, maxConnectionLifeTime, maxConnecting, connectTimeout, timeout, socketTimeout, sslEnabled,
18311861
sslInvalidHostnameAllowed, requiredReplicaSetName, serverSelectionTimeout, localThreshold, heartbeatFrequency,
18321862
serverMonitoringMode, applicationName, compressorList, uuidRepresentation, srvServiceName, srvMaxHosts, proxyHost,

0 commit comments

Comments
 (0)