Skip to content

[9.0] ShardedTenancy: never reassign an already-assigned tenant when its shard isn't loaded (#5995) - #5997

Merged
jeremydmiller merged 2 commits into
9.0from
fix/5995-shard-reassignment-9.0
Nov 16, 2026
Merged

jeremydmiller merged 2 commits into
9.0from
fix/5995-shard-reassignment-9.0

Conversation

@jeremydmiller

Copy link
Copy Markdown
Member

Backport of #5996

Fixes #5995 on the 9.0 branch. This is the master commit cherry-picked with -x, then origin/9.0 merged in. ShardedTenancy.cs was identical on both branches before the fix, so the cherry-pick applied cleanly.

The details are in #5996. In short: an existing assignment row is now never overwritten by re-running the assignment strategy. When the assigned shard isn't cached locally (no AddDatabase, no BuildDatabases), it's loaded from the pool table. When the pool no longer has the shard, the new AssignedShardNotFoundException names the tenant and the shard.

Evidence on this branch (net10.0, database marten_9_5995)

  • Bug_5995_sharded_existing_assignment_is_never_reassigned, with the fix reverted: 5 of 7 failed (the row moved from _a to _b, the racing stores disagreed, no exception for the missing shard)
  • With the fix: 7/7
  • MultiTenancyTests sharded classes: 48/48
  • TenantPartitionedEventsTests.Sharded: 44/44

🤖 Generated with Claude Code

jeremydmiller and others added 2 commits November 15, 2026 18:53
…ard isn't loaded (#5995)

A store that was given no AddDatabase() and had not run BuildDatabases() did not
find an assigned tenant's shard in its local cache. findOrAssignTenantDatabaseAsync
then treated the tenant as new under the advisory lock, re-ran the assignment
strategy and overwrote the assignment row with "on conflict do update", leaving the
tenant's data behind on the old shard without a word.

An existing assignment is now final on both checks, before and under the lock: the
assigned shard is loaded from the pool table the way BuildDatabases loads it, and
the strategy runs only for a tenant with no assignment row at all. When the pool
no longer has the assigned shard, the new AssignedShardNotFoundException names the
tenant and the shard instead of moving the tenant.

The same defect let two processes resolving one new tenant disagree: the lock
serialized them, but the second did not have the first's shard cached and
reassigned the tenant to its own pick. The race test covers that.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit e7dc0ac)
@jeremydmiller
jeremydmiller merged commit ef0949b into 9.0 Nov 16, 2026
31 checks passed
@jeremydmiller
jeremydmiller deleted the fix/5995-shard-reassignment-9.0 branch November 16, 2026 01:43
@jeremydmiller jeremydmiller mentioned this pull request Nov 16, 2026
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