Skip to content

Adding timeoutMS for explain helpers #1770

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 4 commits into
base: main
Choose a base branch
from
Open

Adding timeoutMS for explain helpers #1770

wants to merge 4 commits into from

Conversation

nhachicha
Copy link
Contributor

Fixes JAVA-5579

@nhachicha nhachicha self-assigned this Jul 18, 2025
@nhachicha nhachicha requested review from vbabanin and Copilot July 18, 2025 17:57
@nhachicha nhachicha marked this pull request as ready for review July 18, 2025 17:58
@nhachicha nhachicha requested a review from a team as a code owner July 18, 2025 17:58
Copilot

This comment was marked as outdated.

Copy link
Member

@rozza rozza left a comment

Choose a reason for hiding this comment

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

@nhachicha - looks like some tests are failing:

com.mongodb.reactivestreams.client.ExplainTest.testExplainWithMaxTimeMS - 5 Failed

com.mongodb.client.ExplainTest.testExplainWithMaxTimeMS - 3 Failed

@nhachicha
Copy link
Contributor Author

@nhachicha nhachicha requested review from Copilot and rozza July 22, 2025 17:53
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds timeout support to the MongoDB driver's explain operations for find and aggregate queries. The implementation adds new overloaded methods that accept a timeoutMS parameter, allowing developers to specify custom timeouts for explain operations rather than relying on global timeout settings.

Key changes include:

  • Addition of four new explain method overloads accepting timeoutMS parameter for both FindIterable and AggregateIterable interfaces
  • Implementation of timeout-aware execute methods that create specialized timeout executors
  • Comprehensive test coverage verifying timeout behavior and command structure

Reviewed Changes

Copilot reviewed 23 out of 23 changed files in this pull request and generated 1 comment.

File Description
AbstractExplainTest.java Adds functional test for explain timeout behavior with fail point testing
FindIterable.java / AggregateIterable.java Interface definitions for new timeout-aware explain methods
FindIterableImpl.java / AggregateIterableImpl.java Core implementation of timeout-aware explain execution
Various driver adapters Implementation of new methods across Scala, Kotlin, and reactive streams adapters

*
* @param timeoutMS the timeout in milliseconds for the explain operation
* @return the execution plan
* @since 5.2
Copy link
Collaborator

Choose a reason for hiding this comment

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

All these should be 5.6.

* @mongodb.driver.manual reference/command/explain/
* @mongodb.server.release 3.2
*/
Document explain(long timeoutMS);
Copy link
Collaborator

@jyemin jyemin Jul 22, 2025

Choose a reason for hiding this comment

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

I'm not clear why all these overloads are needed. For the rest of CSOT, applications specify the timeout via MongoCluster/MongoDatabase/MongoCollection#withTimeout. Would that suffice for specifying the timeout for explain?

@vbabanin
Copy link
Member

vbabanin commented Jul 22, 2025

The changes covered by this ticket were already implemented in JAVA-5580, where we also decided not to introduce any new knobs on explain helpers (during one of the catch-ups).

According to the existing CSOT specification, timeoutMS is intended to be specified per operation. The unified spec tests also require timeoutMS to be passed explicitly for each operation.

Our API supports this model via withTimeout() methods on MongoCollection, MongoDatabase, and MongoClient, which allow users to define a timeout at the desired scope, and subsequently per operation. For example, if a user wants to apply a timeout to a specific operation, they can call:

collection.withTimeout(100, TimeUnit.SECONDS).find(...).explain();

In that case,timeoutMS is applied to the actual operation that is executed, thus explain command, not a command being explained. The maxTimeMS on the command being explained is ignored by the server, even if it was present.

This approach aligns with our existing API design, which already includes withWriteConcern() and withReadPreference() on the aforementioned API components.

We've documented this design approach, along with the reasoning and tradeoffs, here:
Design doc.

To clarify: timeoutMS and maxTimeMS are separate APIs. timeoutMS is the successor, and it deprecates maxTimeMS. As noted in the CRUD spec:

"NOTE: This option is deprecated in favor of timeoutMS."

Regarding maxTimeMS (which is going to be marked deprecated once CSOT is GA).

The prose test in the CRUD spec applies only to the deprecated maxTimeMS. In discussion comments of DRIVERS-1256, we agreed not to require drivers to implement maxTimeMS support on explain for drivers that already support CSOT, since it would introduce a soon-to-be-deprecated parameter. See also this related spec PR comment.

While CSOT is currently marked alpha, timeoutMS already works with explain() via MongoCollection and explain(), allowing users to troubleshoot operations if needed.

Given that:

  • CSOT is already in place (albeit alpha).
  • timeoutMS works as intended with explain() via withTimeout on MongoCollection.
  • and JAVA-5580 already covered the necessary changes.

I believe, no additional changes are needed as part of this ticket JAVA-5579.

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