Skip to content

Commit cdd3af9

Browse files
committed
Implement the serverMonitoringMode logic
This change is in accordance with source/server-discovery-and-monitoring/server-monitoring.rst. JAVA-4936
1 parent b139199 commit cdd3af9

File tree

11 files changed

+695
-9
lines changed

11 files changed

+695
-9
lines changed

driver-core/src/main/com/mongodb/internal/connection/DefaultClusterFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public Cluster createCluster(final ClusterSettings originalClusterSettings, fina
110110
connectionPoolSettings, internalConnectionPoolSettings,
111111
streamFactory, heartbeatStreamFactory, credential, loggerSettings, commandListener, applicationName,
112112
mongoDriverInformation != null ? mongoDriverInformation : MongoDriverInformation.builder().build(), compressorList,
113-
serverApi);
113+
serverApi, FaasEnvironment.getFaasEnvironment() != FaasEnvironment.UNKNOWN);
114114

115115
if (clusterSettings.getMode() == ClusterConnectionMode.SINGLE) {
116116
return new SingleServerCluster(clusterId, clusterSettings, serverFactory);

driver-core/src/main/com/mongodb/internal/connection/DefaultClusterableServerFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public class DefaultClusterableServerFactory implements ClusterableServerFactory
5353
private final List<MongoCompressor> compressorList;
5454
@Nullable
5555
private final ServerApi serverApi;
56+
private final boolean faas;
5657

5758
public DefaultClusterableServerFactory(
5859
final ServerSettings serverSettings, final ConnectionPoolSettings connectionPoolSettings,
@@ -62,7 +63,7 @@ public DefaultClusterableServerFactory(
6263
final LoggerSettings loggerSettings,
6364
@Nullable final CommandListener commandListener,
6465
@Nullable final String applicationName, @Nullable final MongoDriverInformation mongoDriverInformation,
65-
final List<MongoCompressor> compressorList, @Nullable final ServerApi serverApi) {
66+
final List<MongoCompressor> compressorList, @Nullable final ServerApi serverApi, final boolean faas) {
6667
this.serverSettings = serverSettings;
6768
this.connectionPoolSettings = connectionPoolSettings;
6869
this.internalConnectionPoolSettings = internalConnectionPoolSettings;
@@ -75,6 +76,7 @@ public DefaultClusterableServerFactory(
7576
this.mongoDriverInformation = mongoDriverInformation;
7677
this.compressorList = compressorList;
7778
this.serverApi = serverApi;
79+
this.faas = faas;
7880
}
7981

8082
@Override
@@ -86,7 +88,7 @@ public ClusterableServer create(final Cluster cluster, final ServerAddress serve
8688
// no credentials, compressor list, or command listener for the server monitor factory
8789
new InternalStreamConnectionFactory(clusterMode, true, heartbeatStreamFactory, null, applicationName,
8890
mongoDriverInformation, emptyList(), loggerSettings, null, serverApi),
89-
clusterMode, serverApi, sdamProvider);
91+
clusterMode, serverApi, faas, sdamProvider);
9092
ConnectionPool connectionPool = new DefaultConnectionPool(serverId,
9193
new InternalStreamConnectionFactory(clusterMode, streamFactory, credential, applicationName,
9294
mongoDriverInformation, compressorList, loggerSettings, commandListener, serverApi),

driver-core/src/main/com/mongodb/internal/connection/DefaultServerMonitor.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import static com.mongodb.MongoNamespace.COMMAND_COLLECTION_NAME;
5151
import static com.mongodb.ReadPreference.primary;
5252
import static com.mongodb.assertions.Assertions.assertNotNull;
53+
import static com.mongodb.assertions.Assertions.fail;
5354
import static com.mongodb.assertions.Assertions.notNull;
5455
import static com.mongodb.connection.ServerType.UNKNOWN;
5556
import static com.mongodb.internal.Locks.checkedWithLock;
@@ -76,6 +77,7 @@ class DefaultServerMonitor implements ServerMonitor {
7677
private final ClusterConnectionMode clusterConnectionMode;
7778
@Nullable
7879
private final ServerApi serverApi;
80+
private final boolean faas;
7981
private final ServerSettings serverSettings;
8082
private final ServerMonitorRunnable monitor;
8183
private final Thread monitorThread;
@@ -90,13 +92,15 @@ class DefaultServerMonitor implements ServerMonitor {
9092
final InternalConnectionFactory internalConnectionFactory,
9193
final ClusterConnectionMode clusterConnectionMode,
9294
@Nullable final ServerApi serverApi,
95+
final boolean faas,
9396
final Provider<SdamServerDescriptionManager> sdamProvider) {
9497
this.serverSettings = notNull("serverSettings", serverSettings);
9598
this.serverId = notNull("serverId", serverId);
9699
this.serverMonitorListener = singleServerMonitorListener(serverSettings);
97100
this.internalConnectionFactory = notNull("internalConnectionFactory", internalConnectionFactory);
98101
this.clusterConnectionMode = notNull("clusterConnectionMode", clusterConnectionMode);
99102
this.serverApi = serverApi;
103+
this.faas = faas;
100104
this.sdamProvider = sdamProvider;
101105
monitor = new ServerMonitorRunnable();
102106
monitorThread = new Thread(monitor, "cluster-" + this.serverId.getClusterId() + "-" + this.serverId.getAddress());
@@ -251,7 +255,21 @@ private ServerDescription lookupServerDescription(final ServerDescription curren
251255
}
252256

253257
private boolean shouldStreamResponses(final ServerDescription currentServerDescription) {
254-
return currentServerDescription.getTopologyVersion() != null;
258+
boolean serverSupportsStreaming = currentServerDescription.getTopologyVersion() != null;
259+
switch (serverSettings.getServerMonitoringMode()) {
260+
case STREAM: {
261+
return serverSupportsStreaming;
262+
}
263+
case POLL: {
264+
return false;
265+
}
266+
case AUTO: {
267+
return !faas && serverSupportsStreaming;
268+
}
269+
default: {
270+
throw fail();
271+
}
272+
}
255273
}
256274

257275
private CommandMessage createCommandMessage(final BsonDocument command, final InternalConnection connection,

driver-core/src/test/functional/com/mongodb/internal/connection/ServerMonitorSpecification.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ class ServerMonitorSpecification extends OperationFunctionalSpecification {
224224
SocketSettings.builder().connectTimeout(500, TimeUnit.MILLISECONDS).build(), getSslSettings()),
225225
getCredentialWithCache(), null, null, [], LoggerSettings.builder().build(), null,
226226
getServerApi()),
227-
getClusterConnectionMode(), getServerApi(), SameObjectProvider.initialized(sdam))
227+
getClusterConnectionMode(), getServerApi(), false, SameObjectProvider.initialized(sdam))
228228
serverMonitor.start()
229229
serverMonitor
230230
}

driver-core/src/test/functional/com/mongodb/internal/connection/SingleServerClusterTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ private void setUpCluster(final ServerAddress serverAddress) {
6969
streamFactory, streamFactory, getCredential(),
7070

7171
LoggerSettings.builder().build(), null, null, null,
72-
Collections.emptyList(), getServerApi()));
72+
Collections.emptyList(), getServerApi(), false));
7373
}
7474

