Skip to content

Commit 931f6e0

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 931f6e0

File tree

10 files changed

+505
-9
lines changed

10 files changed

+505
-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

0 commit comments

Comments
 (0)