Skip to content

Commit 2b92871

Browse files
committed
PR review updates
1 parent b6ff5ac commit 2b92871

7 files changed

Lines changed: 40 additions & 13 deletions

File tree

config/spotbugs/exclude.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@
292292

293293
<!-- DefaultServerMonitor -->
294294
<Match>
295-
<class name="com.mongodb.internal.connection.DefaultServerMonitor" />
295+
<Class name="com.mongodb.internal.connection.DefaultServerMonitor" />
296296
<Bug pattern="RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE" />
297297
</Match>
298298

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

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,12 @@ private ServerDescription doHeartbeat(final ServerDescription currentServerDescr
340340
}
341341

342342
private void logAndNotifyHeartbeatStarted(final boolean shouldStreamResponses) {
343-
ConnectionDescription description = connection.getDescription();
343+
ConnectionDescription description = withLock(lock, () -> {
344+
if (connection != null) {
345+
return connection.getDescription();
346+
}
347+
return null;
348+
});
344349
if (description != null) {
345350
alreadyLoggedHeartBeatStarted = true;
346351
logHeartbeatStarted(serverId, description, shouldStreamResponses);
@@ -357,20 +362,34 @@ private void logAndNotifyHeartbeatStarted(final boolean shouldStreamResponses) {
357362
private void logAndNotifyHeartbeatSucceeded(final boolean shouldStreamResponses, final BsonDocument helloResult) {
358363
alreadyLoggedHeartBeatStarted = false;
359364
long elapsedTimeNanos = getElapsedTimeNanos();
360-
if (!shouldStreamResponses) {
361-
roundTripTimeSampler.addSample(elapsedTimeNanos);
365+
366+
ConnectionDescription description = withLock(lock, () -> {
367+
if (connection != null) {
368+
return connection.getDescription();
369+
}
370+
return null;
371+
});
372+
if (description != null) {
373+
if (!shouldStreamResponses) {
374+
roundTripTimeSampler.addSample(elapsedTimeNanos);
375+
}
376+
logHeartbeatSucceeded(serverId, description, shouldStreamResponses, elapsedTimeNanos, helloResult);
377+
serverMonitorListener.serverHeartbeatSucceeded(
378+
new ServerHeartbeatSucceededEvent(description.getConnectionId(), helloResult,
379+
elapsedTimeNanos, shouldStreamResponses));
362380
}
363-
logHeartbeatSucceeded(serverId, connection.getDescription(), shouldStreamResponses, elapsedTimeNanos, helloResult);
364-
serverMonitorListener.serverHeartbeatSucceeded(
365-
new ServerHeartbeatSucceededEvent(connection.getDescription().getConnectionId(), helloResult,
366-
elapsedTimeNanos, shouldStreamResponses));
367381
}
368382

369383
private void logAndNotifyHeartbeatFailed(final boolean shouldStreamResponses, final Exception e) {
370384
alreadyLoggedHeartBeatStarted = false;
371385
long elapsedTimeNanos = getElapsedTimeNanos();
372386

373-
ConnectionDescription description = connection != null ? connection.getDescription() : null;
387+
ConnectionDescription description = withLock(lock, () -> {
388+
if (connection != null) {
389+
return connection.getDescription();
390+
}
391+
return null;
392+
});
374393
if (description != null) {
375394
logHeartbeatFailed(serverId, description, shouldStreamResponses, elapsedTimeNanos, e);
376395
serverMonitorListener.serverHeartbeatFailed(
@@ -383,6 +402,7 @@ private void logAndNotifyHeartbeatFailed(final boolean shouldStreamResponses, fi
383402
}
384403
}
385404
}
405+
386406
private long getElapsedTimeNanos() {
387407
return System.nanoTime() - lookupStartTimeNanos;
388408
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.junit.jupiter.params.ParameterizedTest;
2525
import org.junit.jupiter.params.provider.ValueSource;
2626

27+
import java.util.List;
28+
2729
import static com.mongodb.connection.ConnectionDescription.getDefaultMaxMessageSize;
2830
import static org.junit.jupiter.api.Assertions.assertEquals;
2931
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -45,12 +47,15 @@ void testParseReplyHeader(final int responseFlags) {
4547
outputBuffer.writeInt(4);
4648
outputBuffer.writeInt(1);
4749

48-
ByteBuf byteBuf = outputBuffer.getByteBuffers().get(0);
50+
List<ByteBuf> byteBuffers = outputBuffer.getByteBuffers();
51+
ByteBuf byteBuf = byteBuffers.get(0);
4952
ReplyHeader replyHeader = new ReplyHeader(byteBuf, new MessageHeader(byteBuf, getDefaultMaxMessageSize()));
5053

5154
assertEquals(186, replyHeader.getMessageLength());
5255
assertEquals(45, replyHeader.getRequestId());
5356
assertEquals(23, replyHeader.getResponseTo());
57+
58+
byteBuffers.forEach(ByteBuf::release);
5459
}
5560
}
5661

driver-core/src/test/unit/com/mongodb/internal/connection/DefaultServerMonitorTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ void closeDuringConnectionShouldNotLeakBuffers() throws Exception {
286286

287287
// Verify no leaks by checking connection was properly closed
288288
monitor.getServerMonitor().join(5000);
289+
assertFalse(monitor.getServerMonitor().isAlive());
289290
}
290291

291292
@Test

driver-core/src/test/unit/com/mongodb/internal/session/ServerSessionPoolTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import static org.junit.jupiter.api.Assertions.assertNotEquals;
5757
import static org.junit.jupiter.api.Assertions.assertNotNull;
5858
import static org.junit.jupiter.api.Assertions.assertThrows;
59+
import static org.junit.jupiter.api.Assertions.assertTrue;
5960
import static org.mockito.ArgumentMatchers.any;
6061
import static org.mockito.ArgumentMatchers.argThat;
6162
import static org.mockito.Mockito.mock;
@@ -157,7 +158,7 @@ void testPruneSessionsWhenGetting() {
157158
ServerSession sessionTwo = pool.get();
158159

159160
assertNotEquals(sessionTwo, sessionOne);
160-
// Note: Actual closed state verification depends on implementation details
161+
assertTrue(sessionOne.isClosed());
161162
}
162163

163164
@Test

driver-reactive-streams/src/main/com/mongodb/reactivestreams/client/internal/ClientSessionPublisherImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@
4848

4949
final class ClientSessionPublisherImpl extends BaseClientSessionImpl implements ClientSession {
5050

51+
private final AtomicBoolean closed = new AtomicBoolean();
5152
private final MongoClientImpl mongoClient;
5253
private final OperationExecutor executor;
5354
private TransactionState transactionState = TransactionState.NONE;
5455
private boolean messageSentInCurrentTransaction;
5556
private boolean commitInProgress;
5657
private TransactionOptions transactionOptions;
57-
private AtomicBoolean closed = new AtomicBoolean();
5858

5959
ClientSessionPublisherImpl(final ServerSessionPool serverSessionPool, final MongoClientImpl mongoClient,
6060
final ClientSessionOptions options, final OperationExecutor executor) {

driver-sync/src/test/functional/com/mongodb/client/unified/UnifiedTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ public void cleanUp() {
312312
postCleanUp(testDef);
313313
}
314314
// Ask the JVM to run garbage collection.
315-
// This should help with Nettys leak detection
315+
// This should help with Netty's leak detection
316316
System.gc();
317317
}
318318

0 commit comments

Comments
 (0)