3030import com .mongodb .event .ServerHeartbeatSucceededEvent ;
3131import com .mongodb .event .ServerMonitorListener ;
3232import com .mongodb .internal .TimeoutContext ;
33+ import com .mongodb .internal .VisibleForTesting ;
3334import com .mongodb .internal .diagnostics .logging .Logger ;
3435import com .mongodb .internal .diagnostics .logging .Loggers ;
3536import com .mongodb .internal .inject .Provider ;
5556import static com .mongodb .connection .ServerType .UNKNOWN ;
5657import static com .mongodb .internal .Locks .checkedWithLock ;
5758import static com .mongodb .internal .Locks .withLock ;
59+ import static com .mongodb .internal .VisibleForTesting .AccessModifier .PRIVATE ;
5860import static com .mongodb .internal .connection .CommandHelper .HELLO ;
5961import static com .mongodb .internal .connection .CommandHelper .LEGACY_HELLO ;
6062import 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 {
0 commit comments