Skip to content

Introduce necessary executors and implement sleepAsync - #3

Draft
stIncMale wants to merge 16 commits into
introduceRetryPolicyfrom
sleepAsync
Draft

Introduce necessary executors and implement sleepAsync#3
stIncMale wants to merge 16 commits into
introduceRetryPolicyfrom
sleepAsync

Conversation

@stIncMale

@stIncMale stIncMale commented Jun 30, 2026

Copy link
Copy Markdown
Owner

AI usage

AI was used only to review and to suggest ways to deal with the serious bug it discovered (see below).

AI identified a serious bug with CommonExecutor offloading scheduled tasks to another Executor, which I failed to think about on my own. AI also expressed ideas on how one may deal with that problem. One of them I manually implemented in DefaultAsyncClientExecutor.

JAVA-6240

@stIncMale stIncMale self-assigned this Jun 30, 2026
@stIncMale
stIncMale force-pushed the sleepAsync branch 5 times, most recently from 634c8dd to 85b1c3d Compare July 3, 2026 07:39
Comment thread driver-core/src/main/com/mongodb/internal/async/AsyncRunnable.java Outdated
Comment thread driver-core/src/main/com/mongodb/internal/thread/MongoThreadPoolExecutor.java Outdated
* All {@link Throwable}s are logged.</li>
* </ul>
*/
public final class MongoThreadPoolExecutor extends ThreadPoolExecutor {

@stIncMale stIncMale Jul 21, 2026

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

VAKOTODO Update the description of https://jira.mongodb.org/browse/JAVA-6109: mention that all other executor implementations / single threads should be replaces either with virtual threads (https://jira.mongodb.org/browse/JAVA-4930 - distant future), or MongoThreadPoolExecutor/MongoScheduledThreadPoolExecutor (this includes the executors created in AsynchronousTlsChannelGroup, NettyStreamFactoryFactory (NioEventLoopGroup)), unless it's an IO executor supplied by an application.

Also mention in that ticket to document that the executors supplied by applications should themselves make sure uncaught Throwables are not swallowed, potentially the same way MongoThreadPoolExecutor/MongoScheduledThreadPoolExecutor do it.

@jyemin jyemin Aug 6, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is about updating Jira, so not blocking merge.

So consider it resolved from standpoint of merging this PR.

Comment thread driver-core/src/main/com/mongodb/internal/thread/MongoThreadPoolExecutor.java Outdated
Comment thread driver-core/src/main/com/mongodb/internal/thread/CommonExecutor.java Outdated
Comment thread driver-core/src/main/com/mongodb/internal/thread/MongoThreadPoolExecutor.java Outdated
Comment thread driver-core/src/main/com/mongodb/internal/thread/CommonExecutor.java Outdated
Comment thread driver-core/src/main/com/mongodb/internal/connection/StreamFactoryFactory.java Outdated
Comment thread driver-core/src/main/com/mongodb/internal/thread/AsyncClientExecutor.java Outdated
@vbabanin

Copy link
Copy Markdown

LGTM!

Comment thread driver-core/src/main/com/mongodb/internal/thread/AsyncClientExecutor.java Outdated
@stIncMale
stIncMale requested a review from jyemin August 15, 2026 16:33
withLock(closeLock, () -> {
scheduledTasks.add(scheduledTask);
if (closed) {
throw createClosedException();

Copy link
Copy Markdown

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 this both adds scheduledTask to scheduledTasks and also throws here.

@stIncMale stIncMale Aug 17, 2026

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Discussed over Zoom. Leaving a comment for completeness.

We throw an exception here just to catch it below and call scheduledTask.reject:

catch (RejectedExecutionException rejectionCause) {
    scheduledTask.reject(rejectionCause);
}

ScheduledRejectableRunnable.run/reject are implemented mutually exclusive by means of if (scheduledTasks.remove(this)). Thus, if we don't do scheduledTasks.add(scheduledTask), then scheduledTask.reject does nothing.

An alternative is: introduce another reject that does not do if (scheduledTasks.remove(this)) and is dedicated for tasks that are guaranteed not be have been scheduled. This complicates the code by introducing non-uniformity.

Comment thread driver-core/src/main/com/mongodb/internal/thread/AsyncClientExecutor.java Outdated
* @see AsyncClientExecutor
*/
// VAKOTODO decide what to do with https://jira.mongodb.org/browse/JAVA-6279.
public final class CommonExecutor {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Can this be package private?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Done in bbe71e8.

*
* @see AsyncClientExecutor
*/
// TODO-BACKPRESSURE Valentin decide what to do with https://jira.mongodb.org/browse/JAVA-6279.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

This is a documented (see the description of mongodb#1918) way of leaving TODOs in the backpressure feature branch.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

The refactoring done to this and related classes in 66182bc addresses the VAKOTODO code comment that was left previously in this PR.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Resolved

@stIncMale
stIncMale requested a review from jyemin August 17, 2026 22:01

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Resolved

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.

3 participants