Skip to content

Commit bef1d23

Browse files
committed
Add tests and javadoc.
1 parent a69ed65 commit bef1d23

6 files changed

Lines changed: 70 additions & 21 deletions

File tree

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,10 +1494,11 @@ public Boolean getRetryReads() {
14941494
/**
14951495
* Gets whether overload retargeting is enabled.
14961496
*
1497-
* <p>When enabled, the driver may route a request to a different server on a subsequent retry attempt
1498-
* if the previously used server is overloaded. This applies
1499-
* to reads when retryReads is enabled, and to writes when retryWrites is enabled. Enabling this setting
1500-
* generally improves availability during server overload.</p>
1497+
* <p>When enabled, the previously selected servers on which attempts failed with an error
1498+
* {@linkplain MongoException#hasErrorLabel(String) having}
1499+
* the {@value MongoException#SYSTEM_OVERLOADED_ERROR_LABEL} label may be deprioritized during
1500+
* server selection on subsequent retry attempts. This applies to reads when retryReads is enabled,
1501+
* and to writes when retryWrites is enabled.</p>
15011502
*
15021503
* <p>This setting does not take effect for
15031504
* {@linkplain com.mongodb.connection.ClusterType#SHARDED sharded clusters}.</p>

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

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -467,11 +467,12 @@ public Builder retryReads(final boolean retryReads) {
467467
/**
468468
* Sets whether to enable overload retargeting.
469469
*
470-
* <p>When enabled, the driver may route a request to a different server on a subsequent retry attempt
471-
* if the previously used server is overloaded. This applies
472-
* to reads when {@linkplain #retryReads(boolean) retryReads} is enabled, and to writes when
473-
* {@linkplain #retryWrites(boolean) retryWrites} is enabled. Enabling this setting generally
474-
* improves availability during server overload.</p>
470+
* <p>When enabled, the previously selected servers on which attempts failed with an error
471+
* {@linkplain MongoException#hasErrorLabel(String) having}
472+
* the {@value MongoException#SYSTEM_OVERLOADED_ERROR_LABEL} label may be deprioritized during
473+
* server selection on subsequent retry attempts. This applies to reads when
474+
* {@linkplain #retryReads(boolean) retryReads} is enabled, and to writes when
475+
* {@linkplain #retryWrites(boolean) retryWrites} is enabled.</p>
475476
*
476477
* <p>This setting does not take effect for {@linkplain ClusterType#SHARDED sharded clusters}.</p>
477478
*
@@ -840,16 +841,7 @@ public boolean getRetryReads() {
840841

841842
/**
842843
* Returns whether overload retargeting is enabled.
843-
*
844-
* <p>When enabled, the driver may route a request to a different server on a subsequent retry attempt
845-
* if the previously used server is overloaded. This applies
846-
* to reads when {@linkplain Builder#retryReads(boolean) retryReads} is enabled, and to writes when
847-
* {@linkplain Builder#retryWrites(boolean) retryWrites} is enabled. Enabling this setting generally
848-
* improves availability during server overload.</p>
849-
*
850-
* <p>This setting does not take effect for {@linkplain ClusterType#SHARDED sharded clusters}.</p>
851-
*
852-
* <p>Defaults to {@code false}.</p>
844+
* See {@link Builder#enableOverloadRetargeting(boolean)} for more information.
853845
*
854846
* @return the enableOverloadRetargeting value
855847
* @see Builder#enableOverloadRetargeting(boolean)

driver-legacy/src/main/com/mongodb/MongoClientOptions.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,18 @@ public boolean getRetryReads() {
465465
return wrapped.getRetryReads();
466466
}
467467

468+
/**
469+
* Returns whether overload retargeting is enabled.
470+
* See {@link MongoClientSettings.Builder#enableOverloadRetargeting(boolean)} for more information.
471+
*
472+
* @return the enableOverloadRetargeting value
473+
* @see MongoClientSettings.Builder#enableOverloadRetargeting(boolean)
474+
* @since 5.7
475+
*/
476+
public boolean getEnableOverloadRetargeting() {
477+
return wrapped.getEnableOverloadRetargeting();
478+
}
479+
468480
/**
469481
* <p>The read concern to use.</p>
470482
*
@@ -1049,6 +1061,20 @@ public Builder retryReads(final boolean retryReads) {
10491061
return this;
10501062
}
10511063

1064+
/**
1065+
* Sets whether to enable overload retargeting.
1066+
* See {@link MongoClientSettings.Builder#enableOverloadRetargeting(boolean)} for more information.
1067+
*
1068+
* @param enableOverloadRetargeting whether to enable overload retargeting
1069+
* @return {@code this}
1070+
* @see #getEnableOverloadRetargeting()
1071+
* @since 5.7
1072+
*/
1073+
public Builder enableOverloadRetargeting(final boolean enableOverloadRetargeting) {
1074+
wrapped.enableOverloadRetargeting(enableOverloadRetargeting);
1075+
return this;
1076+
}
1077+
10521078
/**
10531079
* Sets the read concern.
10541080
*

driver-legacy/src/main/com/mongodb/MongoClientURI.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@
151151
* Defaults to false.</li>
152152
* <li>{@code retryReads=true|false}. If true the driver will retry supported read operations if they fail due to a network error.
153153
* Defaults to false.</li>
154+
* <li>{@code enableOverloadRetargeting=true|false}. If true the driver may route a request to a different server on a subsequent
155+
* retry attempt if the previously used server is overloaded. Does not take effect for
156+
* {@linkplain com.mongodb.connection.ClusterType#SHARDED sharded clusters}. Defaults to false.</li>
154157
* </ul>
155158
*
156159
*
@@ -218,6 +221,9 @@
218221
* Defaults to true.</li>
219222
* <li>{@code retryReads=true|false}. If true the driver will retry supported read operations if they fail due to a network error.
220223
* Defaults to true.</li>
224+
* <li>{@code enableOverloadRetargeting=true|false}. If true the driver may route a request to a different server on a subsequent
225+
* retry attempt if the previously used server is overloaded. Does not take effect for
226+
* {@linkplain com.mongodb.connection.ClusterType#SHARDED sharded clusters}. Defaults to false.</li>
221227
* <li>{@code uuidRepresentation=unspecified|standard|javaLegacy|csharpLegacy|pythonLegacy}. See
222228
* {@link MongoClientOptions#getUuidRepresentation()} for documentation of semantics of this parameter. Defaults to "javaLegacy", but
223229
* will change to "unspecified" in the next major release.</li>
@@ -387,6 +393,11 @@ public MongoClientOptions getOptions() {
387393
builder.retryReads(retryReads);
388394
}
389395

396+
Boolean enableOverloadRetargeting = proxied.getEnableOverloadRetargeting();
397+
if (enableOverloadRetargeting != null) {
398+
builder.enableOverloadRetargeting(enableOverloadRetargeting);
399+
}
400+
390401
Integer maxConnectionPoolSize = proxied.getMaxConnectionPoolSize();
391402
if (maxConnectionPoolSize != null) {
392403
builder.connectionsPerHost(maxConnectionPoolSize);

driver-legacy/src/test/unit/com/mongodb/MongoClientOptionsSpecification.groovy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class MongoClientOptionsSpecification extends Specification {
4646
options.getWriteConcern() == WriteConcern.ACKNOWLEDGED
4747
options.getRetryWrites()
4848
options.getRetryReads()
49+
!options.getEnableOverloadRetargeting()
4950
options.getCodecRegistry() == MongoClientSettings.defaultCodecRegistry
5051
options.getUuidRepresentation() == UuidRepresentation.UNSPECIFIED
5152
options.getMinConnectionsPerHost() == 0
@@ -116,6 +117,7 @@ class MongoClientOptionsSpecification extends Specification {
116117
.readPreference(ReadPreference.secondary())
117118
.retryWrites(true)
118119
.retryReads(false)
120+
.enableOverloadRetargeting(true)
119121
.writeConcern(WriteConcern.JOURNALED)
120122
.readConcern(ReadConcern.MAJORITY)
121123
.minConnectionsPerHost(30)
@@ -162,6 +164,7 @@ class MongoClientOptionsSpecification extends Specification {
162164
options.getServerSelector() == serverSelector
163165
options.getRetryWrites()
164166
!options.getRetryReads()
167+
options.getEnableOverloadRetargeting()
165168
options.getServerSelectionTimeout() == 150
166169
options.getTimeout() == 10_000
167170
options.getMaxWaitTime() == 200
@@ -318,6 +321,7 @@ class MongoClientOptionsSpecification extends Specification {
318321
.applicationName('appName')
319322
.readPreference(ReadPreference.secondary())
320323
.retryReads(true)
324+
.enableOverloadRetargeting(true)
321325
.uuidRepresentation(UuidRepresentation.STANDARD)
322326
.writeConcern(WriteConcern.JOURNALED)
323327
.minConnectionsPerHost(30)
@@ -619,6 +623,7 @@ class MongoClientOptionsSpecification extends Specification {
619623
.writeConcern(WriteConcern.JOURNALED)
620624
.retryWrites(true)
621625
.retryReads(true)
626+
.enableOverloadRetargeting(true)
622627
.uuidRepresentation(UuidRepresentation.STANDARD)
623628
.minConnectionsPerHost(30)
624629
.connectionsPerHost(500)

driver-legacy/src/test/unit/com/mongodb/MongoClientURISpecification.groovy

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ class MongoClientURISpecification extends Specification {
131131
+ 'heartbeatFrequencyMS=20000&'
132132
+ 'retryWrites=true&'
133133
+ 'retryReads=true&'
134+
+ 'enableOverloadRetargeting=true&'
134135
+ 'uuidRepresentation=csharpLegacy&'
135136
+ 'appName=app1&'
136137
+ 'timeoutMS=10000')
@@ -158,6 +159,7 @@ class MongoClientURISpecification extends Specification {
158159
options.getHeartbeatFrequency() == 20000
159160
options.getRetryWrites()
160161
options.getRetryReads()
162+
options.getEnableOverloadRetargeting()
161163
options.getUuidRepresentation() == UuidRepresentation.C_SHARP_LEGACY
162164
options.getApplicationName() == 'app1'
163165
}
@@ -178,6 +180,7 @@ class MongoClientURISpecification extends Specification {
178180
!options.isSslEnabled()
179181
options.getRetryWrites()
180182
options.getRetryReads()
183+
!options.getEnableOverloadRetargeting()
181184
options.getUuidRepresentation() == UuidRepresentation.UNSPECIFIED
182185
}
183186

@@ -188,6 +191,7 @@ class MongoClientURISpecification extends Specification {
188191
.readPreference(ReadPreference.secondary())
189192
.retryWrites(true)
190193
.retryReads(true)
194+
.enableOverloadRetargeting(true)
191195
.writeConcern(WriteConcern.JOURNALED)
192196
.minConnectionsPerHost(30)
193197
.connectionsPerHost(500)
@@ -220,6 +224,7 @@ class MongoClientURISpecification extends Specification {
220224
options.getWriteConcern() == WriteConcern.JOURNALED
221225
options.getRetryWrites()
222226
options.getRetryReads()
227+
options.getEnableOverloadRetargeting()
223228
options.getTimeout() == 10_000
224229
options.getServerSelectionTimeout() == 150
225230
options.getMaxWaitTime() == 200
@@ -314,24 +319,33 @@ class MongoClientURISpecification extends Specification {
314319

315320
def 'should respect MongoClientOptions builder'() {
316321
given:
317-
def uri = new MongoClientURI('mongodb://localhost/', MongoClientOptions.builder().connectionsPerHost(200))
322+
def uri = new MongoClientURI('mongodb://localhost/', MongoClientOptions.builder()
323+
.connectionsPerHost(200)
324+
.enableOverloadRetargeting(true))
318325

319326
when:
320327
def options = uri.getOptions()
321328

322329
then:
323330
options.getConnectionsPerHost() == 200
331+
options.getEnableOverloadRetargeting()
324332
}
325333

326334
def 'should override MongoClientOptions builder'() {
327335
given:
328-
def uri = new MongoClientURI('mongodb://localhost/?maxPoolSize=250', MongoClientOptions.builder().connectionsPerHost(200))
336+
def uri = new MongoClientURI('mongodb://localhost/?'
337+
+ 'maxPoolSize=250'
338+
+ '&enableOverloadRetargeting=false',
339+
MongoClientOptions.builder()
340+
.connectionsPerHost(200)
341+
.enableOverloadRetargeting(true))
329342

330343
when:
331344
def options = uri.getOptions()
332345

333346
then:
334347
options.getConnectionsPerHost() == 250
348+
!options.getEnableOverloadRetargeting()
335349
}
336350

337351
def 'should be equal to another MongoClientURI with the same string values'() {

0 commit comments

Comments
 (0)