Skip to content

Set a multi-thread scheduled executor for tests#4348

Draft
ScottDugas wants to merge 4 commits into
FoundationDB:mainfrom
ScottDugas:subissue-4333
Draft

Set a multi-thread scheduled executor for tests#4348
ScottDugas wants to merge 4 commits into
FoundationDB:mainfrom
ScottDugas:subissue-4333

Conversation

@ScottDugas

Copy link
Copy Markdown
Collaborator

MoreAsyncUtil installs a JVM-wide single-thread ScheduledThreadPoolExecutor by default. When the suite runs N tests concurrently and they each schedule deadline timers on that single shared thread, the timers fire late and produce spurious DeadlineExceededException from AsyncLoadingCache -- most prominently FDBDatabase.resolverStateCache during KeySpacePath.toTuple cleanup.

Override the FDBDatabaseFactory default scheduled executor in FDBDatabaseExtension and other tests with a multi-threaded pool (max(availableProcessors, 4)) shared across all tests.

Closes #4333

MoreAsyncUtil installs a JVM-wide single-thread ScheduledThreadPoolExecutor
by default. When the suite runs N tests concurrently and they each schedule
deadline timers on that single shared thread, the timers fire late and
produce spurious DeadlineExceededException from AsyncLoadingCache -- most
prominently FDBDatabase.resolverStateCache during KeySpacePath.toTuple
cleanup.

Override the FDBDatabaseFactory default scheduled executor in
FDBDatabaseExtension with a multi-threaded pool
(max(availableProcessors, 4)) shared across all tests using the extension.

The same override should be applied to any other test extension that uses
the default scheduler; that is left as follow-up.

Closes FoundationDB#4333
Move the multi-thread ScheduledExecutorService that FDBDatabaseExtension
installs on its FDBDatabaseFactory (added in the previous commit) into
TestExecutors.defaultScheduledThreadPool(), mirroring the existing
newThreadPool / defaultThreadPool pair.

Behavior-neutral refactor: same pool sizing (max(cpu, 4) threads),
same TestThreadFactory naming. Prepares the way for the next commit
which sprinkles the same override across every other test-time
FDBDatabaseFactory site.

For FoundationDB#4333
Follow-up to the FDBDatabaseExtension change: apply the same
setScheduledExecutor(TestExecutors.defaultScheduledThreadPool())
override to every other test-time construction of an
FDBDatabaseFactory I could find. Without this, JUnit-parallel
tests that don't go through FDBDatabaseExtension still race against
MoreAsyncUtil's single-thread default scheduler and see spurious
DeadlineExceededException from AsyncLoadingCache (most prominently
FDBDatabase.resolverStateCache during KeySpacePath.toTuple cleanup).

Sites updated:
  - EmbeddedRelationalExtension (its setup() @beforeeach — the
    biggest win; used by many parallel tests)
  - RecordLayerStoreCatalogImplTest.setUpCatalog
  - RecordLayerStoreCatalogWithNoTemplateOperationsTest.setUpCatalog
  - CatalogMetaDataProviderTest.canLoadMetaDataFromStore
  - JDBCEmbedDriverTest (its per-test factory setup)
  - LocatableResolverTest.testParallelDbAndScopeGetVersion (the one
    site there that constructs a fresh FDBDatabaseFactoryImpl; other
    sites in that file already go through dbExtension.getDatabaseFactory)
  - yaml-tests Command.applyMetadataOperationDirectly

Sites that already inherit the override (no change): every test that
delegates via dbExtension.getDatabaseFactory() — FDBRecordStorePerformanceTest,
FDBLuceneQueryTest, subclasses of FDBRecordStoreConcurrentTestBase.

Closes FoundationDB#4333
@ScottDugas ScottDugas added the testing improvement Change that improves our testing label Jul 13, 2026
@ScottDugas
ScottDugas requested a review from alecgrieser July 13, 2026 19:24
@ScottDugas
ScottDugas marked this pull request as ready for review July 13, 2026 19:24

@alecgrieser alecgrieser left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Having a multi-threaded scheduled executor doesn't seem like a bad idea, but I do have some questions

* {@code FDBDatabaseFactory} should use this rather than the single-thread scheduler
* {@code MoreAsyncUtil} installs by default — under JUnit-parallel execution the
* single-thread default falls behind, causing {@code DeadlineExceededException}s from
* {@code AsyncLoadingCache}.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Hm, I do have a few questions about this.

For one, how backed up are we talking about here? I haven't looked too deeply here, so my intuition could be wrong here, but it seems like the tasks on the single threaded scheduled executor service really shouldn't be that much, at least under reasonable parallelism. However, looking at delayedFuture, the way the code is currently written, callbacks get executed on the scheduled executor service thread. So, it may be the case that we should modify delayedFuture so that we call completeAsync(null, executor) here:

scheduledExecutor.schedule(() -> future.complete(null), delay, unit);

That will result in the callbacks running off of the scheduled thread, which should improve the scheduling accuracy.

But actually, more to the point, it kind of seems like if the scheduling thread is backed up, then we'd get fewer DeadlineExceededExceptions, not more. Looking at getWithDeadline:

return CompletableFuture.anyOf(MoreAsyncUtil.delayedFuture(deadlineTimeMillis, TimeUnit.MILLISECONDS, scheduledExecutor), valueFuture)
.thenCompose(ignore -> {
if (!valueFuture.isDone()) {
// if the future is not ready then we exceeded the timeout
valueFuture.completeExceptionally(new DeadlineExceededException(deadlineTimeMillis));
}
return valueFuture;
});

We should only get a DeadlineExceededException if the delayed future completes before the valueFuture. Unless the scheduled executor responds to too much work by completing tasks prematurely, then a busy scheduling thread means that the deadline is lengthened.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think the issue is that the logic that is resulting in DeadlineExceededException is also doing retries with exponential backoff. The overwhelming majority in the tests, IIUC is resolving directory layers.
So I think what is happening is that it:

  1. schedules the exception
  2. starts the resolution
  3. fails, and schedules a delayed future to retry

The ScheduledExecutor is backed up, which, since it was unable to execute either at the time it was supposed to executes the DeadlineExceededException first, because it was submitted first.

This could be indicative that there is something running on the ScheduledThreadPool that is not supposed to.
When I get a chance I will try to install a ScheduledThreadPool that tracks the time it takes for its runnables to run and throws an error if anything takes longer than some time, hopefully allowing us to find a task that is running there, but should be on a different executor. If that doesn't prove too fruitful, I think it's probably worthwhile to just change our executor to the modestly sized pool used in this PR.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yeah, that makes sense. Doing the experiment sounds like a good idea, and if it comes up inconclusive (or is harder to execute than we'd hope), then merging this PR as it is sounds okay

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I did some more investigation, and it appears to hint that the issue is not due to overwhelming the executor.
I suspect it is one of the other executors getting overwhelmed. I'm going to hold onto this PR as a draft until more of the work to allow parallelism gets in, and we can see if this problem happens again.

@ScottDugas
ScottDugas marked this pull request as draft July 15, 2026 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing improvement Change that improves our testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Configure FDBDatabaseExtension with a multi-thread scheduled executor

2 participants