Repository navigation
[9.0] Resolve uncached tenants synchronously on the sync GetTenant path (#5985) - #5988
Merged
Merged
Conversation
…ync GetTenant path (#5985) The synchronous session factories and ForTenant resolve an uncached tenant through GetTenant, which blocked a pool thread on the asynchronous control-table read. A burst of first sessions for new tenants starved the thread pool: 86 sync session opens stalled a 10 ms heartbeat for 2.6 s on 18 processors. GetTenant now reads the tenant's row with synchronous ADO.NET on the calling thread (one statement, no Task in the chain), sharing the SQL text, row mapping and database build with the async path. A multiplexed control database keeps the async read, because Npgsql refuses synchronous I/O when multiplexing is on. SingleServerMultiTenancy and ShardedTenancy still block on async work for a tenant's first resolution (weasel#839, #5986); the multi-tenancy docs now describe the hazard and the remedies. Part of #5985 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 16, 2026
Merged
This was referenced Nov 16, 2026
This was referenced Nov 26, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #5985
Backport of #5987, plus the fix for
MasterTableTenancy. That fix is possible on 9.0 because the class is still Marten's own; on master its tenant cache belongs to Weasel'sMasterTableTenancyBase(JasperFx/weasel#838).The problem
The synchronous session factories and synchronous
ForTenantresolve a tenant that is not cached yet throughITenancy.GetTenant. That blocked a pool thread on the asynchronous lookup, which needed another pool thread to finish: the starvation shape of #5969. It happens once per tenant per process, so a burst of first sessions for new tenants (after a deploy, or a batch of new tenants) stalls the whole process.Design
MasterTableTenancy.GetTenantis now synchronous end to end. On a cache miss,tryFindTenantDatabaseSynchronouslydoes a syncOpenConnection()andExecuteReader()with one statement on the calling thread. There is noTaskanywhere in the chain. Npgsql writes and reads a single-statement command entirely on the calling thread (see #5972).Shared with the async path:
selectTenantRowSql);readTenantRowValues: the row is already buffered, so the sync accessors do no I/O on either path);buildTenantDatabase).Apart from that, the async path is unchanged.
Caching: the same as before. Both paths add to the
ImHashMapwithAddOrUpdateand hold no lock, and this lookup never provisions anything.Multiplexing: Npgsql refuses synchronous I/O when
Multiplexing=true("Synchronous command execution is not supported when multiplexing is on"). A multiplexed control database therefore keeps the old sync-over-async read rather than starting to throwNotSupportedException.Public API: no new public members and no signature changes.
Still sync-over-async, and why
MasterTableTenancyMultiplexing=trueSingleServerMultiTenancyCREATE DATABASE. The database cache and its lock are private to Weasel'sSingleServerDatabaseCollection<T>ShardedTenancydocs/configuration/multitenancy.mdhas a new section, "Synchronous sessions and tenants that are not cached yet". It covers:ApplyAllDatabaseChangesOnStartup(),await store.Tenancy.GetTenantAsync(tenantId)before a new tenant's first sync session, or an async*SerializableSessionAsyncfactory where a serializable session fits.Tests (MultiTenancyTests, net10.0, PostgreSQL 17.10)
sync_tenant_resolution_thread_pool_tests(own non-parallelTenantResolutionThreadPoolCollection): writes 87 tenant rows behind the store's back, warms the control-table check with one async resolution, then opens 86 sync sessions at once from pool threads while a 10 ms heartbeat measures how late it runs. The threshold is 500 ms.DOTNET_PROCESSOR_COUNT=2sync_master_table_tenant_resolution_with_multiplexing: with a multiplexed control database, a sync session and a syncGetTenantfor uncached tenants succeed, and an unknown tenant is still refused withUnknownTenantIdException. With the multiplexing guard removed it fails withNotSupportedException.master_table*,using_master_table*),find_database_for_tenantand the two new classes pass: 47/47.docs/**/*.md.🤖 Generated with Claude Code