Skip to content

DRIVERS-2884 Avoid connection churn when operations timeout #1675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 46 commits into
base: master
Choose a base branch
from

Conversation

prestonvasquez
Copy link
Member

@prestonvasquez prestonvasquez commented Oct 14, 2024

This PR implements the design for connection pooling improvements described in DRIVERS-2884, based on the CSOT (Client-Side Operation Timeout) spec. It addresses connection churn caused by network timeouts during operations, especially in environments with low client-side timeouts and high latency.

When a connection is checked out after a network timeout, the driver now attempts to resume and complete reading any pending server response (instead of closing and discarding the connection). This may require multiple checkouts.
Each pending response read is subject to a cumulative 3-second static timeout. The timeout is refreshed after each successful read, acknowledging that progress is being made. If no data is read and the timeout is exceeded, the connection is closed.

To reduce unnecessary latency, if the timeout has expired while the connection was idle in the pool, a non-blocking single-byte read is performed; if no data is available, the connection is closed immediately.
This update introduces new CMAP events and logging messages (PendingResponseStarted, PendingResponseSucceeded, PendingResponseFailed) to improve observability of this path.

Please complete the following before merging:

@prestonvasquez prestonvasquez marked this pull request as ready for review April 25, 2025 21:36
@prestonvasquez prestonvasquez requested a review from a team as a code owner April 25, 2025 21:36
connectionId: int64;

/**
* The time it took to complete the pending read.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Can we clarify that in the description of duration? We can take inspiration from the definitions of duration for checkout failed and checkout succeeded events. Ex:

  /**
   * The time it took to establish the connection.
   * In accordance with the definition of establishment of a connection
   * specified by `ConnectionPoolOptions.maxConnecting`,
   * it is the time elapsed between emitting a `ConnectionCreatedEvent`
   * and emitting this event as part of the same checking out.
   *
   * Naturally, when establishing a connection is part of checking out,
   * this duration is not greater than
   * `ConnectionCheckedOutEvent`/`ConnectionCheckOutFailedEvent.duration`.
   *
   * A driver MAY choose the type idiomatic to the driver.
   * If the type chosen does not convey units, e.g., `int64`,
   * then the driver MAY include units in the name, e.g., `durationMS`.
   */
  duration: Duration;

So, maybe something like:

  /**
   * The time it took to complete the pending read.
   * This duration is defined as the time elapsed between emitting a `PendingResponseStarted` event
   * and emitting this event as part of the same checking out.
   *
   * A driver MAY choose the type idiomatic to the driver.
   * If the type chosen does not convey units, e.g., `int64`,
   * then the driver MAY include units in the name, e.g., `durationMS`.
   */
  duration: Duration;

connectionId: int64;

/**
* The time it took to complete the pending read.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(same comment for other definitions of duration in this PR).

@prestonvasquez prestonvasquez removed the request for review from qingyang-hu May 6, 2025 23:28
- connectionCheckedInEvent: {} # Second find succeeds.
# If the connection is closed server-side while draining the response, the
# driver must close the connection.
- description: "connection closed server-side while draining response"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be a sufficient check that if the awaitPendingResponse function fails with a non-timeout error the connection should be closed.

@prestonvasquez prestonvasquez requested a review from a team as a code owner May 28, 2025 22:10
@prestonvasquez prestonvasquez requested review from isabelatkinson and removed request for a team May 28, 2025 22:10
@@ -176,6 +176,11 @@ The RetryableWriteError label might be added to an error in a variety of ways:
RetryableWriteError label to that error if the MongoClient performing the operation has the retryWrites
configuration option set to true.

- When the driver encounters a network error checking out a connection, it MUST add a RetryableWriteError label to that
error if the MongoClient performing the operation has the retryWrites configuration option set to true. For example,
a network error encountered when checking out a connection that must attempt to discard a pending response from the
Copy link
Member

@ShaneHarvey ShaneHarvey Jun 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a network error encountered when checking out a connection that must attempt to discard a pending response

Is this sentence correct? I'm getting tripped up by the "that must attempt". Should it be

a network error encountered when reading a pending response during connection checkout.

Comment on lines +336 to +346
- connectionCheckedOutEvent: {}
- connectionCheckedInEvent: {} # Ping finishes.
- connectionCheckedOutEvent: {}
- connectionCheckedInEvent: {} # Insert fails.
- connectionPendingResponseStarted: {} # Pending read fails on first find
- connectionPendingResponseFailed:
reason: error
- connectionClosedEvent:
reason: error
- connectionCheckedOutEvent: {}
- connectionCheckedInEvent: {} # Find finishes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add server selection events to this list (maybe we could use logging tests for this, even though not all drivers have implemented the CLAM spec?)? It would be nice to clarify that the retry happens because the error returned is retryable and we use the existing retry mechanism, not that we retry checkout directly. Basically:

- connectionPendingResponseStarted: {} # Pending read fails on first find
          - connectionPendingResponseFailed:
              reason: error
          - connectionClosedEvent:
              reason: error
          - serverSelectionStarted
          - serverSelectionFinished
          - connectionCheckedOutEvent: {}
          - connectionCheckedInEvent: {} # Find finishes.

arguments:
filter: {_id: 1}
expectError:
isTimeoutError: true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test fails in Node because isTimeoutError only applies when timeoutMS is configured:

isTimeoutError: Optional boolean. If true, the test runner MUST assert that the error represents a timeout due to use of the timeoutMS option. If false, the test runner MUST assert that the error does not represent a timeout.

Maybe splitting hairs here but in Node, we only throw a CSOT timeout error when timeoutMS is enabled for the operation. Because the findOne doesn't have timeoutMS configured, even though the timeout is caused by a CSOT-enabled command, the findOne itself doesn't throw a CSOT timeout error.

Zooming out further from just the test runner - what do you think the best solution would be for this situation? Should Node just throw a CSOT timeout error in this circumstance?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants