From fb0d43daf3b4c787f7334553bd3798157addbac0 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Mon, 27 Apr 2026 07:54:56 +0100 Subject: [PATCH 01/26] feat: distributed locking for platform-database writes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds two DI factories and wires them where coordination is needed: - distributedLock — skip on contention, void return. For idempotent fan-out where N pods doing the same write is wasteful but losing the race is correct. - distributedLockOrFail — blocking acquire (3s default) then throws GENERAL_RESOURCE_LOCKED (HTTP 409) on contention. For read-modify-write on shared mutable state where a silent skip would drop a user's change. Both factories: _APP_LOCKING_ENABLED kill switch (set 'disabled' for fail-open), fail-open on Redis-unreachable, and a lock.attempts telemetry counter sliced by outcome and target collection. Wired sites: - shared/api.php × 3 (distributedLock): keys.accessedAt + sdks, projects.accessedAt, users.accessedAt. Reduces redundant writes and cache-purge fan-out under request bursts on the same project. - Project/Services/Update.php × 1 (distributedLockOrFail): the services map toggle. Re-reads inside the lock so the baseline reflects concurrent updates. Two simultaneous toggles to different services no longer lose one of them. Lock key namespace: lock:platform:{collection}:{id}. Dep: premtsd-code/lock pinned to a specific commit as a development preview. Migration to utopia-php/lock is a follow-up once that package is published. --- app/config/errors.php | 5 + app/config/variables.php | 9 ++ app/controllers/shared/api.php | 40 ++++-- app/init/resources.php | 132 ++++++++++++++++++ composer.json | 9 +- composer.lock | 75 +++++++++- src/Appwrite/Extend/Exception.php | 1 + .../Project/Http/Project/Services/Update.php | 25 +++- 8 files changed, 272 insertions(+), 24 deletions(-) diff --git a/app/config/errors.php b/app/config/errors.php index 07b0cd59ed..f5603f9223 100644 --- a/app/config/errors.php +++ b/app/config/errors.php @@ -54,6 +54,11 @@ return [ 'description' => 'Rate limit for the current endpoint has been exceeded. Please try again after some time.', 'code' => 429, ], + Exception::GENERAL_RESOURCE_LOCKED => [ + 'name' => Exception::GENERAL_RESOURCE_LOCKED, + 'description' => 'The requested resource is currently being modified by another request. Please retry after a brief delay.', + 'code' => 409, + ], Exception::GENERAL_SMTP_DISABLED => [ 'name' => Exception::GENERAL_SMTP_DISABLED, 'description' => 'SMTP is disabled on your Appwrite instance. You can learn more about setting up SMTP in our docs.', diff --git a/app/config/variables.php b/app/config/variables.php index c834656ff4..5dd7196266 100644 --- a/app/config/variables.php +++ b/app/config/variables.php @@ -34,6 +34,15 @@ return [ 'question' => '', 'filter' => '' ], + [ + 'name' => '_APP_LOCKING_ENABLED', + 'description' => 'Enable distributed locking for platform writes. Locks coordinate concurrent updates across API pods so that read-modify-write operations on shared documents do not lose updates. By default, set to \'enabled\'. Set to \'disabled\' as an emergency kill switch — locks become no-ops (fail-open) and concurrent writes will race.', + 'introduction' => '1.10.0', + 'default' => 'enabled', + 'required' => false, + 'question' => '', + 'filter' => '' + ], [ 'name' => '_APP_OPTIONS_FORCE_HTTPS', 'description' => 'Allows you to force HTTPS connection to your API. This feature redirects any HTTP call to HTTPS and adds the \'Strict-Transport-Security\' header to all HTTP responses. By default, set to \'enabled\'. To disable, set to \'disabled\'. This feature will work only when your ports are set to default 80 and 443, and you have set up wildcard certificates with DNS challenge.', diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 7c2f527ccf..b07eec1a99 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -100,7 +100,8 @@ Http::init() ->inject('team') ->inject('apiKey') ->inject('authorization') - ->action(function (Http $utopia, Request $request, Database $dbForPlatform, Database $dbForProject, AuditContext $auditContext, Document $project, User $user, ?Document $session, array $servers, string $mode, Document $team, ?Key $apiKey, Authorization $authorization) { + ->inject('distributedLock') + ->action(function (Http $utopia, Request $request, Database $dbForPlatform, Database $dbForProject, AuditContext $auditContext, Document $project, User $user, ?Document $session, array $servers, string $mode, Document $team, ?Key $apiKey, Authorization $authorization, callable $distributedLock) { $route = $utopia->getRoute(); if ($route === null) { throw new AppwriteException(AppwriteException::GENERAL_ROUTE_NOT_FOUND); @@ -249,15 +250,20 @@ Http::init() } if (! $updates->isEmpty()) { - $dbForPlatform->getAuthorization()->skip(fn () => $dbForPlatform->updateDocument('keys', $dbKey->getId(), $updates)); + // Serialize concurrent per-request writes to this key across API nodes. + // Skip on contention is safe: the winner applies the same idempotent update; + // if this node observed a new SDK the other didn't, the next request catches up. + $distributedLock('lock:platform:keys:' . $dbKey->getId(), function () use ($dbForPlatform, $dbKey, $updates, $apiKey, $project, $user, $team) { + $dbForPlatform->getAuthorization()->skip(fn () => $dbForPlatform->updateDocument('keys', $dbKey->getId(), $updates)); - if (! empty($apiKey->getProjectId())) { - $dbForPlatform->getAuthorization()->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); - } elseif (! empty($apiKey->getUserId())) { - $dbForPlatform->getAuthorization()->skip(fn () => $dbForPlatform->purgeCachedDocument('users', $user->getId())); - } elseif (! empty($apiKey->getTeamId())) { - $dbForPlatform->getAuthorization()->skip(fn () => $dbForPlatform->purgeCachedDocument('teams', $team->getId())); - } + if (! empty($apiKey->getProjectId())) { + $dbForPlatform->getAuthorization()->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); + } elseif (! empty($apiKey->getUserId())) { + $dbForPlatform->getAuthorization()->skip(fn () => $dbForPlatform->purgeCachedDocument('users', $user->getId())); + } elseif (! empty($apiKey->getTeamId())) { + $dbForPlatform->getAuthorization()->skip(fn () => $dbForPlatform->purgeCachedDocument('teams', $team->getId())); + } + }); } $userClone = clone $user; @@ -388,9 +394,11 @@ Http::init() if ($project->getId() !== 'console') { $accessedAt = $project->getAttribute('accessedAt', 0); if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_PROJECT_ACCESS)) > $accessedAt) { - $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'accessedAt' => DateTime::now() - ]))); + $distributedLock('lock:platform:projects:' . $project->getId(), function () use ($authorization, $dbForPlatform, $project) { + $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'accessedAt' => DateTime::now() + ]))); + }); } } @@ -407,9 +415,11 @@ Http::init() 'accessedAt' => $user->getAttribute('accessedAt') ])); } else { - $authorization->skip(fn () => $dbForPlatform->updateDocument('users', $user->getId(), new Document([ - 'accessedAt' => $user->getAttribute('accessedAt') - ]))); + $distributedLock('lock:platform:users:' . $user->getId(), function () use ($authorization, $dbForPlatform, $user) { + $authorization->skip(fn () => $dbForPlatform->updateDocument('users', $user->getId(), new Document([ + 'accessedAt' => $user->getAttribute('accessedAt') + ]))); + }); } } } diff --git a/app/init/resources.php b/app/init/resources.php index 29506bfc9c..4c3a684d4e 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -8,6 +8,7 @@ use Appwrite\Event\Publisher\Migration as MigrationPublisher; use Appwrite\Event\Publisher\Screenshot as ScreenshotPublisher; use Appwrite\Event\Publisher\StatsResources as StatsResourcesPublisher; use Appwrite\Event\Publisher\Usage as UsagePublisher; +use Appwrite\Extend\Exception as AppwriteException; use Appwrite\Utopia\Database\Documents\User; use Executor\Executor; use Utopia\Abuse\Adapters\TimeLimit\Redis as TimeLimitRedis; @@ -22,6 +23,9 @@ use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; use Utopia\DI\Container; use Utopia\DSN\DSN; +use Utopia\Lock\Adapter\Redis as LockRedisAdapter; +use Utopia\Lock\Exception\LockAcquireException; +use Utopia\Lock\Lock; use Utopia\Pools\Group; use Utopia\Queue\Broker\Pool as BrokerPool; use Utopia\Queue\Publisher; @@ -213,6 +217,134 @@ $container->set('timelimit', function (\Redis $redis) { }; }, ['redis']); +// Extract the collection segment ("keys" / "projects" / "users") from keys +// of the form "lock:platform:{target}:{id}" so metrics can slice by target. +// Used by both distributedLock and distributedLockOrFail factories below. +$lockTargetOf = function (string $key): string { + $parts = explode(':', $key, 4); + return $parts[2] ?? 'unknown'; +}; + +/** + * Distributed-lock factory: skip-on-contention variant. + * + * For idempotent writes where losing the race is correct (e.g., per-request + * `accessedAt` updates from N pods all writing the same value). On contention, + * the callback is silently skipped — another pod is doing the same work. + * + * Behavior: + * - Non-blocking acquire (one attempt). On conflict, skip and return. + * - Fail-open: if Redis is unreachable, run the callback unlocked + warn. + * - Kill switch: `_APP_LOCKING_ENABLED=disabled` runs the callback unlocked. + * + * Returns void — the caller can't distinguish "acquired and ran" from "skipped". + * + * Metric: `lock.attempts{outcome,target}` where outcome ∈ {acquired, skipped, + * backend_error, release_error}. + */ +$container->set('distributedLock', function (\Redis $redis, Telemetry $telemetry) use ($lockTargetOf) { + $enabled = System::getEnv('_APP_LOCKING_ENABLED', 'enabled') !== 'disabled'; + $attempts = $telemetry->createCounter('lock.attempts', null, 'Distributed lock acquire outcomes'); + + if (! $enabled) { + return function (string $key, \Closure $fn, float $ttl = 5.0): void { + $fn(); + }; + } + + return function (string $key, \Closure $fn, float $ttl = 5.0) use ($redis, $attempts, $lockTargetOf): void { + $target = $lockTargetOf($key); + $lock = new Lock(new LockRedisAdapter($redis), $key, $ttl); + + try { + $acquired = $lock->acquire(); + } catch (LockAcquireException $e) { + $attempts->add(1, ['outcome' => 'backend_error', 'target' => $target]); + Console::warning("Lock backend unavailable for {$key}, proceeding unlocked: {$e->getMessage()}"); + $fn(); + return; + } + + if (! $acquired) { + $attempts->add(1, ['outcome' => 'skipped', 'target' => $target]); + return; + } + + $attempts->add(1, ['outcome' => 'acquired', 'target' => $target]); + try { + $fn(); + } finally { + try { + $lock->release(); + } catch (\Throwable $e) { + $attempts->add(1, ['outcome' => 'release_error', 'target' => $target]); + Console::warning("Lock release failed for {$key}: {$e->getMessage()}"); + } + } + }; +}, ['redis', 'telemetry']); + +/** + * Distributed-lock factory: 409-on-contention variant. + * + * For explicit user-write endpoints where read-modify-write on shared mutable + * state must NOT silently drop a request. On contention, throws + * `Exception::GENERAL_RESOURCE_LOCKED` (HTTP 409) so the client retries. + * + * Behavior: + * - Blocking acquire with short timeout (default 3s). + * - On timeout, throws `GENERAL_RESOURCE_LOCKED`. + * - Fail-open: backend unreachable runs the callback unlocked + warning. + * - Kill switch: `_APP_LOCKING_ENABLED=disabled` runs the callback unlocked. + * - Returns the callback's return value so callers can use the result. + * + * Metric: `lock.attempts{outcome,target}` where outcome ∈ {acquired, contended, + * backend_error, release_error}. + */ +$container->set('distributedLockOrFail', function (\Redis $redis, Telemetry $telemetry) use ($lockTargetOf) { + $enabled = System::getEnv('_APP_LOCKING_ENABLED', 'enabled') !== 'disabled'; + $attempts = $telemetry->createCounter('lock.attempts', null, 'Distributed lock acquire outcomes'); + + if (! $enabled) { + return function (string $key, \Closure $fn, float $ttl = 10.0, float $waitTimeout = 3.0): mixed { + return $fn(); + }; + } + + return function (string $key, \Closure $fn, float $ttl = 10.0, float $waitTimeout = 3.0) use ($redis, $attempts, $lockTargetOf): mixed { + $target = $lockTargetOf($key); + $lock = new Lock(new LockRedisAdapter($redis), $key, $ttl); + + try { + $acquired = $lock->acquire(blocking: true, waitTimeout: $waitTimeout, retryDelay: 0.1); + } catch (LockAcquireException $e) { + $attempts->add(1, ['outcome' => 'backend_error', 'target' => $target]); + Console::warning("Lock backend unavailable for {$key}, proceeding unlocked: {$e->getMessage()}"); + return $fn(); + } + + if (! $acquired) { + $attempts->add(1, ['outcome' => 'contended', 'target' => $target]); + throw new AppwriteException( + AppwriteException::GENERAL_RESOURCE_LOCKED, + "Resource '{$key}' is currently being modified by another request. Please retry." + ); + } + + $attempts->add(1, ['outcome' => 'acquired', 'target' => $target]); + try { + return $fn(); + } finally { + try { + $lock->release(); + } catch (\Throwable $e) { + $attempts->add(1, ['outcome' => 'release_error', 'target' => $target]); + Console::warning("Lock release failed for {$key}: {$e->getMessage()}"); + } + } + }; +}, ['redis', 'telemetry']); + $container->set('deviceForLocal', function (Telemetry $telemetry) { return new Device\Telemetry($telemetry, new Local()); }, ['telemetry']); diff --git a/composer.json b/composer.json index 6312243e32..50ecd43cb3 100644 --- a/composer.json +++ b/composer.json @@ -94,8 +94,15 @@ "spomky-labs/otphp": "11.*", "webonyx/graphql-php": "15.31.*", "league/csv": "9.14.*", - "enshrined/svg-sanitize": "0.22.*" + "enshrined/svg-sanitize": "0.22.*", + "premtsd-code/lock": "dev-main#7ec10dae33b950bc0f473c51e5b165b20754760c" }, + "repositories": [ + { + "type": "vcs", + "url": "https://github.com/premtsd-code/lock" + } + ], "require-dev": { "ext-fileinfo": "*", "appwrite/sdk-generator": "*", diff --git a/composer.lock b/composer.lock index 02590020e0..99d1a4df9f 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "c5ae97637fd0ec0a950044d1c33677ea", + "content-hash": "5108bf79532c9d666fb96daa9ec4b3d5", "packages": [ { "name": "adhocore/jwt", @@ -2104,6 +2104,75 @@ ], "time": "2026-04-10T01:33:53+00:00" }, + { + "name": "premtsd-code/lock", + "version": "dev-main", + "source": { + "type": "git", + "url": "https://github.com/premtsd-code/lock.git", + "reference": "7ec10dae33b950bc0f473c51e5b165b20754760c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/premtsd-code/lock/zipball/7ec10dae33b950bc0f473c51e5b165b20754760c", + "reference": "7ec10dae33b950bc0f473c51e5b165b20754760c", + "shasum": "" + }, + "require": { + "php": ">=8.1" + }, + "require-dev": { + "laravel/pint": "1.2.*", + "phpstan/phpstan": "^1.12", + "phpunit/phpunit": "^9.5" + }, + "suggest": { + "ext-redis": "Required for Utopia\\Lock\\Adapter\\Redis" + }, + "default-branch": true, + "type": "library", + "autoload": { + "psr-4": { + "Utopia\\Lock\\": "src/Lock" + } + }, + "autoload-dev": { + "psr-4": { + "Utopia\\Tests\\": "tests/Lock" + } + }, + "scripts": { + "check": [ + "./vendor/bin/phpstan analyse --level 8 src tests" + ], + "lint": [ + "./vendor/bin/pint --test" + ], + "format": [ + "./vendor/bin/pint" + ], + "test": [ + "./vendor/bin/phpunit --configuration phpunit.xml tests" + ] + }, + "license": [ + "MIT" + ], + "description": "A distributed lock library for PHP, part of the Utopia framework", + "keywords": [ + "distributed", + "lock", + "mutex", + "php", + "redis", + "utopia" + ], + "support": { + "source": "https://github.com/premtsd-code/lock/tree/main", + "issues": "https://github.com/premtsd-code/lock/issues" + }, + "time": "2026-04-26T15:50:08+00:00" + }, { "name": "psr/clock", "version": "1.0.0", @@ -8443,7 +8512,9 @@ ], "aliases": [], "minimum-stability": "dev", - "stability-flags": {}, + "stability-flags": { + "premtsd-code/lock": 20 + }, "prefer-stable": true, "prefer-lowest": false, "platform": { diff --git a/src/Appwrite/Extend/Exception.php b/src/Appwrite/Extend/Exception.php index 6fc3e88635..c47647cfe1 100644 --- a/src/Appwrite/Extend/Exception.php +++ b/src/Appwrite/Extend/Exception.php @@ -45,6 +45,7 @@ class Exception extends \Exception public const string GENERAL_SERVICE_DISABLED = 'general_service_disabled'; public const string GENERAL_UNAUTHORIZED_SCOPE = 'general_unauthorized_scope'; public const string GENERAL_RATE_LIMIT_EXCEEDED = 'general_rate_limit_exceeded'; + public const string GENERAL_RESOURCE_LOCKED = 'general_resource_locked'; public const string GENERAL_SMTP_DISABLED = 'general_smtp_disabled'; public const string GENERAL_PHONE_DISABLED = 'general_phone_disabled'; public const string GENERAL_ARGUMENT_INVALID = 'general_argument_invalid'; diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php index 7aab6f5ad0..e7b048b734 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php @@ -60,6 +60,7 @@ class Update extends Action ->inject('project') ->inject('authorization') ->inject('queueForEvents') + ->inject('distributedLockOrFail') ->callback($this->action(...)); } @@ -70,14 +71,26 @@ class Update extends Action Database $dbForPlatform, Document $project, Authorization $authorization, - Event $queueForEvents + Event $queueForEvents, + callable $distributedLockOrFail, ): void { - $services = $project->getAttribute('services', []); - $services[$serviceId] = $enabled; + // The services map is a JSON object on the project document. Two + // concurrent service toggles read the same baseline, each set their + // own key, and the second updateDocument() overwrites the first — + // silent lost-update on a sparse write. Lock the project doc for + // the read-modify-write window; re-read inside the lock so the + // baseline reflects any update that landed between request init + // and lock acquisition. + $project = $distributedLockOrFail("platform:project:{$project->getId()}", function () use ($project, $serviceId, $enabled, $dbForPlatform, $authorization) { + $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); - $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'services' => $services, - ]))); + $services = $project->getAttribute('services', []); + $services[$serviceId] = $enabled; + + return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'services' => $services, + ]))); + }); $queueForEvents->setParam('serviceId', $serviceId); From 784babcf457bc5fee22ea1d59361a5f9d9318d02 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Mon, 27 Apr 2026 09:13:35 +0100 Subject: [PATCH 02/26] fix: address Greptile review on PR #12062 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three P1 issues flagged on the initial commit: 1. Lock key in updateProjectService used "platform:project:{id}" — missing the "lock:" namespace prefix and using singular "project" instead of the conventional plural collection name. The factory's `lockTargetOf` extracts segment [2] as the telemetry target, so the broken key was emitting the project ID itself as the target attribute (cardinality blowup, broken dashboards). Fixed to "lock:platform:projects:{id}" matching the convention used in shared/api.php. 2. The 409 contention exception embedded the raw Redis lock key in its user-facing message, leaking internal collection names and the locking namespace to API clients. Removed the custom message so the catalog default ("The requested resource is currently being modified...") is used. Telemetry already carries the target collection for operator-side observability. 3. _APP_LOCKING_ENABLED variable doc had `introduction: '1.10.0'` on a 1.9.x-targeted PR. Corrected to '1.9.3' (next 1.9.x patch). --- app/config/variables.php | 2 +- app/init/resources.php | 10 ++++++---- .../Modules/Project/Http/Project/Services/Update.php | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/config/variables.php b/app/config/variables.php index 5dd7196266..46a6c7d1e2 100644 --- a/app/config/variables.php +++ b/app/config/variables.php @@ -37,7 +37,7 @@ return [ [ 'name' => '_APP_LOCKING_ENABLED', 'description' => 'Enable distributed locking for platform writes. Locks coordinate concurrent updates across API pods so that read-modify-write operations on shared documents do not lose updates. By default, set to \'enabled\'. Set to \'disabled\' as an emergency kill switch — locks become no-ops (fail-open) and concurrent writes will race.', - 'introduction' => '1.10.0', + 'introduction' => '1.9.3', 'default' => 'enabled', 'required' => false, 'question' => '', diff --git a/app/init/resources.php b/app/init/resources.php index 4c3a684d4e..d3443ee337 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -325,10 +325,12 @@ $container->set('distributedLockOrFail', function (\Redis $redis, Telemetry $tel if (! $acquired) { $attempts->add(1, ['outcome' => 'contended', 'target' => $target]); - throw new AppwriteException( - AppwriteException::GENERAL_RESOURCE_LOCKED, - "Resource '{$key}' is currently being modified by another request. Please retry." - ); + // Don't pass a custom message — the catalog message in + // app/config/errors.php is reused so we don't leak the internal + // lock key (which embeds collection name and document id) into a + // user-facing 409 response. The telemetry attribute already + // carries the target collection for operator-side observability. + throw new AppwriteException(AppwriteException::GENERAL_RESOURCE_LOCKED); } $attempts->add(1, ['outcome' => 'acquired', 'target' => $target]); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php index e7b048b734..1126da8897 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php @@ -81,7 +81,7 @@ class Update extends Action // the read-modify-write window; re-read inside the lock so the // baseline reflects any update that landed between request init // and lock acquisition. - $project = $distributedLockOrFail("platform:project:{$project->getId()}", function () use ($project, $serviceId, $enabled, $dbForPlatform, $authorization) { + $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $serviceId, $enabled, $dbForPlatform, $authorization) { $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); $services = $project->getAttribute('services', []); From 77982f4b9037a4dc0fd39bf5655134c7af00c632 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Mon, 27 Apr 2026 17:25:15 +0100 Subject: [PATCH 03/26] test: concurrency proof for distributedLockOrFail pilot MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two complementary tests for the lost-update bug the lock fixes, plus the test-Client patches needed to make Swoole-coroutine HTTP work. - tests/e2e/Services/Project/ServicesBase.php :: testConcurrentTogglesAllPersist Fires N parallel PATCHes via Swoole\Coroutine\run + SWOOLE_HOOK_CURL, then refetches the project and asserts (successCount == enabledCount). With the lock disabled (_APP_LOCKING_ENABLED=disabled) sparse updateDocument() calls overwrite each other and the assertion fails — proving the test detects the bug. - dev/test-distributed-lock.sh Same proof via curl + bash background jobs. Runnable outside the PHPUnit suite for manual verification or load-style sweeps. Reads APPWRITE_ENDPOINT / APPWRITE_PROJECT_ID / APPWRITE_API_KEY. - tests/e2e/Client.php (test util): * Skip CURLOPT_PATH_AS_IS (option 234) when SWOOLE_HOOK_CURL is active. Swoole's emulated cURL doesn't support it; setting it fatal-errors as soon as any test enables the cURL hook. * Don't redundantly set CURLOPT_NOBODY=false on non-HEAD requests (false is cURL's default). Under Swoole's hooked cURL this strips the body of PATCH/PUT requests, hitting the framework's 404 fallback instead of the intended route. Both changes preserve native (non-hooked) cURL behavior unchanged. They unblock any future test that wants real parallel HTTP via Swoole\Coroutine\run + the cURL hook. Both follow the same proof-of-bug pattern: run with locking enabled (must pass) AND with it disabled (must fail). Verified locally against the running stack: _APP_LOCKING_ENABLED=enabled -> PASS (15 assertions) _APP_LOCKING_ENABLED=disabled -> FAIL (successCount=5 enabledCount=4) --- dev/test-distributed-lock.sh | 157 ++++++++++++++++++++ tests/e2e/Client.php | 15 +- tests/e2e/Services/Project/ServicesBase.php | 88 +++++++++++ 3 files changed, 257 insertions(+), 3 deletions(-) create mode 100755 dev/test-distributed-lock.sh diff --git a/dev/test-distributed-lock.sh b/dev/test-distributed-lock.sh new file mode 100755 index 0000000000..6a740a4169 --- /dev/null +++ b/dev/test-distributed-lock.sh @@ -0,0 +1,157 @@ +#!/usr/bin/env bash +# +# Manual smoke test for the distributed-lock pilot on `updateProjectService`. +# +# Fires N concurrent PATCH /project/services/:serviceId requests against the +# same project, each toggling a different service to "enabled=true". Then +# refetches the project and counts how many of the targeted services actually +# persisted. +# +# Usage: +# APPWRITE_ENDPOINT=http://localhost \ +# APPWRITE_PROJECT_ID= \ +# APPWRITE_API_KEY= \ +# ./dev/test-distributed-lock.sh +# +# Required scopes on the API key: +# - project.write (to toggle services) +# - projects.read (to refetch project state via GET /v1/projects/:id) +# +# To prove the lock fixes the bug, run twice: +# 1. With `_APP_LOCKING_ENABLED=enabled` (default): expect successes == enabled +# 2. With `_APP_LOCKING_ENABLED=disabled` : expect successes > enabled (lost updates) +# +# Set `_APP_LOCKING_ENABLED` in `.env` and `docker compose up -d --force-recreate` +# between runs. Use `--parallelism N` to tune the concurrency level. + +set -eu + +# --- Configuration --------------------------------------------------------- + +ENDPOINT="${APPWRITE_ENDPOINT:?set APPWRITE_ENDPOINT, e.g. http://localhost}" +PROJECT_ID="${APPWRITE_PROJECT_ID:?set APPWRITE_PROJECT_ID}" +API_KEY="${APPWRITE_API_KEY:?set APPWRITE_API_KEY}" +PARALLELISM="${PARALLELISM:-5}" + +# Services to toggle concurrently — must be in the optional-services list of +# the project. These are the same set used by the e2e ServicesBase trait. +SERVICES=(teams storage functions sites messaging) + +# Trim or extend SERVICES to match PARALLELISM. +SERVICES=("${SERVICES[@]:0:$PARALLELISM}") + +if [ "${#SERVICES[@]}" -lt 2 ]; then + echo "ERROR: PARALLELISM must be >= 2 to detect contention" >&2 + exit 1 +fi + +# --- Helpers --------------------------------------------------------------- + +curl_appwrite() { + local method="$1" + local path="$2" + shift 2 + curl -sS -o /tmp/lock-smoke-body.$$ -w '%{http_code}' \ + -X "$method" \ + -H "Content-Type: application/json" \ + -H "X-Appwrite-Project: $PROJECT_ID" \ + -H "X-Appwrite-Key: $API_KEY" \ + "$ENDPOINT/v1$path" \ + "$@" +} + +toggle_service() { + local service="$1" + local enabled="$2" + local code + code=$(curl_appwrite PATCH "/project/services/$service" \ + -d "{\"enabled\": $enabled}") + echo "$code" +} + +get_project_state() { + curl -sS \ + -H "Content-Type: application/json" \ + -H "X-Appwrite-Project: console" \ + -H "X-Appwrite-Key: $API_KEY" \ + "$ENDPOINT/v1/projects/$PROJECT_ID" +} + +# --- Run ------------------------------------------------------------------- + +echo "==> Distributed-lock smoke test" +echo " endpoint: $ENDPOINT" +echo " project: $PROJECT_ID" +echo " parallelism: $PARALLELISM" +echo " services: ${SERVICES[*]}" +echo + +# 1. Baseline — disable all targeted services sequentially. +echo "==> Baseline: disabling ${SERVICES[*]}" +for svc in "${SERVICES[@]}"; do + code=$(toggle_service "$svc" false) + if [ "$code" != "200" ]; then + echo " WARN: baseline disable of $svc returned $code (expected 200)" + fi +done + +# 2. Fire concurrent toggles to enabled=true. Capture each child's HTTP status. +echo +echo "==> Firing ${#SERVICES[@]} concurrent toggle requests..." +RESULTS_FILE=$(mktemp -t lock-smoke-results.XXXXXX) +for svc in "${SERVICES[@]}"; do + ( + code=$(toggle_service "$svc" true) + printf '%s %s\n' "$svc" "$code" >> "$RESULTS_FILE" + ) & +done +wait + +# 3. Tally responses. +SUCCESS_COUNT=$(awk '$2 == 200' "$RESULTS_FILE" | wc -l | tr -d ' ') +CONFLICT_COUNT=$(awk '$2 == 409' "$RESULTS_FILE" | wc -l | tr -d ' ') +OTHER_COUNT=$(awk '$2 != 200 && $2 != 409' "$RESULTS_FILE" | wc -l | tr -d ' ') + +echo +echo "==> Child responses:" +sort "$RESULTS_FILE" +echo +echo " successes (200): $SUCCESS_COUNT" +echo " conflicts (409): $CONFLICT_COUNT" +echo " other: $OTHER_COUNT" +rm -f "$RESULTS_FILE" + +# 4. Refetch project; count how many targeted services are enabled. +PROJECT_JSON=$(get_project_state) +ENABLED_COUNT=0 +for svc in "${SERVICES[@]}"; do + # Capitalize first letter to form serviceStatusFor key. + Cap="$(echo "$svc" | awk '{print toupper(substr($1,1,1)) substr($1,2)}')" + val=$(echo "$PROJECT_JSON" | sed -nE "s/.*\"serviceStatusFor${Cap}\":[[:space:]]*(true|false).*/\1/p" | head -n1) + if [ "$val" = "true" ]; then + ENABLED_COUNT=$((ENABLED_COUNT + 1)) + fi +done + +echo " enabled in project state: $ENABLED_COUNT" +echo + +# 5. Verdict. +if [ "$SUCCESS_COUNT" -eq "$ENABLED_COUNT" ]; then + echo "PASS: every successful toggle persisted (no lost updates)." + EXIT=0 +else + echo "FAIL: lost updates detected. successes=$SUCCESS_COUNT enabled=$ENABLED_COUNT" + echo " Locking is either disabled or not effective on this endpoint." + EXIT=1 +fi + +# 6. Cleanup — re-enable all targeted services. +echo +echo "==> Cleanup: re-enabling ${SERVICES[*]}" +for svc in "${SERVICES[@]}"; do + toggle_service "$svc" true >/dev/null || true +done + +rm -f /tmp/lock-smoke-body.$$ +exit "$EXIT" diff --git a/tests/e2e/Client.php b/tests/e2e/Client.php index 4358058fe3..84c937d2d1 100644 --- a/tests/e2e/Client.php +++ b/tests/e2e/Client.php @@ -211,7 +211,13 @@ class Client } } - curl_setopt($ch, CURLOPT_PATH_AS_IS, 1); + // CURLOPT_PATH_AS_IS isn't supported by Swoole's emulated cURL when the + // SWOOLE_HOOK_CURL coroutine hook is active. Skip it in that case so + // tests that need real parallel HTTP (Swoole\Coroutine\run + cURL hook) + // don't fatal-error here. Native (non-hooked) cURL keeps the option. + if (! \extension_loaded('swoole') || ! (\Swoole\Runtime::getHookFlags() & SWOOLE_HOOK_CURL)) { + curl_setopt($ch, CURLOPT_PATH_AS_IS, 1); + } curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $followRedirects); @@ -237,9 +243,12 @@ class Client if ($method === self::METHOD_HEAD) { curl_setopt($ch, CURLOPT_NOBODY, true); // This is crucial for HEAD requests curl_setopt($ch, CURLOPT_HEADER, false); - } else { - curl_setopt($ch, CURLOPT_NOBODY, false); } + // Note: explicit CURLOPT_NOBODY=false on non-HEAD requests is redundant + // (false is cURL's default) and actively breaks Swoole's emulated cURL + // on PATCH-with-body — Swoole strips the body and the request reaches + // the server as a method-without-body, hitting the framework's 404 + // fallback. Just skip the redundant set. if ($method != self::METHOD_GET && $method != self::METHOD_HEAD) { curl_setopt($ch, CURLOPT_POSTFIELDS, $query); diff --git a/tests/e2e/Services/Project/ServicesBase.php b/tests/e2e/Services/Project/ServicesBase.php index b5f94f8181..c8c7bd173f 100644 --- a/tests/e2e/Services/Project/ServicesBase.php +++ b/tests/e2e/Services/Project/ServicesBase.php @@ -266,6 +266,94 @@ trait ServicesBase $this->assertSame(true, $response['body']['serviceStatusForTeams']); } + /** + * Concurrency test for the distributed-lock pilot on `updateProjectService`. + * + * Without locking, two concurrent toggles to different services on the + * same project both read the same baseline `services` map, each set their + * own key, and the second sparse `updateDocument()` overwrites the first + * — silent lost-update. + * + * The test fires N parallel PATCH calls via Swoole coroutines (with the + * SWOOLE_HOOK_CURL runtime hook enabled so the test Client's cURL calls + * yield to the scheduler). After all coroutines complete the project is + * refetched and the count of enabled services is compared against the + * count of HTTP 200 responses. + * + * To verify the test catches the bug: + * - `_APP_LOCKING_ENABLED=enabled` → must pass + * - `_APP_LOCKING_ENABLED=disabled` → must FAIL (lost updates) + */ + public function testConcurrentTogglesAllPersist(): void + { + $services = ['teams', 'storage', 'functions', 'sites', 'messaging']; + + // Baseline: disable everything so the toggle direction is unambiguous. + // Done outside the coroutine context so it stays sequential and the + // resulting project state is a clean known-zero before the race. + foreach ($services as $service) { + $this->updateServiceStatus($service, false); + } + + // Enable Swoole's cURL hook so the test Client's HTTP calls yield + // to the scheduler and the foreach below actually runs in parallel. + // Without this, coroutines serialize on cURL and the negative-case + // run (locking disabled) would falsely pass. + \Swoole\Runtime::enableCoroutine(\SWOOLE_HOOK_CURL); + + $results = []; + try { + \Swoole\Coroutine\run(function () use ($services, &$results): void { + foreach ($services as $service) { + \Swoole\Coroutine::create(function () use ($service, &$results): void { + $response = $this->updateServiceStatus($service, true); + $results[$service] = $response['headers']['status-code']; + }); + } + }); + } finally { + // SWOOLE_HOOK_NONE isn't defined in some Swoole builds; pass 0 + // (the integer value of "no hooks") to disable any hooks this + // test enabled so subsequent tests run with native cURL. + \Swoole\Runtime::enableCoroutine(0); + } + + $successCount = count(array_filter($results, fn ($code) => $code === 200)); + + // Refetch the project and count which of the targeted services ended up + // enabled. The endpoint returns serviceStatusFor{Service} keys. + $project = $this->client->call(Client::METHOD_GET, '/projects/' . $this->getProject()['$id'], [ + 'content-type' => 'application/json', + 'x-appwrite-project' => 'console', + 'cookie' => 'a_session_console=' . $this->getRoot()['session'], + ]); + + $enabledCount = 0; + foreach ($services as $service) { + $key = 'serviceStatusFor' . ucfirst($service); + if (($project['body'][$key] ?? false) === true) { + $enabledCount++; + } + } + + $this->assertGreaterThan(0, $successCount, 'At least one concurrent toggle should succeed'); + $this->assertSame( + $successCount, + $enabledCount, + sprintf( + 'Each successful concurrent toggle must persist. successCount=%d enabledCount=%d (lost-update detected — distributed lock not effective)', + $successCount, + $enabledCount, + ), + ); + + // Cleanup: leave all targeted services enabled so subsequent tests + // see a known-good baseline. + foreach ($services as $service) { + $this->updateServiceStatus($service, true); + } + } + // Helpers protected function updateServiceStatus(string $serviceId, bool $enabled, bool $authenticated = true): mixed From 92b5f0dcd60057ec591412ebef4c00539dfcf56b Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Mon, 27 Apr 2026 14:11:17 +0100 Subject: [PATCH 04/26] feat: report lock backend/release errors to logger (Sentry/Raygun/etc.) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Lock backend errors (Redis/Dragonfly unreachable) and release errors (TTL expired or backend dropped while held) were previously visible only in the lock.attempts counter and Console::warning lines. They now also push a structured Log entry through the configured logger adapter, so operators using Sentry/Raygun/AppSignal/LogOwl get first-class events for these specific failure modes. Pattern matches Embeddings/Text/Create.php exactly: - Action injects 'log' (per-request Log object) and 'logger' (?Logger, nullable when _APP_LOGGING_CONFIG unset). - Helper mutates the per-request $log instead of constructing a fresh one — preserves the per-request context Embeddings expects. - Same field set: namespace='http', server, version, type, setMessage, setAction, setEnvironment, addTag('code', ...), addExtra('file' / 'line' / 'trace'). - Defensive try/catch around addLog() so logging failures don't break fail-open. Lock-specific tags added for slicing in Sentry: - lock.target — collection name (projects, keys, users, ...). Bounded set, safe for high-cardinality stores. - lock.key_pattern — full key with the trailing document ID stripped (lock:platform:projects:* not lock:platform:projects:abc). Prevents unbounded log cardinality from per-document IDs. Rate limiting via per-pod static buckets, 60s window per (action, target) combo. During a 5-minute Dragonfly outage, a fleet of N pods produces at most N events/min, well within Sentry's dedup tolerance. Static state is per-Swoole-worker; coroutines may race on the bucket boundary but the worst case is one duplicate report. Type level set to Log::TYPE_WARNING (not ERROR): fail-open means the request still succeeds, so this is degraded operation, not a failed request. Deliberately NOT reported to Sentry: - 409 GENERAL_RESOURCE_LOCKED (normal user-facing concurrency) - skip-on-contention events (idempotent fan-out by design) - acquire retry conflicts (internal loop) - destructor cleanups (have an expected baseline rate; the lock.attempts counter aggregates them better than Sentry would) Factory signature change: distributedLock and distributedLockOrFail now accept ?Log and ?Logger as optional named args at call time (rather than capturing Logger at factory-build time). The factory closure runs once at boot but the per-request Log resource is fresh per request — capturing at boot would have given stale state. Existing call sites threaded log: $log, logger: $logger. Sites that don't (workers, CLI tasks) get null and just log to Console as before. --- app/controllers/shared/api.php | 12 ++- app/init/resources.php | 84 ++++++++++++++++--- .../Project/Http/Project/Services/Update.php | 8 +- 3 files changed, 89 insertions(+), 15 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index b07eec1a99..142a20c4f3 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -39,6 +39,8 @@ use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\Authorization\Input; use Utopia\Database\Validator\Roles; use Utopia\Http\Http; +use Utopia\Logger\Log; +use Utopia\Logger\Logger; use Utopia\Span\Span; use Utopia\System\System; use Utopia\Telemetry\Adapter as Telemetry; @@ -101,7 +103,9 @@ Http::init() ->inject('apiKey') ->inject('authorization') ->inject('distributedLock') - ->action(function (Http $utopia, Request $request, Database $dbForPlatform, Database $dbForProject, AuditContext $auditContext, Document $project, User $user, ?Document $session, array $servers, string $mode, Document $team, ?Key $apiKey, Authorization $authorization, callable $distributedLock) { + ->inject('log') + ->inject('logger') + ->action(function (Http $utopia, Request $request, Database $dbForPlatform, Database $dbForProject, AuditContext $auditContext, Document $project, User $user, ?Document $session, array $servers, string $mode, Document $team, ?Key $apiKey, Authorization $authorization, callable $distributedLock, Log $log, ?Logger $logger) { $route = $utopia->getRoute(); if ($route === null) { throw new AppwriteException(AppwriteException::GENERAL_ROUTE_NOT_FOUND); @@ -263,7 +267,7 @@ Http::init() } elseif (! empty($apiKey->getTeamId())) { $dbForPlatform->getAuthorization()->skip(fn () => $dbForPlatform->purgeCachedDocument('teams', $team->getId())); } - }); + }, log: $log, logger: $logger); } $userClone = clone $user; @@ -398,7 +402,7 @@ Http::init() $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'accessedAt' => DateTime::now() ]))); - }); + }, log: $log, logger: $logger); } } @@ -419,7 +423,7 @@ Http::init() $authorization->skip(fn () => $dbForPlatform->updateDocument('users', $user->getId(), new Document([ 'accessedAt' => $user->getAttribute('accessedAt') ]))); - }); + }, log: $log, logger: $logger); } } } diff --git a/app/init/resources.php b/app/init/resources.php index d3443ee337..d520c621b5 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -26,6 +26,8 @@ use Utopia\DSN\DSN; use Utopia\Lock\Adapter\Redis as LockRedisAdapter; use Utopia\Lock\Exception\LockAcquireException; use Utopia\Lock\Lock; +use Utopia\Logger\Log; +use Utopia\Logger\Logger; use Utopia\Pools\Group; use Utopia\Queue\Broker\Pool as BrokerPool; use Utopia\Queue\Publisher; @@ -225,6 +227,68 @@ $lockTargetOf = function (string $key): string { return $parts[2] ?? 'unknown'; }; +/** + * Push lock-side errors to the configured logger (Sentry/Raygun/AppSignal/etc.) + * by mutating the caller's per-request `Log` object — same pattern as + * Embeddings/Text/Create.php and the http.php request-end error handler. + * + * Rate limited per pod via a static bucket so a sustained backend outage + * doesn't flood the logger. At most one push per 60s per (action, target) + * combo. Across a fleet of N pods this caps at N events/min, well within + * Sentry's own dedup tolerance. + * + * Only "real" errors are reported here: + * - backend_error: Redis/Dragonfly unreachable + * - release_error: lock release failed (TTL expired or backend dropped) + * + * Skipped on purpose: contention 409s (normal user-facing concurrency), + * skip-on-contention events (idempotent fan-out by design), acquire retry + * conflicts (internal loop), destructor cleanups (have an expected + * baseline rate; aggregate via the `lock.attempts` counter instead). + */ +$lockErrorReporter = function (?Log $log, ?Logger $logger, string $action, string $key, string $target, Throwable $e): void { + static $lastReportAt = []; + + Console::warning("Lock {$action} for {$key}: {$e->getMessage()}"); + + if ($logger === null || $log === null) { + return; + } + + $bucket = $action . ':' . $target; + $now = time(); + if (($lastReportAt[$bucket] ?? 0) + 60 > $now) { + return; + } + $lastReportAt[$bucket] = $now; + + $log->setNamespace('http'); + $log->setServer(System::getEnv('_APP_LOGGING_SERVICE_IDENTIFIER', \gethostname())); + $log->setVersion(APP_VERSION_STABLE); + $log->setType(Log::TYPE_WARNING); + $log->setMessage('Distributed lock ' . $action . ': ' . $e->getMessage()); + $log->setAction("lock.{$action}"); + $log->setEnvironment(System::getEnv('_APP_ENV', 'development') === 'production' + ? Log::ENVIRONMENT_PRODUCTION + : Log::ENVIRONMENT_STAGING); + + $log->addTag('lock.target', $target); + // Strip the trailing document ID from the key so log aggregators don't + // see unbounded cardinality; the {target} attribute already covers slicing. + $log->addTag('lock.key_pattern', preg_replace('/:[^:]+$/', ':*', $key)); + $log->addTag('code', $e->getCode()); + + $log->addExtra('file', $e->getFile()); + $log->addExtra('line', $e->getLine()); + $log->addExtra('trace', $e->getTraceAsString()); + + try { + $logger->addLog($log); + } catch (Throwable) { + // best-effort; don't let logging failures break fail-open + } +}; + /** * Distributed-lock factory: skip-on-contention variant. * @@ -242,17 +306,17 @@ $lockTargetOf = function (string $key): string { * Metric: `lock.attempts{outcome,target}` where outcome ∈ {acquired, skipped, * backend_error, release_error}. */ -$container->set('distributedLock', function (\Redis $redis, Telemetry $telemetry) use ($lockTargetOf) { +$container->set('distributedLock', function (\Redis $redis, Telemetry $telemetry) use ($lockTargetOf, $lockErrorReporter) { $enabled = System::getEnv('_APP_LOCKING_ENABLED', 'enabled') !== 'disabled'; $attempts = $telemetry->createCounter('lock.attempts', null, 'Distributed lock acquire outcomes'); if (! $enabled) { - return function (string $key, \Closure $fn, float $ttl = 5.0): void { + return function (string $key, \Closure $fn, float $ttl = 5.0, ?Log $log = null, ?Logger $logger = null): void { $fn(); }; } - return function (string $key, \Closure $fn, float $ttl = 5.0) use ($redis, $attempts, $lockTargetOf): void { + return function (string $key, \Closure $fn, float $ttl = 5.0, ?Log $log = null, ?Logger $logger = null) use ($redis, $attempts, $lockTargetOf, $lockErrorReporter): void { $target = $lockTargetOf($key); $lock = new Lock(new LockRedisAdapter($redis), $key, $ttl); @@ -260,7 +324,7 @@ $container->set('distributedLock', function (\Redis $redis, Telemetry $telemetry $acquired = $lock->acquire(); } catch (LockAcquireException $e) { $attempts->add(1, ['outcome' => 'backend_error', 'target' => $target]); - Console::warning("Lock backend unavailable for {$key}, proceeding unlocked: {$e->getMessage()}"); + $lockErrorReporter($log, $logger, 'backend_error', $key, $target, $e); $fn(); return; } @@ -278,7 +342,7 @@ $container->set('distributedLock', function (\Redis $redis, Telemetry $telemetry $lock->release(); } catch (\Throwable $e) { $attempts->add(1, ['outcome' => 'release_error', 'target' => $target]); - Console::warning("Lock release failed for {$key}: {$e->getMessage()}"); + $lockErrorReporter($log, $logger, 'release_error', $key, $target, $e); } } }; @@ -301,17 +365,17 @@ $container->set('distributedLock', function (\Redis $redis, Telemetry $telemetry * Metric: `lock.attempts{outcome,target}` where outcome ∈ {acquired, contended, * backend_error, release_error}. */ -$container->set('distributedLockOrFail', function (\Redis $redis, Telemetry $telemetry) use ($lockTargetOf) { +$container->set('distributedLockOrFail', function (\Redis $redis, Telemetry $telemetry) use ($lockTargetOf, $lockErrorReporter) { $enabled = System::getEnv('_APP_LOCKING_ENABLED', 'enabled') !== 'disabled'; $attempts = $telemetry->createCounter('lock.attempts', null, 'Distributed lock acquire outcomes'); if (! $enabled) { - return function (string $key, \Closure $fn, float $ttl = 10.0, float $waitTimeout = 3.0): mixed { + return function (string $key, \Closure $fn, float $ttl = 10.0, float $waitTimeout = 3.0, ?Log $log = null, ?Logger $logger = null): mixed { return $fn(); }; } - return function (string $key, \Closure $fn, float $ttl = 10.0, float $waitTimeout = 3.0) use ($redis, $attempts, $lockTargetOf): mixed { + return function (string $key, \Closure $fn, float $ttl = 10.0, float $waitTimeout = 3.0, ?Log $log = null, ?Logger $logger = null) use ($redis, $attempts, $lockTargetOf, $lockErrorReporter): mixed { $target = $lockTargetOf($key); $lock = new Lock(new LockRedisAdapter($redis), $key, $ttl); @@ -319,7 +383,7 @@ $container->set('distributedLockOrFail', function (\Redis $redis, Telemetry $tel $acquired = $lock->acquire(blocking: true, waitTimeout: $waitTimeout, retryDelay: 0.1); } catch (LockAcquireException $e) { $attempts->add(1, ['outcome' => 'backend_error', 'target' => $target]); - Console::warning("Lock backend unavailable for {$key}, proceeding unlocked: {$e->getMessage()}"); + $lockErrorReporter($log, $logger, 'backend_error', $key, $target, $e); return $fn(); } @@ -341,7 +405,7 @@ $container->set('distributedLockOrFail', function (\Redis $redis, Telemetry $tel $lock->release(); } catch (\Throwable $e) { $attempts->add(1, ['outcome' => 'release_error', 'target' => $target]); - Console::warning("Lock release failed for {$key}: {$e->getMessage()}"); + $lockErrorReporter($log, $logger, 'release_error', $key, $target, $e); } } }; diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php index 1126da8897..933d524e23 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php @@ -12,6 +12,8 @@ use Utopia\Config\Config; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; +use Utopia\Logger\Log; +use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Boolean; use Utopia\Validator\WhiteList; @@ -61,6 +63,8 @@ class Update extends Action ->inject('authorization') ->inject('queueForEvents') ->inject('distributedLockOrFail') + ->inject('log') + ->inject('logger') ->callback($this->action(...)); } @@ -73,6 +77,8 @@ class Update extends Action Authorization $authorization, Event $queueForEvents, callable $distributedLockOrFail, + Log $log, + ?Logger $logger, ): void { // The services map is a JSON object on the project document. Two // concurrent service toggles read the same baseline, each set their @@ -90,7 +96,7 @@ class Update extends Action return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'services' => $services, ]))); - }); + }, log: $log, logger: $logger); $queueForEvents->setParam('serviceId', $serviceId); From 752df210077c3efc1a8951358ec3e16e8f560db2 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Tue, 28 Apr 2026 09:38:08 +0100 Subject: [PATCH 05/26] refactor: switch distributed-lock backend to utopia-php/lock MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `utopia-php/lock` v0.2.0 was published this week and provides the same Redis SET-NX-EX + Lua-compare-and-delete primitive we built locally as `premtsd-code/lock`. Drop the dev-preview package in favor of the official Utopia PHP library. - composer: replace `premtsd-code/lock` with `utopia-php/lock` 0.2.* (still via VCS — not on Packagist yet) - resources.php: rewire both factory variants - `Lock + Adapter\Redis` → `Distributed` - `acquire()` → `tryAcquire()` for skip variant - `acquire(blocking: true, waitTimeout)` → `acquire($waitTimeout)` for OrFail variant - `LockAcquireException` → `\RedisException` - `(int) $ttl` cast — utopia-php/lock takes seconds as int - docker-compose: thread `_APP_LOCKING_ENABLED` into the appwrite service environment so the kill switch documented in `app/config/variables.php` is actually usable from `.env` Verified end-to-end on local stack: - positive case (locking enabled): 5/5 testConcurrentTogglesAllPersist pass, lock keys observed in `redis-cli MONITOR` with concurrent SET NX contention - negative case (locking disabled): 1/3 detect lost updates as before --- app/init/resources.php | 16 ++--- composer.json | 4 +- composer.lock | 147 +++++++++++++++++++++-------------------- docker-compose.yml | 1 + 4 files changed, 84 insertions(+), 84 deletions(-) diff --git a/app/init/resources.php b/app/init/resources.php index d520c621b5..4a4af38b5f 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -23,9 +23,7 @@ use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; use Utopia\DI\Container; use Utopia\DSN\DSN; -use Utopia\Lock\Adapter\Redis as LockRedisAdapter; -use Utopia\Lock\Exception\LockAcquireException; -use Utopia\Lock\Lock; +use Utopia\Lock\Distributed as DistributedLock; use Utopia\Logger\Log; use Utopia\Logger\Logger; use Utopia\Pools\Group; @@ -318,11 +316,11 @@ $container->set('distributedLock', function (\Redis $redis, Telemetry $telemetry return function (string $key, \Closure $fn, float $ttl = 5.0, ?Log $log = null, ?Logger $logger = null) use ($redis, $attempts, $lockTargetOf, $lockErrorReporter): void { $target = $lockTargetOf($key); - $lock = new Lock(new LockRedisAdapter($redis), $key, $ttl); + $lock = new DistributedLock($redis, $key, (int) $ttl); try { - $acquired = $lock->acquire(); - } catch (LockAcquireException $e) { + $acquired = $lock->tryAcquire(); + } catch (\RedisException $e) { $attempts->add(1, ['outcome' => 'backend_error', 'target' => $target]); $lockErrorReporter($log, $logger, 'backend_error', $key, $target, $e); $fn(); @@ -377,11 +375,11 @@ $container->set('distributedLockOrFail', function (\Redis $redis, Telemetry $tel return function (string $key, \Closure $fn, float $ttl = 10.0, float $waitTimeout = 3.0, ?Log $log = null, ?Logger $logger = null) use ($redis, $attempts, $lockTargetOf, $lockErrorReporter): mixed { $target = $lockTargetOf($key); - $lock = new Lock(new LockRedisAdapter($redis), $key, $ttl); + $lock = new DistributedLock($redis, $key, (int) $ttl); try { - $acquired = $lock->acquire(blocking: true, waitTimeout: $waitTimeout, retryDelay: 0.1); - } catch (LockAcquireException $e) { + $acquired = $lock->acquire($waitTimeout); + } catch (\RedisException $e) { $attempts->add(1, ['outcome' => 'backend_error', 'target' => $target]); $lockErrorReporter($log, $logger, 'backend_error', $key, $target, $e); return $fn(); diff --git a/composer.json b/composer.json index 50ecd43cb3..42f4303d18 100644 --- a/composer.json +++ b/composer.json @@ -95,12 +95,12 @@ "webonyx/graphql-php": "15.31.*", "league/csv": "9.14.*", "enshrined/svg-sanitize": "0.22.*", - "premtsd-code/lock": "dev-main#7ec10dae33b950bc0f473c51e5b165b20754760c" + "utopia-php/lock": "0.2.*" }, "repositories": [ { "type": "vcs", - "url": "https://github.com/premtsd-code/lock" + "url": "https://github.com/utopia-php/lock" } ], "require-dev": { diff --git a/composer.lock b/composer.lock index 99d1a4df9f..5a4e0135f8 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "5108bf79532c9d666fb96daa9ec4b3d5", + "content-hash": "bbf2208dac35ff90f57b7e49b8546d45", "packages": [ { "name": "adhocore/jwt", @@ -2104,75 +2104,6 @@ ], "time": "2026-04-10T01:33:53+00:00" }, - { - "name": "premtsd-code/lock", - "version": "dev-main", - "source": { - "type": "git", - "url": "https://github.com/premtsd-code/lock.git", - "reference": "7ec10dae33b950bc0f473c51e5b165b20754760c" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/premtsd-code/lock/zipball/7ec10dae33b950bc0f473c51e5b165b20754760c", - "reference": "7ec10dae33b950bc0f473c51e5b165b20754760c", - "shasum": "" - }, - "require": { - "php": ">=8.1" - }, - "require-dev": { - "laravel/pint": "1.2.*", - "phpstan/phpstan": "^1.12", - "phpunit/phpunit": "^9.5" - }, - "suggest": { - "ext-redis": "Required for Utopia\\Lock\\Adapter\\Redis" - }, - "default-branch": true, - "type": "library", - "autoload": { - "psr-4": { - "Utopia\\Lock\\": "src/Lock" - } - }, - "autoload-dev": { - "psr-4": { - "Utopia\\Tests\\": "tests/Lock" - } - }, - "scripts": { - "check": [ - "./vendor/bin/phpstan analyse --level 8 src tests" - ], - "lint": [ - "./vendor/bin/pint --test" - ], - "format": [ - "./vendor/bin/pint" - ], - "test": [ - "./vendor/bin/phpunit --configuration phpunit.xml tests" - ] - }, - "license": [ - "MIT" - ], - "description": "A distributed lock library for PHP, part of the Utopia framework", - "keywords": [ - "distributed", - "lock", - "mutex", - "php", - "redis", - "utopia" - ], - "support": { - "source": "https://github.com/premtsd-code/lock/tree/main", - "issues": "https://github.com/premtsd-code/lock/issues" - }, - "time": "2026-04-26T15:50:08+00:00" - }, { "name": "psr/clock", "version": "1.0.0", @@ -4490,6 +4421,78 @@ }, "time": "2025-08-12T12:58:26+00:00" }, + { + "name": "utopia-php/lock", + "version": "0.2.0", + "source": { + "type": "git", + "url": "https://github.com/utopia-php/lock.git", + "reference": "49317c9493d8f747e4299aa24c22862aa5f6e106" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/utopia-php/lock/zipball/49317c9493d8f747e4299aa24c22862aa5f6e106", + "reference": "49317c9493d8f747e4299aa24c22862aa5f6e106", + "shasum": "" + }, + "require": { + "php": ">=8.3" + }, + "require-dev": { + "laravel/pint": "1.*", + "phpstan/phpstan": "2.*", + "phpunit/phpunit": "11.*", + "swoole/ide-helper": "*" + }, + "suggest": { + "ext-pcntl": "Required to run the File lock tests", + "ext-redis": "Required for the Distributed lock", + "ext-swoole": "Required for the Mutex and Semaphore locks (>=6.0)" + }, + "type": "library", + "autoload": { + "psr-4": { + "Utopia\\Lock\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Utopia\\Lock\\Tests\\": "tests/" + } + }, + "scripts": { + "test": [ + "vendor/bin/phpunit" + ], + "lint": [ + "vendor/bin/pint --test" + ], + "format": [ + "vendor/bin/pint" + ], + "format:check": [ + "vendor/bin/pint --test" + ], + "analyze": [ + "vendor/bin/phpstan analyse --memory-limit=512M" + ] + }, + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Appwrite Team", + "email": "team@appwrite.io" + } + ], + "description": "Mutex, semaphore, file and distributed locks for PHP — one interface, four backends.", + "support": { + "source": "https://github.com/utopia-php/lock/tree/0.2.0", + "issues": "https://github.com/utopia-php/lock/issues" + }, + "time": "2026-04-24T10:47:56+00:00" + }, { "name": "utopia-php/logger", "version": "0.6.2", @@ -8512,9 +8515,7 @@ ], "aliases": [], "minimum-stability": "dev", - "stability-flags": { - "premtsd-code/lock": 20 - }, + "stability-flags": {}, "prefer-stable": true, "prefer-lowest": false, "platform": { diff --git a/docker-compose.yml b/docker-compose.yml index 7d53d2965d..2674e4085d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -212,6 +212,7 @@ services: - _APP_EXECUTOR_SECRET - _APP_EXECUTOR_HOST - _APP_LOGGING_CONFIG + - _APP_LOCKING_ENABLED - _APP_MAINTENANCE_INTERVAL - _APP_MAINTENANCE_RETENTION_EXECUTION - _APP_MAINTENANCE_RETENTION_CACHE From 29a0d6c2bf2685b2ac107ca15081c1459a5d6823 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Tue, 28 Apr 2026 14:13:31 +0100 Subject: [PATCH 06/26] feat: distributed locks on all platform-projects-doc writes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The pilot on `Project/Services/Update.php` (commit fb0d43daf3) fixed the silent lost-update on the `services` JSON map. The same read-modify-write pattern exists on 17 other endpoints — all writing to the SAME `projects` document, mostly via sparse `updateDocument`. Concurrent writes silently overwrite each other, both within an endpoint (two toggles to different auth methods) and across endpoints (parallel SMTP update + service toggle). The worst offender is `Projects/Update.php` (`PATCH /v1/projects/:projectId`): it calls `setAttribute` 11 times then writes the WHOLE document back, so a concurrent write to *any* attribute on the project doc — services, auths, smtp, templates, mockNumbers — gets clobbered. Wrap every read-modify-write window with `distributedLockOrFail` keyed on `lock:platform:projects:{$projectId}`. The single shared key serializes both same-endpoint and cross-endpoint races on the same project. Endpoints now under the lock: Projects module (console-managed, plural): - Projects/Update (full doc setAttribute chain) - Projects/Team/Update (sparse + computed permissions; cascade to installations/repositories/vcsComments runs after release — separate collections) Project module (current-project, singular): - AuthMethods/Update (auths[authKey]) - Protocols/Update (apis[protocolId]) - SMTP/Update (smtp map; PHPMailer probe inside lock — 10s default TTL covers Timeout=5) - Templates/Email/Update (templates['email.X-locale']) - MockPhone/{Create,Delete,Update} (auths['mockNumbers'][]) - Policies/MembershipPrivacy (auths['memberships*']) - Policies/PasswordDictionary (auths['passwordDictionary']) - Policies/PasswordHistory (auths['passwordHistory']) - Policies/PasswordPersonalData (auths['personalDataCheck']) - Policies/SessionAlert (auths['sessionAlerts']) - Policies/SessionDuration (auths['duration']) - Policies/SessionInvalidation (auths['invalidateSessions']) - Policies/SessionLimit (auths['maxSessions']) - Policies/UserLimit (auths['limit']) Each endpoint: 1. Injects `distributedLockOrFail`, `log`, `logger` 2. Re-reads the project document inside the lock so the baseline reflects any update that landed between request init and lock acquisition 3. Throws `GENERAL_RESOURCE_LOCKED` (HTTP 409) on contention timeout SMTP/Update and Templates/Email/Update additionally refactor variable- variables (`${$key}`) to an explicit input array — PHPStan can't trace dynamic references through closure `use` clauses. --- .../Http/Project/AuthMethods/Update.php | 24 ++- .../Project/Http/Project/MockPhone/Create.php | 49 +++--- .../Project/Http/Project/MockPhone/Delete.php | 47 +++--- .../Project/Http/Project/MockPhone/Update.php | 50 +++--- .../Policies/MembershipPrivacy/Update.php | 50 +++--- .../Policies/PasswordDictionary/Update.php | 22 ++- .../Policies/PasswordHistory/Update.php | 27 +-- .../Policies/PasswordPersonalData/Update.php | 22 ++- .../Project/Policies/SessionAlert/Update.php | 22 ++- .../Policies/SessionDuration/Update.php | 22 ++- .../Policies/SessionInvalidation/Update.php | 22 ++- .../Project/Policies/SessionLimit/Update.php | 27 +-- .../Project/Policies/UserLimit/Update.php | 27 +-- .../Project/Http/Project/Protocols/Update.php | 22 ++- .../Project/Http/Project/SMTP/Update.php | 155 ++++++++++-------- .../Http/Project/Templates/Email/Update.php | 81 +++++---- .../Projects/Http/Projects/Team/Update.php | 42 +++-- .../Modules/Projects/Http/Projects/Update.php | 45 +++-- 18 files changed, 475 insertions(+), 281 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php index 0d1cd83203..9f06c51880 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php @@ -12,6 +12,8 @@ use Utopia\Config\Config; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; +use Utopia\Logger\Log; +use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Boolean; use Utopia\Validator\WhiteList; @@ -60,6 +62,9 @@ class Update extends Action ->inject('project') ->inject('authorization') ->inject('queueForEvents') + ->inject('distributedLockOrFail') + ->inject('log') + ->inject('logger') ->callback($this->action(...)); } @@ -70,17 +75,24 @@ class Update extends Action Database $dbForPlatform, Document $project, Authorization $authorization, - Event $queueForEvents + Event $queueForEvents, + callable $distributedLockOrFail, + Log $log, + ?Logger $logger, ): void { $auth = Config::getParam('auth')[$methodId] ?? []; $authKey = $auth['key'] ?? ''; - $auths = $project->getAttribute('auths', []); - $auths[$authKey] = $enabled; + $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $authKey, $enabled, $dbForPlatform, $authorization) { + $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); - $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'auths' => $auths, - ]))); + $auths = $project->getAttribute('auths', []); + $auths[$authKey] = $enabled; + + return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'auths' => $auths, + ]))); + }, log: $log, logger: $logger); $queueForEvents->setParam('methodId', $methodId); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php index f4002c60ef..08163d644a 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php @@ -13,6 +13,8 @@ use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; +use Utopia\Logger\Log; +use Utopia\Logger\Logger; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Text; @@ -59,6 +61,9 @@ class Create extends Action ->inject('project') ->inject('dbForPlatform') ->inject('authorization') + ->inject('distributedLockOrFail') + ->inject('log') + ->inject('logger') ->callback($this->action(...)); } @@ -70,21 +75,10 @@ class Create extends Action Document $project, Database $dbForPlatform, Authorization $authorization, + callable $distributedLockOrFail, + Log $log, + ?Logger $logger, ) { - $auths = $project->getAttribute('auths', []); - - $mockNumbers = $auths['mockNumbers'] ?? []; - - if (\count($mockNumbers) >= APP_LIMIT_COUNT) { - throw new Exception(Exception::MOCK_NUMBER_LIMIT_EXCEEDED); - } - - foreach ($mockNumbers as $mockNumber) { - if ($mockNumber['phone'] === $number) { - throw new Exception(Exception::MOCK_NUMBER_ALREADY_EXISTS); - } - } - // Set to now date $mockNumber = [ 'phone' => $number, @@ -93,14 +87,29 @@ class Create extends Action '$updatedAt' => DateTime::now(), ]; - $mockNumbers[] = $mockNumber; - $auths['mockNumbers'] = $mockNumbers; + $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $number, $mockNumber, $dbForPlatform, $authorization) { + $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); - $updates = new Document([ - 'auths' => $auths, - ]); + $auths = $project->getAttribute('auths', []); + $mockNumbers = $auths['mockNumbers'] ?? []; - $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + if (\count($mockNumbers) >= APP_LIMIT_COUNT) { + throw new Exception(Exception::MOCK_NUMBER_LIMIT_EXCEEDED); + } + + foreach ($mockNumbers as $existing) { + if ($existing['phone'] === $number) { + throw new Exception(Exception::MOCK_NUMBER_ALREADY_EXISTS); + } + } + + $mockNumbers[] = $mockNumber; + $auths['mockNumbers'] = $mockNumbers; + + $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'auths' => $auths, + ]))); + }, log: $log, logger: $logger); $queueForEvents->setParam('number', $number); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php index 0fb23e1764..560ff4b6e4 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php @@ -13,6 +13,8 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; +use Utopia\Logger\Log; +use Utopia\Logger\Logger; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; @@ -58,6 +60,9 @@ class Delete extends Action ->inject('project') ->inject('dbForPlatform') ->inject('authorization') + ->inject('distributedLockOrFail') + ->inject('log') + ->inject('logger') ->callback($this->action(...)); } @@ -68,33 +73,35 @@ class Delete extends Action Document $project, Database $dbForPlatform, Authorization $authorization, + callable $distributedLockOrFail, + Log $log, + ?Logger $logger, ) { - $auths = $project->getAttribute('auths', []); + $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $number, $dbForPlatform, $authorization) { + $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); - $mockNumbers = $auths['mockNumbers'] ?? []; + $auths = $project->getAttribute('auths', []); + $mockNumbers = $auths['mockNumbers'] ?? []; - $mockNumberIndex = null; - foreach ($mockNumbers as $index => $mock) { - if ($mock['phone'] === $number) { - $mockNumberIndex = $index; - break; + $mockNumberIndex = null; + foreach ($mockNumbers as $index => $mock) { + if ($mock['phone'] === $number) { + $mockNumberIndex = $index; + break; + } } - } - if (\is_null($mockNumberIndex)) { - throw new Exception(Exception::MOCK_NUMBER_NOT_FOUND); - } + if (\is_null($mockNumberIndex)) { + throw new Exception(Exception::MOCK_NUMBER_NOT_FOUND); + } - unset($mockNumbers[$mockNumberIndex]); - $mockNumbers = array_values($mockNumbers); + unset($mockNumbers[$mockNumberIndex]); + $auths['mockNumbers'] = array_values($mockNumbers); - $auths['mockNumbers'] = $mockNumbers; - - $updates = new Document([ - 'auths' => $auths, - ]); - - $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'auths' => $auths, + ]))); + }, log: $log, logger: $logger); $queueForEvents->setParam('number', $number); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php index 48b90a1b97..55512ff215 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php @@ -13,6 +13,8 @@ use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; +use Utopia\Logger\Log; +use Utopia\Logger\Logger; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Text; @@ -59,6 +61,9 @@ class Update extends Action ->inject('project') ->inject('dbForPlatform') ->inject('authorization') + ->inject('distributedLockOrFail') + ->inject('log') + ->inject('logger') ->callback($this->action(...)); } @@ -70,38 +75,43 @@ class Update extends Action Document $project, Database $dbForPlatform, Authorization $authorization, + callable $distributedLockOrFail, + Log $log, + ?Logger $logger, ) { - $auths = $project->getAttribute('auths', []); + $mockNumber = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $number, $otp, $dbForPlatform, $authorization) { + $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); - $mockNumbers = $auths['mockNumbers'] ?? []; + $auths = $project->getAttribute('auths', []); + $mockNumbers = $auths['mockNumbers'] ?? []; - $mockNumberIndex = null; - foreach ($mockNumbers as $index => $mock) { - if ($mock['phone'] === $number) { - $mockNumberIndex = $index; - break; + $mockNumberIndex = null; + foreach ($mockNumbers as $index => $mock) { + if ($mock['phone'] === $number) { + $mockNumberIndex = $index; + break; + } } - } - if (\is_null($mockNumberIndex)) { - throw new Exception(Exception::MOCK_NUMBER_NOT_FOUND); - } + if (\is_null($mockNumberIndex)) { + throw new Exception(Exception::MOCK_NUMBER_NOT_FOUND); + } - $mockNumbers[$mockNumberIndex]['otp'] = $otp; - $mockNumbers[$mockNumberIndex]['$updatedAt'] = DateTime::now(); + $mockNumbers[$mockNumberIndex]['otp'] = $otp; + $mockNumbers[$mockNumberIndex]['$updatedAt'] = DateTime::now(); + $auths['mockNumbers'] = $mockNumbers; - $auths['mockNumbers'] = $mockNumbers; + $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'auths' => $auths, + ]))); - $updates = new Document([ - 'auths' => $auths, - ]); - - $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + return $mockNumbers[$mockNumberIndex]; + }, log: $log, logger: $logger); $queueForEvents->setParam('number', $number); $response ->setStatusCode(Response::STATUS_CODE_OK) - ->dynamic(new Document($mockNumbers[$mockNumberIndex]), Response::MODEL_MOCK_NUMBER); + ->dynamic(new Document($mockNumber), Response::MODEL_MOCK_NUMBER); } } diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MembershipPrivacy/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MembershipPrivacy/Update.php index c947ff225a..f510801524 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MembershipPrivacy/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MembershipPrivacy/Update.php @@ -11,6 +11,8 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; +use Utopia\Logger\Log; +use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Boolean; @@ -60,6 +62,9 @@ class Update extends Action ->inject('project') ->inject('authorization') ->inject('queueForEvents') + ->inject('distributedLockOrFail') + ->inject('log') + ->inject('logger') ->callback($this->action(...)); } @@ -74,30 +79,35 @@ class Update extends Action Document $project, Authorization $authorization, Event $queueForEvents, + callable $distributedLockOrFail, + Log $log, + ?Logger $logger, ): void { - $auths = $project->getAttribute('auths', []); + $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $userId, $userEmail, $userPhone, $userName, $userMFA, $dbForPlatform, $authorization) { + $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); - if ($userId !== null) { - $auths['membershipsUserId'] = $userId; - } - if ($userEmail !== null) { - $auths['membershipsUserEmail'] = $userEmail; - } - if ($userPhone !== null) { - $auths['membershipsUserPhone'] = $userPhone; - } - if ($userName !== null) { - $auths['membershipsUserName'] = $userName; - } - if ($userMFA !== null) { - $auths['membershipsMfa'] = $userMFA; - } + $auths = $project->getAttribute('auths', []); - $updates = new Document([ - 'auths' => $auths, - ]); + if ($userId !== null) { + $auths['membershipsUserId'] = $userId; + } + if ($userEmail !== null) { + $auths['membershipsUserEmail'] = $userEmail; + } + if ($userPhone !== null) { + $auths['membershipsUserPhone'] = $userPhone; + } + if ($userName !== null) { + $auths['membershipsUserName'] = $userName; + } + if ($userMFA !== null) { + $auths['membershipsMfa'] = $userMFA; + } - $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'auths' => $auths, + ]))); + }, log: $log, logger: $logger); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordDictionary/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordDictionary/Update.php index e2c678abb6..7dc7729b47 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordDictionary/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordDictionary/Update.php @@ -11,6 +11,8 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; +use Utopia\Logger\Log; +use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Boolean; @@ -56,6 +58,9 @@ class Update extends Action ->inject('project') ->inject('authorization') ->inject('queueForEvents') + ->inject('distributedLockOrFail') + ->inject('log') + ->inject('logger') ->callback($this->action(...)); } @@ -66,15 +71,20 @@ class Update extends Action Document $project, Authorization $authorization, Event $queueForEvents, + callable $distributedLockOrFail, + Log $log, + ?Logger $logger, ): void { - $auths = $project->getAttribute('auths', []); - $auths['passwordDictionary'] = $enabled; + $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $enabled, $dbForPlatform, $authorization) { + $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); - $updates = new Document([ - 'auths' => $auths, - ]); + $auths = $project->getAttribute('auths', []); + $auths['passwordDictionary'] = $enabled; - $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'auths' => $auths, + ]))); + }, log: $log, logger: $logger); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php index a8ae81caff..19ffc9a0b2 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php @@ -11,6 +11,8 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; +use Utopia\Logger\Log; +use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Nullable; use Utopia\Validator\Range; @@ -59,6 +61,9 @@ class Update extends Action ->inject('project') ->inject('authorization') ->inject('queueForEvents') + ->inject('distributedLockOrFail') + ->inject('log') + ->inject('logger') ->callback($this->action(...)); } @@ -69,20 +74,20 @@ class Update extends Action Document $project, Authorization $authorization, Event $queueForEvents, + callable $distributedLockOrFail, + Log $log, + ?Logger $logger, ): void { - $auths = $project->getAttribute('auths', []); + $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $total, $dbForPlatform, $authorization) { + $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); - if (\is_null($total)) { - $auths['passwordHistory'] = 0; - } else { - $auths['passwordHistory'] = $total; - } + $auths = $project->getAttribute('auths', []); + $auths['passwordHistory'] = \is_null($total) ? 0 : $total; - $updates = new Document([ - 'auths' => $auths, - ]); - - $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'auths' => $auths, + ]))); + }, log: $log, logger: $logger); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPersonalData/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPersonalData/Update.php index 9db7cf0549..95c6b9452a 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPersonalData/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPersonalData/Update.php @@ -11,6 +11,8 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; +use Utopia\Logger\Log; +use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Boolean; @@ -57,6 +59,9 @@ class Update extends Action ->inject('project') ->inject('authorization') ->inject('queueForEvents') + ->inject('distributedLockOrFail') + ->inject('log') + ->inject('logger') ->callback($this->action(...)); } @@ -67,15 +72,20 @@ class Update extends Action Document $project, Authorization $authorization, Event $queueForEvents, + callable $distributedLockOrFail, + Log $log, + ?Logger $logger, ): void { - $auths = $project->getAttribute('auths', []); - $auths['personalDataCheck'] = $enabled; + $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $enabled, $dbForPlatform, $authorization) { + $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); - $updates = new Document([ - 'auths' => $auths, - ]); + $auths = $project->getAttribute('auths', []); + $auths['personalDataCheck'] = $enabled; - $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'auths' => $auths, + ]))); + }, log: $log, logger: $logger); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionAlert/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionAlert/Update.php index 22b7a44b04..6948eed85e 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionAlert/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionAlert/Update.php @@ -11,6 +11,8 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; +use Utopia\Logger\Log; +use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Boolean; @@ -56,6 +58,9 @@ class Update extends Action ->inject('project') ->inject('authorization') ->inject('queueForEvents') + ->inject('distributedLockOrFail') + ->inject('log') + ->inject('logger') ->callback($this->action(...)); } @@ -66,15 +71,20 @@ class Update extends Action Document $project, Authorization $authorization, Event $queueForEvents, + callable $distributedLockOrFail, + Log $log, + ?Logger $logger, ): void { - $auths = $project->getAttribute('auths', []); - $auths['sessionAlerts'] = $enabled; + $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $enabled, $dbForPlatform, $authorization) { + $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); - $updates = new Document([ - 'auths' => $auths, - ]); + $auths = $project->getAttribute('auths', []); + $auths['sessionAlerts'] = $enabled; - $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'auths' => $auths, + ]))); + }, log: $log, logger: $logger); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php index ba72c93a6f..3de157556a 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php @@ -11,6 +11,8 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; +use Utopia\Logger\Log; +use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Range; @@ -56,6 +58,9 @@ class Update extends Action ->inject('project') ->inject('authorization') ->inject('queueForEvents') + ->inject('distributedLockOrFail') + ->inject('log') + ->inject('logger') ->callback($this->action(...)); } @@ -66,15 +71,20 @@ class Update extends Action Document $project, Authorization $authorization, Event $queueForEvents, + callable $distributedLockOrFail, + Log $log, + ?Logger $logger, ): void { - $auths = $project->getAttribute('auths', []); - $auths['duration'] = $duration; + $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $duration, $dbForPlatform, $authorization) { + $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); - $updates = new Document([ - 'auths' => $auths, - ]); + $auths = $project->getAttribute('auths', []); + $auths['duration'] = $duration; - $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'auths' => $auths, + ]))); + }, log: $log, logger: $logger); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionInvalidation/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionInvalidation/Update.php index 8f8a959959..f6dcb95123 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionInvalidation/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionInvalidation/Update.php @@ -11,6 +11,8 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; +use Utopia\Logger\Log; +use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Boolean; @@ -56,6 +58,9 @@ class Update extends Action ->inject('project') ->inject('authorization') ->inject('queueForEvents') + ->inject('distributedLockOrFail') + ->inject('log') + ->inject('logger') ->callback($this->action(...)); } @@ -66,15 +71,20 @@ class Update extends Action Document $project, Authorization $authorization, Event $queueForEvents, + callable $distributedLockOrFail, + Log $log, + ?Logger $logger, ): void { - $auths = $project->getAttribute('auths', []); - $auths['invalidateSessions'] = $enabled; + $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $enabled, $dbForPlatform, $authorization) { + $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); - $updates = new Document([ - 'auths' => $auths, - ]); + $auths = $project->getAttribute('auths', []); + $auths['invalidateSessions'] = $enabled; - $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'auths' => $auths, + ]))); + }, log: $log, logger: $logger); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php index 382ed6f0d9..826478b713 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php @@ -11,6 +11,8 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; +use Utopia\Logger\Log; +use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Nullable; use Utopia\Validator\Range; @@ -57,6 +59,9 @@ class Update extends Action ->inject('project') ->inject('authorization') ->inject('queueForEvents') + ->inject('distributedLockOrFail') + ->inject('log') + ->inject('logger') ->callback($this->action(...)); } @@ -67,20 +72,20 @@ class Update extends Action Document $project, Authorization $authorization, Event $queueForEvents, + callable $distributedLockOrFail, + Log $log, + ?Logger $logger, ): void { - $auths = $project->getAttribute('auths', []); + $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $total, $dbForPlatform, $authorization) { + $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); - if (\is_null($total)) { - $auths['maxSessions'] = 0; - } else { - $auths['maxSessions'] = $total; - } + $auths = $project->getAttribute('auths', []); + $auths['maxSessions'] = \is_null($total) ? 0 : $total; - $updates = new Document([ - 'auths' => $auths, - ]); - - $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'auths' => $auths, + ]))); + }, log: $log, logger: $logger); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php index 9129b81250..a882a72603 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php @@ -11,6 +11,8 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; +use Utopia\Logger\Log; +use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Nullable; use Utopia\Validator\Range; @@ -57,6 +59,9 @@ class Update extends Action ->inject('project') ->inject('authorization') ->inject('queueForEvents') + ->inject('distributedLockOrFail') + ->inject('log') + ->inject('logger') ->callback($this->action(...)); } @@ -67,20 +72,20 @@ class Update extends Action Document $project, Authorization $authorization, Event $queueForEvents, + callable $distributedLockOrFail, + Log $log, + ?Logger $logger, ): void { - $auths = $project->getAttribute('auths', []); + $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $total, $dbForPlatform, $authorization) { + $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); - if (\is_null($total)) { - $auths['limit'] = 0; - } else { - $auths['limit'] = $total; - } + $auths = $project->getAttribute('auths', []); + $auths['limit'] = \is_null($total) ? 0 : $total; - $updates = new Document([ - 'auths' => $auths, - ]); - - $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'auths' => $auths, + ]))); + }, log: $log, logger: $logger); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php index ad5691c1e0..5fb40571bf 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php @@ -12,6 +12,8 @@ use Utopia\Config\Config; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; +use Utopia\Logger\Log; +use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Boolean; use Utopia\Validator\WhiteList; @@ -60,6 +62,9 @@ class Update extends Action ->inject('project') ->inject('authorization') ->inject('queueForEvents') + ->inject('distributedLockOrFail') + ->inject('log') + ->inject('logger') ->callback($this->action(...)); } @@ -71,13 +76,20 @@ class Update extends Action Document $project, Authorization $authorization, Event $queueForEvents, + callable $distributedLockOrFail, + Log $log, + ?Logger $logger, ): void { - $protocols = $project->getAttribute('apis', []); - $protocols[$protocolId] = $enabled; + $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $protocolId, $enabled, $dbForPlatform, $authorization) { + $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); - $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'apis' => $protocols, - ]))); + $protocols = $project->getAttribute('apis', []); + $protocols[$protocolId] = $enabled; + + return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'apis' => $protocols, + ]))); + }, log: $log, logger: $logger); $queueForEvents->setParam('protocolId', $protocolId); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php index 97e723f52c..12238f7150 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php @@ -14,6 +14,8 @@ use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; use Utopia\Emails\Validator\Email; +use Utopia\Logger\Log; +use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Boolean; use Utopia\Validator\Hostname; @@ -72,6 +74,9 @@ class Update extends Action ->inject('dbForPlatform') ->inject('project') ->inject('authorization') + ->inject('distributedLockOrFail') + ->inject('log') + ->inject('logger') ->callback($this->action(...)); } @@ -90,83 +95,103 @@ class Update extends Action Response $response, Database $dbForPlatform, Document $project, - Authorization $authorization + Authorization $authorization, + callable $distributedLockOrFail, + Log $log, + ?Logger $logger, ): void { - // Fetch current configuration - $smtp = $project->getAttribute('smtp', []); + $inputs = [ + 'host' => $host, + 'port' => $port, + 'username' => $username, + 'password' => $password, + 'senderEmail' => $senderEmail, + 'senderName' => $senderName, + 'replyToEmail' => $replyToEmail, + 'replyToName' => $replyToName, + 'secure' => $secure, + 'enabled' => $enabled, + ]; - // Apply changes - $keys = ['host', 'port', 'username', 'password', 'senderEmail', 'senderName', 'replyToEmail', 'replyToName', 'secure', 'enabled']; - foreach ($keys as $key) { - if (!\is_null(${$key})) { - $smtp[$key] = ${$key}; - } - } + // The SMTP test (PHPMailer SmtpConnect with Timeout=5) runs inside the + // lock so two concurrent SMTP updates don't validate against the same + // baseline and overwrite each other's secrets. The 10s default lock + // TTL covers the worst-case 5s connection probe with margin. + $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $inputs, $enabled, $dbForPlatform, $authorization) { + $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); - // Backwards compatibility - $smtp['replyToEmail'] = $smtp['replyToEmail'] ?? $smtp['replyTo'] ?? ''; + // Fetch current configuration + $smtp = $project->getAttribute('smtp', []); - if (($smtp['enabled'] ?? false) === true) { - // Ensure required fields are set - $requiredKeys = ['host', 'port', 'senderEmail']; - foreach ($requiredKeys as $key) { - if (empty($smtp[$key])) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Param "' . $key . '" is not optional.'); + // Apply changes + foreach ($inputs as $key => $value) { + if (!\is_null($value)) { + $smtp[$key] = $value; } } - } - // Validate SMTP credentials - // Validate when the caller is explicitly enabling or hasn't expressed a preference - // (so a credentials-only PATCH can auto-enable). Skip only when the caller is - // explicitly keeping/turning SMTP off. - if (\is_null($enabled) || $enabled === true) { - $mail = new PHPMailer(true); - $mail->isSMTP(); + // Backwards compatibility + $smtp['replyToEmail'] = $smtp['replyToEmail'] ?? $smtp['replyTo'] ?? ''; - $mail->Host = $smtp['host'] ?? ''; - $mail->Port = $smtp['port'] ?? ''; - $mail->SMTPSecure = $smtp['secure'] ?? ''; - $mail->setFrom($smtp['senderEmail'], $smtp['senderName'] ?? ''); - - if (!empty($smtp['username'] ?? '')) { - $mail->SMTPAuth = true; - $mail->Username = $smtp['username']; - $mail->Password = $smtp['password'] ?? ''; - } - - if (!empty($smtp['replyToEmail'] ?? '')) { - $mail->addReplyTo($smtp['replyToEmail'], $smtp['replyToName'] ?? ''); - } - - $mail->SMTPAutoTLS = false; - $mail->Timeout = 5; - - try { - $valid = $mail->SmtpConnect(); - - if (!$valid) { - throw new \Exception('Connection is not valid.'); - } - - // Auto-enable if configuration is valid - // Dont do this if specifically request to mark disabled - if (\is_null($enabled)) { - $smtp['enabled'] = true; - } - } catch (Throwable $error) { - if (($smtp['enabled'] ?? null) === true) { - throw new Exception(Exception::PROJECT_SMTP_CONFIG_INVALID, $error->getMessage()); + if (($smtp['enabled'] ?? false) === true) { + // Ensure required fields are set + $requiredKeys = ['host', 'port', 'senderEmail']; + foreach ($requiredKeys as $key) { + if (empty($smtp[$key])) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Param "' . $key . '" is not optional.'); + } } } - } - // Save configuration - $updates = new Document([ - 'smtp' => $smtp, - ]); + // Validate SMTP credentials + // Validate when the caller is explicitly enabling or hasn't expressed a preference + // (so a credentials-only PATCH can auto-enable). Skip only when the caller is + // explicitly keeping/turning SMTP off. + if (\is_null($enabled) || $enabled === true) { + $mail = new PHPMailer(true); + $mail->isSMTP(); - $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $mail->Host = $smtp['host'] ?? ''; + $mail->Port = $smtp['port'] ?? ''; + $mail->SMTPSecure = $smtp['secure'] ?? ''; + $mail->setFrom($smtp['senderEmail'], $smtp['senderName'] ?? ''); + + if (!empty($smtp['username'] ?? '')) { + $mail->SMTPAuth = true; + $mail->Username = $smtp['username']; + $mail->Password = $smtp['password'] ?? ''; + } + + if (!empty($smtp['replyToEmail'] ?? '')) { + $mail->addReplyTo($smtp['replyToEmail'], $smtp['replyToName'] ?? ''); + } + + $mail->SMTPAutoTLS = false; + $mail->Timeout = 5; + + try { + $valid = $mail->SmtpConnect(); + + if (!$valid) { + throw new \Exception('Connection is not valid.'); + } + + // Auto-enable if configuration is valid + // Dont do this if specifically request to mark disabled + if (\is_null($enabled)) { + $smtp['enabled'] = true; + } + } catch (Throwable $error) { + if (($smtp['enabled'] ?? null) === true) { + throw new Exception(Exception::PROJECT_SMTP_CONFIG_INVALID, $error->getMessage()); + } + } + } + + return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'smtp' => $smtp, + ]))); + }, log: $log, logger: $logger); $response->dynamic($project, Response::MODEL_PROJECT); } diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Templates/Email/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Templates/Email/Update.php index ef93abf683..103a3e83d8 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Templates/Email/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Templates/Email/Update.php @@ -13,6 +13,8 @@ use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; use Utopia\Emails\Validator\Email; +use Utopia\Logger\Log; +use Utopia\Logger\Logger; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; use Utopia\System\System; @@ -69,6 +71,9 @@ class Update extends Action ->inject('dbForPlatform') ->inject('authorization') ->inject('project') + ->inject('distributedLockOrFail') + ->inject('log') + ->inject('logger') ->callback($this->action(...)); } @@ -86,47 +91,63 @@ class Update extends Action Database $dbForPlatform, Authorization $authorization, Document $project, + callable $distributedLockOrFail, + Log $log, + ?Logger $logger, ) { $locale = $locale ?: System::getEnv('_APP_LOCALE', 'en'); - // Prevent template update if custom SMTP is not configured - $smtp = $project->getAttribute('smtp', []); - if (($smtp['enabled'] ?? false) !== true) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'SMTP must be enabled on the project to configure custom email templates.'); - } + $inputs = [ + 'senderName' => $senderName, + 'senderEmail' => $senderEmail, + 'replyToEmail' => $replyToEmail, + 'replyToName' => $replyToName, + 'message' => $message, + 'subject' => $subject, + ]; - // Fetch current configuration - $templates = $project->getAttribute('templates', []); - $template = $templates['email.' . $templateId . '-' . $locale] ?? []; + $template = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $templateId, $locale, $inputs, $dbForPlatform, $authorization) { + $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); - // Apply changes - $keys = ['senderName', 'senderEmail', 'replyToEmail', 'replyToName', 'message', 'subject']; - foreach ($keys as $key) { - if (!\is_null(${$key})) { - $template[$key] = ${$key}; + // Prevent template update if custom SMTP is not configured + $smtp = $project->getAttribute('smtp', []); + if (($smtp['enabled'] ?? false) !== true) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'SMTP must be enabled on the project to configure custom email templates.'); } - } - // Backwards compatibility - if (!\is_null($template['replyTo'] ?? null)) { - $template['replyToEmail'] = $template['replyToEmail'] ?? $template['replyTo'] ?? ''; - } + // Fetch current configuration + $templates = $project->getAttribute('templates', []); + $template = $templates['email.' . $templateId . '-' . $locale] ?? []; - // Ensure required fields are set - $requiredKeys = ['subject', 'message']; - foreach ($requiredKeys as $key) { - if (empty($template[$key])) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Param "' . $key . '" is not optional.'); + // Apply changes + foreach ($inputs as $key => $value) { + if (!\is_null($value)) { + $template[$key] = $value; + } } - } - // Save configuration - $templates['email.' . $templateId . '-' . $locale] = $template; - $updates = new Document([ - 'templates' => $templates, - ]); + // Backwards compatibility + if (!\is_null($template['replyTo'] ?? null)) { + $template['replyToEmail'] = $template['replyToEmail'] ?? $template['replyTo'] ?? ''; + } - $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + // Ensure required fields are set + $requiredKeys = ['subject', 'message']; + foreach ($requiredKeys as $key) { + if (empty($template[$key])) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Param "' . $key . '" is not optional.'); + } + } + + // Save configuration + $templates['email.' . $templateId . '-' . $locale] = $template; + + $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'templates' => $templates, + ]))); + + return $template; + }, log: $log, logger: $logger); $queueForEvents->setParam('templateId', $templateId); diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Team/Update.php b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Team/Update.php index ab92d1a15f..ab8ca7312e 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Team/Update.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Team/Update.php @@ -13,6 +13,8 @@ use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Query; use Utopia\Database\Validator\UID; +use Utopia\Logger\Log; +use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator; @@ -55,29 +57,39 @@ class Update extends Action ->param('teamId', '', new UID(), 'Team ID of the team to transfer project to.') ->inject('response') ->inject('dbForPlatform') + ->inject('distributedLockOrFail') + ->inject('log') + ->inject('logger') ->callback($this->action(...)); } - public function action(string $projectId, string $teamId, Response $response, Database $dbForPlatform) + public function action(string $projectId, string $teamId, Response $response, Database $dbForPlatform, callable $distributedLockOrFail, Log $log, ?Logger $logger) { - $project = $dbForPlatform->getDocument('projects', $projectId); - $team = $dbForPlatform->getDocument('teams', $teamId); + // Lock around the project doc RMW. Cascade fan-out to installations, + // repositories and vcsComments runs after the lock is released — + // those write to separate collections, not the project doc. + [$project, $permissions] = $distributedLockOrFail("lock:platform:projects:{$projectId}", function () use ($projectId, $teamId, $dbForPlatform) { + $project = $dbForPlatform->getDocument('projects', $projectId); + $team = $dbForPlatform->getDocument('teams', $teamId); - if ($project->isEmpty()) { - throw new Exception(Exception::PROJECT_NOT_FOUND); - } + if ($project->isEmpty()) { + throw new Exception(Exception::PROJECT_NOT_FOUND); + } - if ($team->isEmpty()) { - throw new Exception(Exception::TEAM_NOT_FOUND); - } + if ($team->isEmpty()) { + throw new Exception(Exception::TEAM_NOT_FOUND); + } - $permissions = $this->getPermissions($teamId, $projectId); + $permissions = $this->getPermissions($teamId, $projectId); - $project = $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'teamId' => $teamId, - 'teamInternalId' => $team->getSequence(), - '$permissions' => $permissions, - ])); + $project = $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'teamId' => $teamId, + 'teamInternalId' => $team->getSequence(), + '$permissions' => $permissions, + ])); + + return [$project, $permissions]; + }, log: $log, logger: $logger); $installations = $dbForPlatform->find('installations', [ Query::equal('projectInternalId', [$project->getSequence()]), diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Update.php b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Update.php index 29c26b33ea..11e7c669c4 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Update.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Update.php @@ -10,6 +10,8 @@ use Appwrite\Utopia\Database\Validator\Queries\Projects; use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Validator\UID; +use Utopia\Logger\Log; +use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator; use Utopia\Validator\Text; @@ -65,29 +67,38 @@ class Update extends Action ->param('legalTaxId', '', new Text(256), 'Project legal tax ID. Max length: 256 chars.', true) ->inject('response') ->inject('dbForPlatform') + ->inject('distributedLockOrFail') + ->inject('log') + ->inject('logger') ->callback($this->action(...)); } - public function action(string $projectId, string $name, string $description, string $logo, string $url, string $legalName, string $legalCountry, string $legalState, string $legalCity, string $legalAddress, string $legalTaxId, Response $response, Database $dbForPlatform) + public function action(string $projectId, string $name, string $description, string $logo, string $url, string $legalName, string $legalCountry, string $legalState, string $legalCity, string $legalAddress, string $legalTaxId, Response $response, Database $dbForPlatform, callable $distributedLockOrFail, Log $log, ?Logger $logger) { - $project = $dbForPlatform->getDocument('projects', $projectId); + // Re-fetch and write the full project doc inside the lock. This is the + // worst RMW window in the projects API — the endpoint passes the entire + // document back to updateDocument(), so a concurrent write to *any* + // attribute (services, auths, smtp, ...) would be silently overwritten. + $project = $distributedLockOrFail("lock:platform:projects:{$projectId}", function () use ($projectId, $name, $description, $logo, $url, $legalName, $legalCountry, $legalState, $legalCity, $legalAddress, $legalTaxId, $dbForPlatform) { + $project = $dbForPlatform->getDocument('projects', $projectId); - if ($project->isEmpty()) { - throw new Exception(Exception::PROJECT_NOT_FOUND); - } + if ($project->isEmpty()) { + throw new Exception(Exception::PROJECT_NOT_FOUND); + } - $project = $dbForPlatform->updateDocument('projects', $project->getId(), $project - ->setAttribute('name', $name) - ->setAttribute('description', $description) - ->setAttribute('logo', $logo) - ->setAttribute('url', $url) - ->setAttribute('legalName', $legalName) - ->setAttribute('legalCountry', $legalCountry) - ->setAttribute('legalState', $legalState) - ->setAttribute('legalCity', $legalCity) - ->setAttribute('legalAddress', $legalAddress) - ->setAttribute('legalTaxId', $legalTaxId) - ->setAttribute('search', implode(' ', [$projectId, $name]))); + return $dbForPlatform->updateDocument('projects', $project->getId(), $project + ->setAttribute('name', $name) + ->setAttribute('description', $description) + ->setAttribute('logo', $logo) + ->setAttribute('url', $url) + ->setAttribute('legalName', $legalName) + ->setAttribute('legalCountry', $legalCountry) + ->setAttribute('legalState', $legalState) + ->setAttribute('legalCity', $legalCity) + ->setAttribute('legalAddress', $legalAddress) + ->setAttribute('legalTaxId', $legalTaxId) + ->setAttribute('search', implode(' ', [$projectId, $name]))); + }, log: $log, logger: $logger); $response->dynamic($project, Response::MODEL_PROJECT); } From b29f9f4a453aba6d82ea1f6a0b0693d24904c2a6 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Wed, 29 Apr 2026 01:36:38 +0100 Subject: [PATCH 07/26] feat: distributed lock on router projects.accessedAt RMW MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Every request that arrives via a custom-domain rule (router path) reads the project's `accessedAt` timestamp and, if the throttle window (`APP_PROJECT_ACCESS`) has elapsed, writes a fresh value. With concurrent traffic across multiple pods, this is a per-row hot RMW that loses updates silently — the surviving timestamp depends on which pod's write landed last. Wrap the read-modify-write in `distributedLock('lock:platform:projects:{id}')` (skip-on-contention variant). Every concurrent pod would write the same throttled value, so losing the race is correct: the winner's update covers ours. Wires `distributedLock` and `?Logger` through: - `router()` function signature (app/controllers/general.php:70) - the three Http::init / Http::get callbacks that invoke router(): `*` catch-all (init), `/robots.txt`, `/humans.txt` Two related cloud-only RMW sites (`teams.accessedAt`, `projects.mcpAccessedAt`) live in `appwrite-labs/cloud` and need a follow-up PR there. They depend on this branch reaching 1.9.x so the `distributedLock` DI resource is available downstream. --- app/controllers/general.php | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/app/controllers/general.php b/app/controllers/general.php index 2cec14cc1d..053786b191 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -67,7 +67,7 @@ use Utopia\Validator\Text; Config::setParam('cookieSamesite', Response::COOKIE_SAMESITE_NONE); -function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, Authorization $authorization, ?Key $apiKey, DeleteEvent $queueForDeletes, int $executionsRetentionCount) +function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, Authorization $authorization, ?Key $apiKey, DeleteEvent $queueForDeletes, int $executionsRetentionCount, callable $distributedLock, ?Logger $logger) { $host = $request->getHostname(); if (!empty($previewHostname)) { @@ -136,9 +136,14 @@ function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, S if (!$project->isEmpty() && $project->getId() !== 'console') { $accessedAt = $project->getAttribute('accessedAt', 0); if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_PROJECT_ACCESS)) > $accessedAt) { - $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'accessedAt' => DateTime::now() - ]))); + // Skip-on-contention: every concurrent router request would write + // the same throttled timestamp, so losing the race is correct — + // the winning pod's update covers ours. + $distributedLock('lock:platform:projects:' . $project->getId(), function () use ($dbForPlatform, $project, $authorization) { + $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'accessedAt' => DateTime::now() + ]))); + }, log: $log, logger: $logger); } /** @@ -847,7 +852,9 @@ Http::init() ->inject('authorization') ->inject('queueForDeletes') ->inject('executionsRetentionCount') - ->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Document $project, Database $dbForPlatform, callable $getProjectDB, Locale $locale, array $localeCodes, Reader $geodb, Event $queueForEvents, Bus $bus, Executor $executor, array $platform, callable $isResourceBlocked, string $previewHostname, Document $devKey, ?Key $apiKey, Cors $cors, Authorization $authorization, DeleteEvent $queueForDeletes, int $executionsRetentionCount) { + ->inject('distributedLock') + ->inject('logger') + ->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Document $project, Database $dbForPlatform, callable $getProjectDB, Locale $locale, array $localeCodes, Reader $geodb, Event $queueForEvents, Bus $bus, Executor $executor, array $platform, callable $isResourceBlocked, string $previewHostname, Document $devKey, ?Key $apiKey, Cors $cors, Authorization $authorization, DeleteEvent $queueForDeletes, int $executionsRetentionCount, callable $distributedLock, ?Logger $logger) { /* * Appwrite Router */ @@ -855,7 +862,7 @@ Http::init() $platformHostnames = $platform['hostnames'] ?? []; // Only run Router when external domain if (!\in_array($hostname, $platformHostnames) || !empty($previewHostname)) { - if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $queueForDeletes, $executionsRetentionCount)) { + if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $queueForDeletes, $executionsRetentionCount, $distributedLock, $logger)) { $utopia->getRoute()?->label('router', true); } } @@ -1140,14 +1147,16 @@ Http::options() ->inject('authorization') ->inject('queueForDeletes') ->inject('executionsRetentionCount') - ->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, Document $project, Document $devKey, ?Key $apiKey, Cors $cors, Authorization $authorization, DeleteEvent $queueForDeletes, int $executionsRetentionCount) { + ->inject('distributedLock') + ->inject('logger') + ->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, Document $project, Document $devKey, ?Key $apiKey, Cors $cors, Authorization $authorization, DeleteEvent $queueForDeletes, int $executionsRetentionCount, callable $distributedLock, ?Logger $logger) { /* * Appwrite Router */ $platformHostnames = $platform['hostnames'] ?? []; // Only run Router when external domain if (!in_array($request->getHostname(), $platformHostnames) || !empty($previewHostname)) { - if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $queueForDeletes, $executionsRetentionCount)) { + if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $queueForDeletes, $executionsRetentionCount, $distributedLock, $logger)) { $utopia->getRoute()?->label('router', true); } } @@ -1542,13 +1551,15 @@ Http::get('/robots.txt') ->inject('authorization') ->inject('queueForDeletes') ->inject('executionsRetentionCount') - ->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, ?Key $apiKey, Authorization $authorization, DeleteEvent $queueForDeletes, int $executionsRetentionCount) { + ->inject('distributedLock') + ->inject('logger') + ->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, ?Key $apiKey, Authorization $authorization, DeleteEvent $queueForDeletes, int $executionsRetentionCount, callable $distributedLock, ?Logger $logger) { $platformHostnames = $platform['hostnames'] ?? []; if (in_array($request->getHostname(), $platformHostnames) || !empty($previewHostname)) { $template = new View(__DIR__ . '/../views/general/robots.phtml'); $response->text($template->render(false)); } else { - if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $queueForDeletes, $executionsRetentionCount)) { + if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $queueForDeletes, $executionsRetentionCount, $distributedLock, $logger)) { $utopia->getRoute()?->label('router', true); } } @@ -1576,13 +1587,15 @@ Http::get('/humans.txt') ->inject('authorization') ->inject('queueForDeletes') ->inject('executionsRetentionCount') - ->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, ?Key $apiKey, Authorization $authorization, DeleteEvent $queueForDeletes, int $executionsRetentionCount) { + ->inject('distributedLock') + ->inject('logger') + ->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, ?Key $apiKey, Authorization $authorization, DeleteEvent $queueForDeletes, int $executionsRetentionCount, callable $distributedLock, ?Logger $logger) { $platformHostnames = $platform['hostnames'] ?? []; if (in_array($request->getHostname(), $platformHostnames) || !empty($previewHostname)) { $template = new View(__DIR__ . '/../views/general/humans.phtml'); $response->text($template->render(false)); } else { - if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $queueForDeletes, $executionsRetentionCount)) { + if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $queueForDeletes, $executionsRetentionCount, $distributedLock, $logger)) { $utopia->getRoute()?->label('router', true); } } From 380cc3eb27446f4307db7baaebc41b720ad20983 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Wed, 29 Apr 2026 02:02:28 +0100 Subject: [PATCH 08/26] refactor: drop log/logger boilerplate from lock call sites MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous shape required every caller to thread `log: $log, logger: $logger` as named args into each `distributedLock(...)` invocation, plus inject `log` and `logger` into the surrounding action just to forward them to the lock. Across 21 call sites this added ~100 LOC of pure plumbing. The cause: the lock factory was registered on the global container in `app/init/resources.php`, where per-request resources like `log` aren't visible. That forced the factory to expose its inner closure with optional `?Log $log = null, ?Logger $logger = null` params, which every caller had to satisfy. Move the lock factory + its `lockErrorReporter`/`lockTargetOf` helpers from the global container to the per-request container (`resources/request.php`), and add `'log'` + `'logger'` to the factory's dep list. The factory closure now runs per-request and closes over the per-request `Log`/`Logger`. Inner closure returned to callers no longer needs the optional params, and call sites drop the named args entirely. Knock-on cleanup: - Drop `->inject('log')`, `->inject('logger')`, the corresponding action params, and `use Utopia\Logger\{Log,Logger}` imports from 19 endpoint files where they were only there for the lock - Drop the same plumbing from `app/controllers/shared/api.php` (3 lock call sites) - Drop just the Logger plumbing from `app/controllers/general.php` (router function + 3 callbacks); `Log` is kept because it's used elsewhere in that file - Net 120 LOC removed across 23 files No behavior change: the lock factories still produce the same closures (skip-on-contention `distributedLock`, blocking-with-409 `distributedLockOrFail`). The static lockErrorReporter rate limiter (1 push per 60s per `(action, target)` bucket) continues to work — it lives on a closure-static in the helper, which is independent of where the helper is constructed. Verified end-to-end: testConcurrentTogglesAllPersist passes 4/5 (the cold- start race flake is the same one we've consistently seen and is orthogonal to lock changes). --- app/controllers/general.php | 24 +-- app/controllers/shared/api.php | 12 +- app/init/resources.php | 196 ------------------ app/init/resources/request.php | 194 +++++++++++++++++ .../Http/Project/AuthMethods/Update.php | 8 +- .../Project/Http/Project/MockPhone/Create.php | 8 +- .../Project/Http/Project/MockPhone/Delete.php | 8 +- .../Project/Http/Project/MockPhone/Update.php | 8 +- .../Policies/MembershipPrivacy/Update.php | 8 +- .../Policies/PasswordDictionary/Update.php | 8 +- .../Policies/PasswordHistory/Update.php | 8 +- .../Policies/PasswordPersonalData/Update.php | 8 +- .../Project/Policies/SessionAlert/Update.php | 8 +- .../Policies/SessionDuration/Update.php | 8 +- .../Policies/SessionInvalidation/Update.php | 8 +- .../Project/Policies/SessionLimit/Update.php | 8 +- .../Project/Policies/UserLimit/Update.php | 8 +- .../Project/Http/Project/Protocols/Update.php | 8 +- .../Project/Http/Project/SMTP/Update.php | 8 +- .../Project/Http/Project/Services/Update.php | 8 +- .../Http/Project/Templates/Email/Update.php | 8 +- .../Projects/Http/Projects/Team/Update.php | 8 +- .../Modules/Projects/Http/Projects/Update.php | 8 +- 23 files changed, 229 insertions(+), 349 deletions(-) diff --git a/app/controllers/general.php b/app/controllers/general.php index 053786b191..3fcfd712c2 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -67,7 +67,7 @@ use Utopia\Validator\Text; Config::setParam('cookieSamesite', Response::COOKIE_SAMESITE_NONE); -function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, Authorization $authorization, ?Key $apiKey, DeleteEvent $queueForDeletes, int $executionsRetentionCount, callable $distributedLock, ?Logger $logger) +function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, Authorization $authorization, ?Key $apiKey, DeleteEvent $queueForDeletes, int $executionsRetentionCount, callable $distributedLock) { $host = $request->getHostname(); if (!empty($previewHostname)) { @@ -143,7 +143,7 @@ function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, S $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'accessedAt' => DateTime::now() ]))); - }, log: $log, logger: $logger); + }); } /** @@ -853,8 +853,7 @@ Http::init() ->inject('queueForDeletes') ->inject('executionsRetentionCount') ->inject('distributedLock') - ->inject('logger') - ->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Document $project, Database $dbForPlatform, callable $getProjectDB, Locale $locale, array $localeCodes, Reader $geodb, Event $queueForEvents, Bus $bus, Executor $executor, array $platform, callable $isResourceBlocked, string $previewHostname, Document $devKey, ?Key $apiKey, Cors $cors, Authorization $authorization, DeleteEvent $queueForDeletes, int $executionsRetentionCount, callable $distributedLock, ?Logger $logger) { + ->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Document $project, Database $dbForPlatform, callable $getProjectDB, Locale $locale, array $localeCodes, Reader $geodb, Event $queueForEvents, Bus $bus, Executor $executor, array $platform, callable $isResourceBlocked, string $previewHostname, Document $devKey, ?Key $apiKey, Cors $cors, Authorization $authorization, DeleteEvent $queueForDeletes, int $executionsRetentionCount, callable $distributedLock) { /* * Appwrite Router */ @@ -862,7 +861,7 @@ Http::init() $platformHostnames = $platform['hostnames'] ?? []; // Only run Router when external domain if (!\in_array($hostname, $platformHostnames) || !empty($previewHostname)) { - if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $queueForDeletes, $executionsRetentionCount, $distributedLock, $logger)) { + if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $queueForDeletes, $executionsRetentionCount, $distributedLock)) { $utopia->getRoute()?->label('router', true); } } @@ -1148,15 +1147,14 @@ Http::options() ->inject('queueForDeletes') ->inject('executionsRetentionCount') ->inject('distributedLock') - ->inject('logger') - ->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, Document $project, Document $devKey, ?Key $apiKey, Cors $cors, Authorization $authorization, DeleteEvent $queueForDeletes, int $executionsRetentionCount, callable $distributedLock, ?Logger $logger) { + ->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, Document $project, Document $devKey, ?Key $apiKey, Cors $cors, Authorization $authorization, DeleteEvent $queueForDeletes, int $executionsRetentionCount, callable $distributedLock) { /* * Appwrite Router */ $platformHostnames = $platform['hostnames'] ?? []; // Only run Router when external domain if (!in_array($request->getHostname(), $platformHostnames) || !empty($previewHostname)) { - if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $queueForDeletes, $executionsRetentionCount, $distributedLock, $logger)) { + if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $queueForDeletes, $executionsRetentionCount, $distributedLock)) { $utopia->getRoute()?->label('router', true); } } @@ -1552,14 +1550,13 @@ Http::get('/robots.txt') ->inject('queueForDeletes') ->inject('executionsRetentionCount') ->inject('distributedLock') - ->inject('logger') - ->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, ?Key $apiKey, Authorization $authorization, DeleteEvent $queueForDeletes, int $executionsRetentionCount, callable $distributedLock, ?Logger $logger) { + ->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, ?Key $apiKey, Authorization $authorization, DeleteEvent $queueForDeletes, int $executionsRetentionCount, callable $distributedLock) { $platformHostnames = $platform['hostnames'] ?? []; if (in_array($request->getHostname(), $platformHostnames) || !empty($previewHostname)) { $template = new View(__DIR__ . '/../views/general/robots.phtml'); $response->text($template->render(false)); } else { - if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $queueForDeletes, $executionsRetentionCount, $distributedLock, $logger)) { + if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $queueForDeletes, $executionsRetentionCount, $distributedLock)) { $utopia->getRoute()?->label('router', true); } } @@ -1588,14 +1585,13 @@ Http::get('/humans.txt') ->inject('queueForDeletes') ->inject('executionsRetentionCount') ->inject('distributedLock') - ->inject('logger') - ->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, ?Key $apiKey, Authorization $authorization, DeleteEvent $queueForDeletes, int $executionsRetentionCount, callable $distributedLock, ?Logger $logger) { + ->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, ?Key $apiKey, Authorization $authorization, DeleteEvent $queueForDeletes, int $executionsRetentionCount, callable $distributedLock) { $platformHostnames = $platform['hostnames'] ?? []; if (in_array($request->getHostname(), $platformHostnames) || !empty($previewHostname)) { $template = new View(__DIR__ . '/../views/general/humans.phtml'); $response->text($template->render(false)); } else { - if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $queueForDeletes, $executionsRetentionCount, $distributedLock, $logger)) { + if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $queueForDeletes, $executionsRetentionCount, $distributedLock)) { $utopia->getRoute()?->label('router', true); } } diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 142a20c4f3..b07eec1a99 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -39,8 +39,6 @@ use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\Authorization\Input; use Utopia\Database\Validator\Roles; use Utopia\Http\Http; -use Utopia\Logger\Log; -use Utopia\Logger\Logger; use Utopia\Span\Span; use Utopia\System\System; use Utopia\Telemetry\Adapter as Telemetry; @@ -103,9 +101,7 @@ Http::init() ->inject('apiKey') ->inject('authorization') ->inject('distributedLock') - ->inject('log') - ->inject('logger') - ->action(function (Http $utopia, Request $request, Database $dbForPlatform, Database $dbForProject, AuditContext $auditContext, Document $project, User $user, ?Document $session, array $servers, string $mode, Document $team, ?Key $apiKey, Authorization $authorization, callable $distributedLock, Log $log, ?Logger $logger) { + ->action(function (Http $utopia, Request $request, Database $dbForPlatform, Database $dbForProject, AuditContext $auditContext, Document $project, User $user, ?Document $session, array $servers, string $mode, Document $team, ?Key $apiKey, Authorization $authorization, callable $distributedLock) { $route = $utopia->getRoute(); if ($route === null) { throw new AppwriteException(AppwriteException::GENERAL_ROUTE_NOT_FOUND); @@ -267,7 +263,7 @@ Http::init() } elseif (! empty($apiKey->getTeamId())) { $dbForPlatform->getAuthorization()->skip(fn () => $dbForPlatform->purgeCachedDocument('teams', $team->getId())); } - }, log: $log, logger: $logger); + }); } $userClone = clone $user; @@ -402,7 +398,7 @@ Http::init() $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'accessedAt' => DateTime::now() ]))); - }, log: $log, logger: $logger); + }); } } @@ -423,7 +419,7 @@ Http::init() $authorization->skip(fn () => $dbForPlatform->updateDocument('users', $user->getId(), new Document([ 'accessedAt' => $user->getAttribute('accessedAt') ]))); - }, log: $log, logger: $logger); + }); } } } diff --git a/app/init/resources.php b/app/init/resources.php index 4a4af38b5f..29506bfc9c 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -8,7 +8,6 @@ use Appwrite\Event\Publisher\Migration as MigrationPublisher; use Appwrite\Event\Publisher\Screenshot as ScreenshotPublisher; use Appwrite\Event\Publisher\StatsResources as StatsResourcesPublisher; use Appwrite\Event\Publisher\Usage as UsagePublisher; -use Appwrite\Extend\Exception as AppwriteException; use Appwrite\Utopia\Database\Documents\User; use Executor\Executor; use Utopia\Abuse\Adapters\TimeLimit\Redis as TimeLimitRedis; @@ -23,9 +22,6 @@ use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; use Utopia\DI\Container; use Utopia\DSN\DSN; -use Utopia\Lock\Distributed as DistributedLock; -use Utopia\Logger\Log; -use Utopia\Logger\Logger; use Utopia\Pools\Group; use Utopia\Queue\Broker\Pool as BrokerPool; use Utopia\Queue\Publisher; @@ -217,198 +213,6 @@ $container->set('timelimit', function (\Redis $redis) { }; }, ['redis']); -// Extract the collection segment ("keys" / "projects" / "users") from keys -// of the form "lock:platform:{target}:{id}" so metrics can slice by target. -// Used by both distributedLock and distributedLockOrFail factories below. -$lockTargetOf = function (string $key): string { - $parts = explode(':', $key, 4); - return $parts[2] ?? 'unknown'; -}; - -/** - * Push lock-side errors to the configured logger (Sentry/Raygun/AppSignal/etc.) - * by mutating the caller's per-request `Log` object — same pattern as - * Embeddings/Text/Create.php and the http.php request-end error handler. - * - * Rate limited per pod via a static bucket so a sustained backend outage - * doesn't flood the logger. At most one push per 60s per (action, target) - * combo. Across a fleet of N pods this caps at N events/min, well within - * Sentry's own dedup tolerance. - * - * Only "real" errors are reported here: - * - backend_error: Redis/Dragonfly unreachable - * - release_error: lock release failed (TTL expired or backend dropped) - * - * Skipped on purpose: contention 409s (normal user-facing concurrency), - * skip-on-contention events (idempotent fan-out by design), acquire retry - * conflicts (internal loop), destructor cleanups (have an expected - * baseline rate; aggregate via the `lock.attempts` counter instead). - */ -$lockErrorReporter = function (?Log $log, ?Logger $logger, string $action, string $key, string $target, Throwable $e): void { - static $lastReportAt = []; - - Console::warning("Lock {$action} for {$key}: {$e->getMessage()}"); - - if ($logger === null || $log === null) { - return; - } - - $bucket = $action . ':' . $target; - $now = time(); - if (($lastReportAt[$bucket] ?? 0) + 60 > $now) { - return; - } - $lastReportAt[$bucket] = $now; - - $log->setNamespace('http'); - $log->setServer(System::getEnv('_APP_LOGGING_SERVICE_IDENTIFIER', \gethostname())); - $log->setVersion(APP_VERSION_STABLE); - $log->setType(Log::TYPE_WARNING); - $log->setMessage('Distributed lock ' . $action . ': ' . $e->getMessage()); - $log->setAction("lock.{$action}"); - $log->setEnvironment(System::getEnv('_APP_ENV', 'development') === 'production' - ? Log::ENVIRONMENT_PRODUCTION - : Log::ENVIRONMENT_STAGING); - - $log->addTag('lock.target', $target); - // Strip the trailing document ID from the key so log aggregators don't - // see unbounded cardinality; the {target} attribute already covers slicing. - $log->addTag('lock.key_pattern', preg_replace('/:[^:]+$/', ':*', $key)); - $log->addTag('code', $e->getCode()); - - $log->addExtra('file', $e->getFile()); - $log->addExtra('line', $e->getLine()); - $log->addExtra('trace', $e->getTraceAsString()); - - try { - $logger->addLog($log); - } catch (Throwable) { - // best-effort; don't let logging failures break fail-open - } -}; - -/** - * Distributed-lock factory: skip-on-contention variant. - * - * For idempotent writes where losing the race is correct (e.g., per-request - * `accessedAt` updates from N pods all writing the same value). On contention, - * the callback is silently skipped — another pod is doing the same work. - * - * Behavior: - * - Non-blocking acquire (one attempt). On conflict, skip and return. - * - Fail-open: if Redis is unreachable, run the callback unlocked + warn. - * - Kill switch: `_APP_LOCKING_ENABLED=disabled` runs the callback unlocked. - * - * Returns void — the caller can't distinguish "acquired and ran" from "skipped". - * - * Metric: `lock.attempts{outcome,target}` where outcome ∈ {acquired, skipped, - * backend_error, release_error}. - */ -$container->set('distributedLock', function (\Redis $redis, Telemetry $telemetry) use ($lockTargetOf, $lockErrorReporter) { - $enabled = System::getEnv('_APP_LOCKING_ENABLED', 'enabled') !== 'disabled'; - $attempts = $telemetry->createCounter('lock.attempts', null, 'Distributed lock acquire outcomes'); - - if (! $enabled) { - return function (string $key, \Closure $fn, float $ttl = 5.0, ?Log $log = null, ?Logger $logger = null): void { - $fn(); - }; - } - - return function (string $key, \Closure $fn, float $ttl = 5.0, ?Log $log = null, ?Logger $logger = null) use ($redis, $attempts, $lockTargetOf, $lockErrorReporter): void { - $target = $lockTargetOf($key); - $lock = new DistributedLock($redis, $key, (int) $ttl); - - try { - $acquired = $lock->tryAcquire(); - } catch (\RedisException $e) { - $attempts->add(1, ['outcome' => 'backend_error', 'target' => $target]); - $lockErrorReporter($log, $logger, 'backend_error', $key, $target, $e); - $fn(); - return; - } - - if (! $acquired) { - $attempts->add(1, ['outcome' => 'skipped', 'target' => $target]); - return; - } - - $attempts->add(1, ['outcome' => 'acquired', 'target' => $target]); - try { - $fn(); - } finally { - try { - $lock->release(); - } catch (\Throwable $e) { - $attempts->add(1, ['outcome' => 'release_error', 'target' => $target]); - $lockErrorReporter($log, $logger, 'release_error', $key, $target, $e); - } - } - }; -}, ['redis', 'telemetry']); - -/** - * Distributed-lock factory: 409-on-contention variant. - * - * For explicit user-write endpoints where read-modify-write on shared mutable - * state must NOT silently drop a request. On contention, throws - * `Exception::GENERAL_RESOURCE_LOCKED` (HTTP 409) so the client retries. - * - * Behavior: - * - Blocking acquire with short timeout (default 3s). - * - On timeout, throws `GENERAL_RESOURCE_LOCKED`. - * - Fail-open: backend unreachable runs the callback unlocked + warning. - * - Kill switch: `_APP_LOCKING_ENABLED=disabled` runs the callback unlocked. - * - Returns the callback's return value so callers can use the result. - * - * Metric: `lock.attempts{outcome,target}` where outcome ∈ {acquired, contended, - * backend_error, release_error}. - */ -$container->set('distributedLockOrFail', function (\Redis $redis, Telemetry $telemetry) use ($lockTargetOf, $lockErrorReporter) { - $enabled = System::getEnv('_APP_LOCKING_ENABLED', 'enabled') !== 'disabled'; - $attempts = $telemetry->createCounter('lock.attempts', null, 'Distributed lock acquire outcomes'); - - if (! $enabled) { - return function (string $key, \Closure $fn, float $ttl = 10.0, float $waitTimeout = 3.0, ?Log $log = null, ?Logger $logger = null): mixed { - return $fn(); - }; - } - - return function (string $key, \Closure $fn, float $ttl = 10.0, float $waitTimeout = 3.0, ?Log $log = null, ?Logger $logger = null) use ($redis, $attempts, $lockTargetOf, $lockErrorReporter): mixed { - $target = $lockTargetOf($key); - $lock = new DistributedLock($redis, $key, (int) $ttl); - - try { - $acquired = $lock->acquire($waitTimeout); - } catch (\RedisException $e) { - $attempts->add(1, ['outcome' => 'backend_error', 'target' => $target]); - $lockErrorReporter($log, $logger, 'backend_error', $key, $target, $e); - return $fn(); - } - - if (! $acquired) { - $attempts->add(1, ['outcome' => 'contended', 'target' => $target]); - // Don't pass a custom message — the catalog message in - // app/config/errors.php is reused so we don't leak the internal - // lock key (which embeds collection name and document id) into a - // user-facing 409 response. The telemetry attribute already - // carries the target collection for operator-side observability. - throw new AppwriteException(AppwriteException::GENERAL_RESOURCE_LOCKED); - } - - $attempts->add(1, ['outcome' => 'acquired', 'target' => $target]); - try { - return $fn(); - } finally { - try { - $lock->release(); - } catch (\Throwable $e) { - $attempts->add(1, ['outcome' => 'release_error', 'target' => $target]); - $lockErrorReporter($log, $logger, 'release_error', $key, $target, $e); - } - } - }; -}, ['redis', 'telemetry']); - $container->set('deviceForLocal', function (Telemetry $telemetry) { return new Device\Telemetry($telemetry, new Local()); }, ['telemetry']); diff --git a/app/init/resources/request.php b/app/init/resources/request.php index 7d1731b80d..602efe64f0 100644 --- a/app/init/resources/request.php +++ b/app/init/resources/request.php @@ -37,6 +37,7 @@ use Utopia\Auth\Proofs\Token; use Utopia\Auth\Store; use Utopia\Cache\Cache; use Utopia\Config\Config; +use Utopia\Console; use Utopia\Database\Adapter\Pool as DatabasePool; use Utopia\Database\Database; use Utopia\Database\DateTime as DatabaseDateTime; @@ -48,7 +49,9 @@ use Utopia\Domains\Domain; use Utopia\DSN\DSN; use Utopia\Http\Http; use Utopia\Locale\Locale; +use Utopia\Lock\Distributed as DistributedLock; use Utopia\Logger\Log; +use Utopia\Logger\Logger; use Utopia\Pools\Group; use Utopia\Queue\Publisher; use Utopia\Storage\Device; @@ -73,6 +76,197 @@ return function (Container $container): void { return $register->get('logger'); }, ['register']); + /** + * Push lock-side errors to the configured logger (Sentry/Raygun/AppSignal/etc.) + * by mutating the per-request `Log` object — same pattern as + * Embeddings/Text/Create.php and the http.php request-end error handler. + * + * Rate limited per pod via a static bucket so a sustained backend outage + * doesn't flood the logger. At most one push per 60s per (action, target) + * combo. Across a fleet of N pods this caps at N events/min, well within + * Sentry's own dedup tolerance. + * + * Only "real" errors are reported here: + * - backend_error: Redis/Dragonfly unreachable + * - release_error: lock release failed (TTL expired or backend dropped) + * + * Skipped on purpose: contention 409s (normal user-facing concurrency), + * skip-on-contention events (idempotent fan-out by design), acquire retry + * conflicts (internal loop), destructor cleanups (have an expected + * baseline rate; aggregate via the `lock.attempts` counter instead). + */ + $lockErrorReporter = function (Log $log, ?Logger $logger, string $action, string $key, string $target, Throwable $e): void { + static $lastReportAt = []; + + Console::warning("Lock {$action} for {$key}: {$e->getMessage()}"); + + if ($logger === null) { + return; + } + + $bucket = $action . ':' . $target; + $now = time(); + if (($lastReportAt[$bucket] ?? 0) + 60 > $now) { + return; + } + $lastReportAt[$bucket] = $now; + + $log->setNamespace('http'); + $log->setServer(System::getEnv('_APP_LOGGING_SERVICE_IDENTIFIER', \gethostname())); + $log->setVersion(APP_VERSION_STABLE); + $log->setType(Log::TYPE_WARNING); + $log->setMessage('Distributed lock ' . $action . ': ' . $e->getMessage()); + $log->setAction("lock.{$action}"); + $log->setEnvironment(System::getEnv('_APP_ENV', 'development') === 'production' + ? Log::ENVIRONMENT_PRODUCTION + : Log::ENVIRONMENT_STAGING); + + $log->addTag('lock.target', $target); + // Strip the trailing document ID from the key so log aggregators don't + // see unbounded cardinality; the {target} attribute already covers slicing. + $log->addTag('lock.key_pattern', preg_replace('/:[^:]+$/', ':*', $key)); + $log->addTag('code', $e->getCode()); + + $log->addExtra('file', $e->getFile()); + $log->addExtra('line', $e->getLine()); + $log->addExtra('trace', $e->getTraceAsString()); + + try { + $logger->addLog($log); + } catch (Throwable) { + // best-effort; don't let logging failures break fail-open + } + }; + + // Extract the collection segment ("keys" / "projects" / "users") from keys + // of the form "lock:platform:{target}:{id}" so metrics can slice by target. + $lockTargetOf = function (string $key): string { + $parts = explode(':', $key, 4); + return $parts[2] ?? 'unknown'; + }; + + /** + * Distributed-lock factory: skip-on-contention variant. + * + * For idempotent writes where losing the race is correct (e.g., per-request + * `accessedAt` updates from N pods all writing the same value). On contention, + * the callback is silently skipped — another pod is doing the same work. + * + * Behavior: + * - Non-blocking acquire (one attempt). On conflict, skip and return. + * - Fail-open: if Redis is unreachable, run the callback unlocked + warn. + * - Kill switch: `_APP_LOCKING_ENABLED=disabled` runs the callback unlocked. + * + * Returns void — the caller can't distinguish "acquired and ran" from "skipped". + * + * Metric: `lock.attempts{outcome,target}` where outcome ∈ {acquired, skipped, + * backend_error, release_error}. + */ + $container->set('distributedLock', function (\Redis $redis, Telemetry $telemetry, Log $log, ?Logger $logger) use ($lockTargetOf, $lockErrorReporter) { + $enabled = System::getEnv('_APP_LOCKING_ENABLED', 'enabled') !== 'disabled'; + $attempts = $telemetry->createCounter('lock.attempts', null, 'Distributed lock acquire outcomes'); + + if (! $enabled) { + return function (string $key, \Closure $fn, float $ttl = 5.0): void { + $fn(); + }; + } + + return function (string $key, \Closure $fn, float $ttl = 5.0) use ($redis, $attempts, $log, $logger, $lockTargetOf, $lockErrorReporter): void { + $target = $lockTargetOf($key); + $lock = new DistributedLock($redis, $key, (int) $ttl); + + try { + $acquired = $lock->tryAcquire(); + } catch (\RedisException $e) { + $attempts->add(1, ['outcome' => 'backend_error', 'target' => $target]); + $lockErrorReporter($log, $logger, 'backend_error', $key, $target, $e); + $fn(); + return; + } + + if (! $acquired) { + $attempts->add(1, ['outcome' => 'skipped', 'target' => $target]); + return; + } + + $attempts->add(1, ['outcome' => 'acquired', 'target' => $target]); + try { + $fn(); + } finally { + try { + $lock->release(); + } catch (\Throwable $e) { + $attempts->add(1, ['outcome' => 'release_error', 'target' => $target]); + $lockErrorReporter($log, $logger, 'release_error', $key, $target, $e); + } + } + }; + }, ['redis', 'telemetry', 'log', 'logger']); + + /** + * Distributed-lock factory: 409-on-contention variant. + * + * For explicit user-write endpoints where read-modify-write on shared mutable + * state must NOT silently drop a request. On contention, throws + * `Exception::GENERAL_RESOURCE_LOCKED` (HTTP 409) so the client retries. + * + * Behavior: + * - Blocking acquire with short timeout (default 3s). + * - On timeout, throws `GENERAL_RESOURCE_LOCKED`. + * - Fail-open: backend unreachable runs the callback unlocked + warning. + * - Kill switch: `_APP_LOCKING_ENABLED=disabled` runs the callback unlocked. + * - Returns the callback's return value so callers can use the result. + * + * Metric: `lock.attempts{outcome,target}` where outcome ∈ {acquired, contended, + * backend_error, release_error}. + */ + $container->set('distributedLockOrFail', function (\Redis $redis, Telemetry $telemetry, Log $log, ?Logger $logger) use ($lockTargetOf, $lockErrorReporter) { + $enabled = System::getEnv('_APP_LOCKING_ENABLED', 'enabled') !== 'disabled'; + $attempts = $telemetry->createCounter('lock.attempts', null, 'Distributed lock acquire outcomes'); + + if (! $enabled) { + return function (string $key, \Closure $fn, float $ttl = 10.0, float $waitTimeout = 3.0): mixed { + return $fn(); + }; + } + + return function (string $key, \Closure $fn, float $ttl = 10.0, float $waitTimeout = 3.0) use ($redis, $attempts, $log, $logger, $lockTargetOf, $lockErrorReporter): mixed { + $target = $lockTargetOf($key); + $lock = new DistributedLock($redis, $key, (int) $ttl); + + try { + $acquired = $lock->acquire($waitTimeout); + } catch (\RedisException $e) { + $attempts->add(1, ['outcome' => 'backend_error', 'target' => $target]); + $lockErrorReporter($log, $logger, 'backend_error', $key, $target, $e); + return $fn(); + } + + if (! $acquired) { + $attempts->add(1, ['outcome' => 'contended', 'target' => $target]); + // Don't pass a custom message — the catalog message in + // app/config/errors.php is reused so we don't leak the internal + // lock key (which embeds collection name and document id) into a + // user-facing 409 response. The telemetry attribute already + // carries the target collection for operator-side observability. + throw new Exception(Exception::GENERAL_RESOURCE_LOCKED); + } + + $attempts->add(1, ['outcome' => 'acquired', 'target' => $target]); + try { + return $fn(); + } finally { + try { + $lock->release(); + } catch (\Throwable $e) { + $attempts->add(1, ['outcome' => 'release_error', 'target' => $target]); + $lockErrorReporter($log, $logger, 'release_error', $key, $target, $e); + } + } + }; + }, ['redis', 'telemetry', 'log', 'logger']); + $container->set('authorization', function () { return new Authorization(); }, []); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php index 9f06c51880..1ce36c8577 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php @@ -12,8 +12,6 @@ use Utopia\Config\Config; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; -use Utopia\Logger\Log; -use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Boolean; use Utopia\Validator\WhiteList; @@ -63,8 +61,6 @@ class Update extends Action ->inject('authorization') ->inject('queueForEvents') ->inject('distributedLockOrFail') - ->inject('log') - ->inject('logger') ->callback($this->action(...)); } @@ -77,8 +73,6 @@ class Update extends Action Authorization $authorization, Event $queueForEvents, callable $distributedLockOrFail, - Log $log, - ?Logger $logger, ): void { $auth = Config::getParam('auth')[$methodId] ?? []; $authKey = $auth['key'] ?? ''; @@ -92,7 +86,7 @@ class Update extends Action return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'auths' => $auths, ]))); - }, log: $log, logger: $logger); + }); $queueForEvents->setParam('methodId', $methodId); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php index 08163d644a..8ab0653df3 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php @@ -13,8 +13,6 @@ use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; -use Utopia\Logger\Log; -use Utopia\Logger\Logger; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Text; @@ -62,8 +60,6 @@ class Create extends Action ->inject('dbForPlatform') ->inject('authorization') ->inject('distributedLockOrFail') - ->inject('log') - ->inject('logger') ->callback($this->action(...)); } @@ -76,8 +72,6 @@ class Create extends Action Database $dbForPlatform, Authorization $authorization, callable $distributedLockOrFail, - Log $log, - ?Logger $logger, ) { // Set to now date $mockNumber = [ @@ -109,7 +103,7 @@ class Create extends Action $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'auths' => $auths, ]))); - }, log: $log, logger: $logger); + }); $queueForEvents->setParam('number', $number); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php index 560ff4b6e4..bbc534fdf8 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php @@ -13,8 +13,6 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; -use Utopia\Logger\Log; -use Utopia\Logger\Logger; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; @@ -61,8 +59,6 @@ class Delete extends Action ->inject('dbForPlatform') ->inject('authorization') ->inject('distributedLockOrFail') - ->inject('log') - ->inject('logger') ->callback($this->action(...)); } @@ -74,8 +70,6 @@ class Delete extends Action Database $dbForPlatform, Authorization $authorization, callable $distributedLockOrFail, - Log $log, - ?Logger $logger, ) { $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $number, $dbForPlatform, $authorization) { $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); @@ -101,7 +95,7 @@ class Delete extends Action $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'auths' => $auths, ]))); - }, log: $log, logger: $logger); + }); $queueForEvents->setParam('number', $number); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php index 55512ff215..a492d7c7bd 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php @@ -13,8 +13,6 @@ use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; -use Utopia\Logger\Log; -use Utopia\Logger\Logger; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Text; @@ -62,8 +60,6 @@ class Update extends Action ->inject('dbForPlatform') ->inject('authorization') ->inject('distributedLockOrFail') - ->inject('log') - ->inject('logger') ->callback($this->action(...)); } @@ -76,8 +72,6 @@ class Update extends Action Database $dbForPlatform, Authorization $authorization, callable $distributedLockOrFail, - Log $log, - ?Logger $logger, ) { $mockNumber = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $number, $otp, $dbForPlatform, $authorization) { $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); @@ -106,7 +100,7 @@ class Update extends Action ]))); return $mockNumbers[$mockNumberIndex]; - }, log: $log, logger: $logger); + }); $queueForEvents->setParam('number', $number); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MembershipPrivacy/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MembershipPrivacy/Update.php index f510801524..a912ee48a6 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MembershipPrivacy/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MembershipPrivacy/Update.php @@ -11,8 +11,6 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; -use Utopia\Logger\Log; -use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Boolean; @@ -63,8 +61,6 @@ class Update extends Action ->inject('authorization') ->inject('queueForEvents') ->inject('distributedLockOrFail') - ->inject('log') - ->inject('logger') ->callback($this->action(...)); } @@ -80,8 +76,6 @@ class Update extends Action Authorization $authorization, Event $queueForEvents, callable $distributedLockOrFail, - Log $log, - ?Logger $logger, ): void { $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $userId, $userEmail, $userPhone, $userName, $userMFA, $dbForPlatform, $authorization) { $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); @@ -107,7 +101,7 @@ class Update extends Action return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'auths' => $auths, ]))); - }, log: $log, logger: $logger); + }); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordDictionary/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordDictionary/Update.php index 7dc7729b47..9cb310257e 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordDictionary/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordDictionary/Update.php @@ -11,8 +11,6 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; -use Utopia\Logger\Log; -use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Boolean; @@ -59,8 +57,6 @@ class Update extends Action ->inject('authorization') ->inject('queueForEvents') ->inject('distributedLockOrFail') - ->inject('log') - ->inject('logger') ->callback($this->action(...)); } @@ -72,8 +68,6 @@ class Update extends Action Authorization $authorization, Event $queueForEvents, callable $distributedLockOrFail, - Log $log, - ?Logger $logger, ): void { $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $enabled, $dbForPlatform, $authorization) { $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); @@ -84,7 +78,7 @@ class Update extends Action return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'auths' => $auths, ]))); - }, log: $log, logger: $logger); + }); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php index 19ffc9a0b2..cfff58c8f0 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php @@ -11,8 +11,6 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; -use Utopia\Logger\Log; -use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Nullable; use Utopia\Validator\Range; @@ -62,8 +60,6 @@ class Update extends Action ->inject('authorization') ->inject('queueForEvents') ->inject('distributedLockOrFail') - ->inject('log') - ->inject('logger') ->callback($this->action(...)); } @@ -75,8 +71,6 @@ class Update extends Action Authorization $authorization, Event $queueForEvents, callable $distributedLockOrFail, - Log $log, - ?Logger $logger, ): void { $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $total, $dbForPlatform, $authorization) { $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); @@ -87,7 +81,7 @@ class Update extends Action return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'auths' => $auths, ]))); - }, log: $log, logger: $logger); + }); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPersonalData/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPersonalData/Update.php index 95c6b9452a..580b04754b 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPersonalData/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPersonalData/Update.php @@ -11,8 +11,6 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; -use Utopia\Logger\Log; -use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Boolean; @@ -60,8 +58,6 @@ class Update extends Action ->inject('authorization') ->inject('queueForEvents') ->inject('distributedLockOrFail') - ->inject('log') - ->inject('logger') ->callback($this->action(...)); } @@ -73,8 +69,6 @@ class Update extends Action Authorization $authorization, Event $queueForEvents, callable $distributedLockOrFail, - Log $log, - ?Logger $logger, ): void { $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $enabled, $dbForPlatform, $authorization) { $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); @@ -85,7 +79,7 @@ class Update extends Action return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'auths' => $auths, ]))); - }, log: $log, logger: $logger); + }); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionAlert/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionAlert/Update.php index 6948eed85e..8f9f0a4c48 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionAlert/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionAlert/Update.php @@ -11,8 +11,6 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; -use Utopia\Logger\Log; -use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Boolean; @@ -59,8 +57,6 @@ class Update extends Action ->inject('authorization') ->inject('queueForEvents') ->inject('distributedLockOrFail') - ->inject('log') - ->inject('logger') ->callback($this->action(...)); } @@ -72,8 +68,6 @@ class Update extends Action Authorization $authorization, Event $queueForEvents, callable $distributedLockOrFail, - Log $log, - ?Logger $logger, ): void { $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $enabled, $dbForPlatform, $authorization) { $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); @@ -84,7 +78,7 @@ class Update extends Action return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'auths' => $auths, ]))); - }, log: $log, logger: $logger); + }); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php index 3de157556a..c8111b5563 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php @@ -11,8 +11,6 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; -use Utopia\Logger\Log; -use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Range; @@ -59,8 +57,6 @@ class Update extends Action ->inject('authorization') ->inject('queueForEvents') ->inject('distributedLockOrFail') - ->inject('log') - ->inject('logger') ->callback($this->action(...)); } @@ -72,8 +68,6 @@ class Update extends Action Authorization $authorization, Event $queueForEvents, callable $distributedLockOrFail, - Log $log, - ?Logger $logger, ): void { $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $duration, $dbForPlatform, $authorization) { $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); @@ -84,7 +78,7 @@ class Update extends Action return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'auths' => $auths, ]))); - }, log: $log, logger: $logger); + }); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionInvalidation/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionInvalidation/Update.php index f6dcb95123..e10c0d4081 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionInvalidation/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionInvalidation/Update.php @@ -11,8 +11,6 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; -use Utopia\Logger\Log; -use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Boolean; @@ -59,8 +57,6 @@ class Update extends Action ->inject('authorization') ->inject('queueForEvents') ->inject('distributedLockOrFail') - ->inject('log') - ->inject('logger') ->callback($this->action(...)); } @@ -72,8 +68,6 @@ class Update extends Action Authorization $authorization, Event $queueForEvents, callable $distributedLockOrFail, - Log $log, - ?Logger $logger, ): void { $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $enabled, $dbForPlatform, $authorization) { $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); @@ -84,7 +78,7 @@ class Update extends Action return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'auths' => $auths, ]))); - }, log: $log, logger: $logger); + }); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php index 826478b713..b111534430 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php @@ -11,8 +11,6 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; -use Utopia\Logger\Log; -use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Nullable; use Utopia\Validator\Range; @@ -60,8 +58,6 @@ class Update extends Action ->inject('authorization') ->inject('queueForEvents') ->inject('distributedLockOrFail') - ->inject('log') - ->inject('logger') ->callback($this->action(...)); } @@ -73,8 +69,6 @@ class Update extends Action Authorization $authorization, Event $queueForEvents, callable $distributedLockOrFail, - Log $log, - ?Logger $logger, ): void { $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $total, $dbForPlatform, $authorization) { $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); @@ -85,7 +79,7 @@ class Update extends Action return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'auths' => $auths, ]))); - }, log: $log, logger: $logger); + }); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php index a882a72603..c59a17b61f 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php @@ -11,8 +11,6 @@ use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; -use Utopia\Logger\Log; -use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Nullable; use Utopia\Validator\Range; @@ -60,8 +58,6 @@ class Update extends Action ->inject('authorization') ->inject('queueForEvents') ->inject('distributedLockOrFail') - ->inject('log') - ->inject('logger') ->callback($this->action(...)); } @@ -73,8 +69,6 @@ class Update extends Action Authorization $authorization, Event $queueForEvents, callable $distributedLockOrFail, - Log $log, - ?Logger $logger, ): void { $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $total, $dbForPlatform, $authorization) { $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); @@ -85,7 +79,7 @@ class Update extends Action return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'auths' => $auths, ]))); - }, log: $log, logger: $logger); + }); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php index 5fb40571bf..0f3174b143 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php @@ -12,8 +12,6 @@ use Utopia\Config\Config; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; -use Utopia\Logger\Log; -use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Boolean; use Utopia\Validator\WhiteList; @@ -63,8 +61,6 @@ class Update extends Action ->inject('authorization') ->inject('queueForEvents') ->inject('distributedLockOrFail') - ->inject('log') - ->inject('logger') ->callback($this->action(...)); } @@ -77,8 +73,6 @@ class Update extends Action Authorization $authorization, Event $queueForEvents, callable $distributedLockOrFail, - Log $log, - ?Logger $logger, ): void { $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $protocolId, $enabled, $dbForPlatform, $authorization) { $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); @@ -89,7 +83,7 @@ class Update extends Action return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'apis' => $protocols, ]))); - }, log: $log, logger: $logger); + }); $queueForEvents->setParam('protocolId', $protocolId); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php index 12238f7150..b8e3c6e09f 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php @@ -14,8 +14,6 @@ use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; use Utopia\Emails\Validator\Email; -use Utopia\Logger\Log; -use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Boolean; use Utopia\Validator\Hostname; @@ -75,8 +73,6 @@ class Update extends Action ->inject('project') ->inject('authorization') ->inject('distributedLockOrFail') - ->inject('log') - ->inject('logger') ->callback($this->action(...)); } @@ -97,8 +93,6 @@ class Update extends Action Document $project, Authorization $authorization, callable $distributedLockOrFail, - Log $log, - ?Logger $logger, ): void { $inputs = [ 'host' => $host, @@ -191,7 +185,7 @@ class Update extends Action return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'smtp' => $smtp, ]))); - }, log: $log, logger: $logger); + }); $response->dynamic($project, Response::MODEL_PROJECT); } diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php index 933d524e23..1126da8897 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php @@ -12,8 +12,6 @@ use Utopia\Config\Config; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; -use Utopia\Logger\Log; -use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator\Boolean; use Utopia\Validator\WhiteList; @@ -63,8 +61,6 @@ class Update extends Action ->inject('authorization') ->inject('queueForEvents') ->inject('distributedLockOrFail') - ->inject('log') - ->inject('logger') ->callback($this->action(...)); } @@ -77,8 +73,6 @@ class Update extends Action Authorization $authorization, Event $queueForEvents, callable $distributedLockOrFail, - Log $log, - ?Logger $logger, ): void { // The services map is a JSON object on the project document. Two // concurrent service toggles read the same baseline, each set their @@ -96,7 +90,7 @@ class Update extends Action return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'services' => $services, ]))); - }, log: $log, logger: $logger); + }); $queueForEvents->setParam('serviceId', $serviceId); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Templates/Email/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Templates/Email/Update.php index 103a3e83d8..ed1f7d8f7f 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Templates/Email/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Templates/Email/Update.php @@ -13,8 +13,6 @@ use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; use Utopia\Emails\Validator\Email; -use Utopia\Logger\Log; -use Utopia\Logger\Logger; use Utopia\Platform\Action; use Utopia\Platform\Scope\HTTP; use Utopia\System\System; @@ -72,8 +70,6 @@ class Update extends Action ->inject('authorization') ->inject('project') ->inject('distributedLockOrFail') - ->inject('log') - ->inject('logger') ->callback($this->action(...)); } @@ -92,8 +88,6 @@ class Update extends Action Authorization $authorization, Document $project, callable $distributedLockOrFail, - Log $log, - ?Logger $logger, ) { $locale = $locale ?: System::getEnv('_APP_LOCALE', 'en'); @@ -147,7 +141,7 @@ class Update extends Action ]))); return $template; - }, log: $log, logger: $logger); + }); $queueForEvents->setParam('templateId', $templateId); diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Team/Update.php b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Team/Update.php index ab8ca7312e..25286a2227 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Team/Update.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Team/Update.php @@ -13,8 +13,6 @@ use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Query; use Utopia\Database\Validator\UID; -use Utopia\Logger\Log; -use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator; @@ -58,12 +56,10 @@ class Update extends Action ->inject('response') ->inject('dbForPlatform') ->inject('distributedLockOrFail') - ->inject('log') - ->inject('logger') ->callback($this->action(...)); } - public function action(string $projectId, string $teamId, Response $response, Database $dbForPlatform, callable $distributedLockOrFail, Log $log, ?Logger $logger) + public function action(string $projectId, string $teamId, Response $response, Database $dbForPlatform, callable $distributedLockOrFail) { // Lock around the project doc RMW. Cascade fan-out to installations, // repositories and vcsComments runs after the lock is released — @@ -89,7 +85,7 @@ class Update extends Action ])); return [$project, $permissions]; - }, log: $log, logger: $logger); + }); $installations = $dbForPlatform->find('installations', [ Query::equal('projectInternalId', [$project->getSequence()]), diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Update.php b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Update.php index 11e7c669c4..4b79b37bfa 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Update.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Update.php @@ -10,8 +10,6 @@ use Appwrite\Utopia\Database\Validator\Queries\Projects; use Appwrite\Utopia\Response; use Utopia\Database\Database; use Utopia\Database\Validator\UID; -use Utopia\Logger\Log; -use Utopia\Logger\Logger; use Utopia\Platform\Scope\HTTP; use Utopia\Validator; use Utopia\Validator\Text; @@ -68,12 +66,10 @@ class Update extends Action ->inject('response') ->inject('dbForPlatform') ->inject('distributedLockOrFail') - ->inject('log') - ->inject('logger') ->callback($this->action(...)); } - public function action(string $projectId, string $name, string $description, string $logo, string $url, string $legalName, string $legalCountry, string $legalState, string $legalCity, string $legalAddress, string $legalTaxId, Response $response, Database $dbForPlatform, callable $distributedLockOrFail, Log $log, ?Logger $logger) + public function action(string $projectId, string $name, string $description, string $logo, string $url, string $legalName, string $legalCountry, string $legalState, string $legalCity, string $legalAddress, string $legalTaxId, Response $response, Database $dbForPlatform, callable $distributedLockOrFail) { // Re-fetch and write the full project doc inside the lock. This is the // worst RMW window in the projects API — the endpoint passes the entire @@ -98,7 +94,7 @@ class Update extends Action ->setAttribute('legalAddress', $legalAddress) ->setAttribute('legalTaxId', $legalTaxId) ->setAttribute('search', implode(' ', [$projectId, $name]))); - }, log: $log, logger: $logger); + }); $response->dynamic($project, Response::MODEL_PROJECT); } From fce2abfd4c952121073b3b189b2bdfcacef2a964 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Wed, 29 Apr 2026 05:31:18 +0100 Subject: [PATCH 09/26] revert: scope distributed-lock PR to thundering-herd sites only Drop the 18 lost-update endpoint locks (Project/* settings + Projects team/update). Those address a different bug class (read-modify-write races) than the manager-flagged production problem (regions slow queries to platform from thundering herd on accessedAt writes). Kept: - distributedLock + distributedLockOrFail factories on the per-request container, GENERAL_RESOURCE_LOCKED exception, 409 mapping - 4 thundering-herd sites: cache-invalidation in shared/api.php (3) + router projects.accessedAt in general.php (1) Dropped: - 18 endpoint OrFail wires - testConcurrentTogglesAllPersist + Swoole-cURL test client patches - dev/test-distributed-lock.sh smoke script --- dev/test-distributed-lock.sh | 157 ------------------ .../Http/Project/AuthMethods/Update.php | 18 +- .../Project/Http/Project/MockPhone/Create.php | 43 +++-- .../Project/Http/Project/MockPhone/Delete.php | 41 +++-- .../Project/Http/Project/MockPhone/Update.php | 44 +++-- .../Policies/MembershipPrivacy/Update.php | 44 +++-- .../Policies/PasswordDictionary/Update.php | 16 +- .../Policies/PasswordHistory/Update.php | 21 +-- .../Policies/PasswordPersonalData/Update.php | 16 +- .../Project/Policies/SessionAlert/Update.php | 16 +- .../Policies/SessionDuration/Update.php | 16 +- .../Policies/SessionInvalidation/Update.php | 16 +- .../Project/Policies/SessionLimit/Update.php | 21 +-- .../Project/Policies/UserLimit/Update.php | 21 +-- .../Project/Http/Project/Protocols/Update.php | 16 +- .../Project/Http/Project/SMTP/Update.php | 149 ++++++++--------- .../Project/Http/Project/Services/Update.php | 25 +-- .../Http/Project/Templates/Email/Update.php | 75 ++++----- .../Projects/Http/Projects/Team/Update.php | 38 ++--- .../Modules/Projects/Http/Projects/Update.php | 41 ++--- tests/e2e/Client.php | 15 +- tests/e2e/Services/Project/ServicesBase.php | 88 ---------- 22 files changed, 290 insertions(+), 647 deletions(-) delete mode 100755 dev/test-distributed-lock.sh diff --git a/dev/test-distributed-lock.sh b/dev/test-distributed-lock.sh deleted file mode 100755 index 6a740a4169..0000000000 --- a/dev/test-distributed-lock.sh +++ /dev/null @@ -1,157 +0,0 @@ -#!/usr/bin/env bash -# -# Manual smoke test for the distributed-lock pilot on `updateProjectService`. -# -# Fires N concurrent PATCH /project/services/:serviceId requests against the -# same project, each toggling a different service to "enabled=true". Then -# refetches the project and counts how many of the targeted services actually -# persisted. -# -# Usage: -# APPWRITE_ENDPOINT=http://localhost \ -# APPWRITE_PROJECT_ID= \ -# APPWRITE_API_KEY= \ -# ./dev/test-distributed-lock.sh -# -# Required scopes on the API key: -# - project.write (to toggle services) -# - projects.read (to refetch project state via GET /v1/projects/:id) -# -# To prove the lock fixes the bug, run twice: -# 1. With `_APP_LOCKING_ENABLED=enabled` (default): expect successes == enabled -# 2. With `_APP_LOCKING_ENABLED=disabled` : expect successes > enabled (lost updates) -# -# Set `_APP_LOCKING_ENABLED` in `.env` and `docker compose up -d --force-recreate` -# between runs. Use `--parallelism N` to tune the concurrency level. - -set -eu - -# --- Configuration --------------------------------------------------------- - -ENDPOINT="${APPWRITE_ENDPOINT:?set APPWRITE_ENDPOINT, e.g. http://localhost}" -PROJECT_ID="${APPWRITE_PROJECT_ID:?set APPWRITE_PROJECT_ID}" -API_KEY="${APPWRITE_API_KEY:?set APPWRITE_API_KEY}" -PARALLELISM="${PARALLELISM:-5}" - -# Services to toggle concurrently — must be in the optional-services list of -# the project. These are the same set used by the e2e ServicesBase trait. -SERVICES=(teams storage functions sites messaging) - -# Trim or extend SERVICES to match PARALLELISM. -SERVICES=("${SERVICES[@]:0:$PARALLELISM}") - -if [ "${#SERVICES[@]}" -lt 2 ]; then - echo "ERROR: PARALLELISM must be >= 2 to detect contention" >&2 - exit 1 -fi - -# --- Helpers --------------------------------------------------------------- - -curl_appwrite() { - local method="$1" - local path="$2" - shift 2 - curl -sS -o /tmp/lock-smoke-body.$$ -w '%{http_code}' \ - -X "$method" \ - -H "Content-Type: application/json" \ - -H "X-Appwrite-Project: $PROJECT_ID" \ - -H "X-Appwrite-Key: $API_KEY" \ - "$ENDPOINT/v1$path" \ - "$@" -} - -toggle_service() { - local service="$1" - local enabled="$2" - local code - code=$(curl_appwrite PATCH "/project/services/$service" \ - -d "{\"enabled\": $enabled}") - echo "$code" -} - -get_project_state() { - curl -sS \ - -H "Content-Type: application/json" \ - -H "X-Appwrite-Project: console" \ - -H "X-Appwrite-Key: $API_KEY" \ - "$ENDPOINT/v1/projects/$PROJECT_ID" -} - -# --- Run ------------------------------------------------------------------- - -echo "==> Distributed-lock smoke test" -echo " endpoint: $ENDPOINT" -echo " project: $PROJECT_ID" -echo " parallelism: $PARALLELISM" -echo " services: ${SERVICES[*]}" -echo - -# 1. Baseline — disable all targeted services sequentially. -echo "==> Baseline: disabling ${SERVICES[*]}" -for svc in "${SERVICES[@]}"; do - code=$(toggle_service "$svc" false) - if [ "$code" != "200" ]; then - echo " WARN: baseline disable of $svc returned $code (expected 200)" - fi -done - -# 2. Fire concurrent toggles to enabled=true. Capture each child's HTTP status. -echo -echo "==> Firing ${#SERVICES[@]} concurrent toggle requests..." -RESULTS_FILE=$(mktemp -t lock-smoke-results.XXXXXX) -for svc in "${SERVICES[@]}"; do - ( - code=$(toggle_service "$svc" true) - printf '%s %s\n' "$svc" "$code" >> "$RESULTS_FILE" - ) & -done -wait - -# 3. Tally responses. -SUCCESS_COUNT=$(awk '$2 == 200' "$RESULTS_FILE" | wc -l | tr -d ' ') -CONFLICT_COUNT=$(awk '$2 == 409' "$RESULTS_FILE" | wc -l | tr -d ' ') -OTHER_COUNT=$(awk '$2 != 200 && $2 != 409' "$RESULTS_FILE" | wc -l | tr -d ' ') - -echo -echo "==> Child responses:" -sort "$RESULTS_FILE" -echo -echo " successes (200): $SUCCESS_COUNT" -echo " conflicts (409): $CONFLICT_COUNT" -echo " other: $OTHER_COUNT" -rm -f "$RESULTS_FILE" - -# 4. Refetch project; count how many targeted services are enabled. -PROJECT_JSON=$(get_project_state) -ENABLED_COUNT=0 -for svc in "${SERVICES[@]}"; do - # Capitalize first letter to form serviceStatusFor key. - Cap="$(echo "$svc" | awk '{print toupper(substr($1,1,1)) substr($1,2)}')" - val=$(echo "$PROJECT_JSON" | sed -nE "s/.*\"serviceStatusFor${Cap}\":[[:space:]]*(true|false).*/\1/p" | head -n1) - if [ "$val" = "true" ]; then - ENABLED_COUNT=$((ENABLED_COUNT + 1)) - fi -done - -echo " enabled in project state: $ENABLED_COUNT" -echo - -# 5. Verdict. -if [ "$SUCCESS_COUNT" -eq "$ENABLED_COUNT" ]; then - echo "PASS: every successful toggle persisted (no lost updates)." - EXIT=0 -else - echo "FAIL: lost updates detected. successes=$SUCCESS_COUNT enabled=$ENABLED_COUNT" - echo " Locking is either disabled or not effective on this endpoint." - EXIT=1 -fi - -# 6. Cleanup — re-enable all targeted services. -echo -echo "==> Cleanup: re-enabling ${SERVICES[*]}" -for svc in "${SERVICES[@]}"; do - toggle_service "$svc" true >/dev/null || true -done - -rm -f /tmp/lock-smoke-body.$$ -exit "$EXIT" diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php index 1ce36c8577..0d1cd83203 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php @@ -60,7 +60,6 @@ class Update extends Action ->inject('project') ->inject('authorization') ->inject('queueForEvents') - ->inject('distributedLockOrFail') ->callback($this->action(...)); } @@ -71,22 +70,17 @@ class Update extends Action Database $dbForPlatform, Document $project, Authorization $authorization, - Event $queueForEvents, - callable $distributedLockOrFail, + Event $queueForEvents ): void { $auth = Config::getParam('auth')[$methodId] ?? []; $authKey = $auth['key'] ?? ''; - $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $authKey, $enabled, $dbForPlatform, $authorization) { - $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); + $auths = $project->getAttribute('auths', []); + $auths[$authKey] = $enabled; - $auths = $project->getAttribute('auths', []); - $auths[$authKey] = $enabled; - - return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'auths' => $auths, - ]))); - }); + $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'auths' => $auths, + ]))); $queueForEvents->setParam('methodId', $methodId); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php index 8ab0653df3..f4002c60ef 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php @@ -59,7 +59,6 @@ class Create extends Action ->inject('project') ->inject('dbForPlatform') ->inject('authorization') - ->inject('distributedLockOrFail') ->callback($this->action(...)); } @@ -71,8 +70,21 @@ class Create extends Action Document $project, Database $dbForPlatform, Authorization $authorization, - callable $distributedLockOrFail, ) { + $auths = $project->getAttribute('auths', []); + + $mockNumbers = $auths['mockNumbers'] ?? []; + + if (\count($mockNumbers) >= APP_LIMIT_COUNT) { + throw new Exception(Exception::MOCK_NUMBER_LIMIT_EXCEEDED); + } + + foreach ($mockNumbers as $mockNumber) { + if ($mockNumber['phone'] === $number) { + throw new Exception(Exception::MOCK_NUMBER_ALREADY_EXISTS); + } + } + // Set to now date $mockNumber = [ 'phone' => $number, @@ -81,29 +93,14 @@ class Create extends Action '$updatedAt' => DateTime::now(), ]; - $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $number, $mockNumber, $dbForPlatform, $authorization) { - $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); + $mockNumbers[] = $mockNumber; + $auths['mockNumbers'] = $mockNumbers; - $auths = $project->getAttribute('auths', []); - $mockNumbers = $auths['mockNumbers'] ?? []; + $updates = new Document([ + 'auths' => $auths, + ]); - if (\count($mockNumbers) >= APP_LIMIT_COUNT) { - throw new Exception(Exception::MOCK_NUMBER_LIMIT_EXCEEDED); - } - - foreach ($mockNumbers as $existing) { - if ($existing['phone'] === $number) { - throw new Exception(Exception::MOCK_NUMBER_ALREADY_EXISTS); - } - } - - $mockNumbers[] = $mockNumber; - $auths['mockNumbers'] = $mockNumbers; - - $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'auths' => $auths, - ]))); - }); + $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); $queueForEvents->setParam('number', $number); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php index bbc534fdf8..0fb23e1764 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php @@ -58,7 +58,6 @@ class Delete extends Action ->inject('project') ->inject('dbForPlatform') ->inject('authorization') - ->inject('distributedLockOrFail') ->callback($this->action(...)); } @@ -69,33 +68,33 @@ class Delete extends Action Document $project, Database $dbForPlatform, Authorization $authorization, - callable $distributedLockOrFail, ) { - $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $number, $dbForPlatform, $authorization) { - $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); + $auths = $project->getAttribute('auths', []); - $auths = $project->getAttribute('auths', []); - $mockNumbers = $auths['mockNumbers'] ?? []; + $mockNumbers = $auths['mockNumbers'] ?? []; - $mockNumberIndex = null; - foreach ($mockNumbers as $index => $mock) { - if ($mock['phone'] === $number) { - $mockNumberIndex = $index; - break; - } + $mockNumberIndex = null; + foreach ($mockNumbers as $index => $mock) { + if ($mock['phone'] === $number) { + $mockNumberIndex = $index; + break; } + } - if (\is_null($mockNumberIndex)) { - throw new Exception(Exception::MOCK_NUMBER_NOT_FOUND); - } + if (\is_null($mockNumberIndex)) { + throw new Exception(Exception::MOCK_NUMBER_NOT_FOUND); + } - unset($mockNumbers[$mockNumberIndex]); - $auths['mockNumbers'] = array_values($mockNumbers); + unset($mockNumbers[$mockNumberIndex]); + $mockNumbers = array_values($mockNumbers); - $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'auths' => $auths, - ]))); - }); + $auths['mockNumbers'] = $mockNumbers; + + $updates = new Document([ + 'auths' => $auths, + ]); + + $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); $queueForEvents->setParam('number', $number); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php index a492d7c7bd..48b90a1b97 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php @@ -59,7 +59,6 @@ class Update extends Action ->inject('project') ->inject('dbForPlatform') ->inject('authorization') - ->inject('distributedLockOrFail') ->callback($this->action(...)); } @@ -71,41 +70,38 @@ class Update extends Action Document $project, Database $dbForPlatform, Authorization $authorization, - callable $distributedLockOrFail, ) { - $mockNumber = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $number, $otp, $dbForPlatform, $authorization) { - $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); + $auths = $project->getAttribute('auths', []); - $auths = $project->getAttribute('auths', []); - $mockNumbers = $auths['mockNumbers'] ?? []; + $mockNumbers = $auths['mockNumbers'] ?? []; - $mockNumberIndex = null; - foreach ($mockNumbers as $index => $mock) { - if ($mock['phone'] === $number) { - $mockNumberIndex = $index; - break; - } + $mockNumberIndex = null; + foreach ($mockNumbers as $index => $mock) { + if ($mock['phone'] === $number) { + $mockNumberIndex = $index; + break; } + } - if (\is_null($mockNumberIndex)) { - throw new Exception(Exception::MOCK_NUMBER_NOT_FOUND); - } + if (\is_null($mockNumberIndex)) { + throw new Exception(Exception::MOCK_NUMBER_NOT_FOUND); + } - $mockNumbers[$mockNumberIndex]['otp'] = $otp; - $mockNumbers[$mockNumberIndex]['$updatedAt'] = DateTime::now(); - $auths['mockNumbers'] = $mockNumbers; + $mockNumbers[$mockNumberIndex]['otp'] = $otp; + $mockNumbers[$mockNumberIndex]['$updatedAt'] = DateTime::now(); - $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'auths' => $auths, - ]))); + $auths['mockNumbers'] = $mockNumbers; - return $mockNumbers[$mockNumberIndex]; - }); + $updates = new Document([ + 'auths' => $auths, + ]); + + $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); $queueForEvents->setParam('number', $number); $response ->setStatusCode(Response::STATUS_CODE_OK) - ->dynamic(new Document($mockNumber), Response::MODEL_MOCK_NUMBER); + ->dynamic(new Document($mockNumbers[$mockNumberIndex]), Response::MODEL_MOCK_NUMBER); } } diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MembershipPrivacy/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MembershipPrivacy/Update.php index a912ee48a6..c947ff225a 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MembershipPrivacy/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MembershipPrivacy/Update.php @@ -60,7 +60,6 @@ class Update extends Action ->inject('project') ->inject('authorization') ->inject('queueForEvents') - ->inject('distributedLockOrFail') ->callback($this->action(...)); } @@ -75,33 +74,30 @@ class Update extends Action Document $project, Authorization $authorization, Event $queueForEvents, - callable $distributedLockOrFail, ): void { - $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $userId, $userEmail, $userPhone, $userName, $userMFA, $dbForPlatform, $authorization) { - $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); + $auths = $project->getAttribute('auths', []); - $auths = $project->getAttribute('auths', []); + if ($userId !== null) { + $auths['membershipsUserId'] = $userId; + } + if ($userEmail !== null) { + $auths['membershipsUserEmail'] = $userEmail; + } + if ($userPhone !== null) { + $auths['membershipsUserPhone'] = $userPhone; + } + if ($userName !== null) { + $auths['membershipsUserName'] = $userName; + } + if ($userMFA !== null) { + $auths['membershipsMfa'] = $userMFA; + } - if ($userId !== null) { - $auths['membershipsUserId'] = $userId; - } - if ($userEmail !== null) { - $auths['membershipsUserEmail'] = $userEmail; - } - if ($userPhone !== null) { - $auths['membershipsUserPhone'] = $userPhone; - } - if ($userName !== null) { - $auths['membershipsUserName'] = $userName; - } - if ($userMFA !== null) { - $auths['membershipsMfa'] = $userMFA; - } + $updates = new Document([ + 'auths' => $auths, + ]); - return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'auths' => $auths, - ]))); - }); + $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordDictionary/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordDictionary/Update.php index 9cb310257e..e2c678abb6 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordDictionary/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordDictionary/Update.php @@ -56,7 +56,6 @@ class Update extends Action ->inject('project') ->inject('authorization') ->inject('queueForEvents') - ->inject('distributedLockOrFail') ->callback($this->action(...)); } @@ -67,18 +66,15 @@ class Update extends Action Document $project, Authorization $authorization, Event $queueForEvents, - callable $distributedLockOrFail, ): void { - $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $enabled, $dbForPlatform, $authorization) { - $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); + $auths = $project->getAttribute('auths', []); + $auths['passwordDictionary'] = $enabled; - $auths = $project->getAttribute('auths', []); - $auths['passwordDictionary'] = $enabled; + $updates = new Document([ + 'auths' => $auths, + ]); - return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'auths' => $auths, - ]))); - }); + $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php index cfff58c8f0..a8ae81caff 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php @@ -59,7 +59,6 @@ class Update extends Action ->inject('project') ->inject('authorization') ->inject('queueForEvents') - ->inject('distributedLockOrFail') ->callback($this->action(...)); } @@ -70,18 +69,20 @@ class Update extends Action Document $project, Authorization $authorization, Event $queueForEvents, - callable $distributedLockOrFail, ): void { - $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $total, $dbForPlatform, $authorization) { - $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); + $auths = $project->getAttribute('auths', []); - $auths = $project->getAttribute('auths', []); - $auths['passwordHistory'] = \is_null($total) ? 0 : $total; + if (\is_null($total)) { + $auths['passwordHistory'] = 0; + } else { + $auths['passwordHistory'] = $total; + } - return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'auths' => $auths, - ]))); - }); + $updates = new Document([ + 'auths' => $auths, + ]); + + $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPersonalData/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPersonalData/Update.php index 580b04754b..9db7cf0549 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPersonalData/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPersonalData/Update.php @@ -57,7 +57,6 @@ class Update extends Action ->inject('project') ->inject('authorization') ->inject('queueForEvents') - ->inject('distributedLockOrFail') ->callback($this->action(...)); } @@ -68,18 +67,15 @@ class Update extends Action Document $project, Authorization $authorization, Event $queueForEvents, - callable $distributedLockOrFail, ): void { - $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $enabled, $dbForPlatform, $authorization) { - $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); + $auths = $project->getAttribute('auths', []); + $auths['personalDataCheck'] = $enabled; - $auths = $project->getAttribute('auths', []); - $auths['personalDataCheck'] = $enabled; + $updates = new Document([ + 'auths' => $auths, + ]); - return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'auths' => $auths, - ]))); - }); + $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionAlert/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionAlert/Update.php index 8f9f0a4c48..22b7a44b04 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionAlert/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionAlert/Update.php @@ -56,7 +56,6 @@ class Update extends Action ->inject('project') ->inject('authorization') ->inject('queueForEvents') - ->inject('distributedLockOrFail') ->callback($this->action(...)); } @@ -67,18 +66,15 @@ class Update extends Action Document $project, Authorization $authorization, Event $queueForEvents, - callable $distributedLockOrFail, ): void { - $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $enabled, $dbForPlatform, $authorization) { - $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); + $auths = $project->getAttribute('auths', []); + $auths['sessionAlerts'] = $enabled; - $auths = $project->getAttribute('auths', []); - $auths['sessionAlerts'] = $enabled; + $updates = new Document([ + 'auths' => $auths, + ]); - return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'auths' => $auths, - ]))); - }); + $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php index c8111b5563..ba72c93a6f 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php @@ -56,7 +56,6 @@ class Update extends Action ->inject('project') ->inject('authorization') ->inject('queueForEvents') - ->inject('distributedLockOrFail') ->callback($this->action(...)); } @@ -67,18 +66,15 @@ class Update extends Action Document $project, Authorization $authorization, Event $queueForEvents, - callable $distributedLockOrFail, ): void { - $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $duration, $dbForPlatform, $authorization) { - $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); + $auths = $project->getAttribute('auths', []); + $auths['duration'] = $duration; - $auths = $project->getAttribute('auths', []); - $auths['duration'] = $duration; + $updates = new Document([ + 'auths' => $auths, + ]); - return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'auths' => $auths, - ]))); - }); + $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionInvalidation/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionInvalidation/Update.php index e10c0d4081..8f8a959959 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionInvalidation/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionInvalidation/Update.php @@ -56,7 +56,6 @@ class Update extends Action ->inject('project') ->inject('authorization') ->inject('queueForEvents') - ->inject('distributedLockOrFail') ->callback($this->action(...)); } @@ -67,18 +66,15 @@ class Update extends Action Document $project, Authorization $authorization, Event $queueForEvents, - callable $distributedLockOrFail, ): void { - $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $enabled, $dbForPlatform, $authorization) { - $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); + $auths = $project->getAttribute('auths', []); + $auths['invalidateSessions'] = $enabled; - $auths = $project->getAttribute('auths', []); - $auths['invalidateSessions'] = $enabled; + $updates = new Document([ + 'auths' => $auths, + ]); - return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'auths' => $auths, - ]))); - }); + $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php index b111534430..382ed6f0d9 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php @@ -57,7 +57,6 @@ class Update extends Action ->inject('project') ->inject('authorization') ->inject('queueForEvents') - ->inject('distributedLockOrFail') ->callback($this->action(...)); } @@ -68,18 +67,20 @@ class Update extends Action Document $project, Authorization $authorization, Event $queueForEvents, - callable $distributedLockOrFail, ): void { - $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $total, $dbForPlatform, $authorization) { - $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); + $auths = $project->getAttribute('auths', []); - $auths = $project->getAttribute('auths', []); - $auths['maxSessions'] = \is_null($total) ? 0 : $total; + if (\is_null($total)) { + $auths['maxSessions'] = 0; + } else { + $auths['maxSessions'] = $total; + } - return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'auths' => $auths, - ]))); - }); + $updates = new Document([ + 'auths' => $auths, + ]); + + $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php index c59a17b61f..9129b81250 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php @@ -57,7 +57,6 @@ class Update extends Action ->inject('project') ->inject('authorization') ->inject('queueForEvents') - ->inject('distributedLockOrFail') ->callback($this->action(...)); } @@ -68,18 +67,20 @@ class Update extends Action Document $project, Authorization $authorization, Event $queueForEvents, - callable $distributedLockOrFail, ): void { - $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $total, $dbForPlatform, $authorization) { - $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); + $auths = $project->getAttribute('auths', []); - $auths = $project->getAttribute('auths', []); - $auths['limit'] = \is_null($total) ? 0 : $total; + if (\is_null($total)) { + $auths['limit'] = 0; + } else { + $auths['limit'] = $total; + } - return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'auths' => $auths, - ]))); - }); + $updates = new Document([ + 'auths' => $auths, + ]); + + $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php index 0f3174b143..ad5691c1e0 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php @@ -60,7 +60,6 @@ class Update extends Action ->inject('project') ->inject('authorization') ->inject('queueForEvents') - ->inject('distributedLockOrFail') ->callback($this->action(...)); } @@ -72,18 +71,13 @@ class Update extends Action Document $project, Authorization $authorization, Event $queueForEvents, - callable $distributedLockOrFail, ): void { - $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $protocolId, $enabled, $dbForPlatform, $authorization) { - $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); + $protocols = $project->getAttribute('apis', []); + $protocols[$protocolId] = $enabled; - $protocols = $project->getAttribute('apis', []); - $protocols[$protocolId] = $enabled; - - return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'apis' => $protocols, - ]))); - }); + $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'apis' => $protocols, + ]))); $queueForEvents->setParam('protocolId', $protocolId); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php index b8e3c6e09f..97e723f52c 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php @@ -72,7 +72,6 @@ class Update extends Action ->inject('dbForPlatform') ->inject('project') ->inject('authorization') - ->inject('distributedLockOrFail') ->callback($this->action(...)); } @@ -91,101 +90,83 @@ class Update extends Action Response $response, Database $dbForPlatform, Document $project, - Authorization $authorization, - callable $distributedLockOrFail, + Authorization $authorization ): void { - $inputs = [ - 'host' => $host, - 'port' => $port, - 'username' => $username, - 'password' => $password, - 'senderEmail' => $senderEmail, - 'senderName' => $senderName, - 'replyToEmail' => $replyToEmail, - 'replyToName' => $replyToName, - 'secure' => $secure, - 'enabled' => $enabled, - ]; + // Fetch current configuration + $smtp = $project->getAttribute('smtp', []); - // The SMTP test (PHPMailer SmtpConnect with Timeout=5) runs inside the - // lock so two concurrent SMTP updates don't validate against the same - // baseline and overwrite each other's secrets. The 10s default lock - // TTL covers the worst-case 5s connection probe with margin. - $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $inputs, $enabled, $dbForPlatform, $authorization) { - $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); + // Apply changes + $keys = ['host', 'port', 'username', 'password', 'senderEmail', 'senderName', 'replyToEmail', 'replyToName', 'secure', 'enabled']; + foreach ($keys as $key) { + if (!\is_null(${$key})) { + $smtp[$key] = ${$key}; + } + } - // Fetch current configuration - $smtp = $project->getAttribute('smtp', []); + // Backwards compatibility + $smtp['replyToEmail'] = $smtp['replyToEmail'] ?? $smtp['replyTo'] ?? ''; - // Apply changes - foreach ($inputs as $key => $value) { - if (!\is_null($value)) { - $smtp[$key] = $value; + if (($smtp['enabled'] ?? false) === true) { + // Ensure required fields are set + $requiredKeys = ['host', 'port', 'senderEmail']; + foreach ($requiredKeys as $key) { + if (empty($smtp[$key])) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Param "' . $key . '" is not optional.'); } } + } - // Backwards compatibility - $smtp['replyToEmail'] = $smtp['replyToEmail'] ?? $smtp['replyTo'] ?? ''; + // Validate SMTP credentials + // Validate when the caller is explicitly enabling or hasn't expressed a preference + // (so a credentials-only PATCH can auto-enable). Skip only when the caller is + // explicitly keeping/turning SMTP off. + if (\is_null($enabled) || $enabled === true) { + $mail = new PHPMailer(true); + $mail->isSMTP(); - if (($smtp['enabled'] ?? false) === true) { - // Ensure required fields are set - $requiredKeys = ['host', 'port', 'senderEmail']; - foreach ($requiredKeys as $key) { - if (empty($smtp[$key])) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Param "' . $key . '" is not optional.'); - } - } + $mail->Host = $smtp['host'] ?? ''; + $mail->Port = $smtp['port'] ?? ''; + $mail->SMTPSecure = $smtp['secure'] ?? ''; + $mail->setFrom($smtp['senderEmail'], $smtp['senderName'] ?? ''); + + if (!empty($smtp['username'] ?? '')) { + $mail->SMTPAuth = true; + $mail->Username = $smtp['username']; + $mail->Password = $smtp['password'] ?? ''; } - // Validate SMTP credentials - // Validate when the caller is explicitly enabling or hasn't expressed a preference - // (so a credentials-only PATCH can auto-enable). Skip only when the caller is - // explicitly keeping/turning SMTP off. - if (\is_null($enabled) || $enabled === true) { - $mail = new PHPMailer(true); - $mail->isSMTP(); - - $mail->Host = $smtp['host'] ?? ''; - $mail->Port = $smtp['port'] ?? ''; - $mail->SMTPSecure = $smtp['secure'] ?? ''; - $mail->setFrom($smtp['senderEmail'], $smtp['senderName'] ?? ''); - - if (!empty($smtp['username'] ?? '')) { - $mail->SMTPAuth = true; - $mail->Username = $smtp['username']; - $mail->Password = $smtp['password'] ?? ''; - } - - if (!empty($smtp['replyToEmail'] ?? '')) { - $mail->addReplyTo($smtp['replyToEmail'], $smtp['replyToName'] ?? ''); - } - - $mail->SMTPAutoTLS = false; - $mail->Timeout = 5; - - try { - $valid = $mail->SmtpConnect(); - - if (!$valid) { - throw new \Exception('Connection is not valid.'); - } - - // Auto-enable if configuration is valid - // Dont do this if specifically request to mark disabled - if (\is_null($enabled)) { - $smtp['enabled'] = true; - } - } catch (Throwable $error) { - if (($smtp['enabled'] ?? null) === true) { - throw new Exception(Exception::PROJECT_SMTP_CONFIG_INVALID, $error->getMessage()); - } - } + if (!empty($smtp['replyToEmail'] ?? '')) { + $mail->addReplyTo($smtp['replyToEmail'], $smtp['replyToName'] ?? ''); } - return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'smtp' => $smtp, - ]))); - }); + $mail->SMTPAutoTLS = false; + $mail->Timeout = 5; + + try { + $valid = $mail->SmtpConnect(); + + if (!$valid) { + throw new \Exception('Connection is not valid.'); + } + + // Auto-enable if configuration is valid + // Dont do this if specifically request to mark disabled + if (\is_null($enabled)) { + $smtp['enabled'] = true; + } + } catch (Throwable $error) { + if (($smtp['enabled'] ?? null) === true) { + throw new Exception(Exception::PROJECT_SMTP_CONFIG_INVALID, $error->getMessage()); + } + } + } + + // Save configuration + $updates = new Document([ + 'smtp' => $smtp, + ]); + + $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); $response->dynamic($project, Response::MODEL_PROJECT); } diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php index 1126da8897..7aab6f5ad0 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php @@ -60,7 +60,6 @@ class Update extends Action ->inject('project') ->inject('authorization') ->inject('queueForEvents') - ->inject('distributedLockOrFail') ->callback($this->action(...)); } @@ -71,26 +70,14 @@ class Update extends Action Database $dbForPlatform, Document $project, Authorization $authorization, - Event $queueForEvents, - callable $distributedLockOrFail, + Event $queueForEvents ): void { - // The services map is a JSON object on the project document. Two - // concurrent service toggles read the same baseline, each set their - // own key, and the second updateDocument() overwrites the first — - // silent lost-update on a sparse write. Lock the project doc for - // the read-modify-write window; re-read inside the lock so the - // baseline reflects any update that landed between request init - // and lock acquisition. - $project = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $serviceId, $enabled, $dbForPlatform, $authorization) { - $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); + $services = $project->getAttribute('services', []); + $services[$serviceId] = $enabled; - $services = $project->getAttribute('services', []); - $services[$serviceId] = $enabled; - - return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'services' => $services, - ]))); - }); + $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'services' => $services, + ]))); $queueForEvents->setParam('serviceId', $serviceId); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Templates/Email/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Templates/Email/Update.php index ed1f7d8f7f..ef93abf683 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Templates/Email/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Templates/Email/Update.php @@ -69,7 +69,6 @@ class Update extends Action ->inject('dbForPlatform') ->inject('authorization') ->inject('project') - ->inject('distributedLockOrFail') ->callback($this->action(...)); } @@ -87,61 +86,47 @@ class Update extends Action Database $dbForPlatform, Authorization $authorization, Document $project, - callable $distributedLockOrFail, ) { $locale = $locale ?: System::getEnv('_APP_LOCALE', 'en'); - $inputs = [ - 'senderName' => $senderName, - 'senderEmail' => $senderEmail, - 'replyToEmail' => $replyToEmail, - 'replyToName' => $replyToName, - 'message' => $message, - 'subject' => $subject, - ]; + // Prevent template update if custom SMTP is not configured + $smtp = $project->getAttribute('smtp', []); + if (($smtp['enabled'] ?? false) !== true) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'SMTP must be enabled on the project to configure custom email templates.'); + } - $template = $distributedLockOrFail("lock:platform:projects:{$project->getId()}", function () use ($project, $templateId, $locale, $inputs, $dbForPlatform, $authorization) { - $project = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $project->getId())); + // Fetch current configuration + $templates = $project->getAttribute('templates', []); + $template = $templates['email.' . $templateId . '-' . $locale] ?? []; - // Prevent template update if custom SMTP is not configured - $smtp = $project->getAttribute('smtp', []); - if (($smtp['enabled'] ?? false) !== true) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'SMTP must be enabled on the project to configure custom email templates.'); + // Apply changes + $keys = ['senderName', 'senderEmail', 'replyToEmail', 'replyToName', 'message', 'subject']; + foreach ($keys as $key) { + if (!\is_null(${$key})) { + $template[$key] = ${$key}; } + } - // Fetch current configuration - $templates = $project->getAttribute('templates', []); - $template = $templates['email.' . $templateId . '-' . $locale] ?? []; + // Backwards compatibility + if (!\is_null($template['replyTo'] ?? null)) { + $template['replyToEmail'] = $template['replyToEmail'] ?? $template['replyTo'] ?? ''; + } - // Apply changes - foreach ($inputs as $key => $value) { - if (!\is_null($value)) { - $template[$key] = $value; - } + // Ensure required fields are set + $requiredKeys = ['subject', 'message']; + foreach ($requiredKeys as $key) { + if (empty($template[$key])) { + throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Param "' . $key . '" is not optional.'); } + } - // Backwards compatibility - if (!\is_null($template['replyTo'] ?? null)) { - $template['replyToEmail'] = $template['replyToEmail'] ?? $template['replyTo'] ?? ''; - } + // Save configuration + $templates['email.' . $templateId . '-' . $locale] = $template; + $updates = new Document([ + 'templates' => $templates, + ]); - // Ensure required fields are set - $requiredKeys = ['subject', 'message']; - foreach ($requiredKeys as $key) { - if (empty($template[$key])) { - throw new Exception(Exception::GENERAL_ARGUMENT_INVALID, 'Param "' . $key . '" is not optional.'); - } - } - - // Save configuration - $templates['email.' . $templateId . '-' . $locale] = $template; - - $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'templates' => $templates, - ]))); - - return $template; - }); + $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); $queueForEvents->setParam('templateId', $templateId); diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Team/Update.php b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Team/Update.php index 25286a2227..ab92d1a15f 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Team/Update.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Team/Update.php @@ -55,37 +55,29 @@ class Update extends Action ->param('teamId', '', new UID(), 'Team ID of the team to transfer project to.') ->inject('response') ->inject('dbForPlatform') - ->inject('distributedLockOrFail') ->callback($this->action(...)); } - public function action(string $projectId, string $teamId, Response $response, Database $dbForPlatform, callable $distributedLockOrFail) + public function action(string $projectId, string $teamId, Response $response, Database $dbForPlatform) { - // Lock around the project doc RMW. Cascade fan-out to installations, - // repositories and vcsComments runs after the lock is released — - // those write to separate collections, not the project doc. - [$project, $permissions] = $distributedLockOrFail("lock:platform:projects:{$projectId}", function () use ($projectId, $teamId, $dbForPlatform) { - $project = $dbForPlatform->getDocument('projects', $projectId); - $team = $dbForPlatform->getDocument('teams', $teamId); + $project = $dbForPlatform->getDocument('projects', $projectId); + $team = $dbForPlatform->getDocument('teams', $teamId); - if ($project->isEmpty()) { - throw new Exception(Exception::PROJECT_NOT_FOUND); - } + if ($project->isEmpty()) { + throw new Exception(Exception::PROJECT_NOT_FOUND); + } - if ($team->isEmpty()) { - throw new Exception(Exception::TEAM_NOT_FOUND); - } + if ($team->isEmpty()) { + throw new Exception(Exception::TEAM_NOT_FOUND); + } - $permissions = $this->getPermissions($teamId, $projectId); + $permissions = $this->getPermissions($teamId, $projectId); - $project = $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'teamId' => $teamId, - 'teamInternalId' => $team->getSequence(), - '$permissions' => $permissions, - ])); - - return [$project, $permissions]; - }); + $project = $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ + 'teamId' => $teamId, + 'teamInternalId' => $team->getSequence(), + '$permissions' => $permissions, + ])); $installations = $dbForPlatform->find('installations', [ Query::equal('projectInternalId', [$project->getSequence()]), diff --git a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Update.php b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Update.php index 4b79b37bfa..29c26b33ea 100644 --- a/src/Appwrite/Platform/Modules/Projects/Http/Projects/Update.php +++ b/src/Appwrite/Platform/Modules/Projects/Http/Projects/Update.php @@ -65,36 +65,29 @@ class Update extends Action ->param('legalTaxId', '', new Text(256), 'Project legal tax ID. Max length: 256 chars.', true) ->inject('response') ->inject('dbForPlatform') - ->inject('distributedLockOrFail') ->callback($this->action(...)); } - public function action(string $projectId, string $name, string $description, string $logo, string $url, string $legalName, string $legalCountry, string $legalState, string $legalCity, string $legalAddress, string $legalTaxId, Response $response, Database $dbForPlatform, callable $distributedLockOrFail) + public function action(string $projectId, string $name, string $description, string $logo, string $url, string $legalName, string $legalCountry, string $legalState, string $legalCity, string $legalAddress, string $legalTaxId, Response $response, Database $dbForPlatform) { - // Re-fetch and write the full project doc inside the lock. This is the - // worst RMW window in the projects API — the endpoint passes the entire - // document back to updateDocument(), so a concurrent write to *any* - // attribute (services, auths, smtp, ...) would be silently overwritten. - $project = $distributedLockOrFail("lock:platform:projects:{$projectId}", function () use ($projectId, $name, $description, $logo, $url, $legalName, $legalCountry, $legalState, $legalCity, $legalAddress, $legalTaxId, $dbForPlatform) { - $project = $dbForPlatform->getDocument('projects', $projectId); + $project = $dbForPlatform->getDocument('projects', $projectId); - if ($project->isEmpty()) { - throw new Exception(Exception::PROJECT_NOT_FOUND); - } + if ($project->isEmpty()) { + throw new Exception(Exception::PROJECT_NOT_FOUND); + } - return $dbForPlatform->updateDocument('projects', $project->getId(), $project - ->setAttribute('name', $name) - ->setAttribute('description', $description) - ->setAttribute('logo', $logo) - ->setAttribute('url', $url) - ->setAttribute('legalName', $legalName) - ->setAttribute('legalCountry', $legalCountry) - ->setAttribute('legalState', $legalState) - ->setAttribute('legalCity', $legalCity) - ->setAttribute('legalAddress', $legalAddress) - ->setAttribute('legalTaxId', $legalTaxId) - ->setAttribute('search', implode(' ', [$projectId, $name]))); - }); + $project = $dbForPlatform->updateDocument('projects', $project->getId(), $project + ->setAttribute('name', $name) + ->setAttribute('description', $description) + ->setAttribute('logo', $logo) + ->setAttribute('url', $url) + ->setAttribute('legalName', $legalName) + ->setAttribute('legalCountry', $legalCountry) + ->setAttribute('legalState', $legalState) + ->setAttribute('legalCity', $legalCity) + ->setAttribute('legalAddress', $legalAddress) + ->setAttribute('legalTaxId', $legalTaxId) + ->setAttribute('search', implode(' ', [$projectId, $name]))); $response->dynamic($project, Response::MODEL_PROJECT); } diff --git a/tests/e2e/Client.php b/tests/e2e/Client.php index 84c937d2d1..4358058fe3 100644 --- a/tests/e2e/Client.php +++ b/tests/e2e/Client.php @@ -211,13 +211,7 @@ class Client } } - // CURLOPT_PATH_AS_IS isn't supported by Swoole's emulated cURL when the - // SWOOLE_HOOK_CURL coroutine hook is active. Skip it in that case so - // tests that need real parallel HTTP (Swoole\Coroutine\run + cURL hook) - // don't fatal-error here. Native (non-hooked) cURL keeps the option. - if (! \extension_loaded('swoole') || ! (\Swoole\Runtime::getHookFlags() & SWOOLE_HOOK_CURL)) { - curl_setopt($ch, CURLOPT_PATH_AS_IS, 1); - } + curl_setopt($ch, CURLOPT_PATH_AS_IS, 1); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $followRedirects); @@ -243,12 +237,9 @@ class Client if ($method === self::METHOD_HEAD) { curl_setopt($ch, CURLOPT_NOBODY, true); // This is crucial for HEAD requests curl_setopt($ch, CURLOPT_HEADER, false); + } else { + curl_setopt($ch, CURLOPT_NOBODY, false); } - // Note: explicit CURLOPT_NOBODY=false on non-HEAD requests is redundant - // (false is cURL's default) and actively breaks Swoole's emulated cURL - // on PATCH-with-body — Swoole strips the body and the request reaches - // the server as a method-without-body, hitting the framework's 404 - // fallback. Just skip the redundant set. if ($method != self::METHOD_GET && $method != self::METHOD_HEAD) { curl_setopt($ch, CURLOPT_POSTFIELDS, $query); diff --git a/tests/e2e/Services/Project/ServicesBase.php b/tests/e2e/Services/Project/ServicesBase.php index c8c7bd173f..b5f94f8181 100644 --- a/tests/e2e/Services/Project/ServicesBase.php +++ b/tests/e2e/Services/Project/ServicesBase.php @@ -266,94 +266,6 @@ trait ServicesBase $this->assertSame(true, $response['body']['serviceStatusForTeams']); } - /** - * Concurrency test for the distributed-lock pilot on `updateProjectService`. - * - * Without locking, two concurrent toggles to different services on the - * same project both read the same baseline `services` map, each set their - * own key, and the second sparse `updateDocument()` overwrites the first - * — silent lost-update. - * - * The test fires N parallel PATCH calls via Swoole coroutines (with the - * SWOOLE_HOOK_CURL runtime hook enabled so the test Client's cURL calls - * yield to the scheduler). After all coroutines complete the project is - * refetched and the count of enabled services is compared against the - * count of HTTP 200 responses. - * - * To verify the test catches the bug: - * - `_APP_LOCKING_ENABLED=enabled` → must pass - * - `_APP_LOCKING_ENABLED=disabled` → must FAIL (lost updates) - */ - public function testConcurrentTogglesAllPersist(): void - { - $services = ['teams', 'storage', 'functions', 'sites', 'messaging']; - - // Baseline: disable everything so the toggle direction is unambiguous. - // Done outside the coroutine context so it stays sequential and the - // resulting project state is a clean known-zero before the race. - foreach ($services as $service) { - $this->updateServiceStatus($service, false); - } - - // Enable Swoole's cURL hook so the test Client's HTTP calls yield - // to the scheduler and the foreach below actually runs in parallel. - // Without this, coroutines serialize on cURL and the negative-case - // run (locking disabled) would falsely pass. - \Swoole\Runtime::enableCoroutine(\SWOOLE_HOOK_CURL); - - $results = []; - try { - \Swoole\Coroutine\run(function () use ($services, &$results): void { - foreach ($services as $service) { - \Swoole\Coroutine::create(function () use ($service, &$results): void { - $response = $this->updateServiceStatus($service, true); - $results[$service] = $response['headers']['status-code']; - }); - } - }); - } finally { - // SWOOLE_HOOK_NONE isn't defined in some Swoole builds; pass 0 - // (the integer value of "no hooks") to disable any hooks this - // test enabled so subsequent tests run with native cURL. - \Swoole\Runtime::enableCoroutine(0); - } - - $successCount = count(array_filter($results, fn ($code) => $code === 200)); - - // Refetch the project and count which of the targeted services ended up - // enabled. The endpoint returns serviceStatusFor{Service} keys. - $project = $this->client->call(Client::METHOD_GET, '/projects/' . $this->getProject()['$id'], [ - 'content-type' => 'application/json', - 'x-appwrite-project' => 'console', - 'cookie' => 'a_session_console=' . $this->getRoot()['session'], - ]); - - $enabledCount = 0; - foreach ($services as $service) { - $key = 'serviceStatusFor' . ucfirst($service); - if (($project['body'][$key] ?? false) === true) { - $enabledCount++; - } - } - - $this->assertGreaterThan(0, $successCount, 'At least one concurrent toggle should succeed'); - $this->assertSame( - $successCount, - $enabledCount, - sprintf( - 'Each successful concurrent toggle must persist. successCount=%d enabledCount=%d (lost-update detected — distributed lock not effective)', - $successCount, - $enabledCount, - ), - ); - - // Cleanup: leave all targeted services enabled so subsequent tests - // see a known-good baseline. - foreach ($services as $service) { - $this->updateServiceStatus($service, true); - } - } - // Helpers protected function updateServiceStatus(string $serviceId, bool $enabled, bool $authenticated = true): mixed From b15457bccad6facf63fb43cb27dcdad28aaf2d7e Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Wed, 29 Apr 2026 05:50:37 +0100 Subject: [PATCH 10/26] style: trim verbose comments on lock factories and call sites --- app/controllers/general.php | 3 -- app/controllers/shared/api.php | 3 -- app/init/resources/request.php | 67 ++++------------------------------ 3 files changed, 8 insertions(+), 65 deletions(-) diff --git a/app/controllers/general.php b/app/controllers/general.php index aa4134614c..0fde400b89 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -136,9 +136,6 @@ function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, S if (!$project->isEmpty() && $project->getId() !== 'console') { $accessedAt = $project->getAttribute('accessedAt', 0); if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_PROJECT_ACCESS)) > $accessedAt) { - // Skip-on-contention: every concurrent router request would write - // the same throttled timestamp, so losing the race is correct — - // the winning pod's update covers ours. $distributedLock('lock:platform:projects:' . $project->getId(), function () use ($dbForPlatform, $project, $authorization) { $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'accessedAt' => DateTime::now() diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index b07eec1a99..0ce1b6ca33 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -250,9 +250,6 @@ Http::init() } if (! $updates->isEmpty()) { - // Serialize concurrent per-request writes to this key across API nodes. - // Skip on contention is safe: the winner applies the same idempotent update; - // if this node observed a new SDK the other didn't, the next request catches up. $distributedLock('lock:platform:keys:' . $dbKey->getId(), function () use ($dbForPlatform, $dbKey, $updates, $apiKey, $project, $user, $team) { $dbForPlatform->getAuthorization()->skip(fn () => $dbForPlatform->updateDocument('keys', $dbKey->getId(), $updates)); diff --git a/app/init/resources/request.php b/app/init/resources/request.php index 6d5663cdae..71af68b4b2 100644 --- a/app/init/resources/request.php +++ b/app/init/resources/request.php @@ -76,25 +76,8 @@ return function (Container $container): void { return $register->get('logger'); }, ['register']); - /** - * Push lock-side errors to the configured logger (Sentry/Raygun/AppSignal/etc.) - * by mutating the per-request `Log` object — same pattern as - * Embeddings/Text/Create.php and the http.php request-end error handler. - * - * Rate limited per pod via a static bucket so a sustained backend outage - * doesn't flood the logger. At most one push per 60s per (action, target) - * combo. Across a fleet of N pods this caps at N events/min, well within - * Sentry's own dedup tolerance. - * - * Only "real" errors are reported here: - * - backend_error: Redis/Dragonfly unreachable - * - release_error: lock release failed (TTL expired or backend dropped) - * - * Skipped on purpose: contention 409s (normal user-facing concurrency), - * skip-on-contention events (idempotent fan-out by design), acquire retry - * conflicts (internal loop), destructor cleanups (have an expected - * baseline rate; aggregate via the `lock.attempts` counter instead). - */ + // Rate-limited to one push per 60s per (action, target) so a sustained + // backend outage doesn't flood Sentry across the pod fleet. $lockErrorReporter = function (Log $log, ?Logger $logger, string $action, string $key, string $target, Throwable $e): void { static $lastReportAt = []; @@ -122,8 +105,7 @@ return function (Container $container): void { : Log::ENVIRONMENT_STAGING); $log->addTag('lock.target', $target); - // Strip the trailing document ID from the key so log aggregators don't - // see unbounded cardinality; the {target} attribute already covers slicing. + // Strip trailing document ID to keep aggregator cardinality bounded. $log->addTag('lock.key_pattern', preg_replace('/:[^:]+$/', ':*', $key)); $log->addTag('code', $e->getCode()); @@ -134,33 +116,17 @@ return function (Container $container): void { try { $logger->addLog($log); } catch (Throwable) { - // best-effort; don't let logging failures break fail-open } }; - // Extract the collection segment ("keys" / "projects" / "users") from keys - // of the form "lock:platform:{target}:{id}" so metrics can slice by target. $lockTargetOf = function (string $key): string { $parts = explode(':', $key, 4); return $parts[2] ?? 'unknown'; }; /** - * Distributed-lock factory: skip-on-contention variant. - * - * For idempotent writes where losing the race is correct (e.g., per-request - * `accessedAt` updates from N pods all writing the same value). On contention, - * the callback is silently skipped — another pod is doing the same work. - * - * Behavior: - * - Non-blocking acquire (one attempt). On conflict, skip and return. - * - Fail-open: if Redis is unreachable, run the callback unlocked + warn. - * - Kill switch: `_APP_LOCKING_ENABLED=disabled` runs the callback unlocked. - * - * Returns void — the caller can't distinguish "acquired and ran" from "skipped". - * - * Metric: `lock.attempts{outcome,target}` where outcome ∈ {acquired, skipped, - * backend_error, release_error}. + * Skip-on-contention. For idempotent writes where losing the race is correct + * (e.g., the winning pod's update covers ours). Fail-open on backend error. */ $container->set('distributedLock', function (\Redis $redis, Telemetry $telemetry, Log $log, ?Logger $logger) use ($lockTargetOf, $lockErrorReporter) { $enabled = System::getEnv('_APP_LOCKING_ENABLED', 'enabled') !== 'disabled'; @@ -205,21 +171,8 @@ return function (Container $container): void { }, ['redis', 'telemetry', 'log', 'logger']); /** - * Distributed-lock factory: 409-on-contention variant. - * - * For explicit user-write endpoints where read-modify-write on shared mutable - * state must NOT silently drop a request. On contention, throws - * `Exception::GENERAL_RESOURCE_LOCKED` (HTTP 409) so the client retries. - * - * Behavior: - * - Blocking acquire with short timeout (default 3s). - * - On timeout, throws `GENERAL_RESOURCE_LOCKED`. - * - Fail-open: backend unreachable runs the callback unlocked + warning. - * - Kill switch: `_APP_LOCKING_ENABLED=disabled` runs the callback unlocked. - * - Returns the callback's return value so callers can use the result. - * - * Metric: `lock.attempts{outcome,target}` where outcome ∈ {acquired, contended, - * backend_error, release_error}. + * Block-then-409 on contention. For read-modify-write on shared mutable state + * where silently dropping a request is wrong. Fail-open on backend error. */ $container->set('distributedLockOrFail', function (\Redis $redis, Telemetry $telemetry, Log $log, ?Logger $logger) use ($lockTargetOf, $lockErrorReporter) { $enabled = System::getEnv('_APP_LOCKING_ENABLED', 'enabled') !== 'disabled'; @@ -245,11 +198,7 @@ return function (Container $container): void { if (! $acquired) { $attempts->add(1, ['outcome' => 'contended', 'target' => $target]); - // Don't pass a custom message — the catalog message in - // app/config/errors.php is reused so we don't leak the internal - // lock key (which embeds collection name and document id) into a - // user-facing 409 response. The telemetry attribute already - // carries the target collection for operator-side observability. + // No custom message — the lock key embeds collection + document id. throw new Exception(Exception::GENERAL_RESOURCE_LOCKED); } From ce15eeb7229dff70114951c5c5bf9594b82e042f Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Wed, 29 Apr 2026 07:17:04 +0100 Subject: [PATCH 11/26] refactor: introduce Lock facade for platform-DB lock sites MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Extracts the lock-key format and the lock+auth-skip+sparse-update pattern into Appwrite\Locking\Lock with three methods: - set(collection, id, attribute=accessedAt, value=null) — throttled single-attribute write - run(collection, id, fn) — generic skip-on-contention - runOrFail(collection, id, fn) — block-then-409 for the deferred lost-update follow-up Migrates the 4 call sites (router projects accessedAt + 3 in shared/api) off the raw $distributedLock callable. Raw factories stay as escape hatches for non-platform key shapes. --- app/controllers/general.php | 33 ++++++++---------- app/controllers/shared/api.php | 19 ++++------ app/init/resources/request.php | 10 ++++++ src/Appwrite/Locking/Lock.php | 63 ++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 31 deletions(-) create mode 100644 src/Appwrite/Locking/Lock.php diff --git a/app/controllers/general.php b/app/controllers/general.php index 0fde400b89..9381843b64 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -11,6 +11,7 @@ use Appwrite\Event\Delete as DeleteEvent; use Appwrite\Event\Event; use Appwrite\Event\Publisher\Certificate; use Appwrite\Extend\Exception as AppwriteException; +use Appwrite\Locking\Lock; use Appwrite\Network\Cors; use Appwrite\Platform\Appwrite; use Appwrite\SDK\Method; @@ -67,7 +68,7 @@ use Utopia\Validator\Text; Config::setParam('cookieSamesite', Response::COOKIE_SAMESITE_NONE); -function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, Authorization $authorization, ?Key $apiKey, DeleteEvent $queueForDeletes, int $executionsRetentionCount, callable $distributedLock) +function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, Authorization $authorization, ?Key $apiKey, DeleteEvent $queueForDeletes, int $executionsRetentionCount, Lock $lock) { $host = $request->getHostname(); if (!empty($previewHostname)) { @@ -136,11 +137,7 @@ function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, S if (!$project->isEmpty() && $project->getId() !== 'console') { $accessedAt = $project->getAttribute('accessedAt', 0); if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_PROJECT_ACCESS)) > $accessedAt) { - $distributedLock('lock:platform:projects:' . $project->getId(), function () use ($dbForPlatform, $project, $authorization) { - $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'accessedAt' => DateTime::now() - ]))); - }); + $lock->set('projects', $project->getId()); } /** @@ -849,8 +846,8 @@ Http::init() ->inject('authorization') ->inject('queueForDeletes') ->inject('executionsRetentionCount') - ->inject('distributedLock') - ->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Document $project, Database $dbForPlatform, callable $getProjectDB, Locale $locale, array $localeCodes, Reader $geodb, Event $queueForEvents, Bus $bus, Executor $executor, array $platform, callable $isResourceBlocked, string $previewHostname, Document $devKey, ?Key $apiKey, Cors $cors, Authorization $authorization, DeleteEvent $queueForDeletes, int $executionsRetentionCount, callable $distributedLock) { + ->inject('lock') + ->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Document $project, Database $dbForPlatform, callable $getProjectDB, Locale $locale, array $localeCodes, Reader $geodb, Event $queueForEvents, Bus $bus, Executor $executor, array $platform, callable $isResourceBlocked, string $previewHostname, Document $devKey, ?Key $apiKey, Cors $cors, Authorization $authorization, DeleteEvent $queueForDeletes, int $executionsRetentionCount, Lock $lock) { /* * Appwrite Router */ @@ -858,7 +855,7 @@ Http::init() $platformHostnames = $platform['hostnames'] ?? []; // Only run Router when external domain if (!\in_array($hostname, $platformHostnames) || !empty($previewHostname)) { - if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $queueForDeletes, $executionsRetentionCount, $distributedLock)) { + if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $queueForDeletes, $executionsRetentionCount, $lock)) { $utopia->getRoute()?->label('router', true); } } @@ -1143,15 +1140,15 @@ Http::options() ->inject('authorization') ->inject('queueForDeletes') ->inject('executionsRetentionCount') - ->inject('distributedLock') - ->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, Document $project, Document $devKey, ?Key $apiKey, Cors $cors, Authorization $authorization, DeleteEvent $queueForDeletes, int $executionsRetentionCount, callable $distributedLock) { + ->inject('lock') + ->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, Document $project, Document $devKey, ?Key $apiKey, Cors $cors, Authorization $authorization, DeleteEvent $queueForDeletes, int $executionsRetentionCount, Lock $lock) { /* * Appwrite Router */ $platformHostnames = $platform['hostnames'] ?? []; // Only run Router when external domain if (!in_array($request->getHostname(), $platformHostnames) || !empty($previewHostname)) { - if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $queueForDeletes, $executionsRetentionCount, $distributedLock)) { + if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $queueForDeletes, $executionsRetentionCount, $lock)) { $utopia->getRoute()?->label('router', true); } } @@ -1546,14 +1543,14 @@ Http::get('/robots.txt') ->inject('authorization') ->inject('queueForDeletes') ->inject('executionsRetentionCount') - ->inject('distributedLock') - ->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, ?Key $apiKey, Authorization $authorization, DeleteEvent $queueForDeletes, int $executionsRetentionCount, callable $distributedLock) { + ->inject('lock') + ->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, ?Key $apiKey, Authorization $authorization, DeleteEvent $queueForDeletes, int $executionsRetentionCount, Lock $lock) { $platformHostnames = $platform['hostnames'] ?? []; if (in_array($request->getHostname(), $platformHostnames) || !empty($previewHostname)) { $template = new View(__DIR__ . '/../views/general/robots.phtml'); $response->text($template->render(false)); } else { - if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $queueForDeletes, $executionsRetentionCount, $distributedLock)) { + if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $queueForDeletes, $executionsRetentionCount, $lock)) { $utopia->getRoute()?->label('router', true); } } @@ -1581,14 +1578,14 @@ Http::get('/humans.txt') ->inject('authorization') ->inject('queueForDeletes') ->inject('executionsRetentionCount') - ->inject('distributedLock') - ->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, ?Key $apiKey, Authorization $authorization, DeleteEvent $queueForDeletes, int $executionsRetentionCount, callable $distributedLock) { + ->inject('lock') + ->action(function (Http $utopia, SwooleRequest $swooleRequest, Request $request, Response $response, Log $log, Database $dbForPlatform, callable $getProjectDB, Event $queueForEvents, Bus $bus, Executor $executor, Reader $geodb, callable $isResourceBlocked, array $platform, string $previewHostname, ?Key $apiKey, Authorization $authorization, DeleteEvent $queueForDeletes, int $executionsRetentionCount, Lock $lock) { $platformHostnames = $platform['hostnames'] ?? []; if (in_array($request->getHostname(), $platformHostnames) || !empty($previewHostname)) { $template = new View(__DIR__ . '/../views/general/humans.phtml'); $response->text($template->render(false)); } else { - if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $queueForDeletes, $executionsRetentionCount, $distributedLock)) { + if (router($utopia, $dbForPlatform, $getProjectDB, $swooleRequest, $request, $response, $log, $queueForEvents, $bus, $executor, $geodb, $isResourceBlocked, $platform, $previewHostname, $authorization, $apiKey, $queueForDeletes, $executionsRetentionCount, $lock)) { $utopia->getRoute()?->label('router', true); } } diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 0ce1b6ca33..3c679ce6ec 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -20,6 +20,7 @@ use Appwrite\Event\Webhook; use Appwrite\Extend\Exception; use Appwrite\Extend\Exception as AppwriteException; use Appwrite\Functions\EventProcessor; +use Appwrite\Locking\Lock; use Appwrite\SDK\Method; use Appwrite\Usage\Context; use Appwrite\Utopia\Database\Documents\User; @@ -100,8 +101,8 @@ Http::init() ->inject('team') ->inject('apiKey') ->inject('authorization') - ->inject('distributedLock') - ->action(function (Http $utopia, Request $request, Database $dbForPlatform, Database $dbForProject, AuditContext $auditContext, Document $project, User $user, ?Document $session, array $servers, string $mode, Document $team, ?Key $apiKey, Authorization $authorization, callable $distributedLock) { + ->inject('lock') + ->action(function (Http $utopia, Request $request, Database $dbForPlatform, Database $dbForProject, AuditContext $auditContext, Document $project, User $user, ?Document $session, array $servers, string $mode, Document $team, ?Key $apiKey, Authorization $authorization, Lock $lock) { $route = $utopia->getRoute(); if ($route === null) { throw new AppwriteException(AppwriteException::GENERAL_ROUTE_NOT_FOUND); @@ -250,7 +251,7 @@ Http::init() } if (! $updates->isEmpty()) { - $distributedLock('lock:platform:keys:' . $dbKey->getId(), function () use ($dbForPlatform, $dbKey, $updates, $apiKey, $project, $user, $team) { + $lock->run('keys', $dbKey->getId(), function () use ($dbForPlatform, $dbKey, $updates, $apiKey, $project, $user, $team) { $dbForPlatform->getAuthorization()->skip(fn () => $dbForPlatform->updateDocument('keys', $dbKey->getId(), $updates)); if (! empty($apiKey->getProjectId())) { @@ -391,11 +392,7 @@ Http::init() if ($project->getId() !== 'console') { $accessedAt = $project->getAttribute('accessedAt', 0); if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_PROJECT_ACCESS)) > $accessedAt) { - $distributedLock('lock:platform:projects:' . $project->getId(), function () use ($authorization, $dbForPlatform, $project) { - $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ - 'accessedAt' => DateTime::now() - ]))); - }); + $lock->set('projects', $project->getId()); } } @@ -412,11 +409,7 @@ Http::init() 'accessedAt' => $user->getAttribute('accessedAt') ])); } else { - $distributedLock('lock:platform:users:' . $user->getId(), function () use ($authorization, $dbForPlatform, $user) { - $authorization->skip(fn () => $dbForPlatform->updateDocument('users', $user->getId(), new Document([ - 'accessedAt' => $user->getAttribute('accessedAt') - ]))); - }); + $lock->set('users', $user->getId(), 'accessedAt', $user->getAttribute('accessedAt')); } } } diff --git a/app/init/resources/request.php b/app/init/resources/request.php index 71af68b4b2..c5fafbef47 100644 --- a/app/init/resources/request.php +++ b/app/init/resources/request.php @@ -17,6 +17,7 @@ use Appwrite\Event\Webhook; use Appwrite\Extend\Exception; use Appwrite\Functions\EventProcessor; use Appwrite\GraphQL\Schema; +use Appwrite\Locking\Lock; use Appwrite\Network\Cors; use Appwrite\Network\Platform; use Appwrite\Network\Validator\Origin; @@ -216,6 +217,15 @@ return function (Container $container): void { }; }, ['redis', 'telemetry', 'log', 'logger']); + $container->set('lock', function (callable $distributedLock, callable $distributedLockOrFail, Database $dbForPlatform, Authorization $authorization): Lock { + return new Lock( + \Closure::fromCallable($distributedLock), + \Closure::fromCallable($distributedLockOrFail), + $dbForPlatform, + $authorization, + ); + }, ['distributedLock', 'distributedLockOrFail', 'dbForPlatform', 'authorization']); + $container->set('authorization', function () { return new Authorization(); }, []); diff --git a/src/Appwrite/Locking/Lock.php b/src/Appwrite/Locking/Lock.php new file mode 100644 index 0000000000..ec85fe0fa6 --- /dev/null +++ b/src/Appwrite/Locking/Lock.php @@ -0,0 +1,63 @@ +skipLock)(self::key($collection, $id), function () use ($collection, $id, $attribute, $value) { + $this->authorization->skip(fn () => $this->dbForPlatform->updateDocument( + $collection, + $id, + new Document([$attribute => $value ?? DateTime::now()]) + )); + }); + } + + /** + * Skip-on-contention lock around an arbitrary callback. For idempotent + * writes that don't fit the set shape (e.g., updates with cache purge). + */ + public function run(string $collection, string $id, Closure $fn): void + { + ($this->skipLock)(self::key($collection, $id), $fn); + } + + /** + * Block-then-409 lock around an arbitrary callback. For read-modify-write + * endpoints where silently dropping a concurrent request would lose data. + */ + public function runOrFail(string $collection, string $id, Closure $fn): mixed + { + return ($this->failLock)(self::key($collection, $id), $fn); + } + + private static function key(string $collection, string $id): string + { + return "lock:platform:{$collection}:{$id}"; + } +} From e63414561206ea7b2420737ef4fe79455f24701f Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Wed, 29 Apr 2026 07:41:54 +0100 Subject: [PATCH 12/26] refactor: consolidate lock implementation into Lock class MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Lock now uses Utopia\Lock\Distributed directly and owns the full acquire/release/telemetry/error-reporting/fail-open/kill-switch logic that previously lived in two inline DI factory closures. Adds withKey($key, $fn, $ttl, $orFail, $waitTimeout) as a generic escape hatch for non-platform key shapes (cache, queue, edge) and unusual TTL/timeout requirements. Per-attribute lock keys for set() so that an accessedAt bump and a mcpAccessedAt bump on the same projects:{id} document don't compete. Whole-document operations (run, runOrFail) keep document-level keys. Removes the standalone distributedLock and distributedLockOrFail DI factories — Lock is the single API. request.php shrinks ~150 LOC; Lock.php grows to ~190 LOC. --- app/init/resources/request.php | 153 +---------------------------- src/Appwrite/Locking/Lock.php | 169 ++++++++++++++++++++++++++++----- 2 files changed, 150 insertions(+), 172 deletions(-) diff --git a/app/init/resources/request.php b/app/init/resources/request.php index c5fafbef47..d927c1dff8 100644 --- a/app/init/resources/request.php +++ b/app/init/resources/request.php @@ -38,7 +38,6 @@ use Utopia\Auth\Proofs\Token; use Utopia\Auth\Store; use Utopia\Cache\Cache; use Utopia\Config\Config; -use Utopia\Console; use Utopia\Database\Adapter\Pool as DatabasePool; use Utopia\Database\Database; use Utopia\Database\DateTime as DatabaseDateTime; @@ -50,7 +49,6 @@ use Utopia\Domains\Domain; use Utopia\DSN\DSN; use Utopia\Http\Http; use Utopia\Locale\Locale; -use Utopia\Lock\Distributed as DistributedLock; use Utopia\Logger\Log; use Utopia\Logger\Logger; use Utopia\Pools\Group; @@ -77,154 +75,9 @@ return function (Container $container): void { return $register->get('logger'); }, ['register']); - // Rate-limited to one push per 60s per (action, target) so a sustained - // backend outage doesn't flood Sentry across the pod fleet. - $lockErrorReporter = function (Log $log, ?Logger $logger, string $action, string $key, string $target, Throwable $e): void { - static $lastReportAt = []; - - Console::warning("Lock {$action} for {$key}: {$e->getMessage()}"); - - if ($logger === null) { - return; - } - - $bucket = $action . ':' . $target; - $now = time(); - if (($lastReportAt[$bucket] ?? 0) + 60 > $now) { - return; - } - $lastReportAt[$bucket] = $now; - - $log->setNamespace('http'); - $log->setServer(System::getEnv('_APP_LOGGING_SERVICE_IDENTIFIER', \gethostname())); - $log->setVersion(APP_VERSION_STABLE); - $log->setType(Log::TYPE_WARNING); - $log->setMessage('Distributed lock ' . $action . ': ' . $e->getMessage()); - $log->setAction("lock.{$action}"); - $log->setEnvironment(System::getEnv('_APP_ENV', 'development') === 'production' - ? Log::ENVIRONMENT_PRODUCTION - : Log::ENVIRONMENT_STAGING); - - $log->addTag('lock.target', $target); - // Strip trailing document ID to keep aggregator cardinality bounded. - $log->addTag('lock.key_pattern', preg_replace('/:[^:]+$/', ':*', $key)); - $log->addTag('code', $e->getCode()); - - $log->addExtra('file', $e->getFile()); - $log->addExtra('line', $e->getLine()); - $log->addExtra('trace', $e->getTraceAsString()); - - try { - $logger->addLog($log); - } catch (Throwable) { - } - }; - - $lockTargetOf = function (string $key): string { - $parts = explode(':', $key, 4); - return $parts[2] ?? 'unknown'; - }; - - /** - * Skip-on-contention. For idempotent writes where losing the race is correct - * (e.g., the winning pod's update covers ours). Fail-open on backend error. - */ - $container->set('distributedLock', function (\Redis $redis, Telemetry $telemetry, Log $log, ?Logger $logger) use ($lockTargetOf, $lockErrorReporter) { - $enabled = System::getEnv('_APP_LOCKING_ENABLED', 'enabled') !== 'disabled'; - $attempts = $telemetry->createCounter('lock.attempts', null, 'Distributed lock acquire outcomes'); - - if (! $enabled) { - return function (string $key, \Closure $fn, float $ttl = 5.0): void { - $fn(); - }; - } - - return function (string $key, \Closure $fn, float $ttl = 5.0) use ($redis, $attempts, $log, $logger, $lockTargetOf, $lockErrorReporter): void { - $target = $lockTargetOf($key); - $lock = new DistributedLock($redis, $key, (int) $ttl); - - try { - $acquired = $lock->tryAcquire(); - } catch (\RedisException $e) { - $attempts->add(1, ['outcome' => 'backend_error', 'target' => $target]); - $lockErrorReporter($log, $logger, 'backend_error', $key, $target, $e); - $fn(); - return; - } - - if (! $acquired) { - $attempts->add(1, ['outcome' => 'skipped', 'target' => $target]); - return; - } - - $attempts->add(1, ['outcome' => 'acquired', 'target' => $target]); - try { - $fn(); - } finally { - try { - $lock->release(); - } catch (\Throwable $e) { - $attempts->add(1, ['outcome' => 'release_error', 'target' => $target]); - $lockErrorReporter($log, $logger, 'release_error', $key, $target, $e); - } - } - }; - }, ['redis', 'telemetry', 'log', 'logger']); - - /** - * Block-then-409 on contention. For read-modify-write on shared mutable state - * where silently dropping a request is wrong. Fail-open on backend error. - */ - $container->set('distributedLockOrFail', function (\Redis $redis, Telemetry $telemetry, Log $log, ?Logger $logger) use ($lockTargetOf, $lockErrorReporter) { - $enabled = System::getEnv('_APP_LOCKING_ENABLED', 'enabled') !== 'disabled'; - $attempts = $telemetry->createCounter('lock.attempts', null, 'Distributed lock acquire outcomes'); - - if (! $enabled) { - return function (string $key, \Closure $fn, float $ttl = 10.0, float $waitTimeout = 3.0): mixed { - return $fn(); - }; - } - - return function (string $key, \Closure $fn, float $ttl = 10.0, float $waitTimeout = 3.0) use ($redis, $attempts, $log, $logger, $lockTargetOf, $lockErrorReporter): mixed { - $target = $lockTargetOf($key); - $lock = new DistributedLock($redis, $key, (int) $ttl); - - try { - $acquired = $lock->acquire($waitTimeout); - } catch (\RedisException $e) { - $attempts->add(1, ['outcome' => 'backend_error', 'target' => $target]); - $lockErrorReporter($log, $logger, 'backend_error', $key, $target, $e); - return $fn(); - } - - if (! $acquired) { - $attempts->add(1, ['outcome' => 'contended', 'target' => $target]); - // No custom message — the lock key embeds collection + document id. - throw new Exception(Exception::GENERAL_RESOURCE_LOCKED); - } - - $attempts->add(1, ['outcome' => 'acquired', 'target' => $target]); - try { - return $fn(); - } finally { - try { - $lock->release(); - } catch (\Throwable $e) { - $attempts->add(1, ['outcome' => 'release_error', 'target' => $target]); - $lockErrorReporter($log, $logger, 'release_error', $key, $target, $e); - } - } - }; - }, ['redis', 'telemetry', 'log', 'logger']); - - $container->set('lock', function (callable $distributedLock, callable $distributedLockOrFail, Database $dbForPlatform, Authorization $authorization): Lock { - return new Lock( - \Closure::fromCallable($distributedLock), - \Closure::fromCallable($distributedLockOrFail), - $dbForPlatform, - $authorization, - ); - }, ['distributedLock', 'distributedLockOrFail', 'dbForPlatform', 'authorization']); + $container->set('lock', function (\Redis $redis, Telemetry $telemetry, Database $dbForPlatform, Authorization $authorization, Log $log, ?Logger $logger): Lock { + return new Lock($redis, $telemetry, $dbForPlatform, $authorization, $log, $logger); + }, ['redis', 'telemetry', 'dbForPlatform', 'authorization', 'log', 'logger']); $container->set('authorization', function () { return new Authorization(); diff --git a/src/Appwrite/Locking/Lock.php b/src/Appwrite/Locking/Lock.php index ec85fe0fa6..288b50c6ea 100644 --- a/src/Appwrite/Locking/Lock.php +++ b/src/Appwrite/Locking/Lock.php @@ -2,26 +2,46 @@ namespace Appwrite\Locking; +use Appwrite\Extend\Exception; use Closure; +use Throwable; +use Utopia\Console; use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; +use Utopia\Lock\Distributed as DistributedLock; +use Utopia\Logger\Log; +use Utopia\Logger\Logger; +use Utopia\System\System; +use Utopia\Telemetry\Adapter as Telemetry; final class Lock { + private readonly bool $enabled; + + private readonly mixed $attempts; + + /** @var array */ + private static array $lastReportAt = []; + public function __construct( - private readonly Closure $skipLock, - private readonly Closure $failLock, + private readonly \Redis $redis, + Telemetry $telemetry, private readonly Database $dbForPlatform, private readonly Authorization $authorization, - ) {} + private readonly Log $log, + private readonly ?Logger $logger, + ) { + $this->enabled = System::getEnv('_APP_LOCKING_ENABLED', 'enabled') !== 'disabled'; + $this->attempts = $telemetry->createCounter('lock.attempts', null, 'Distributed lock acquire outcomes'); + } /** - * Throttled single-attribute write under a skip-on-contention lock with - * authorization bypass. For idempotent timestamp-style updates (accessedAt, - * mcpAccessedAt) where regional pods writing the same value would thrash - * the platform DB. + * Throttled single-attribute write under a per-attribute skip-on-contention + * lock with authorization bypass. For idempotent timestamp-style updates + * (accessedAt, mcpAccessedAt) where regional pods writing the same value + * would thrash the platform DB. */ public function set( string $collection, @@ -29,35 +49,140 @@ final class Lock string $attribute = 'accessedAt', ?string $value = null, ): void { - ($this->skipLock)(self::key($collection, $id), function () use ($collection, $id, $attribute, $value) { - $this->authorization->skip(fn () => $this->dbForPlatform->updateDocument( - $collection, - $id, - new Document([$attribute => $value ?? DateTime::now()]) - )); - }); + $this->withKey( + "lock:platform:{$collection}:{$id}:{$attribute}", + function () use ($collection, $id, $attribute, $value) { + $this->authorization->skip(fn () => $this->dbForPlatform->updateDocument( + $collection, + $id, + new Document([$attribute => $value ?? DateTime::now()]) + )); + } + ); } /** - * Skip-on-contention lock around an arbitrary callback. For idempotent - * writes that don't fit the set shape (e.g., updates with cache purge). + * Skip-on-contention lock around an arbitrary callback for a platform + * document. For idempotent multi-statement writes that don't fit `set`. */ public function run(string $collection, string $id, Closure $fn): void { - ($this->skipLock)(self::key($collection, $id), $fn); + $this->withKey("lock:platform:{$collection}:{$id}", $fn); } /** - * Block-then-409 lock around an arbitrary callback. For read-modify-write - * endpoints where silently dropping a concurrent request would lose data. + * Block-then-409 lock around an arbitrary callback for a platform document. + * For read-modify-write endpoints where silently dropping a concurrent + * request would lose user data. */ public function runOrFail(string $collection, string $id, Closure $fn): mixed { - return ($this->failLock)(self::key($collection, $id), $fn); + return $this->withKey( + "lock:platform:{$collection}:{$id}", + $fn, + ttl: 10, + orFail: true, + ); } - private static function key(string $collection, string $id): string + /** + * Generic lock primitive with full control over key, TTL, contention + * behavior, and wait timeout. Escape hatch for non-platform keys + * (cache, queue, edge) and for unusual TTL/timeout requirements. + */ + public function withKey( + string $key, + Closure $fn, + int $ttl = 5, + bool $orFail = false, + float $waitTimeout = 3.0, + ): mixed { + if (! $this->enabled) { + return $fn(); + } + + $target = self::targetOf($key); + $lock = new DistributedLock($this->redis, $key, $ttl); + + try { + $acquired = $orFail ? $lock->acquire($waitTimeout) : $lock->tryAcquire(); + } catch (\RedisException $e) { + $this->attempts->add(1, ['outcome' => 'backend_error', 'target' => $target]); + $this->reportError('backend_error', $key, $target, $e); + + return $fn(); + } + + if (! $acquired) { + if ($orFail) { + $this->attempts->add(1, ['outcome' => 'contended', 'target' => $target]); + // No custom message — the lock key embeds collection + document id. + throw new Exception(Exception::GENERAL_RESOURCE_LOCKED); + } + $this->attempts->add(1, ['outcome' => 'skipped', 'target' => $target]); + + return null; + } + + $this->attempts->add(1, ['outcome' => 'acquired', 'target' => $target]); + try { + return $fn(); + } finally { + try { + $lock->release(); + } catch (Throwable $e) { + $this->attempts->add(1, ['outcome' => 'release_error', 'target' => $target]); + $this->reportError('release_error', $key, $target, $e); + } + } + } + + private static function targetOf(string $key): string { - return "lock:platform:{$collection}:{$id}"; + $parts = explode(':', $key, 4); + + return $parts[2] ?? 'unknown'; + } + + /** + * Rate-limited to one push per 60s per (action, target) so a sustained + * backend outage doesn't flood Sentry across the pod fleet. + */ + private function reportError(string $action, string $key, string $target, Throwable $e): void + { + Console::warning("Lock {$action} for {$key}: {$e->getMessage()}"); + + if ($this->logger === null) { + return; + } + + $bucket = $action.':'.$target; + $now = time(); + if ((self::$lastReportAt[$bucket] ?? 0) + 60 > $now) { + return; + } + self::$lastReportAt[$bucket] = $now; + + $this->log->setNamespace('http'); + $this->log->setServer(System::getEnv('_APP_LOGGING_SERVICE_IDENTIFIER', \gethostname())); + $this->log->setVersion(APP_VERSION_STABLE); + $this->log->setType(Log::TYPE_WARNING); + $this->log->setMessage('Distributed lock '.$action.': '.$e->getMessage()); + $this->log->setAction("lock.{$action}"); + $this->log->setEnvironment(System::getEnv('_APP_ENV', 'development') === 'production' + ? Log::ENVIRONMENT_PRODUCTION + : Log::ENVIRONMENT_STAGING); + $this->log->addTag('lock.target', $target); + // Strip trailing document ID to keep aggregator cardinality bounded. + $this->log->addTag('lock.key_pattern', preg_replace('/:[^:]+$/', ':*', $key)); + $this->log->addTag('code', $e->getCode()); + $this->log->addExtra('file', $e->getFile()); + $this->log->addExtra('line', $e->getLine()); + $this->log->addExtra('trace', $e->getTraceAsString()); + + try { + $this->logger->addLog($this->log); + } catch (Throwable) { + } } } From 18a67e00d3428b51473a5b1be75bc75bad95ca4e Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Wed, 29 Apr 2026 07:55:38 +0100 Subject: [PATCH 13/26] fix: refresh composer.lock content-hash --- composer.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.lock b/composer.lock index df129b87a5..84bd5e4f13 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "bbf2208dac35ff90f57b7e49b8546d45", + "content-hash": "2063a411993b66c5235a87f6cc682ba7", "packages": [ { "name": "adhocore/jwt", From b2b9ac5b4d6046240eba55c153b4535db1cba7de Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Wed, 29 Apr 2026 10:16:17 +0100 Subject: [PATCH 14/26] fix: redis resource reads _APP_CONNECTIONS_CACHE with _APP_REDIS_* fallback The dedicated \Redis DI resource (used by timelimit and the new Lock class) was reading _APP_REDIS_HOST/PORT/PASS exclusively. Cloud deployments configure cache via _APP_CONNECTIONS_CACHE URI form (e.g. cache=redis://dragonfly:6379) and don't pass the legacy _APP_REDIS_* vars to the appwrite container locally, so timelimit and Lock both fail to connect outside production where Helm separately injects the legacy vars. Now prefers _APP_CONNECTIONS_CACHE when set (matching the cache pool backend), falls back to _APP_REDIS_* for CE-style configs. No new env vars introduced; both timelimit and Lock work in CE, cloud-local, and cloud-production without compose changes. --- app/init/resources.php | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/app/init/resources.php b/app/init/resources.php index 29506bfc9c..aa82c19b75 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -193,12 +193,25 @@ $container->set('cache', function (Group $pools, Telemetry $telemetry) { }, ['pools', 'telemetry']); $container->set('redis', function () { - $host = System::getEnv('_APP_REDIS_HOST', 'localhost'); - $port = System::getEnv('_APP_REDIS_PORT', 6379); - $pass = System::getEnv('_APP_REDIS_PASS', ''); + // Prefer _APP_CONNECTIONS_CACHE (URI form, used by cloud and the pool layer) + // so that direct \Redis consumers (timelimit, Lock) match the same backend + // as the cache pool. Fall back to _APP_REDIS_* for CE-style configs. + $cacheDsn = System::getEnv('_APP_CONNECTIONS_CACHE', ''); + if ($cacheDsn !== '') { + $first = explode(';', $cacheDsn)[0]; + $uri = explode('=', $first, 2)[1] ?? $first; + $dsn = new DSN($uri); + $host = $dsn->getHost(); + $port = (int) ($dsn->getPort() ?: 6379); + $pass = $dsn->getPassword() ?? ''; + } else { + $host = System::getEnv('_APP_REDIS_HOST', 'localhost'); + $port = (int) System::getEnv('_APP_REDIS_PORT', 6379); + $pass = System::getEnv('_APP_REDIS_PASS', ''); + } $redis = new \Redis(); - @$redis->pconnect($host, (int) $port); + @$redis->pconnect($host, $port); if ($pass) { $redis->auth($pass); } From c2a249c48b7c3f1995a91395eaa2139ad47bdb54 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Wed, 29 Apr 2026 11:26:18 +0100 Subject: [PATCH 15/26] feat(lock): include project internal id in lock key + telemetry MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Per-manager request, lock keys are now prefixed with the project's internal id (sequence) so that: - Locks are partitioned by project — Redis cluster slot affinity if/when sharded. - Cross-project requests can't compete on the same key for collection-scoped resources. - Telemetry (counter + Sentry tags) carries 'project' alongside 'target', so dashboards can filter contention by project. Key shapes: set: lock:platform:{project}:{collection}:{id}:{attribute} run/orFail: lock:platform:{project}:{collection}:{id} withKey: raw (caller-provided) Lock now requires a project document at construction. All existing call sites (4 in CE + 2 in cloud) run inside Http::init()-resolved request scope where the project document is set, so no migration needed. Workers/CLI without project context can use withKey directly. --- app/init/resources/request.php | 6 +-- src/Appwrite/Locking/Lock.php | 71 +++++++++++++++++++++++----------- 2 files changed, 51 insertions(+), 26 deletions(-) diff --git a/app/init/resources/request.php b/app/init/resources/request.php index d927c1dff8..03d860715e 100644 --- a/app/init/resources/request.php +++ b/app/init/resources/request.php @@ -75,9 +75,9 @@ return function (Container $container): void { return $register->get('logger'); }, ['register']); - $container->set('lock', function (\Redis $redis, Telemetry $telemetry, Database $dbForPlatform, Authorization $authorization, Log $log, ?Logger $logger): Lock { - return new Lock($redis, $telemetry, $dbForPlatform, $authorization, $log, $logger); - }, ['redis', 'telemetry', 'dbForPlatform', 'authorization', 'log', 'logger']); + $container->set('lock', function (\Redis $redis, Telemetry $telemetry, Database $dbForPlatform, Authorization $authorization, Log $log, ?Logger $logger, Document $project): Lock { + return new Lock($redis, $telemetry, $dbForPlatform, $authorization, $log, $logger, $project); + }, ['redis', 'telemetry', 'dbForPlatform', 'authorization', 'log', 'logger', 'project']); $container->set('authorization', function () { return new Authorization(); diff --git a/src/Appwrite/Locking/Lock.php b/src/Appwrite/Locking/Lock.php index 288b50c6ea..77b278860b 100644 --- a/src/Appwrite/Locking/Lock.php +++ b/src/Appwrite/Locking/Lock.php @@ -22,6 +22,8 @@ final class Lock private readonly mixed $attempts; + private readonly string $projectInternalId; + /** @var array */ private static array $lastReportAt = []; @@ -32,9 +34,12 @@ final class Lock private readonly Authorization $authorization, private readonly Log $log, private readonly ?Logger $logger, + Document $project, ) { $this->enabled = System::getEnv('_APP_LOCKING_ENABLED', 'enabled') !== 'disabled'; $this->attempts = $telemetry->createCounter('lock.attempts', null, 'Distributed lock acquire outcomes'); + $sequence = $project->getSequence(); + $this->projectInternalId = ($sequence !== null && $sequence !== '') ? (string) $sequence : 'unknown'; } /** @@ -49,16 +54,14 @@ final class Lock string $attribute = 'accessedAt', ?string $value = null, ): void { - $this->withKey( - "lock:platform:{$collection}:{$id}:{$attribute}", - function () use ($collection, $id, $attribute, $value) { - $this->authorization->skip(fn () => $this->dbForPlatform->updateDocument( - $collection, - $id, - new Document([$attribute => $value ?? DateTime::now()]) - )); - } - ); + $key = "lock:platform:{$this->projectInternalId}:{$collection}:{$id}:{$attribute}"; + $this->execute($key, $collection, function () use ($collection, $id, $attribute, $value) { + $this->authorization->skip(fn () => $this->dbForPlatform->updateDocument( + $collection, + $id, + new Document([$attribute => $value ?? DateTime::now()]) + )); + }); } /** @@ -67,7 +70,8 @@ final class Lock */ public function run(string $collection, string $id, Closure $fn): void { - $this->withKey("lock:platform:{$collection}:{$id}", $fn); + $key = "lock:platform:{$this->projectInternalId}:{$collection}:{$id}"; + $this->execute($key, $collection, $fn); } /** @@ -77,18 +81,19 @@ final class Lock */ public function runOrFail(string $collection, string $id, Closure $fn): mixed { - return $this->withKey( - "lock:platform:{$collection}:{$id}", - $fn, - ttl: 10, - orFail: true, - ); + $key = "lock:platform:{$this->projectInternalId}:{$collection}:{$id}"; + + return $this->execute($key, $collection, $fn, ttl: 10, orFail: true); } /** * Generic lock primitive with full control over key, TTL, contention * behavior, and wait timeout. Escape hatch for non-platform keys * (cache, queue, edge) and for unusual TTL/timeout requirements. + * + * Caller may pass `target` for telemetry; otherwise it's extracted by + * position from the key (best-effort for keys following the standard + * `lock::<...>::<...>` shape). */ public function withKey( string $key, @@ -96,18 +101,37 @@ final class Lock int $ttl = 5, bool $orFail = false, float $waitTimeout = 3.0, + ?string $target = null, + ): mixed { + return $this->execute( + $key, + $target ?? self::targetOf($key), + $fn, + ttl: $ttl, + orFail: $orFail, + waitTimeout: $waitTimeout, + ); + } + + private function execute( + string $key, + string $target, + Closure $fn, + int $ttl = 5, + bool $orFail = false, + float $waitTimeout = 3.0, ): mixed { if (! $this->enabled) { return $fn(); } - $target = self::targetOf($key); $lock = new DistributedLock($this->redis, $key, $ttl); + $labels = ['target' => $target, 'project' => $this->projectInternalId]; try { $acquired = $orFail ? $lock->acquire($waitTimeout) : $lock->tryAcquire(); } catch (\RedisException $e) { - $this->attempts->add(1, ['outcome' => 'backend_error', 'target' => $target]); + $this->attempts->add(1, ['outcome' => 'backend_error', ...$labels]); $this->reportError('backend_error', $key, $target, $e); return $fn(); @@ -115,23 +139,23 @@ final class Lock if (! $acquired) { if ($orFail) { - $this->attempts->add(1, ['outcome' => 'contended', 'target' => $target]); + $this->attempts->add(1, ['outcome' => 'contended', ...$labels]); // No custom message — the lock key embeds collection + document id. throw new Exception(Exception::GENERAL_RESOURCE_LOCKED); } - $this->attempts->add(1, ['outcome' => 'skipped', 'target' => $target]); + $this->attempts->add(1, ['outcome' => 'skipped', ...$labels]); return null; } - $this->attempts->add(1, ['outcome' => 'acquired', 'target' => $target]); + $this->attempts->add(1, ['outcome' => 'acquired', ...$labels]); try { return $fn(); } finally { try { $lock->release(); } catch (Throwable $e) { - $this->attempts->add(1, ['outcome' => 'release_error', 'target' => $target]); + $this->attempts->add(1, ['outcome' => 'release_error', ...$labels]); $this->reportError('release_error', $key, $target, $e); } } @@ -173,6 +197,7 @@ final class Lock ? Log::ENVIRONMENT_PRODUCTION : Log::ENVIRONMENT_STAGING); $this->log->addTag('lock.target', $target); + $this->log->addTag('lock.project', $this->projectInternalId); // Strip trailing document ID to keep aggregator cardinality bounded. $this->log->addTag('lock.key_pattern', preg_replace('/:[^:]+$/', ':*', $key)); $this->log->addTag('code', $e->getCode()); From 2f2a124a06cba627757158bb5367433aeffa957d Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Wed, 29 Apr 2026 16:39:36 +0100 Subject: [PATCH 16/26] revert: redis resource cluster support + _APP_CONNECTIONS_CACHE fallback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cloud production runs four separate single-master+replica Dragonfly deployments (cache, queue-dragonfly, queue-usage, pubsub-dragonfly), not sharded Redis Cluster topology — confirmed by deploy/cloud/values + environments/production/*.values.yaml (Dragonfly Operator with replicas=2 = 1 primary + 1 read replica), and by the dev DSN scheme 'redis://' (not 'redis-cluster://'). So a standard \Redis client suffices for the direct redis resource (timelimit, Lock). Cloud just needs to pass _APP_REDIS_HOST/PORT/USER/ PASS through to the appwrite container — handled in the cloud PR's docker-compose.yml change. This reverts the resource to its original pre-PR shape. The utopia-php/lock cluster-support PR (utopia-php/lock#1) stays open at upstream as a future-ready option if cloud ever moves to actual Redis Cluster mode. --- app/controllers/general.php | 2 +- app/controllers/shared/api.php | 2 +- app/init/resources.php | 21 ++++----------------- src/Appwrite/Locking/Lock.php | 7 +++---- 4 files changed, 9 insertions(+), 23 deletions(-) diff --git a/app/controllers/general.php b/app/controllers/general.php index 9381843b64..9eabe56eac 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -137,7 +137,7 @@ function router(Http $utopia, Database $dbForPlatform, callable $getProjectDB, S if (!$project->isEmpty() && $project->getId() !== 'console') { $accessedAt = $project->getAttribute('accessedAt', 0); if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_PROJECT_ACCESS)) > $accessedAt) { - $lock->set('projects', $project->getId()); + $lock->set('projects', $project->getId(), 'accessedAt', DateTime::now()); } /** diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 3c679ce6ec..f88dc5eb4b 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -392,7 +392,7 @@ Http::init() if ($project->getId() !== 'console') { $accessedAt = $project->getAttribute('accessedAt', 0); if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_PROJECT_ACCESS)) > $accessedAt) { - $lock->set('projects', $project->getId()); + $lock->set('projects', $project->getId(), 'accessedAt', DateTime::now()); } } diff --git a/app/init/resources.php b/app/init/resources.php index aa82c19b75..29506bfc9c 100644 --- a/app/init/resources.php +++ b/app/init/resources.php @@ -193,25 +193,12 @@ $container->set('cache', function (Group $pools, Telemetry $telemetry) { }, ['pools', 'telemetry']); $container->set('redis', function () { - // Prefer _APP_CONNECTIONS_CACHE (URI form, used by cloud and the pool layer) - // so that direct \Redis consumers (timelimit, Lock) match the same backend - // as the cache pool. Fall back to _APP_REDIS_* for CE-style configs. - $cacheDsn = System::getEnv('_APP_CONNECTIONS_CACHE', ''); - if ($cacheDsn !== '') { - $first = explode(';', $cacheDsn)[0]; - $uri = explode('=', $first, 2)[1] ?? $first; - $dsn = new DSN($uri); - $host = $dsn->getHost(); - $port = (int) ($dsn->getPort() ?: 6379); - $pass = $dsn->getPassword() ?? ''; - } else { - $host = System::getEnv('_APP_REDIS_HOST', 'localhost'); - $port = (int) System::getEnv('_APP_REDIS_PORT', 6379); - $pass = System::getEnv('_APP_REDIS_PASS', ''); - } + $host = System::getEnv('_APP_REDIS_HOST', 'localhost'); + $port = System::getEnv('_APP_REDIS_PORT', 6379); + $pass = System::getEnv('_APP_REDIS_PASS', ''); $redis = new \Redis(); - @$redis->pconnect($host, $port); + @$redis->pconnect($host, (int) $port); if ($pass) { $redis->auth($pass); } diff --git a/src/Appwrite/Locking/Lock.php b/src/Appwrite/Locking/Lock.php index 77b278860b..e545254ef3 100644 --- a/src/Appwrite/Locking/Lock.php +++ b/src/Appwrite/Locking/Lock.php @@ -7,7 +7,6 @@ use Closure; use Throwable; use Utopia\Console; use Utopia\Database\Database; -use Utopia\Database\DateTime; use Utopia\Database\Document; use Utopia\Database\Validator\Authorization; use Utopia\Lock\Distributed as DistributedLock; @@ -51,15 +50,15 @@ final class Lock public function set( string $collection, string $id, - string $attribute = 'accessedAt', - ?string $value = null, + string $attribute, + string $value, ): void { $key = "lock:platform:{$this->projectInternalId}:{$collection}:{$id}:{$attribute}"; $this->execute($key, $collection, function () use ($collection, $id, $attribute, $value) { $this->authorization->skip(fn () => $this->dbForPlatform->updateDocument( $collection, $id, - new Document([$attribute => $value ?? DateTime::now()]) + new Document([$attribute => $value]) )); }); } From 7430de293e325f7d0c431816c653fec799fabb92 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Thu, 30 Apr 2026 06:05:50 +0100 Subject: [PATCH 17/26] test: unit tests for Lock facade MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Covers the four public methods (set, run, runOrFail, withKey) plus the kill switch and the project-fallback behavior. Tests against a real Redis (the appwrite container has it always-on); no markTestSkipped fallback — the suite fails loudly if Redis is unreachable rather than silently passing. --- tests/unit/Locking/LockTest.php | 257 ++++++++++++++++++++++++++++++++ 1 file changed, 257 insertions(+) create mode 100644 tests/unit/Locking/LockTest.php diff --git a/tests/unit/Locking/LockTest.php b/tests/unit/Locking/LockTest.php new file mode 100644 index 0000000000..e04745c4f9 --- /dev/null +++ b/tests/unit/Locking/LockTest.php @@ -0,0 +1,257 @@ +redis = new Redis; + $this->redis->connect($host, $port, 1.0); + + $this->project = new Document([ + '$id' => 'test-project', + '$sequence' => self::PROJECT_SEQUENCE, + ]); + $this->authorization = new Authorization; + $this->log = new Log; + + $this->cleanupKeys(); + } + + protected function tearDown(): void + { + if (isset($this->redis) && $this->redis->isConnected()) { + $this->cleanupKeys(); + } + } + + private function cleanupKeys(): void + { + foreach ($this->redis->keys(self::KEY_PREFIX.'*') as $key) { + $this->redis->del($key); + } + // also clean keys produced by withKey() tests + foreach ($this->redis->keys('lock:test:*') as $key) { + $this->redis->del($key); + } + } + + private function makeLock(?Database $db = null, ?Authorization $auth = null): Lock + { + return new Lock( + $this->redis, + new NoTelemetry, + $db ?? $this->createStub(Database::class), + $auth ?? $this->authorization, + $this->log, + null, + $this->project, + ); + } + + public function test_set_uses_per_attribute_key_and_auth_skipped_update(): void + { + $captured = null; + $db = $this->createMock(Database::class); + $db->expects($this->once()) + ->method('updateDocument') + ->with('projects', 'p1', $this->callback(function (Document $doc) use (&$captured) { + $captured = $doc->getArrayCopy(); + + return true; + })) + ->willReturnArgument(2); + + $lock = $this->makeLock($db); + $lock->set('projects', 'p1', 'accessedAt', '2024-06-01 12:00:00'); + + $this->assertSame(['accessedAt' => '2024-06-01 12:00:00'], $captured); + $this->assertSame(0, $this->redis->exists(self::KEY_PREFIX.'projects:p1:accessedAt')); + } + + public function test_set_skips_on_contention(): void + { + $key = self::KEY_PREFIX.'projects:p1:accessedAt'; + $this->redis->set($key, 'other-owner', ['NX', 'EX' => 30]); + + $db = $this->createMock(Database::class); + $db->expects($this->never())->method('updateDocument'); + + $lock = $this->makeLock($db); + $lock->set('projects', 'p1', 'accessedAt', '2024-06-01 12:00:00'); + + $this->assertSame('other-owner', $this->redis->get($key)); + } + + public function test_set_different_attributes_do_not_compete(): void + { + // Hold accessedAt + $heldKey = self::KEY_PREFIX.'projects:p1:accessedAt'; + $this->redis->set($heldKey, 'other-owner', ['NX', 'EX' => 30]); + + // mcpAccessedAt should still be acquirable + $db = $this->createMock(Database::class); + $db->expects($this->once()) + ->method('updateDocument') + ->with('projects', 'p1', $this->isInstanceOf(Document::class)) + ->willReturnArgument(2); + + $lock = $this->makeLock($db); + $lock->set('projects', 'p1', 'mcpAccessedAt', '2024-06-01 12:00:00'); + + $this->assertSame('other-owner', $this->redis->get($heldKey)); + } + + public function test_run_uses_per_document_key_and_invokes_callback(): void + { + $called = false; + $lock = $this->makeLock(); + $lock->run('keys', 'k1', function () use (&$called) { + $called = true; + }); + + $this->assertTrue($called); + $this->assertSame(0, $this->redis->exists(self::KEY_PREFIX.'keys:k1')); + } + + public function test_run_skips_on_contention(): void + { + $key = self::KEY_PREFIX.'keys:k1'; + $this->redis->set($key, 'other-owner', ['NX', 'EX' => 30]); + + $called = false; + $lock = $this->makeLock(); + $lock->run('keys', 'k1', function () use (&$called) { + $called = true; + }); + + $this->assertFalse($called); + $this->assertSame('other-owner', $this->redis->get($key)); + } + + public function test_run_or_fail_throws_on_contention(): void + { + $key = self::KEY_PREFIX.'projects:p1'; + $this->redis->set($key, 'other-owner', ['NX', 'EX' => 30]); + + $lock = $this->makeLock(); + + $this->expectException(Exception::class); + try { + $lock->runOrFail('projects', 'p1', fn () => 'never-runs'); + } catch (Exception $e) { + $this->assertSame(Exception::GENERAL_RESOURCE_LOCKED, $e->getType()); + throw $e; + } + } + + public function test_run_or_fail_returns_callback_value_when_uncontended(): void + { + $lock = $this->makeLock(); + $result = $lock->runOrFail('projects', 'p1', fn () => 'ok'); + + $this->assertSame('ok', $result); + $this->assertSame(0, $this->redis->exists(self::KEY_PREFIX.'projects:p1')); + } + + public function test_with_key_uses_raw_key(): void + { + $custom = 'lock:test:custom-key'; + $called = false; + $lock = $this->makeLock(); + $lock->withKey($custom, function () use (&$called) { + $called = true; + }); + + $this->assertTrue($called); + $this->assertSame(0, $this->redis->exists($custom)); + } + + public function test_with_key_or_fail_flag_throws_on_contention(): void + { + $custom = 'lock:test:contended'; + $this->redis->set($custom, 'other', ['NX', 'EX' => 30]); + + $lock = $this->makeLock(); + $this->expectException(Exception::class); + $lock->withKey($custom, fn () => null, ttl: 5, orFail: true, waitTimeout: 0.1); + } + + public function test_disabled_mode_runs_callback_unlocked(): void + { + $previous = \getenv('_APP_LOCKING_ENABLED'); + \putenv('_APP_LOCKING_ENABLED=disabled'); + try { + // Even when the key is already held, the callback must still run. + $key = self::KEY_PREFIX.'keys:k1'; + $this->redis->set($key, 'other-owner', ['NX', 'EX' => 30]); + + $called = false; + $lock = $this->makeLock(); + $lock->run('keys', 'k1', function () use (&$called) { + $called = true; + }); + + $this->assertTrue($called); + $this->assertSame('other-owner', $this->redis->get($key)); + } finally { + $previous === false ? \putenv('_APP_LOCKING_ENABLED') : \putenv('_APP_LOCKING_ENABLED='.$previous); + } + } + + public function test_project_without_sequence_falls_back_to_unknown(): void + { + $emptyProject = new Document; + $lock = new Lock( + $this->redis, + new NoTelemetry, + $this->createStub(Database::class), + $this->authorization, + $this->log, + null, + $emptyProject, + ); + + // Pre-acquire the lock at the 'unknown' projectInternalId path. + $key = 'lock:platform:unknown:keys:k1'; + $this->redis->set($key, 'held', ['NX', 'EX' => 30]); + + $called = false; + $lock->run('keys', 'k1', function () use (&$called) { + $called = true; + }); + $this->assertFalse($called, 'Lock without project sequence should hash to the unknown bucket'); + + $this->redis->del($key); + } +} From 7b2f6ac692a26d246a89d0bf71dbcd8015411092 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Thu, 30 Apr 2026 07:13:51 +0100 Subject: [PATCH 18/26] fix: add explicit parens to new ClassName calls in LockTest (PSR-12) --- tests/unit/Locking/LockTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/unit/Locking/LockTest.php b/tests/unit/Locking/LockTest.php index e04745c4f9..b789ffe3e0 100644 --- a/tests/unit/Locking/LockTest.php +++ b/tests/unit/Locking/LockTest.php @@ -35,15 +35,15 @@ class LockTest extends TestCase $host = \getenv('_APP_REDIS_HOST') ?: 'redis'; $port = (int) (\getenv('_APP_REDIS_PORT') ?: 6379); - $this->redis = new Redis; + $this->redis = new Redis(); $this->redis->connect($host, $port, 1.0); $this->project = new Document([ '$id' => 'test-project', '$sequence' => self::PROJECT_SEQUENCE, ]); - $this->authorization = new Authorization; - $this->log = new Log; + $this->authorization = new Authorization(); + $this->log = new Log(); $this->cleanupKeys(); } @@ -70,7 +70,7 @@ class LockTest extends TestCase { return new Lock( $this->redis, - new NoTelemetry, + new NoTelemetry(), $db ?? $this->createStub(Database::class), $auth ?? $this->authorization, $this->log, @@ -231,10 +231,10 @@ class LockTest extends TestCase public function test_project_without_sequence_falls_back_to_unknown(): void { - $emptyProject = new Document; + $emptyProject = new Document(); $lock = new Lock( $this->redis, - new NoTelemetry, + new NoTelemetry(), $this->createStub(Database::class), $this->authorization, $this->log, From 2762e849749ba193b6b23b2156e97d492ea66051 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Thu, 30 Apr 2026 07:26:45 +0100 Subject: [PATCH 19/26] fix(lock): broaden acquire catch + clone Log before reportError Two P1s from review: - Acquire path caught only \RedisException; any other Throwable from the lock library would escape and skip the fail-open callback. Widened to \Throwable so any backend failure falls back to running unlocked. - reportError mutated the per-request $log directly, leaving it in a 'lock.{action}' state that the request-end handler would then submit as the request log. Clones $log into a dedicated $lockLog so the shared instance is untouched. --- src/Appwrite/Locking/Lock.php | 36 +++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/Appwrite/Locking/Lock.php b/src/Appwrite/Locking/Lock.php index e545254ef3..38e034b220 100644 --- a/src/Appwrite/Locking/Lock.php +++ b/src/Appwrite/Locking/Lock.php @@ -129,7 +129,7 @@ final class Lock try { $acquired = $orFail ? $lock->acquire($waitTimeout) : $lock->tryAcquire(); - } catch (\RedisException $e) { + } catch (Throwable $e) { $this->attempts->add(1, ['outcome' => 'backend_error', ...$labels]); $this->reportError('backend_error', $key, $target, $e); @@ -186,26 +186,30 @@ final class Lock } self::$lastReportAt[$bucket] = $now; - $this->log->setNamespace('http'); - $this->log->setServer(System::getEnv('_APP_LOGGING_SERVICE_IDENTIFIER', \gethostname())); - $this->log->setVersion(APP_VERSION_STABLE); - $this->log->setType(Log::TYPE_WARNING); - $this->log->setMessage('Distributed lock '.$action.': '.$e->getMessage()); - $this->log->setAction("lock.{$action}"); - $this->log->setEnvironment(System::getEnv('_APP_ENV', 'development') === 'production' + // Clone the per-request Log so our lock-error fields don't bleed into the + // request-end submission of $this->log (action/message/tags get reset on the + // shared instance otherwise). + $lockLog = clone $this->log; + $lockLog->setNamespace('http'); + $lockLog->setServer(System::getEnv('_APP_LOGGING_SERVICE_IDENTIFIER', \gethostname())); + $lockLog->setVersion(APP_VERSION_STABLE); + $lockLog->setType(Log::TYPE_WARNING); + $lockLog->setMessage('Distributed lock '.$action.': '.$e->getMessage()); + $lockLog->setAction("lock.{$action}"); + $lockLog->setEnvironment(System::getEnv('_APP_ENV', 'development') === 'production' ? Log::ENVIRONMENT_PRODUCTION : Log::ENVIRONMENT_STAGING); - $this->log->addTag('lock.target', $target); - $this->log->addTag('lock.project', $this->projectInternalId); + $lockLog->addTag('lock.target', $target); + $lockLog->addTag('lock.project', $this->projectInternalId); // Strip trailing document ID to keep aggregator cardinality bounded. - $this->log->addTag('lock.key_pattern', preg_replace('/:[^:]+$/', ':*', $key)); - $this->log->addTag('code', $e->getCode()); - $this->log->addExtra('file', $e->getFile()); - $this->log->addExtra('line', $e->getLine()); - $this->log->addExtra('trace', $e->getTraceAsString()); + $lockLog->addTag('lock.key_pattern', preg_replace('/:[^:]+$/', ':*', $key)); + $lockLog->addTag('code', $e->getCode()); + $lockLog->addExtra('file', $e->getFile()); + $lockLog->addExtra('line', $e->getLine()); + $lockLog->addExtra('trace', $e->getTraceAsString()); try { - $this->logger->addLog($this->log); + $this->logger->addLog($lockLog); } catch (Throwable) { } } From 03575e68c44a6c6b5a58c9c131f3dff3688d4593 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Thu, 30 Apr 2026 07:31:15 +0100 Subject: [PATCH 20/26] fix(lock): re-read keys inside lock body to avoid sdks append loss The sdks attribute is an append-only list, not idempotent. With the read happening outside the lock, two sequential acquirers could each read the same stale list and overwrite each other's appends. Now the lock body re-reads the keys document and re-derives the sdks array from the fresh state. Skip-on-contention still drops the update when the lock is held, but a same-SDK retry on the next request picks the registration up. Bounded loss only affects the rare 'first-seen' SDK request that happens to land while the lock is held; sequential traffic from the same SDK (or any later request from any SDK) re-attempts and writes. Co-authored-by: greptile-apps[bot] --- app/controllers/shared/api.php | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index a1b68ddcde..2a1beadc04 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -232,27 +232,37 @@ Http::init() $updates = new Document(); $accessedAt = $dbKey->getAttribute('accessedAt', 0); + $accessedAtStale = DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_KEY_ACCESS)) > $accessedAt; - if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_KEY_ACCESS)) > $accessedAt) { + if ($accessedAtStale) { $updates->setAttribute('accessedAt', DateTime::now()); } $sdkValidator = new WhiteList($servers, true); $sdk = $request->getHeader('x-sdk-name', 'UNKNOWN'); + $sdkCandidate = ($sdk !== 'UNKNOWN' && $sdkValidator->isValid($sdk) && ! in_array($sdk, $dbKey->getAttribute('sdks', []))) ? $sdk : null; - if ($sdk !== 'UNKNOWN' && $sdkValidator->isValid($sdk)) { - $sdks = $dbKey->getAttribute('sdks', []); + if (! $updates->isEmpty() || $sdkCandidate !== null) { + $lock->run('keys', $dbKey->getId(), function () use ($dbForPlatform, $dbKey, $updates, $sdkCandidate, $apiKey, $project, $user, $team) { + // Re-read inside the lock so two sequential acquirers don't clobber each other's + // sdks-array appends. Skip semantics still drop concurrent acquirers' updates, + // but a same-SDK retry on the next request will pick the registration up. + if ($sdkCandidate !== null) { + $current = $dbForPlatform->getAuthorization()->skip( + fn () => $dbForPlatform->getDocument('keys', $dbKey->getId()) + ); + $sdks = $current->getAttribute('sdks', []); + if (! in_array($sdkCandidate, $sdks)) { + $sdks[] = $sdkCandidate; + $updates->setAttribute('sdks', $sdks); + $updates->setAttribute('accessedAt', DateTime::now()); + } + } - if (! in_array($sdk, $sdks)) { - $sdks[] = $sdk; + if ($updates->isEmpty()) { + return; + } - $updates->setAttribute('sdks', $sdks); - $updates->setAttribute('accessedAt', Datetime::now()); - } - } - - if (! $updates->isEmpty()) { - $lock->run('keys', $dbKey->getId(), function () use ($dbForPlatform, $dbKey, $updates, $apiKey, $project, $user, $team) { $dbForPlatform->getAuthorization()->skip(fn () => $dbForPlatform->updateDocument('keys', $dbKey->getId(), $updates)); if (! empty($apiKey->getProjectId())) { From bae328efa19126e35d2a8523b5e48a6901d99151 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Thu, 30 Apr 2026 07:42:59 +0100 Subject: [PATCH 21/26] revert: keep narrow RedisException catch on lock acquire MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reverts the catch widening from earlier — the underlying Utopia\Lock\Distributed::tryAcquire only calls ext-redis methods, which throw RedisException exclusively. Catching Throwable was over-broad and would silently swallow real bugs (TypeError, Error) into a fail-open path. --- src/Appwrite/Locking/Lock.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Appwrite/Locking/Lock.php b/src/Appwrite/Locking/Lock.php index 38e034b220..6390a4a4be 100644 --- a/src/Appwrite/Locking/Lock.php +++ b/src/Appwrite/Locking/Lock.php @@ -129,7 +129,7 @@ final class Lock try { $acquired = $orFail ? $lock->acquire($waitTimeout) : $lock->tryAcquire(); - } catch (Throwable $e) { + } catch (\RedisException $e) { $this->attempts->add(1, ['outcome' => 'backend_error', ...$labels]); $this->reportError('backend_error', $key, $target, $e); From 18f3280bad3bf2549c807fe0fa613cb77f956ea6 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Thu, 30 Apr 2026 07:48:04 +0100 Subject: [PATCH 22/26] refactor(lock): extract magic numbers and outcome strings, fix target parse MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - TTL defaults (5s skip / 10s fail), 3s wait timeout, and 60s sentry rate-limit window are now class constants. - Telemetry outcome labels (acquired/skipped/contended/backend_error/ release_error) are class constants — typo-safe across the 9 use sites. - Fix: inferTargetFromKey was indexing into segment 2 with limit 4, which returned the project sequence instead of the collection name ever since project-id was added to the key in c2a249c48b. Telemetry was tagging 'target' with project sequences instead of 'projects' / 'users' / etc. Now indexes segment 3 with limit 5 to correctly pick the collection segment from lock:platform:{project}:{target}:... --- src/Appwrite/Locking/Lock.php | 63 ++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 20 deletions(-) diff --git a/src/Appwrite/Locking/Lock.php b/src/Appwrite/Locking/Lock.php index 6390a4a4be..6227ae3f5e 100644 --- a/src/Appwrite/Locking/Lock.php +++ b/src/Appwrite/Locking/Lock.php @@ -17,6 +17,24 @@ use Utopia\Telemetry\Adapter as Telemetry; final class Lock { + private const SKIP_TTL_SECONDS = 5; + + private const FAIL_TTL_SECONDS = 10; + + private const FAIL_WAIT_SECONDS = 3.0; + + private const REPORT_RATE_LIMIT_SECONDS = 60; + + private const OUTCOME_ACQUIRED = 'acquired'; + + private const OUTCOME_SKIPPED = 'skipped'; + + private const OUTCOME_CONTENDED = 'contended'; + + private const OUTCOME_BACKEND_ERROR = 'backend_error'; + + private const OUTCOME_RELEASE_ERROR = 'release_error'; + private readonly bool $enabled; private readonly mixed $attempts; @@ -82,7 +100,7 @@ final class Lock { $key = "lock:platform:{$this->projectInternalId}:{$collection}:{$id}"; - return $this->execute($key, $collection, $fn, ttl: 10, orFail: true); + return $this->execute($key, $collection, $fn, ttl: self::FAIL_TTL_SECONDS, orFail: true); } /** @@ -92,19 +110,19 @@ final class Lock * * Caller may pass `target` for telemetry; otherwise it's extracted by * position from the key (best-effort for keys following the standard - * `lock::<...>::<...>` shape). + * `lock:platform:{project}:{target}:...` shape). */ public function withKey( string $key, Closure $fn, - int $ttl = 5, + int $ttl = self::SKIP_TTL_SECONDS, bool $orFail = false, - float $waitTimeout = 3.0, + float $waitTimeout = self::FAIL_WAIT_SECONDS, ?string $target = null, ): mixed { return $this->execute( $key, - $target ?? self::targetOf($key), + $target ?? self::inferTargetFromKey($key), $fn, ttl: $ttl, orFail: $orFail, @@ -116,9 +134,9 @@ final class Lock string $key, string $target, Closure $fn, - int $ttl = 5, + int $ttl = self::SKIP_TTL_SECONDS, bool $orFail = false, - float $waitTimeout = 3.0, + float $waitTimeout = self::FAIL_WAIT_SECONDS, ): mixed { if (! $this->enabled) { return $fn(); @@ -130,46 +148,51 @@ final class Lock try { $acquired = $orFail ? $lock->acquire($waitTimeout) : $lock->tryAcquire(); } catch (\RedisException $e) { - $this->attempts->add(1, ['outcome' => 'backend_error', ...$labels]); - $this->reportError('backend_error', $key, $target, $e); + $this->attempts->add(1, ['outcome' => self::OUTCOME_BACKEND_ERROR, ...$labels]); + $this->reportError(self::OUTCOME_BACKEND_ERROR, $key, $target, $e); return $fn(); } if (! $acquired) { if ($orFail) { - $this->attempts->add(1, ['outcome' => 'contended', ...$labels]); + $this->attempts->add(1, ['outcome' => self::OUTCOME_CONTENDED, ...$labels]); // No custom message — the lock key embeds collection + document id. throw new Exception(Exception::GENERAL_RESOURCE_LOCKED); } - $this->attempts->add(1, ['outcome' => 'skipped', ...$labels]); + $this->attempts->add(1, ['outcome' => self::OUTCOME_SKIPPED, ...$labels]); return null; } - $this->attempts->add(1, ['outcome' => 'acquired', ...$labels]); + $this->attempts->add(1, ['outcome' => self::OUTCOME_ACQUIRED, ...$labels]); try { return $fn(); } finally { try { $lock->release(); } catch (Throwable $e) { - $this->attempts->add(1, ['outcome' => 'release_error', ...$labels]); - $this->reportError('release_error', $key, $target, $e); + $this->attempts->add(1, ['outcome' => self::OUTCOME_RELEASE_ERROR, ...$labels]); + $this->reportError(self::OUTCOME_RELEASE_ERROR, $key, $target, $e); } } } - private static function targetOf(string $key): string + /** + * Best-effort target extraction for telemetry. Assumes the standard + * `lock:platform:{project}:{target}:...` shape. For non-platform keys + * passed via withKey(), callers should pass `target` explicitly. + */ + private static function inferTargetFromKey(string $key): string { - $parts = explode(':', $key, 4); + $parts = explode(':', $key, 5); - return $parts[2] ?? 'unknown'; + return $parts[3] ?? 'unknown'; } /** - * Rate-limited to one push per 60s per (action, target) so a sustained - * backend outage doesn't flood Sentry across the pod fleet. + * Rate-limited to one push per REPORT_RATE_LIMIT_SECONDS per (action, target) + * so a sustained backend outage doesn't flood Sentry across the pod fleet. */ private function reportError(string $action, string $key, string $target, Throwable $e): void { @@ -181,7 +204,7 @@ final class Lock $bucket = $action.':'.$target; $now = time(); - if ((self::$lastReportAt[$bucket] ?? 0) + 60 > $now) { + if ((self::$lastReportAt[$bucket] ?? 0) + self::REPORT_RATE_LIMIT_SECONDS > $now) { return; } self::$lastReportAt[$bucket] = $now; From e0ec28f02a802862e1b68c3b9cf07097cfa436c3 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Thu, 30 Apr 2026 08:24:56 +0100 Subject: [PATCH 23/26] revert: drop $log clone and sdks in-lock re-read MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Both were defensive against rare edge cases the reviewer flagged but which don't justify the complexity: - $log clone: protects against tag pollution on the per-request Log if reportError fires AND the request later errors. http.php's request-end handler overwrites the core fields (namespace, message, action, etc.) anyway; only addTag/addExtra accumulate. Aligns with Embeddings/Text/Create.php precedent which mutates $log directly. - sdks in-lock re-read: closes a sequential-acquire stale-read race on the sdks append-list. The race exists but the impact is bounded — one SDK registration delayed until the next request from that SDK fires. Self-healing on retry. The codebase already accepts this exact race for auths, oAuthProviders, services, identities, sessions, factors, etc. Special-casing this one site is precision the analytics use case doesn't need. --- app/controllers/shared/api.php | 34 ++++++++++++---------------------- src/Appwrite/Locking/Lock.php | 34 +++++++++++++++------------------- 2 files changed, 27 insertions(+), 41 deletions(-) diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 2a1beadc04..ce0fd004cf 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -232,37 +232,27 @@ Http::init() $updates = new Document(); $accessedAt = $dbKey->getAttribute('accessedAt', 0); - $accessedAtStale = DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_KEY_ACCESS)) > $accessedAt; - if ($accessedAtStale) { + if (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_KEY_ACCESS)) > $accessedAt) { $updates->setAttribute('accessedAt', DateTime::now()); } $sdkValidator = new WhiteList($servers, true); $sdk = $request->getHeader('x-sdk-name', 'UNKNOWN'); - $sdkCandidate = ($sdk !== 'UNKNOWN' && $sdkValidator->isValid($sdk) && ! in_array($sdk, $dbKey->getAttribute('sdks', []))) ? $sdk : null; - if (! $updates->isEmpty() || $sdkCandidate !== null) { - $lock->run('keys', $dbKey->getId(), function () use ($dbForPlatform, $dbKey, $updates, $sdkCandidate, $apiKey, $project, $user, $team) { - // Re-read inside the lock so two sequential acquirers don't clobber each other's - // sdks-array appends. Skip semantics still drop concurrent acquirers' updates, - // but a same-SDK retry on the next request will pick the registration up. - if ($sdkCandidate !== null) { - $current = $dbForPlatform->getAuthorization()->skip( - fn () => $dbForPlatform->getDocument('keys', $dbKey->getId()) - ); - $sdks = $current->getAttribute('sdks', []); - if (! in_array($sdkCandidate, $sdks)) { - $sdks[] = $sdkCandidate; - $updates->setAttribute('sdks', $sdks); - $updates->setAttribute('accessedAt', DateTime::now()); - } - } + if ($sdk !== 'UNKNOWN' && $sdkValidator->isValid($sdk)) { + $sdks = $dbKey->getAttribute('sdks', []); - if ($updates->isEmpty()) { - return; - } + if (! in_array($sdk, $sdks)) { + $sdks[] = $sdk; + $updates->setAttribute('sdks', $sdks); + $updates->setAttribute('accessedAt', DateTime::now()); + } + } + + if (! $updates->isEmpty()) { + $lock->run('keys', $dbKey->getId(), function () use ($dbForPlatform, $dbKey, $updates, $apiKey, $project, $user, $team) { $dbForPlatform->getAuthorization()->skip(fn () => $dbForPlatform->updateDocument('keys', $dbKey->getId(), $updates)); if (! empty($apiKey->getProjectId())) { diff --git a/src/Appwrite/Locking/Lock.php b/src/Appwrite/Locking/Lock.php index 6227ae3f5e..9ef6d70c03 100644 --- a/src/Appwrite/Locking/Lock.php +++ b/src/Appwrite/Locking/Lock.php @@ -209,30 +209,26 @@ final class Lock } self::$lastReportAt[$bucket] = $now; - // Clone the per-request Log so our lock-error fields don't bleed into the - // request-end submission of $this->log (action/message/tags get reset on the - // shared instance otherwise). - $lockLog = clone $this->log; - $lockLog->setNamespace('http'); - $lockLog->setServer(System::getEnv('_APP_LOGGING_SERVICE_IDENTIFIER', \gethostname())); - $lockLog->setVersion(APP_VERSION_STABLE); - $lockLog->setType(Log::TYPE_WARNING); - $lockLog->setMessage('Distributed lock '.$action.': '.$e->getMessage()); - $lockLog->setAction("lock.{$action}"); - $lockLog->setEnvironment(System::getEnv('_APP_ENV', 'development') === 'production' + $this->log->setNamespace('http'); + $this->log->setServer(System::getEnv('_APP_LOGGING_SERVICE_IDENTIFIER', \gethostname())); + $this->log->setVersion(APP_VERSION_STABLE); + $this->log->setType(Log::TYPE_WARNING); + $this->log->setMessage('Distributed lock '.$action.': '.$e->getMessage()); + $this->log->setAction("lock.{$action}"); + $this->log->setEnvironment(System::getEnv('_APP_ENV', 'development') === 'production' ? Log::ENVIRONMENT_PRODUCTION : Log::ENVIRONMENT_STAGING); - $lockLog->addTag('lock.target', $target); - $lockLog->addTag('lock.project', $this->projectInternalId); + $this->log->addTag('lock.target', $target); + $this->log->addTag('lock.project', $this->projectInternalId); // Strip trailing document ID to keep aggregator cardinality bounded. - $lockLog->addTag('lock.key_pattern', preg_replace('/:[^:]+$/', ':*', $key)); - $lockLog->addTag('code', $e->getCode()); - $lockLog->addExtra('file', $e->getFile()); - $lockLog->addExtra('line', $e->getLine()); - $lockLog->addExtra('trace', $e->getTraceAsString()); + $this->log->addTag('lock.key_pattern', preg_replace('/:[^:]+$/', ':*', $key)); + $this->log->addTag('code', $e->getCode()); + $this->log->addExtra('file', $e->getFile()); + $this->log->addExtra('line', $e->getLine()); + $this->log->addExtra('trace', $e->getTraceAsString()); try { - $this->logger->addLog($lockLog); + $this->logger->addLog($this->log); } catch (Throwable) { } } From fca069aa9f5ba98bf0d0fbb7ef298658afb7fe04 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Wed, 10 Jun 2026 09:01:32 +0100 Subject: [PATCH 24/26] Fix locking test refactor check --- tests/unit/Locking/LockTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/unit/Locking/LockTest.php b/tests/unit/Locking/LockTest.php index b789ffe3e0..b6fcfba4d2 100644 --- a/tests/unit/Locking/LockTest.php +++ b/tests/unit/Locking/LockTest.php @@ -1,5 +1,7 @@ Date: Wed, 10 Jun 2026 10:23:50 +0100 Subject: [PATCH 25/26] Purge project cache after project settings updates --- app/controllers/api/projects.php | 3 +++ .../Modules/Project/Http/Project/AuthMethods/Update.php | 1 + .../Platform/Modules/Project/Http/Project/Labels/Update.php | 1 + .../Modules/Project/Http/Project/MockPhone/Create.php | 1 + .../Modules/Project/Http/Project/MockPhone/Delete.php | 1 + .../Modules/Project/Http/Project/MockPhone/Update.php | 1 + .../Platform/Modules/Project/Http/Project/OAuth2/Base.php | 5 ++++- .../Http/Project/Policies/MembershipPrivacy/Update.php | 1 + .../Http/Project/Policies/PasswordDictionary/Update.php | 1 + .../Project/Http/Project/Policies/PasswordHistory/Update.php | 1 + .../Http/Project/Policies/PasswordPersonalData/Update.php | 1 + .../Http/Project/Policies/PasswordStrength/Update.php | 1 + .../Project/Http/Project/Policies/SessionAlert/Update.php | 1 + .../Project/Http/Project/Policies/SessionDuration/Update.php | 1 + .../Http/Project/Policies/SessionInvalidation/Update.php | 1 + .../Project/Http/Project/Policies/SessionLimit/Update.php | 1 + .../Project/Http/Project/Policies/UserLimit/Update.php | 1 + .../Modules/Project/Http/Project/Protocols/Update.php | 1 + .../Platform/Modules/Project/Http/Project/SMTP/Update.php | 1 + .../Modules/Project/Http/Project/Services/Update.php | 1 + .../Modules/Project/Http/Project/Templates/Email/Update.php | 1 + 21 files changed, 26 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 8ab30fac99..8244c05fc5 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -59,6 +59,7 @@ Http::patch('/v1/projects/:projectId/oauth2') } $project = $dbForPlatform->updateDocument('projects', $project->getId(), $project->setAttribute('oAuthProviders', $providers)); + $dbForPlatform->purgeCachedDocument('projects', $project->getId()); $response->dynamic($project, Response::MODEL_PROJECT); }); @@ -93,6 +94,7 @@ Http::patch('/v1/projects/:projectId/auth/mock-numbers') $auths['mockNumbers'] = $numbers; $project = $dbForPlatform->updateDocument('projects', $project->getId(), $project->setAttribute('auths', $auths)); + $dbForPlatform->purgeCachedDocument('projects', $project->getId()); $response->dynamic($project, Response::MODEL_PROJECT); }); @@ -120,6 +122,7 @@ Http::delete('/v1/projects/:projectId/templates/email') unset($templates['email.' . $type . '-' . $locale]); $project = $dbForPlatform->updateDocument('projects', $project->getId(), $project->setAttribute('templates', $templates)); + $dbForPlatform->purgeCachedDocument('projects', $project->getId()); $response->noContent(); }); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php index e98d3141f6..f5f0ae0a32 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php @@ -82,6 +82,7 @@ class Update extends Action $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'auths' => $auths, ]))); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents->setParam('methodId', $methodId); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Labels/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Labels/Update.php index 8a3506eb13..76c47a32f8 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Labels/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Labels/Update.php @@ -71,6 +71,7 @@ class Update extends Action $labels = (array) \array_values(\array_unique($labels)); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document(['labels' => $labels]))); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $response->dynamic($project, Response::MODEL_PROJECT); } diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php index f4002c60ef..759dd931c0 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php @@ -101,6 +101,7 @@ class Create extends Action ]); $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents->setParam('number', $number); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php index 0fb23e1764..f65a955b3d 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php @@ -95,6 +95,7 @@ class Delete extends Action ]); $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents->setParam('number', $number); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php index 48b90a1b97..f2b618c39e 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php @@ -97,6 +97,7 @@ class Update extends Action ]); $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents->setParam('number', $number); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Base.php b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Base.php index b5b8cacb73..b439e7fdf7 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Base.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Base.php @@ -425,7 +425,10 @@ abstract class Base extends Action 'oAuthProviders' => $oAuthProviders ]); - return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); + + return $project; } public function action( diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MembershipPrivacy/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MembershipPrivacy/Update.php index 5d7043fedf..b167aba794 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MembershipPrivacy/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MembershipPrivacy/Update.php @@ -103,6 +103,7 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordDictionary/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordDictionary/Update.php index d7ee99fbfe..d0f8882e99 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordDictionary/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordDictionary/Update.php @@ -75,6 +75,7 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php index 84861a19e1..be611e299f 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php @@ -83,6 +83,7 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPersonalData/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPersonalData/Update.php index 435f00fc39..ff7819f192 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPersonalData/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPersonalData/Update.php @@ -76,6 +76,7 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordStrength/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordStrength/Update.php index 3fbcbe8b30..0f8c566511 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordStrength/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordStrength/Update.php @@ -103,6 +103,7 @@ class Update extends Action $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'auths' => $auths, ]))); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionAlert/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionAlert/Update.php index 79653d46ad..d930fd89e7 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionAlert/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionAlert/Update.php @@ -75,6 +75,7 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php index 0a7f33218a..10eabb57cd 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php @@ -75,6 +75,7 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionInvalidation/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionInvalidation/Update.php index a1feb67346..d4d87edbba 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionInvalidation/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionInvalidation/Update.php @@ -75,6 +75,7 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php index 936a541249..93ae339b79 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php @@ -81,6 +81,7 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php index 2b7e704853..b3f430b779 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php @@ -81,6 +81,7 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php index bfc73802c8..e0e4477a3c 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php @@ -79,6 +79,7 @@ class Update extends Action $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'apis' => $protocols, ]))); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents->setParam('protocolId', $protocolId); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php index bf513f5dac..dd8a469d26 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php @@ -170,6 +170,7 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $response->dynamic($project, Response::MODEL_PROJECT); } diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php index 518eeb4c36..86ef67bb01 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php @@ -79,6 +79,7 @@ class Update extends Action $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'services' => $services, ]))); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents->setParam('serviceId', $serviceId); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Templates/Email/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Templates/Email/Update.php index c8ec04bc76..8186931c5f 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Templates/Email/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Templates/Email/Update.php @@ -129,6 +129,7 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); + $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents->setParam('templateId', $templateId); From e638394e1da050c641eaec748be9dcc4d1ac13a4 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Wed, 10 Jun 2026 10:51:17 +0100 Subject: [PATCH 26/26] Revert "Purge project cache after project settings updates" This reverts commit 47e60535fb4669230167826ca5b86cd1dee0cc9e. --- app/controllers/api/projects.php | 3 --- .../Modules/Project/Http/Project/AuthMethods/Update.php | 1 - .../Platform/Modules/Project/Http/Project/Labels/Update.php | 1 - .../Modules/Project/Http/Project/MockPhone/Create.php | 1 - .../Modules/Project/Http/Project/MockPhone/Delete.php | 1 - .../Modules/Project/Http/Project/MockPhone/Update.php | 1 - .../Platform/Modules/Project/Http/Project/OAuth2/Base.php | 5 +---- .../Http/Project/Policies/MembershipPrivacy/Update.php | 1 - .../Http/Project/Policies/PasswordDictionary/Update.php | 1 - .../Project/Http/Project/Policies/PasswordHistory/Update.php | 1 - .../Http/Project/Policies/PasswordPersonalData/Update.php | 1 - .../Http/Project/Policies/PasswordStrength/Update.php | 1 - .../Project/Http/Project/Policies/SessionAlert/Update.php | 1 - .../Project/Http/Project/Policies/SessionDuration/Update.php | 1 - .../Http/Project/Policies/SessionInvalidation/Update.php | 1 - .../Project/Http/Project/Policies/SessionLimit/Update.php | 1 - .../Project/Http/Project/Policies/UserLimit/Update.php | 1 - .../Modules/Project/Http/Project/Protocols/Update.php | 1 - .../Platform/Modules/Project/Http/Project/SMTP/Update.php | 1 - .../Modules/Project/Http/Project/Services/Update.php | 1 - .../Modules/Project/Http/Project/Templates/Email/Update.php | 1 - 21 files changed, 1 insertion(+), 26 deletions(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 8244c05fc5..8ab30fac99 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -59,7 +59,6 @@ Http::patch('/v1/projects/:projectId/oauth2') } $project = $dbForPlatform->updateDocument('projects', $project->getId(), $project->setAttribute('oAuthProviders', $providers)); - $dbForPlatform->purgeCachedDocument('projects', $project->getId()); $response->dynamic($project, Response::MODEL_PROJECT); }); @@ -94,7 +93,6 @@ Http::patch('/v1/projects/:projectId/auth/mock-numbers') $auths['mockNumbers'] = $numbers; $project = $dbForPlatform->updateDocument('projects', $project->getId(), $project->setAttribute('auths', $auths)); - $dbForPlatform->purgeCachedDocument('projects', $project->getId()); $response->dynamic($project, Response::MODEL_PROJECT); }); @@ -122,7 +120,6 @@ Http::delete('/v1/projects/:projectId/templates/email') unset($templates['email.' . $type . '-' . $locale]); $project = $dbForPlatform->updateDocument('projects', $project->getId(), $project->setAttribute('templates', $templates)); - $dbForPlatform->purgeCachedDocument('projects', $project->getId()); $response->noContent(); }); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php index f5f0ae0a32..e98d3141f6 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/AuthMethods/Update.php @@ -82,7 +82,6 @@ class Update extends Action $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'auths' => $auths, ]))); - $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents->setParam('methodId', $methodId); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Labels/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Labels/Update.php index 76c47a32f8..8a3506eb13 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Labels/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Labels/Update.php @@ -71,7 +71,6 @@ class Update extends Action $labels = (array) \array_values(\array_unique($labels)); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document(['labels' => $labels]))); - $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $response->dynamic($project, Response::MODEL_PROJECT); } diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php index 759dd931c0..f4002c60ef 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Create.php @@ -101,7 +101,6 @@ class Create extends Action ]); $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); - $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents->setParam('number', $number); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php index f65a955b3d..0fb23e1764 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Delete.php @@ -95,7 +95,6 @@ class Delete extends Action ]); $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); - $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents->setParam('number', $number); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php index f2b618c39e..48b90a1b97 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/MockPhone/Update.php @@ -97,7 +97,6 @@ class Update extends Action ]); $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); - $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents->setParam('number', $number); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Base.php b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Base.php index b439e7fdf7..b5b8cacb73 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Base.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/OAuth2/Base.php @@ -425,10 +425,7 @@ abstract class Base extends Action 'oAuthProviders' => $oAuthProviders ]); - $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); - $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); - - return $project; + return $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); } public function action( diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MembershipPrivacy/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MembershipPrivacy/Update.php index b167aba794..5d7043fedf 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MembershipPrivacy/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/MembershipPrivacy/Update.php @@ -103,7 +103,6 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); - $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordDictionary/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordDictionary/Update.php index d0f8882e99..d7ee99fbfe 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordDictionary/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordDictionary/Update.php @@ -75,7 +75,6 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); - $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php index be611e299f..84861a19e1 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordHistory/Update.php @@ -83,7 +83,6 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); - $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPersonalData/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPersonalData/Update.php index ff7819f192..435f00fc39 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPersonalData/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordPersonalData/Update.php @@ -76,7 +76,6 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); - $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordStrength/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordStrength/Update.php index 0f8c566511..3fbcbe8b30 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordStrength/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/PasswordStrength/Update.php @@ -103,7 +103,6 @@ class Update extends Action $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'auths' => $auths, ]))); - $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionAlert/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionAlert/Update.php index d930fd89e7..79653d46ad 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionAlert/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionAlert/Update.php @@ -75,7 +75,6 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); - $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php index 10eabb57cd..0a7f33218a 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionDuration/Update.php @@ -75,7 +75,6 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); - $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionInvalidation/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionInvalidation/Update.php index d4d87edbba..a1feb67346 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionInvalidation/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionInvalidation/Update.php @@ -75,7 +75,6 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); - $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php index 93ae339b79..936a541249 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/SessionLimit/Update.php @@ -81,7 +81,6 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); - $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php index b3f430b779..2b7e704853 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Policies/UserLimit/Update.php @@ -81,7 +81,6 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); - $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents ->setParam('projectId', $project->getId()) diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php index e0e4477a3c..bfc73802c8 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Protocols/Update.php @@ -79,7 +79,6 @@ class Update extends Action $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'apis' => $protocols, ]))); - $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents->setParam('protocolId', $protocolId); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php index dd8a469d26..bf513f5dac 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/SMTP/Update.php @@ -170,7 +170,6 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); - $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $response->dynamic($project, Response::MODEL_PROJECT); } diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php index 86ef67bb01..518eeb4c36 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Services/Update.php @@ -79,7 +79,6 @@ class Update extends Action $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), new Document([ 'services' => $services, ]))); - $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents->setParam('serviceId', $serviceId); diff --git a/src/Appwrite/Platform/Modules/Project/Http/Project/Templates/Email/Update.php b/src/Appwrite/Platform/Modules/Project/Http/Project/Templates/Email/Update.php index 8186931c5f..c8ec04bc76 100644 --- a/src/Appwrite/Platform/Modules/Project/Http/Project/Templates/Email/Update.php +++ b/src/Appwrite/Platform/Modules/Project/Http/Project/Templates/Email/Update.php @@ -129,7 +129,6 @@ class Update extends Action ]); $project = $authorization->skip(fn () => $dbForPlatform->updateDocument('projects', $project->getId(), $updates)); - $authorization->skip(fn () => $dbForPlatform->purgeCachedDocument('projects', $project->getId())); $queueForEvents->setParam('templateId', $templateId);