7575
@After
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
{
2+
"description": "serverMonitoringMode-Java-specific",
3+
"schemaVersion": "1.17",
4+
"runOnRequirements": [
5+
{
6+
"topologies": [
7+
"single",
8+
"sharded",
9+
"sharded-replicaset"
10+
],
11+
"serverless": "forbid"
12+
}
13+
],
14+
"tests": [
15+
{
16+
"description": "connect with serverMonitoringMode=auto >=4.4 Java-specific",
17+
"runOnRequirements": [
18+
{
19+
"minServerVersion": "4.4.0"
20+
}
21+
],
22+
"operations": [
23+
{
24+
"name": "createEntities",
25+
"object": "testRunner",
26+
"arguments": {
27+
"entities": [
28+
{
29+
"client": {
30+
"id": "client",
31+
"uriOptions": {
32+
"serverMonitoringMode": "auto"
33+
},
34+
"useMultipleMongoses": false,
35+
"observeEvents": [
36+
"serverHeartbeatStartedEvent",
37+
"serverHeartbeatSucceededEvent",
38+
"serverHeartbeatFailedEvent"
39+
]
40+
}
41+
},
42+
{
43+
"database": {
44+
"id": "db",
45+
"client": "client",
46+
"databaseName": "sdam-tests"
47+
}
48+
}
49+
]
50+
}
51+
},
52+
{
53+
"name": "runCommand",
54+
"object": "db",
55+
"arguments": {
56+
"commandName": "ping",
57+
"command": {
58+
"ping": 1
59+
}
60+
},
61+
"expectResult": {
62+
"ok": 1
63+
}
64+
},
65+
{
66+
"name": "waitForEvent",
67+
"object": "testRunner",
68+
"arguments": {
69+
"client": "client",
70+
"event": {
71+
"serverHeartbeatStartedEvent": {}
72+
},
73+
"count": 2
74+
}
75+
}
76+
],
77+
"expectEvents": [
78+
{
79+
"client": "client",
80+
"eventType": "sdam",
81+
"ignoreExtraEvents": true,
82+
"events": [
83+
{
84+
"serverHeartbeatStartedEvent": {
85+
"awaited": true
86+
}
87+
},
88+
{
89+
"serverHeartbeatSucceededEvent": {
90+
"awaited": true
91+
}
92+
},
93+
{
94+
"serverHeartbeatStartedEvent": {
95+
"awaited": true
96+
}
97+
}
98+
]
99+
}
100+
]
101+
},
102+
{
103+
"description": "connect with serverMonitoringMode=stream >=4.4 Java-specific",
104+
"runOnRequirements": [
105+
{
106+
"minServerVersion": "4.4.0"
107+
}
108+
],
109+
"operations": [
110+
{
111+
"name": "createEntities",
112+
"object": "testRunner",
113+
"arguments": {
114+
"entities": [
115+
{
116+
"client": {
117+
"id": "client",
118+
"uriOptions": {
119+
"serverMonitoringMode": "stream"
120+
},
121+
"useMultipleMongoses": false,
122+
"observeEvents": [
123+
"serverHeartbeatStartedEvent",
124+
"serverHeartbeatSucceededEvent",
125+
"serverHeartbeatFailedEvent"
126+
]
127+
}
128+
},
129+
{
130+
"database": {
131+
"id": "db",
132+
"client": "client",
133+
"databaseName": "sdam-tests"
134+
}
135+
}
136+
]
137+
}
138+
},
139+
{
140+
"name": "runCommand",
141+
"object": "db",
142+
"arguments": {
143+
"commandName": "ping",
144+
"command": {
145+
"ping": 1
146+
}
147+
},
148+
"expectResult": {
149+
"ok": 1
150+
}
151+
},
152+
{
153+
"name": "waitForEvent",
154+
"object": "testRunner",
155+
"arguments": {
156+
"client": "client",
157+
"event": {
158+
"serverHeartbeatStartedEvent": {}
159+
},
160+
"count": 2
161+
}
162+
}
163+
],
164+
"expectEvents": [
165+
{
166+
"client": "client",
167+
"eventType": "sdam",
168+
"ignoreExtraEvents": true,
169+
"events": [
170+
{
171+
"serverHeartbeatStartedEvent": {
172+
"awaited": true
173+
}
174+
},
175+
{
176+
"serverHeartbeatSucceededEvent": {
177+
"awaited": true
178+
}
179+
},
180+
{
181+
"serverHeartbeatStartedEvent": {
182+
"awaited": true
183+
}
184+
}
185+
]
186+
}
187+
]
188+
}
189+
]
190+
}

0 commit comments

Comments
 (0)