Set a multi-thread scheduled executor for tests#4348
Conversation
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
alecgrieser
left a comment
There was a problem hiding this comment.
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}. |
There was a problem hiding this comment.
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:
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:
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.
There was a problem hiding this comment.
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:
- schedules the exception
- starts the resolution
- 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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
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