Skip to content

[9.0] Resolve uncached tenants synchronously on the sync GetTenant path (#5985) - #5988

Merged
jeremydmiller merged 1 commit into
9.0from
fix/5985-sync-gettenant-9.0
Nov 15, 2026
Merged

jeremydmiller merged 1 commit into
9.0from
fix/5985-sync-gettenant-9.0

Conversation

@jeremydmiller

Copy link
Copy Markdown
Member

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's MasterTableTenancyBase (JasperFx/weasel#838).

The problem

The synchronous session factories and synchronous ForTenant resolve a tenant that is not cached yet through ITenancy.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.GetTenant is now synchronous end to end. On a cache miss, tryFindTenantDatabaseSynchronously does a sync OpenConnection() and ExecuteReader() with one statement on the calling thread. There is no Task anywhere in the chain. Npgsql writes and reads a single-statement command entirely on the calling thread (see #5972).

  • Shared with the async path:

    • the SQL text (selectTenantRowSql);
    • the row mapping (readTenantRowValues: the row is already buffered, so the sync accessors do no I/O on either path);
    • the database build and the disabled/unknown handling (buildTenantDatabase).

    Apart from that, the async path is unchanged.

  • Caching: the same as before. Both paths add to the ImHashMap with AddOrUpdate and 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 throw NotSupportedException.

  • Public API: no new public members and no signature changes.

Still sync-over-async, and why

Tenancy What still blocks on async work Follow-up
MasterTableTenancy Only a control database with Multiplexing=true —
SingleServerMultiTenancy The existence check and CREATE DATABASE. The database cache and its lock are private to Weasel's SingleServerDatabaseCollection<T> JasperFx/weasel#839
ShardedTenancy Sharding always turns on managed partitioning, so a tenant's first resolution in a process runs the #4942 partition repair (async registry hydrate, catalog sweep, DDL). New tenants also go through the user's async assignment strategy. A sync lookup alone would not remove the blocking #5986

docs/configuration/multitenancy.md has a new section, "Synchronous sessions and tenants that are not cached yet". It covers:

  • the hazard for each tenancy;
  • the remedies: warm known tenants with ApplyAllDatabaseChangesOnStartup(), await store.Tenancy.GetTenantAsync(tenantId) before a new tenant's first sync session, or an async *SerializableSessionAsync factory where a serializable session fits.

Tests (MultiTenancyTests, net10.0, PostgreSQL 17.10)

  • sync_tenant_resolution_thread_pool_tests (own non-parallel TenantResolutionThreadPoolCollection): 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.
Run Processors Worst heartbeat delay
before the fix 18 2,613 ms, 2,676 ms (red)
after the fix 18 1–3 ms over 4 runs; burst 16–19 ms (green)
after the fix, DOTNET_PROCESSOR_COUNT=2 2 1 ms (green)
  • sync_master_table_tenant_resolution_with_multiplexing: with a multiplexed control database, a sync session and a sync GetTenant for uncached tenants succeed, and an unknown tenant is still refused with UnknownTenantIdException. With the multiplexing guard removed it fails with NotSupportedException.
  • The master-table classes (master_table*, using_master_table*), find_database_for_tenant and the two new classes pass: 47/47.
  • markdownlint and cspell are clean over docs/**/*.md.

🤖 Generated with Claude Code

…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>
@jeremydmiller
jeremydmiller merged commit b05c19c into 9.0 Nov 15, 2026
31 checks passed
@jeremydmiller
jeremydmiller deleted the fix/5985-sync-gettenant-9.0 branch November 15, 2026 23:00
This was referenced Nov 16, 2026
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.

1 participant