mirror of
https://github.com/archtechx/tenancy.git
synced 2026-08-06 13:04:03 +00:00
[MINOR BC] Fix pending tenant pull race conditions (#1463)
> Minor breaking change: clearing pending_since no longer fires Eloquent events, PullingPendingTenant is now fired at a different point in the lifecycle and does not guarantee the tenant will actually be pulled. `pullPendingFromPool` had a race condition when user A attempted to pull a tenant at the same time as user B. Both could end up grabbing the same tenant, and the result was unexpected, e.g. one of them ending up with no pending tenant pulled at all even though there was a pending tenant in the pool. instead of selecting a pending tenant and updating the same model, we now run `update()` conditionally -- it clears `pending_since` _only_ if the tenant is still pending, and we check the affected row count. Only one process can get a row back, the other gets 0 and retries with the next pending candidate in the pool. The loop always terminates since every lost claim means the pool shrank by one. Eventually it's empty and we create a new tenant (or return null). The claim and the attribute update happen in a single transaction now, so if updating `$attributes` fails, the claim rolls back and the tenant stays in the pool. Added a regression test that simulates a concurrent "steal" synchronously via a PullingPendingTenant listener. Fails with the old code, passes with the HasPending changes. Very minor BC: - Clearing `pending_since` no longer fires model updating/updated events (since the update goes through query builder). `PendingTenantPulled` still fires the same as before and is the listener you'd want to use anyway. - `PullingPendingTenant` now fires before the claim (and outside the transaction), so it can fire more than once with concurrent pulls (e.g. when a tenant gets claimed by someone else). `PendingTenantPulled` is still the one that fires exactly once for the actually pulled tenant. --------- Co-authored-by: Samuel Stancl <samuel@archte.ch>
This commit is contained in:
parent
652bc987ce
commit
04da9c896b
2 changed files with 88 additions and 17 deletions
|
|
@ -100,27 +100,51 @@ trait HasPending
|
|||
*/
|
||||
public static function pullPendingFromPool(bool $firstOrCreate = false, array $attributes = []): ?Tenant
|
||||
{
|
||||
$tenant = DB::transaction(function () use ($attributes): ?Tenant {
|
||||
/** @var (Model&Tenant)|null $tenant */
|
||||
$tenant = static::onlyPending()->first();
|
||||
// Attempt pulling a pending tenant.
|
||||
// The loop handles the case where a single tenant is being pulled by multiple processes at the same time.
|
||||
// If a tenant was pulled by a concurrent process, try pulling the next one in the pool.
|
||||
while (true) {
|
||||
/** @var (Model&Tenant)|null $pullCandidate */
|
||||
$pullCandidate = static::onlyPending()->first();
|
||||
|
||||
if ($tenant !== null) {
|
||||
event(new PullingPendingTenant($tenant));
|
||||
$tenant->update(array_merge($attributes, [
|
||||
'pending_since' => null,
|
||||
]));
|
||||
if ($pullCandidate === null) {
|
||||
return $firstOrCreate ? static::create($attributes) : null;
|
||||
}
|
||||
|
||||
// Fired before the claim, so it can fire once per attempt, including for a candidate
|
||||
// that ends up being claimed by a different process (in which case the loop retries).
|
||||
// PendingTenantPulled (below) fires exactly once, for the actually pulled tenant.
|
||||
event(new PullingPendingTenant($pullCandidate));
|
||||
|
||||
$tenant = DB::transaction(function () use ($pullCandidate, $attributes): ?Tenant {
|
||||
$tenantWasPulled = static::onlyPending()
|
||||
->whereKey($pullCandidate->getKey())
|
||||
->update([$pullCandidate->getColumnForQuery('pending_since') => null]) > 0;
|
||||
|
||||
if (! $tenantWasPulled) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// The tenant's pending_since was just cleared, and a PullingPendingTenant listener
|
||||
// may have made changes to the tenant, so re-fetch it to make sure it's up to date.
|
||||
/** @var Model&Tenant $pulledTenant */
|
||||
$pulledTenant = static::findOrFail($pullCandidate->getKey());
|
||||
|
||||
if (! empty($attributes)) {
|
||||
$pulledTenant->update($attributes);
|
||||
}
|
||||
|
||||
return $pulledTenant;
|
||||
});
|
||||
|
||||
if ($tenant === null) {
|
||||
// If another pull claimed this tenant first, try claiming the next one
|
||||
continue;
|
||||
}
|
||||
|
||||
event(new PendingTenantPulled($tenant));
|
||||
|
||||
return $tenant;
|
||||
});
|
||||
|
||||
if ($tenant === null) {
|
||||
return $firstOrCreate ? static::create($attributes) : null;
|
||||
}
|
||||
|
||||
// Only triggered if a tenant that was pulled from the pool is returned
|
||||
event(new PendingTenantPulled($tenant));
|
||||
|
||||
return $tenant;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue