Skip to content

Commit 0c0a0ac

Browse files
committed
JAVA-2323: Use -1 to explicitly represent no max staleness in the connection string, and stop using 0 to represent it in the ReadPreference API
1 parent 27dafda commit 0c0a0ac

File tree

8 files changed

+146
-185
lines changed

8 files changed

+146
-185
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@
157157
* </li>
158158
* <li>{@code maxStalenessMS=ms}. The maximum staleness in milliseconds. For use with any non-primary read preference, the driver estimates
159159
* the staleness of each secondary, based on lastWriteDate values provided in server isMaster responses, and selects only those secondaries
160-
* whose staleness is less than or equal to maxStalenessMS. The default is 0, meaning there is no staleness check.
160+
* whose staleness is less than or equal to maxStalenessMS. Not providing the parameter or explicitly setting it to -1 indicates that
161+
* there should be no max staleness check.
161162
* </li>
162163
* </ul>
163164
* <p>Authentication configuration:</p>
@@ -447,7 +448,7 @@ private WriteConcern createWriteConcern(final Map<String, List<String>> optionsM
447448
private ReadPreference createReadPreference(final Map<String, List<String>> optionsMap) {
448449
String readPreferenceType = null;
449450
List<TagSet> tagSetList = new ArrayList<TagSet>();
450-
long maxStalenessMS = 0;
451+
long maxStalenessMS = -1;
451452

452453
for (final String key : READ_PREFERENCE_KEYS) {
453454
String value = getLastValue(optionsMap, key);
@@ -602,11 +603,14 @@ private Map<String, List<String>> parseOptions(final String optionsPart) {
602603
private ReadPreference buildReadPreference(final String readPreferenceType,
603604
final List<TagSet> tagSetList, final long maxStalenessMS) {
604605
if (readPreferenceType != null) {
605-
if (tagSetList.isEmpty() && maxStalenessMS == 0) {
606+
if (tagSetList.isEmpty() && maxStalenessMS == -1) {
606607
return ReadPreference.valueOf(readPreferenceType);
608+
} else if (maxStalenessMS == -1) {
609+
return ReadPreference.valueOf(readPreferenceType, tagSetList);
610+
} else {
611+
return ReadPreference.valueOf(readPreferenceType, tagSetList, maxStalenessMS, TimeUnit.MILLISECONDS);
607612
}
608-
return ReadPreference.valueOf(readPreferenceType, tagSetList, maxStalenessMS, TimeUnit.MILLISECONDS);
609-
} else if (!(tagSetList.isEmpty() && maxStalenessMS == 0)) {
613+
} else if (!(tagSetList.isEmpty() && maxStalenessMS == -1)) {
610614
throw new IllegalArgumentException("Read preference mode must be specified if "
611615
+ "either read preference tags or max staleness is specified");
612616
}

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

Lines changed: 52 additions & 52 deletions
Large diffs are not rendered by default.

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

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@
4040
@Immutable
4141
public abstract class TaggableReadPreference extends ReadPreference {
4242
private final List<TagSet> tagSetList = new ArrayList<TagSet>();
43-
private final long maxStalenessMS;
43+
private final Long maxStalenessMS;
4444

4545
TaggableReadPreference() {
46-
maxStalenessMS = 0;
46+
this.maxStalenessMS = null;
4747
}
4848

49-
TaggableReadPreference(final List<TagSet> tagSetList, final long maxStalenessMS) {
49+
TaggableReadPreference(final List<TagSet> tagSetList, final Long maxStaleness, final TimeUnit timeUnit) {
5050
notNull("tagSetList", tagSetList);
51-
isTrueArgument("maxStaleness >= 0", maxStalenessMS >= 0);
52-
this.maxStalenessMS = maxStalenessMS;
51+
isTrueArgument("maxStaleness is null or >= 0", maxStaleness == null || maxStaleness >= 0);
52+
this.maxStalenessMS = maxStaleness == null ? null : MILLISECONDS.convert(maxStaleness, timeUnit);
5353

5454
for (final TagSet tagSet : tagSetList) {
5555
this.tagSetList.add(tagSet);
@@ -69,7 +69,7 @@ public BsonDocument toDocument() {
6969
readPrefObject.put("tags", tagsListToBsonArray());
7070
}
7171

72-
if (maxStalenessMS != 0) {
72+
if (maxStalenessMS != null) {
7373
readPrefObject.put("maxStalenessMS", new BsonInt64(maxStalenessMS));
7474
}
7575
return readPrefObject;
@@ -89,13 +89,16 @@ public List<TagSet> getTagSetList() {
8989
* Gets the maximum acceptable staleness of a secondary in order to be considered for read operations.
9090
*
9191
* @param timeUnit the time unit in which to return the value
92-
* @return the maximum acceptable staleness in the given time unit. The default is 0, meaning there is no staleness check.
92+
* @return the maximum acceptable staleness in the given time unit, or null if the value is not set
9393
*
9494
* @since 3.4
9595
* @mongodb.server.release 3.4
9696
*/
97-
public long getMaxStaleness(final TimeUnit timeUnit) {
97+
public Long getMaxStaleness(final TimeUnit timeUnit) {
9898
notNull("timeUnit", timeUnit);
99+
if (maxStalenessMS == null) {
100+
return null;
101+
}
99102
return timeUnit.convert(maxStalenessMS, TimeUnit.MILLISECONDS);
100103
}
101104

@@ -104,7 +107,7 @@ public String toString() {
104107
return "ReadPreference{"
105108
+ "name=" + getName()
106109
+ (tagSetList.isEmpty() ? "" : ", tagSetList=" + tagSetList)
107-
+ (maxStalenessMS == 0 ? "" : ", maxStalenessMS=" + maxStalenessMS)
110+
+ (maxStalenessMS == null ? "" : ", maxStalenessMS=" + maxStalenessMS)
108111
+ '}';
109112
}
110113

@@ -119,7 +122,7 @@ public boolean equals(final Object o) {
119122

120123
TaggableReadPreference that = (TaggableReadPreference) o;
121124

122-
if (maxStalenessMS != that.maxStalenessMS) {
125+
if (maxStalenessMS != null ? !maxStalenessMS.equals(that.maxStalenessMS) : that.maxStalenessMS != null) {
123126
return false;
124127
}
125128
if (!tagSetList.equals(that.tagSetList)) {
@@ -133,7 +136,7 @@ public boolean equals(final Object o) {
133136
public int hashCode() {
134137
int result = tagSetList.hashCode();
135138
result = 31 * result + getName().hashCode();
136-
result = 31 * result + (int) (maxStalenessMS ^ (maxStalenessMS >>> 32));
139+
result = 31 * result + (maxStalenessMS != null ? maxStalenessMS.hashCode() : 0);
137140
return result;
138141
}
139142

@@ -154,7 +157,7 @@ protected static ClusterDescription copyClusterDescription(final ClusterDescript
154157

155158
protected List<ServerDescription> selectFreshServers(final ClusterDescription clusterDescription,
156159
final List<ServerDescription> servers) {
157-
if (getMaxStaleness(MILLISECONDS) == 0) {
160+
if (getMaxStaleness(MILLISECONDS) == null) {
158161
return servers;
159162
}
160163

@@ -248,8 +251,8 @@ static class SecondaryReadPreference extends TaggableReadPreference {
248251
SecondaryReadPreference() {
249252
}
250253

251-
SecondaryReadPreference(final List<TagSet> tagSetList, final long maxStalenessMS) {
252-
super(tagSetList, maxStalenessMS);
254+
SecondaryReadPreference(final List<TagSet> tagSetList, final Long maxStaleness, final TimeUnit timeUnit) {
255+
super(tagSetList, maxStaleness, timeUnit);
253256
}
254257

255258
@Override
@@ -283,8 +286,8 @@ static class SecondaryPreferredReadPreference extends SecondaryReadPreference {
283286
SecondaryPreferredReadPreference() {
284287
}
285288

286-
SecondaryPreferredReadPreference(final List<TagSet> tagSetList, final long maxStalenessMS) {
287-
super(tagSetList, maxStalenessMS);
289+
SecondaryPreferredReadPreference(final List<TagSet> tagSetList, final Long maxStaleness, final TimeUnit timeUnit) {
290+
super(tagSetList, maxStaleness, timeUnit);
288291
}
289292

290293
@Override
@@ -310,8 +313,8 @@ static class NearestReadPreference extends TaggableReadPreference {
310313
NearestReadPreference() {
311314
}
312315

313-
NearestReadPreference(final List<TagSet> tagSetList, final long maxStalenessMS) {
314-
super(tagSetList, maxStalenessMS);
316+
NearestReadPreference(final List<TagSet> tagSetList, final Long maxStaleness, final TimeUnit timeUnit) {
317+
super(tagSetList, maxStaleness, timeUnit);
315318
}
316319

317320

@@ -347,8 +350,8 @@ static class PrimaryPreferredReadPreference extends SecondaryReadPreference {
347350
PrimaryPreferredReadPreference() {
348351
}
349352

350-
PrimaryPreferredReadPreference(final List<TagSet> tagSetList, final long maxStalenessMS) {
351-
super(tagSetList, maxStalenessMS);
353+
PrimaryPreferredReadPreference(final List<TagSet> tagSetList, final Long maxStaleness, final TimeUnit timeUnit) {
354+
super(tagSetList, maxStaleness, timeUnit);
352355
}
353356

354357
@Override

driver-core/src/test/resources/max-staleness/server_selection/ReplicaSetWithPrimary/ZeroMaxStaleness.json

Lines changed: 0 additions & 75 deletions
This file was deleted.

driver-core/src/test/unit/com/mongodb/ConnectionStringSpecification.groovy

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,12 @@ class ConnectionStringSpecification extends Specification {
316316
new ConnectionString('mongodb://localhost/' +
317317
'?readPreference=secondary' +
318318
'&maxStalenessMS=1') | secondary(1, MILLISECONDS)
319+
new ConnectionString('mongodb://localhost/' +
320+
'?readPreference=secondary' +
321+
'&maxStalenessMS=0') | secondary(0, MILLISECONDS)
322+
new ConnectionString('mongodb://localhost/' +
323+
'?readPreference=secondary' +
324+
'&maxStalenessMS=-1') | secondary()
319325
}
320326

321327
@Unroll
@@ -379,6 +385,7 @@ class ConnectionStringSpecification extends Specification {
379385
def 'should be not equal to another ConnectionString with the different string values'() {
380386
expect:
381387
uri1 != uri2
388+
uri1.hashCode() != uri2.hashCode()
382389

383390
where:
384391
uri1 | uri2

0 commit comments

Comments
 (0)