Skip to content

Commit d5ec428

Browse files
committed
JAVA-2389: Allow smallest max staleness check to proceed if cluster type is replica set, even when there are no available servers
1 parent c8d013a commit d5ec428

File tree

5 files changed

+85
-24
lines changed

5 files changed

+85
-24
lines changed

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

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import com.mongodb.connection.ClusterDescription;
2121
import com.mongodb.connection.ClusterType;
2222
import com.mongodb.connection.ServerDescription;
23-
import com.mongodb.connection.ServerType;
2423
import org.bson.BsonArray;
2524
import org.bson.BsonDocument;
2625
import org.bson.BsonInt64;
@@ -35,7 +34,6 @@
3534
import static com.mongodb.assertions.Assertions.notNull;
3635
import static java.lang.String.format;
3736
import static java.util.concurrent.TimeUnit.MILLISECONDS;
38-
import static java.util.concurrent.TimeUnit.NANOSECONDS;
3937
import static java.util.concurrent.TimeUnit.SECONDS;
4038

4139
/**
@@ -186,10 +184,6 @@ protected List<ServerDescription> selectFreshServers(final ClusterDescription cl
186184
return servers;
187185
}
188186

189-
if (servers.isEmpty()) {
190-
return servers;
191-
}
192-
193187
long heartbeatFrequencyMS = clusterDescription.getServerSettings().getHeartbeatFrequency(MILLISECONDS);
194188

195189
if (getMaxStaleness(MILLISECONDS) < Math.max(SMALLEST_MAX_STALENESS_MS, heartbeatFrequencyMS + IDLE_WRITE_PERIOD_MS)) {
@@ -229,22 +223,6 @@ protected List<ServerDescription> selectFreshServers(final ClusterDescription cl
229223
return freshServers;
230224
}
231225

232-
private ServerDescription getMostUpToDateServerDescription(final ClusterDescription clusterDescription) {
233-
ServerDescription mostUpToDateServerDescription = null;
234-
for (ServerDescription cur : clusterDescription.getServerDescriptions()) {
235-
if (cur.getType() == ServerType.REPLICA_SET_PRIMARY) {
236-
mostUpToDateServerDescription = cur;
237-
break;
238-
} else if (cur.getType() == ServerType.REPLICA_SET_SECONDARY) {
239-
if (mostUpToDateServerDescription == null
240-
|| cur.getLastUpdateTime(NANOSECONDS) > mostUpToDateServerDescription.getLastUpdateTime(NANOSECONDS)) {
241-
mostUpToDateServerDescription = cur;
242-
}
243-
}
244-
}
245-
return mostUpToDateServerDescription;
246-
}
247-
248226
private long getStalenessOfSecondaryRelativeToPrimary(final ServerDescription primary, final ServerDescription serverDescription,
249227
final long heartbeatFrequencyMS) {
250228
return primary.getLastWriteDate().getTime()

driver-core/src/test/resources/max-staleness/server_selection/ReplicaSetNoPrimary/NoKnownServers.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
{
2-
"in_latency_window": [],
2+
"error": true,
33
"read_preference": {
44
"maxStalenessSeconds": 1,
55
"mode": "Nearest"
66
},
7-
"suitable_servers": [],
87
"topology_description": {
98
"servers": [
109
{
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"error": true,
3+
"read_preference": {
4+
"maxStalenessSeconds": 0,
5+
"mode": "Nearest"
6+
},
7+
"topology_description": {
8+
"servers": [
9+
{
10+
"address": "a:27017",
11+
"avg_rtt_ms": 5,
12+
"lastUpdateTime": 0,
13+
"lastWrite": {
14+
"lastWriteDate": {
15+
"$numberLong": "2"
16+
}
17+
},
18+
"maxWireVersion": 5,
19+
"type": "RSSecondary"
20+
},
21+
{
22+
"address": "b:27017",
23+
"avg_rtt_ms": 5,
24+
"lastUpdateTime": 0,
25+
"lastWrite": {
26+
"lastWriteDate": {
27+
"$numberLong": "1"
28+
}
29+
},
30+
"maxWireVersion": 4,
31+
"type": "RSSecondary"
32+
}
33+
],
34+
"type": "ReplicaSetNoPrimary"
35+
}
36+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@
1818
},
1919
"maxWireVersion": 5,
2020
"type": "RSPrimary"
21+
},
22+
{
23+
"address": "b:27017",
24+
"avg_rtt_ms": 5,
25+
"lastUpdateTime": 0,
26+
"lastWrite": {
27+
"lastWriteDate": {
28+
"$numberLong": "1"
29+
}
30+
},
31+
"maxWireVersion": 5,
32+
"type": "RSSecondary"
2133
}
2234
],
2335
"type": "ReplicaSetWithPrimary"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"error": true,
3+
"read_preference": {
4+
"maxStalenessSeconds": 0,
5+
"mode": "Nearest"
6+
},
7+
"topology_description": {
8+
"servers": [
9+
{
10+
"address": "a:27017",
11+
"avg_rtt_ms": 5,
12+
"lastUpdateTime": 0,
13+
"lastWrite": {
14+
"lastWriteDate": {
15+
"$numberLong": "2"
16+
}
17+
},
18+
"maxWireVersion": 5,
19+
"type": "RSPrimary"
20+
},
21+
{
22+
"address": "b:27017",
23+
"avg_rtt_ms": 5,
24+
"lastUpdateTime": 0,
25+
"lastWrite": {
26+
"lastWriteDate": {
27+
"$numberLong": "1"
28+
}
29+
},
30+
"maxWireVersion": 4,
31+
"type": "RSSecondary"
32+
}
33+
],
34+
"type": "ReplicaSetWithPrimary"
35+
}
36+
}

0 commit comments

Comments
 (0)