Skip to content

Commit 25c0262

Browse files
authored
Ensure serverHeartbeatEvent is sent before opening a connection (mongodb#1715)
Added Heartbeat prose test Migrated DefaultServerMonitorSpecification to JUnit 5. JAVA-5230
1 parent 7e94bc2 commit 25c0262

File tree

5 files changed

+328
-278
lines changed

5 files changed

+328
-278
lines changed

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.mongodb.event.ServerHeartbeatSucceededEvent;
3131
import com.mongodb.event.ServerMonitorListener;
3232
import com.mongodb.internal.TimeoutContext;
33+
import com.mongodb.internal.VisibleForTesting;
3334
import com.mongodb.internal.diagnostics.logging.Logger;
3435
import com.mongodb.internal.diagnostics.logging.Loggers;
3536
import com.mongodb.internal.inject.Provider;
@@ -55,6 +56,7 @@
5556
import static com.mongodb.connection.ServerType.UNKNOWN;
5657
import static com.mongodb.internal.Locks.checkedWithLock;
5758
import static com.mongodb.internal.Locks.withLock;
59+
import static com.mongodb.internal.VisibleForTesting.AccessModifier.PRIVATE;
5860
import static com.mongodb.internal.connection.CommandHelper.HELLO;
5961
import static com.mongodb.internal.connection.CommandHelper.LEGACY_HELLO;
6062
import static com.mongodb.internal.connection.CommandHelper.executeCommand;
@@ -149,8 +151,14 @@ public void cancelCurrentCheck() {
149151
monitor.cancelCurrentCheck();
150152
}
151153

154+
@VisibleForTesting(otherwise = PRIVATE)
155+
ServerMonitor getServerMonitor() {
156+
return monitor;
157+
}
158+
152159
class ServerMonitor extends Thread implements AutoCloseable {
153160
private volatile InternalConnection connection = null;
161+
private volatile boolean alreadyLoggedHeartBeatStarted = false;
154162
private volatile boolean currentCheckCancelled;
155163

156164
ServerMonitor() {
@@ -213,9 +221,13 @@ public void run() {
213221

214222
private ServerDescription lookupServerDescription(final ServerDescription currentServerDescription) {
215223
try {
224+
boolean shouldStreamResponses = shouldStreamResponses(currentServerDescription);
216225
if (connection == null || connection.isClosed()) {
226+
alreadyLoggedHeartBeatStarted = true;
217227
currentCheckCancelled = false;
218228
InternalConnection newConnection = internalConnectionFactory.create(serverId);
229+
serverMonitorListener.serverHearbeatStarted(new ServerHeartbeatStartedEvent(
230+
newConnection.getDescription().getConnectionId(), shouldStreamResponses));
219231
newConnection.open(operationContextFactory.create());
220232
connection = newConnection;
221233
roundTripTimeSampler.addSample(connection.getInitialServerDescription().getRoundTripTimeNanos());
@@ -225,9 +237,11 @@ private ServerDescription lookupServerDescription(final ServerDescription curren
225237
if (LOGGER.isDebugEnabled()) {
226238
LOGGER.debug(format("Checking status of %s", serverId.getAddress()));
227239
}
228-
boolean shouldStreamResponses = shouldStreamResponses(currentServerDescription);
229-
serverMonitorListener.serverHearbeatStarted(new ServerHeartbeatStartedEvent(
230-
connection.getDescription().getConnectionId(), shouldStreamResponses));
240+
if (!alreadyLoggedHeartBeatStarted) {
241+
serverMonitorListener.serverHearbeatStarted(new ServerHeartbeatStartedEvent(
242+
connection.getDescription().getConnectionId(), shouldStreamResponses));
243+
}
244+
alreadyLoggedHeartBeatStarted = false;
231245

232246
long start = System.nanoTime();
233247
try {

driver-core/src/test/unit/com/mongodb/internal/connection/DefaultServerMonitorSpecification.groovy

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

0 commit comments

Comments
 (0)