Skip to content

Commit 42fdd3f

Browse files
authored
Account the time to deliver connection pool events as part of the reported duration (#1290)
JAVA-5121
1 parent 26df551 commit 42fdd3f

File tree

4 files changed

+14
-18
lines changed

4 files changed

+14
-18
lines changed

driver-core/src/main/com/mongodb/event/ConnectionCheckOutFailedEvent.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,13 @@ public Reason getReason() {
109109

110110
/**
111111
* The time it took to check out the connection.
112-
* More specifically, the time elapsed between the {@link ConnectionCheckOutStartedEvent} emitted by the same checking out and this event.
112+
* More specifically, the time elapsed between emitting a {@link ConnectionCheckOutStartedEvent}
113+
* and emitting this event as part of the same checking out.
113114
* <p>
114115
* Naturally, if a new connection was not {@linkplain ConnectionCreatedEvent created}
115116
* and {@linkplain ConnectionReadyEvent established} as part of checking out,
116117
* this duration is usually not greater than {@link ConnectionPoolSettings#getMaxWaitTime(TimeUnit)},
117118
* but may occasionally be greater than that, because the driver does not provide hard real-time guarantees.</p>
118-
* <p>
119-
* This duration does not currently include the time to deliver the {@link ConnectionCheckOutStartedEvent}.
120-
* Subject to change.</p>
121119
*
122120
* @param timeUnit The time unit of the result.
123121
* {@link TimeUnit#convert(long, TimeUnit)} specifies how the conversion from nanoseconds to {@code timeUnit} is done.

driver-core/src/main/com/mongodb/event/ConnectionCheckedOutEvent.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,13 @@ public long getOperationId() {
7171

7272
/**
7373
* The time it took to check out the connection.
74-
* More specifically, the time elapsed between the {@link ConnectionCheckOutStartedEvent} emitted by the same checking out and this event.
74+
* More specifically, the time elapsed between emitting a {@link ConnectionCheckOutStartedEvent}
75+
* and emitting this event as part of the same checking out.
7576
* <p>
7677
* Naturally, if a new connection was not {@linkplain ConnectionCreatedEvent created}
7778
* and {@linkplain ConnectionReadyEvent established} as part of checking out,
7879
* this duration is usually not greater than {@link ConnectionPoolSettings#getMaxWaitTime(TimeUnit)},
7980
* but may occasionally be greater than that, because the driver does not provide hard real-time guarantees.</p>
80-
* <p>
81-
* This duration does not currently include the time to deliver the {@link ConnectionCheckOutStartedEvent}.
82-
* Subject to change.</p>
8381
*
8482
* @param timeUnit The time unit of the result.
8583
* {@link TimeUnit#convert(long, TimeUnit)} specifies how the conversion from nanoseconds to {@code timeUnit} is done.

driver-core/src/main/com/mongodb/event/ConnectionReadyEvent.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,12 @@ public ConnectionId getConnectionId() {
5858

5959
/**
6060
* The time it took to establish the connection.
61-
* More specifically, the time elapsed between the {@link ConnectionCreatedEvent} emitted by the same checking out and this event.
61+
* More specifically, the time elapsed between emitting a {@link ConnectionCreatedEvent}
62+
* and emitting this event as part of the same checking out.
6263
* <p>
6364
* Naturally, when establishing a connection is part of checking out,
6465
* this duration is not greater than
6566
* {@link ConnectionCheckedOutEvent#getElapsedTime(TimeUnit)}/{@link ConnectionCheckOutFailedEvent#getElapsedTime(TimeUnit)}.</p>
66-
* <p>
67-
* This duration does not currently include the time to deliver the {@link ConnectionCreatedEvent}.
68-
* Subject to change.</p>
6967
*
7068
* @param timeUnit The time unit of the result.
7169
* {@link TimeUnit#convert(long, TimeUnit)} specifies how the conversion from nanoseconds to {@code timeUnit} is done.

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -504,19 +504,19 @@ private void connectionPoolCreated(final ConnectionPoolListener connectionPoolLi
504504
* Send both current and deprecated events in order to preserve backwards compatibility.
505505
* Must not throw {@link Exception}s.
506506
*
507-
* @return A {@link TimePoint} after executing {@link ConnectionPoolListener#connectionCreated(ConnectionCreatedEvent)},
508-
* {@link ConnectionPoolListener#connectionCreated(ConnectionCreatedEvent)}.
509-
* This order is required by
507+
* @return A {@link TimePoint} before executing {@link ConnectionPoolListener#connectionCreated(ConnectionCreatedEvent)}
508+
* and logging the event. This order is required by
510509
* <a href="https://github.com/mongodb/specifications/blob/master/source/connection-monitoring-and-pooling/connection-monitoring-and-pooling.rst#events">CMAP</a>
511510
* and {@link ConnectionReadyEvent#getElapsedTime(TimeUnit)}.
512511
*/
513512
private TimePoint connectionCreated(final ConnectionPoolListener connectionPoolListener, final ConnectionId connectionId) {
513+
TimePoint openStart = TimePoint.now();
514514
logEventMessage("Connection created",
515515
"Connection created: address={}:{}, driver-generated ID={}",
516516
connectionId.getLocalValue());
517517

518518
connectionPoolListener.connectionCreated(new ConnectionCreatedEvent(connectionId));
519-
return TimePoint.now();
519+
return openStart;
520520
}
521521

522522
/**
@@ -562,16 +562,18 @@ private void connectionCheckedOut(
562562
}
563563

564564
/**
565-
* @return A {@link TimePoint} after executing {@link ConnectionPoolListener#connectionCheckOutStarted(ConnectionCheckOutStartedEvent)}.
565+
* @return A {@link TimePoint} before executing
566+
* {@link ConnectionPoolListener#connectionCheckOutStarted(ConnectionCheckOutStartedEvent)} and logging the event.
566567
* This order is required by
567568
* <a href="https://github.com/mongodb/specifications/blob/master/source/connection-monitoring-and-pooling/connection-monitoring-and-pooling.rst#events">CMAP</a>
568569
* and {@link ConnectionCheckedOutEvent#getElapsedTime(TimeUnit)}, {@link ConnectionCheckOutFailedEvent#getElapsedTime(TimeUnit)}.
569570
*/
570571
private TimePoint connectionCheckoutStarted(final OperationContext operationContext) {
572+
TimePoint checkoutStart = TimePoint.now();
571573
logEventMessage("Connection checkout started", "Checkout started for connection to {}:{}");
572574

573575
connectionPoolListener.connectionCheckOutStarted(new ConnectionCheckOutStartedEvent(serverId, operationContext.getId()));
574-
return TimePoint.now();
576+
return checkoutStart;
575577
}
576578

577579
/**

0 commit comments

Comments
 (0)