30
30
import com .mongodb .event .ServerHeartbeatSucceededEvent ;
31
31
import com .mongodb .event .ServerMonitorListener ;
32
32
import com .mongodb .internal .TimeoutContext ;
33
+ import com .mongodb .internal .VisibleForTesting ;
33
34
import com .mongodb .internal .diagnostics .logging .Logger ;
34
35
import com .mongodb .internal .diagnostics .logging .Loggers ;
35
36
import com .mongodb .internal .inject .Provider ;
55
56
import static com .mongodb .connection .ServerType .UNKNOWN ;
56
57
import static com .mongodb .internal .Locks .checkedWithLock ;
57
58
import static com .mongodb .internal .Locks .withLock ;
59
+ import static com .mongodb .internal .VisibleForTesting .AccessModifier .PRIVATE ;
58
60
import static com .mongodb .internal .connection .CommandHelper .HELLO ;
59
61
import static com .mongodb .internal .connection .CommandHelper .LEGACY_HELLO ;
60
62
import static com .mongodb .internal .connection .CommandHelper .executeCommand ;
@@ -149,8 +151,14 @@ public void cancelCurrentCheck() {
149
151
monitor .cancelCurrentCheck ();
150
152
}
151
153
154
+ @ VisibleForTesting (otherwise = PRIVATE )
155
+ ServerMonitor getServerMonitor () {
156
+ return monitor ;
157
+ }
158
+
152
159
class ServerMonitor extends Thread implements AutoCloseable {
153
160
private volatile InternalConnection connection = null ;
161
+ private volatile boolean alreadyLoggedHeartBeatStarted = false ;
154
162
private volatile boolean currentCheckCancelled ;
155
163
156
164
ServerMonitor () {
@@ -213,9 +221,13 @@ public void run() {
213
221
214
222
private ServerDescription lookupServerDescription (final ServerDescription currentServerDescription ) {
215
223
try {
224
+ boolean shouldStreamResponses = shouldStreamResponses (currentServerDescription );
216
225
if (connection == null || connection .isClosed ()) {
226
+ alreadyLoggedHeartBeatStarted = true ;
217
227
currentCheckCancelled = false ;
218
228
InternalConnection newConnection = internalConnectionFactory .create (serverId );
229
+ serverMonitorListener .serverHearbeatStarted (new ServerHeartbeatStartedEvent (
230
+ newConnection .getDescription ().getConnectionId (), shouldStreamResponses ));
219
231
newConnection .open (operationContextFactory .create ());
220
232
connection = newConnection ;
221
233
roundTripTimeSampler .addSample (connection .getInitialServerDescription ().getRoundTripTimeNanos ());
@@ -225,9 +237,11 @@ private ServerDescription lookupServerDescription(final ServerDescription curren
225
237
if (LOGGER .isDebugEnabled ()) {
226
238
LOGGER .debug (format ("Checking status of %s" , serverId .getAddress ()));
227
239
}
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 ;
231
245
232
246
long start = System .nanoTime ();
233
247
try {
0 commit comments