Skip to content

Commit c8a7c84

Browse files
committed
Added MongoClientOptions.localThreshold and deprecate MongoClientOptions.acceptableLatencyDifference
JAVA-1610
1 parent e3c4886 commit c8a7c84

File tree

2 files changed

+66
-8
lines changed

2 files changed

+66
-8
lines changed

src/main/com/mongodb/MongoClientOptions.java

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static class Builder {
6262
private int heartbeatConnectTimeout = Integer.parseInt(System.getProperty("com.mongodb.updaterConnectTimeoutMS", "20000"));
6363
private int heartbeatSocketTimeout = Integer.parseInt(System.getProperty("com.mongodb.updaterSocketTimeoutMS", "20000"));
6464
private int heartbeatThreadCount;
65-
private int acceptableLatencyDifference = Integer.parseInt(System.getProperty("com.mongodb.slaveAcceptableLatencyMS", "15"));
65+
private int localThreshold = Integer.parseInt(System.getProperty("com.mongodb.slaveAcceptableLatencyMS", "15"));
6666
private String requiredReplicaSetName;
6767

6868
/**
@@ -167,6 +167,24 @@ public Builder heartbeatThreadCount(final int heartbeatThreadCount) {
167167
return this;
168168
}
169169

170+
/**
171+
* Sets the local threshold.
172+
*
173+
* @param localThreshold the threshold in milliseconds
174+
* @return {@code this}
175+
* @throws IllegalArgumentException if localThreshold < 0
176+
* @see com.mongodb.MongoClientOptions#getLocalThreshold()
177+
* @since 2.13.0
178+
*/
179+
@Deprecated
180+
public Builder localThreshold(final int localThreshold) {
181+
if (localThreshold < 0) {
182+
throw new IllegalArgumentException("localThreshold must be greater than or equal to 0");
183+
}
184+
this.localThreshold = localThreshold;
185+
return this;
186+
}
187+
170188
/**
171189
* Sets the acceptable latency difference.
172190
*
@@ -175,12 +193,14 @@ public Builder heartbeatThreadCount(final int heartbeatThreadCount) {
175193
* @throws IllegalArgumentException if acceptableLatencyDifference &lt; 0
176194
* @see com.mongodb.MongoClientOptions#getAcceptableLatencyDifference()
177195
* @since 2.12.0
196+
* @deprecated Use {@link #localThreshold(int)}
178197
*/
198+
@Deprecated
179199
public Builder acceptableLatencyDifference(final int acceptableLatencyDifference) {
180200
if (acceptableLatencyDifference < 0) {
181201
throw new IllegalArgumentException("acceptableLatencyDifference must be greater than or equal to 0");
182202
}
183-
this.acceptableLatencyDifference = acceptableLatencyDifference;
203+
this.localThreshold = acceptableLatencyDifference;
184204
return this;
185205
}
186206

@@ -808,6 +828,27 @@ public int getHeartbeatThreadCount() {
808828
return heartbeatThreadCount;
809829
}
810830

831+
/**
832+
* <p>Gets the local threshold. When choosing among multiple MongoDB servers to send a request, the MongoClient will only
833+
* send that request to a server whose ping time is less than or equal to the server with the fastest ping time plus the local
834+
* threshold.</p>
835+
*
836+
* <p>For example, let's say that the client is choosing a server to send a query when the read preference is {@code
837+
* ReadPreference.secondary()}, and that there are three secondaries, server1, server2, and server3, whose ping times are 10, 15, and 16
838+
* milliseconds, respectively. With a local threshold of 5 milliseconds, the client will send the query to either
839+
* server1 or server2 (randomly selecting between the two).
840+
* </p>
841+
*
842+
* <p> The default value is 15 milliseconds.</p>
843+
*
844+
* @return the local threshold, in milliseconds
845+
* @since 2.13.0
846+
* @mongodb.driver.manual reference/program/mongos/#cmdoption--localThreshold Local Threshold
847+
*/
848+
public int getLocalThreshold() {
849+
return localThreshold;
850+
}
851+
811852
/**
812853
* <p>Gets the acceptable latency difference. When choosing among multiple MongoDB servers to send a request, the MongoClient will only
813854
* send that request to a server whose ping time is less than or equal to the server with the fastest ping time plus the acceptable
@@ -820,9 +861,11 @@ public int getHeartbeatThreadCount() {
820861
*
821862
* @return the acceptable latency difference, in milliseconds
822863
* @since 2.12.0
864+
* @deprecated Use {@link MongoClientOptions#getLocalThreshold()} instead
823865
*/
866+
@Deprecated
824867
public int getAcceptableLatencyDifference() {
825-
return acceptableLatencyDifference;
868+
return localThreshold;
826869
}
827870

828871
/**
@@ -850,7 +893,7 @@ public boolean equals(final Object o) {
850893

851894
MongoClientOptions that = (MongoClientOptions) o;
852895

853-
if (acceptableLatencyDifference != that.acceptableLatencyDifference) {
896+
if (localThreshold != that.localThreshold) {
854897
return false;
855898
}
856899
if (alwaysUseMBeans != that.alwaysUseMBeans) {
@@ -959,7 +1002,7 @@ public int hashCode() {
9591002
result = 31 * result + heartbeatConnectTimeout;
9601003
result = 31 * result + heartbeatSocketTimeout;
9611004
result = 31 * result + heartbeatThreadCount;
962-
result = 31 * result + acceptableLatencyDifference;
1005+
result = 31 * result + localThreshold;
9631006
result = 31 * result + (requiredReplicaSetName != null ? requiredReplicaSetName.hashCode() : 0);
9641007
return result;
9651008
}
@@ -988,7 +1031,7 @@ public String toString() {
9881031
+ ", heartbeatConnectTimeout=" + heartbeatConnectTimeout
9891032
+ ", heartbeatSocketTimeout=" + heartbeatSocketTimeout
9901033
+ ", heartbeatThreadCount=" + heartbeatThreadCount
991-
+ ", acceptableLatencyDifference=" + acceptableLatencyDifference
1034+
+ ", localThreshold=" + localThreshold
9921035
+ ", requiredReplicaSetName=" + requiredReplicaSetName
9931036
+ '}';
9941037
}
@@ -1018,7 +1061,7 @@ private MongoClientOptions(final Builder builder) {
10181061
heartbeatConnectTimeout = builder.heartbeatConnectTimeout;
10191062
heartbeatSocketTimeout = builder.heartbeatSocketTimeout;
10201063
heartbeatThreadCount = builder.heartbeatThreadCount;
1021-
acceptableLatencyDifference = builder.acceptableLatencyDifference;
1064+
localThreshold = builder.localThreshold;
10221065
requiredReplicaSetName = builder.requiredReplicaSetName;
10231066
}
10241067

@@ -1047,6 +1090,6 @@ private MongoClientOptions(final Builder builder) {
10471090
private final int heartbeatConnectTimeout;
10481091
private final int heartbeatSocketTimeout;
10491092
private final int heartbeatThreadCount;
1050-
private final int acceptableLatencyDifference;
1093+
private final int localThreshold;
10511094
private final String requiredReplicaSetName;
10521095
}

src/test/com/mongodb/MongoClientOptionsTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ public void testBuilderDefaults() {
6262
assertEquals(getProperty("com.mongodb.slaveAcceptableLatencyMS") != null
6363
? parseInt(getProperty("com.mongodb.slaveAcceptableLatencyMS")) : 15,
6464
options.getAcceptableLatencyDifference());
65+
assertEquals(getProperty("com.mongodb.slaveAcceptableLatencyMS") != null
66+
? parseInt(getProperty("com.mongodb.slaveAcceptableLatencyMS")) : 15,
67+
options.getLocalThreshold());
6568
assertNull(options.getRequiredReplicaSetName());
6669
}
6770

@@ -80,6 +83,7 @@ public void testSystemProperties() {
8083
assertEquals(options.getHeartbeatConnectTimeout(), 30000);
8184
assertEquals(options.getHeartbeatSocketTimeout(), 40000);
8285
assertEquals(options.getAcceptableLatencyDifference(), 25);
86+
assertEquals(options.getLocalThreshold(), 25);
8387
} finally {
8488
System.setProperty("com.mongodb.updaterIntervalMS", "5000");
8589
System.setProperty("com.mongodb.updaterIntervalNoMasterMS", "10");
@@ -195,6 +199,12 @@ public void testIllegalArguments() {
195199
} catch (IllegalArgumentException e) {
196200
// all good
197201
}
202+
try {
203+
builder.localThreshold(-1);
204+
fail("should not get here");
205+
} catch (IllegalArgumentException e) {
206+
// all good
207+
}
198208

199209
try {
200210
builder.maxConnectionIdleTime(-1);
@@ -271,6 +281,7 @@ public DBDecoder create() {
271281
assertEquals(true, options.isCursorFinalizerEnabled());
272282
assertEquals(true, options.isAlwaysUseMBeans());
273283
assertEquals(41, options.getAcceptableLatencyDifference());
284+
assertEquals(41, options.getLocalThreshold());
274285
assertEquals(51, options.getHeartbeatFrequency());
275286
assertEquals(62, options.getHeartbeatConnectRetryFrequency());
276287
assertEquals(62, options.getMinHeartbeatFrequency());
@@ -282,6 +293,10 @@ public DBDecoder create() {
282293
assertEquals(encoderFactory, options.getDbEncoderFactory());
283294
assertEquals(decoderFactory, options.getDbDecoderFactory());
284295
assertEquals("test", options.getRequiredReplicaSetName());
296+
297+
options = MongoClientOptions.builder().localThreshold(41).build();
298+
assertEquals(41, options.getLocalThreshold());
299+
assertEquals(41, options.getAcceptableLatencyDifference());
285300
}
286301

287302
@Test

0 commit comments

Comments
 (0)